CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Multiple unit files problems

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








Multiple unit files problems
PostPosted: Fri Nov 16, 2007 8:05 am     Reply with quote

Hello

Below is example code of my problem.
I am trying to use methods from the LED.C file module in my main.c file (in reality any file that imports the relocatable object file). In this case the InitLED() method and LED1_TOGGLE(). However it has no effect, Why!!!!

As you can see I have a file called registers.h that holds all SFR of the PIC18F8520, and I think the set-up I have has made it accessible to all units of the project. In the InitLED() method a try to set the direction of the port connected to the LED (in this case portg pin_3). Therefore in a first define some definitions to make it more readable. And then do the following
LED1_TRIS = 0; // output
LED2_TRIS = 0; // output
LED1 = 1; // ON
LED2= 1; // ON

However there seems to be no effect.The LED does not turn on, in fact TRIS direction reg does not get set in the first place. It seems that the registers do not change. NO COMPILATION ERROR IS GENERATED!!

However if I use the inbuilt functions of ccs ie, set_tris_g(0x00) and then use output_high(PIN_G3) it seems to work. Why is this???

I have tried putting the register.h file in project.h, which all files include and have also put them in separately without any success.

please note: I am using mplab ide which links all the objects files together. In the Project view window I add the source files to the source file folder.

ps: first time I have used this forum, so don’t know if source code is imported in this place as a consequence have pasted code in the message body just in case.



Code:
///////////////////////// LED SOURCE CODE //////////////////////////////////
#define _LED_C

#include "C:\Projects\MCU\Project\project.h"
//#include "C:\Projects\MCU\Global\PIC18F8520_registers.h"
#include "C:\Projects\MCU\Global\tdef.h"


/************************************************************/
/*                                                                            */
/*    CONSTANTS/MACROS (#Def's) AVAILABLE TO FILE ONLY                        */
/*                                                                            */
/************************************************************/
#define LED1 PORTG3  // PORTG3 these are defined in register.h
#define LED2 PORTH4  // PORTH4 these are defined in register.h

#define LED_ON  0
#define LED_OFF 1

#define LED1_TRIS TRISG3 //= 0;//(TRISG &= ~0b00001000)
#define LED2_TRIS TRISH4 //= 0;//(TRISH &= ~0b00010000) 
#include<LED.h> // holds prototypes declarations


// local variables for module only
uint8 x = 0;


extern void InitLED(){
   LED1_TRIS = 0;
        LED2_TRIS = 0;
   //   set_tris_g(0x00); // use this works
   //   set_tris_h(0x00); // use this works
       LED1 = LED_ON;
       LED2 = LED_OFF;
}


extern void LED1_ON() {
   LED1_TRIS = 0;
   LED1 = LED_ON;
}


extern void LED1_OFF() {
   LED1_TRIS = 0;
   LED1 = LED_OFF;
}



extern void LED1_TOGGLE() {
   LED1_TRIS = 0;       //set to output
   //set_tris_g(0x00);
   if(x == 0)    
      LED1 = 1;//(x = ~LED1);
   else
      LED1 = 0;//(x = ~LED1);
}

extern void LED2_ON() {
   LED2_TRIS = 0;
   LED2 = LED_ON;
   
}


extern void LED2_OFF() {
   LED2_TRIS = 0;
   LED2 = LED_OFF;
}
// LED.C END


/////////// LED HEADER FILE THAT CONTAINS PROTOTYPES /////////////
extern void InitLED();
extern void LED1_ON();
extern void LED1_OFF(); 
extern void LED1_TOGGLE();
extern void LED2_ON();
extern void LED2_OFF(); 
// end LED.h



///////////    MAIN SOURCE FILE /////////////////////////////////////////

#define _MAIN_C_

#include "C:\Projects\MCU\Project\project.h"
//#include "C:\Projects\MCU\Global\PIC18F8520_registers.h"
#include "C:\Projects\MCU\Global\tdef.h"
//#include<main.h>  // nothing in this part convention use only

// project defines
#define MAX(A,B) ((A>B)?A:B)


#include "C:\Projects\MCU\LED\LED.h"
#include <stdlib.h>
#include <input.c>

uint16 d = 0;

void main(void) {

    InitLED();   

   delay_ms(2000);
   while(1){
      //LED1_TOGGLE();
   }
}
// end main.c
                           

////////////// PROJECT HEADER ALL FILES INLCUDE THIS FILE ///////

#include<18F8520.h>
//#include "C:\Projects\MCU\Global\PIC18F8520_registers.h"
#FUSES H4                   //High speed osc with HW enabled 4X PLL
#device adc=10
#device  high_ints = true;
#use delay(clock=40M)
#use rs232(baud = 115200,xmit=PIN_C6,rcv=PIN_c7)
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
#use fast_io(D)
#use fast_io(E)
#use fast_io(F)
#use fast_io(G)

//#include "C:\Projects\MCU\Global\Global_Variables.h"

/// PROJECT.h END



Ocean



Joined: 16 Nov 2007
Posts: 2

View user's profile Send private message

Quick Modification
PostPosted: Fri Nov 16, 2007 8:21 am     Reply with quote

Sorry just a quick modification of the code the method TOGGLE_LED() should be


void TOGGLE_LED() {
LED1_TRIS = 0;
if(x == 0){
LED1 = 0;
x = 1;
}else
{
LED1 = 1;
x = 0;
}
}

the problem still persist....
SET



Joined: 15 Nov 2005
Posts: 161
Location: Glasgow, UK

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Fri Nov 16, 2007 11:33 am     Reply with quote

What does file "PIC18F8520_registers.h" contain? I dont think it's a CCS supplied file.. my guess is that in the following:

Code:
#define LED1_TRIS TRISG3


'TRISG3' is a register address (just a number), so:

Code:
LED1_TRIS = 0;


wont do anything. But I'm surprised the compiler didnt flag an error then (LHS is not an lvalue).
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Nov 16, 2007 6:24 pm     Reply with quote

Quote:

I am using mplab ide which links all the objects files together. In the
Project view window I add the source files to the source file folder.

That won't work. Darren has stated that the command line CCS
compilers (PCM, PCH, etc.) that are used with MPLAB can't create .OBJ
files. You only get that capability with the full CCS IDE (PCWH, etc.).
See Darren's post at the following link:
http://www.ccsinfo.com/forum/viewtopic.php?t=26954&start=10

To use the command line compilers with MPLAB, you need to #include
all the module files in the main source file. See the following thread.
It has several links that show how use multiple source files with MPLAB:
http://www.ccsinfo.com/forum/viewtopic.php?t=32221

Also, your program can be written in a much more simple way.
Don't use fast i/o mode. Use standard i/o mode, which is the default
mode of the compiler. Then use CCS pin i/o functions, and the compiler
will set the TRIS for you. You don't have to worry about it.

Here's an example. I've re-written your program to use some macros
in the leds.c file to do the LED operations. Notice how simple the
program is, and how short it is.

main.c file:
Code:
#include<18F8520.h>
#fuses H4,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=40000000) 
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#include "leds.c"

//=============================
void main()
{
LED1_ON;
delay_ms(500);

LED1_OFF;
delay_ms(500);

while(1)
  {
   LED1_TOGGLE;
   delay_ms(500);
  }

}


leds.c file:
Code:

#define LED1  PIN_G3

#define LED1_ON  output_low(LED1)
#define LED1_OFF output_high(LED1)
#define LED1_TOGGLE output_toggle(LED1)
Guest








PostPosted: Sat Nov 17, 2007 6:27 am     Reply with quote

Hey

Thanks for the response.

Hi "SET" in the register.h file contains SFR register definitions i.e

#byte PORTA = 0xF80 // might not be the actual address but example
#bit PORTA1 PORTA.0
#bit PORTA2 PORTA.1
#byte TRISA = 0xF92 // might not be the actual address but example

all the SFR registers are defined here. and therefore to set the tris direction I can either, do,

Code:

     TRISA = 0x00;  // output direction
      PORTA1 = 1;    // send high to porta pin a1


It is not a ccs supplied file.


hi PCM Programmer.
I will try using CCS IDE (PCWH) (i have version 4.061) but I am sure when I make and compile the project in MPlab it does make the OBJ files but will check again.

also Sorry about the long message but thought I would explain myself I initially brought ccs because I had no experience with any embedded programming and also was given the task to build a proof of concept for a electromechnical device. I thought for rapid prototyping it would be good to use the built in functions. Now with a 'little' knowledge I am reluctant to use the built in functions because,

1-Portability, Some other projects we work on are using other compilers like HiTech, therefore would like it to be portable as possible.

2- Learning; the builtin functions are great but does quite a lot for you. I could be wrong but would like to go back to basics and therefore get a better understanding of PICs and programming. (this also applies to the library functions really can not see what they do)
3- Efficency- the output_high function always sets tris direction then sets port pin high. There is an extra instruction. I would like to initially set TRIS direction then i do not need to use the instruction again. thus saving 1 instruction. (might not seem worth it but having problems with efficiency at the mow.)


From the links that you sent my understanding is that you cannot have files, which are separated completely (this is where my OO comes in I think). Therefore I can not have multiple includes in this set up???? I am trying to isolate each file and have them as a separate working entity.


I have looked at the example provided by CCS called MCU (can be found in example folder of your PICC folder). I am using this as a template but for some reason can not use my register.h file which I declare myself. It works with the inbuilt functions but not when you declare your own SFR definitions. Please note it compiles but does not have no effect on the PIC registers.



I probably getting confused really but if I get a better understand I will post back.

Thanks

ocean
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Nov 17, 2007 2:20 pm     Reply with quote

Quote:
1-Portability, Some other projects we work on are using other
compilers like HiTech, therefore would like it to be portable as possible.

Trying to make two different compilers work together in the same
company seems very inefficient. A lot of extra time is wasted trying
to make code that is compatible with both compilers. Compiler specific
extensions can't be used, in order to try to acheive this goal, so the
programming burden is increased. I wouldn't do it.

Quote:

2- Learning; the builtin functions are great but does quite a lot for you. I
could be wrong but would like to go back to basics and therefore get a
better understanding of PICs and programming. (this also applies to the
library functions really can not see what they do)

If you don't use the built-in functions, then why use CCS ? If you do
everything manually, then almost any compiler will do the job.


Quote:

Therefore I can not have multiple includes in this set up???? I am trying
to isolate each file and have them as a separate working entity.

Yes, you can have separate modules (files). Just use the #include
statement to include the files. In one of the links that I suggested
you read, there is a complete example program of how to do it:
http://www.ccsinfo.com/forum/viewtopic.php?t=18649&start=1
Ocean



Joined: 16 Nov 2007
Posts: 2

View user's profile Send private message

PostPosted: Sat Nov 17, 2007 4:19 pm     Reply with quote

Hi PCM programmers
Quote:

Trying to make two different compilers work together in the same
company seems very inefficient. ........

You are right about the different compilers. but it was like this before i got there. We are phasing it out though (with some relunctance by some).
Also I still would like the code to be as portable as possible if any thing happens in the future.

Quote:

If you don't use the built-in functions, then why use CCS ? If you do
everything manually, then almost any compiler will do the job.

Again i was advised to get this ccs compiler due to the builtin functions and the need to rapid prototype a proof of concept, (also truth be told my lack of any embedded experience). As I have gaining some more experience (debatable) I find that I learn more when not using the builtin functions. Also it was a slightly cheaper option compared to other compilers. (I know it shouldn't be the defining reason for choosing software)


The link example works but not really what I wanted to achieve in the sense that yes you can do it this way and this is the way I started off with by including the source files in main. (and will probably stick to this way) but wanted to create different object files that other people can #import and use the methods without the need of the source code and implementation and just require the method declarations. Also doing this way main has access to all files included by other files which I would like to keep hidden ie extern methods and variables local to that file and not just methods. This also includes files that proceed other files which have been included i.e.
Code:

#include<file1.c>    //  file1 includes file4 only
#include<file2.c>    // file2 has access to file4 methods and variables
#include<file3.c>    // file3 has access to file4 methods and variables

void main(){
   // has access to all file methods and local variables defined
   // in include file4. 
}


I have been using the MCU example which is suggested by CCS, and it create multiple o files. They allow me declare variables to SFR’s i.e
#byte PORTB = 0xf98 in the separate files but does not manipulate the registers

I have PCWH version 4.XXX and according to the link below I can use Mplab
http://www.smartcom.co.uk/menus/main.asp?QTopMenu=PIC%20Tools&QSubMenu=Compilers&QOptionItem=New%20C%20Language%20Features


I will keep you posted once I have figured it out. (I have contacted ccs and waiting response)

thanks

ocean
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group