C code Error!! URGENT

Thread Starter

st98

Joined May 27, 2010
33
Hi,

Can someone please kindly tell me what is wrong in the code I've written?
I am using MPLAB and C18 compiler. I was using HTC compiler but I had to swap to C18 compiler so most the code I tried to change as far as you can see here:

*Most of the errosr ask me to define functions

Rich (BB code):
#include<p18f248.h>
#include<stdio.h> 

#pragma config WDT = OFF //Disable watchdog timer
#pragma config OSC = HS  // High Speed mode

unsigned i;
unsigned flag;

void initialpriority()
{
INTCON=0x00; //Set interrupts disable
ADCON1=0x07; //Set PORTA to digital mode
PIE1=0; //Peripheral1 Interrupt disabled
PIE2=0; //Peripheral2 Interrupt disabled
PIE3=0; //Peripheral2 Interrupt disabled
}

void int_can() //Set I/O Ports
{
TRISB=(TRISB|0x08)&0xFB; //Set RB2 as CAN Tx and RB3 as CAN Rx
CANCON=0x80; //Enter CAN CONfiguration mode
while((CANSTAT&0x80)==0) //CANCON not enabled then wait
{
;
}
CIOCON=0x00; //Enable CAN I/O ports

//Baud rate control register=500kbit/sec,FOSC=20MHz, TQ=20Tq
BRGCON1=0x80; //Set Synchronisation Jump Width Time=3xTQ (10)
BRGCON2=0xbe; //Set Phase Segment Time1=8xTQ, bit 5-3
              //Propagation time 7xTQ, bit2-0
BRGCON=0x43; //Set Phase Segment Time2=4xTQ, bit 2-0,bit 5-3 read '0'

//Receive buffer 0
RXB0CON=0x60; //Receive all valid messages(212)~acceptance filter 0
RXB0DLC=0x08; //Data Lenght Config for 11-bit intentifier is 8 bytes    
RXB0SIDH=0xfd; //Shared Implemented Data access bank high
RXB0SIDL0x00; //Shared Implemented Data access bank low

//Set all receive data field byte (RXB0D0 to RXBOD7) clear;
RXB0D0=0x00;
RXB0D1=0x00;
RXB0D2=0x00;
RXB0D3=0x00;
RXB0D4=0x00;
RXB0D5=0x00;
RXB0D6=0x00;
RXB0D8=0x00;

RXF0SIDH=0xfd; //Filter Shared Implemented Data access High
RXF0SIDH=0x00; //Filter Shared Implemented Data access Low

RXM0SIDH=0xff; //Mask shared Implemented Data access High
RXM0SIDH=0xe0; //Mask shared Implemented Data access Low ~@~@~##~^*

CANCON=0x00; //Enter CAN CONfiguration normal mode
while((CANSTAT&0xe0)!=0) //Wait to enter normal mode
{
;
}
PIE3=0x00; //Clear all Peripheral3 Interrupts
PIE3=PIE3|0x01; //Enable CAN receive Peripheral Interrupt
IPR3=IPR3|0x01; //Set Interrupt Priority Register Rx high  
}

void int_usart()
{
TRISC=0x80; ////Set R6 as CAN Tx and R7 as CAN Rx
TXSTA=0x04; //Set Transmit Status and Control register to asynchronous mode
RCSTA=0x80; //Enable Receive Status and control register to read bits of data
SPBRG=0x0c; //Set baud rate generator to high speed mode~@~@~@#'#'***
PIE1=PIE1|0x10; //Peripheral Interrupt enabled (188)
TXIP=0; //Transmit Interrupt Priority bit low
TXEN=1; //Transmit status enabled
}

void interrupt_CANRx()
{
flag=1; //Set interrupt high if CAN data received
RXB0IF=0; //Clear the interrupt flag
RXB0FUL=0; //Set to receive new message
}

void main(void)
{
initialpriority(); //Call function
int_can(); //Call function
int_usart(); //Call function
IPEN=1; //Interrupt priority enabled
INTCON=INTCON|0xc0; // Enable interrupts
TRISA=0x00; //Set RA3, RA0 as inputs| RA5, RA4 as outputs
while(1) //Wait to receive interrupt flag
{

     while(flag==0) //for flag=0¬
{
PORTA=0x00; //Set I/O ports to 0
}
     while(flag==1) //for flag=1¬
PORTA=0x01; //Set PORTA as output (LED_ON)
flag=0; //Clear output data to initialize PORTA¬
//Transmit the data bits one by one
TXREG=RGB0D0; //While data bits in receiver buffer
   for(i=0;i<100;i++){;} // Time delay between read byte data
TXREG=RXB0D1;
   for(i=0;i<100;i++){;} // Time delay between read byte data
TXREG=RXB0D2;
   for(i=0;i<100;i++){;} // Time delay between read byte data
TXREG=RXB0D3; 
   for(i=0;i<100;i++){;} // Time delay between read byte data
TXREG=RXB0D4;
   for(i=0;i<100;i++){;} // Time delay between read byte data
TXREG=RXB0D5; 
   for(i=0;i<100;i++){;} // Time delay between read byte data
TXREG=RXB0D6;
   for(i=0;i<100;i++){;} // Time delay between read byte data
TXREG=RXB0D7;
   for(i=0;i<100;i++){;} // Time delay between read byte data
}
}
 
Last edited by a moderator:

atferrari

Joined Jan 6, 2004
4,764
You do not explain why you say "urgent".

If you do, we could consider the steps to help you as quickly as needed according to your detailed explanation.

You know, it takes TIME.
 

Thread Starter

st98

Joined May 27, 2010
33
Is is urgent because it is a code I have to finish until Friday in order to submit my final year project. This code is for communicate with CAN module and As soon as I have a solution to this I need to compile it and then program again to communicate with a GM862. It is necessary to compile the first bit in order to carry on. I really appreciate your interest. Thank you in advance
 

Thread Starter

st98

Joined May 27, 2010
33
It says CIOCON=0x00; //Enable CAN I/O ports is not defined for example.
Basically I don't know what the differences are in writing the code from HTC to C18 compiler.I am also working on it and changing things around always appears to give other errors by terminating the previous errors.Thank you very much for your time I really appreciate it, thank you.
 

be80be

Joined Jul 5, 2008
2,072
This whats happening
Rich (BB code):
C:\can\main.c:33:Error [1105] symbol 'BRGCON' has not been defined
C:\can\main.c:33:Error [1101] lvalue required
C:\can\main.c:39:Error [1105] symbol 'RXB0SIDL0x00' has not been defined
C:\can\main.c:49:Error [1105] symbol 'RXB0D8' has not been defined
C:\can\main.c:49:Error [1101] lvalue required
C:\can\main.c:74:Error [1105] symbol 'TXIP' has not been defined
C:\can\main.c:74:Error [1101] lvalue required
C:\can\main.c:75:Error [1105] symbol 'TXEN' has not been defined
C:\can\main.c:75:Error [1101] lvalue required
C:\can\main.c:81:Error [1105] symbol 'RXB0IF' has not been defined
C:\can\main.c:81:Error [1101] lvalue required
C:\can\main.c:82:Error [1105] symbol 'RXB0FUL' has not been defined
C:\can\main.c:82:Error [1101] lvalue required
C:\can\main.c:87:Warning [2058] call of function without prototype
C:\can\main.c:88:Warning [2058] call of function without prototype
C:\can\main.c:89:Warning [2058] call of function without prototype
C:\can\main.c:90:Error [1105] symbol 'IPEN' has not been defined
C:\can\main.c:90:Error [1101] lvalue required
C:\can\main.c:104:Error [1105] symbol 'RGB0D0' has not been defined
 

Thread Starter

st98

Joined May 27, 2010
33
Indeed it is! I think the case is that I need to implement the code in order for the compiler C18 to be able to compile it. Any ideas or suggestions or any hints about HTC and C18 syntax differences or commands?

Thank you
 

be80be

Joined Jul 5, 2008
2,072
Some of it is miss spelling i got it down to 4 errors
Rich (BB code):
C:\can\main.c:87:Warning [2058] call of function without prototype
C:\can\main.c:88:Warning [2058] call of function without prototype
C:\can\main.c:89:Warning [2058] call of function without prototype
 

AlexR

Joined Jan 16, 2008
732
This whats happening
Rich (BB code):
C:\can\main.c:33:Error [1105] symbol 'BRGCON' has not been defined
C:\can\main.c:33:Error [1101] lvalue required
C:\can\main.c:39:Error [1105] symbol 'RXB0SIDL0x00' has not been defined
C:\can\main.c:49:Error [1105] symbol 'RXB0D8' has not been defined
C:\can\main.c:49:Error [1101] lvalue required
C:\can\main.c:74:Error [1105] symbol 'TXIP' has not been defined
C:\can\main.c:74:Error [1101] lvalue required
C:\can\main.c:75:Error [1105] symbol 'TXEN' has not been defined
C:\can\main.c:75:Error [1101] lvalue required
C:\can\main.c:81:Error [1105] symbol 'RXB0IF' has not been defined
C:\can\main.c:81:Error [1101] lvalue required
C:\can\main.c:82:Error [1105] symbol 'RXB0FUL' has not been defined
C:\can\main.c:82:Error [1101] lvalue required
C:\can\main.c:87:Warning [2058] call of function without prototype
C:\can\main.c:88:Warning [2058] call of function without prototype
C:\can\main.c:89:Warning [2058] call of function without prototype
C:\can\main.c:90:Error [1105] symbol 'IPEN' has not been defined
C:\can\main.c:90:Error [1101] lvalue required
C:\can\main.c:104:Error [1105] symbol 'RGB0D0' has not been defined
Most of the errors seem to be typos.

E.g "C:\can\main.c:33:Error [1105] symbol 'BRGCON' has not been defined."

If you look in the p18f248.h file you will see that BRGCON is indeed not defined, there is BRGCON1, BRGCON2 and BRGCON3 but no straight out BRGCON. For that matter even the data sheet does not refer to a BRGCON register.

Next error "C:\can\main.c:39:Error [1105] symbol 'RXB0SIDL0x00' has not been defined." Again a typo, missing equal sign to assign 0x00 to the RXB0SIDL register.

And so it goes on. When you get errors saying that a symbol has not been defined check the register definitions and names in the header file to make sure that you are using the correct register name.
 

Thread Starter

st98

Joined May 27, 2010
33
First I want to thank you for your time and your interest.
As for the syntax errors I have corrected them all.
Now, I have also checked the register def and header files
and they seem ok.It still comes up with syntax error though and I can't figure out why. It has to do something with the 1st while loop I think, because it always give me a syntax error at the first line following after the while loop, ex here:
void int_can() //Set I/O Ports
{
TRISB=(TRISB|0x08)&0xFB; //Set RB2 as CAN Tx and RB3 as CAN Rx
/*TRISBbits.TRISB2=1;
TRISBbits.TRISB3=1;*/
CANCON=0x80; //Enter CAN CONfiguration mode
while((CANSTAT&0x80)!=0); //wait to go to config mode
}
CIOCON=0x00; //Config CAN I/O ports

Whatever I have written the line CIOCON=0x00; line it reads it as a syntax error

Any suggestions?
Also, I am writing in C18 compiler should I write TRISBbits.TRISB2=1; and TRISBbits.TRISB3=1; instead of TRISB=(TRISB|0x08)&0xFB; ? Does it make any difference?

Thank you
 

spinnaker

Joined Oct 29, 2009
7,830
First I want to thank you for your time and your interest.
As for the syntax errors I have corrected them all.
Now, I have also checked the register def and header files
and they seem ok.It still comes up with syntax error though and I can't figure out why. It has to do something with the 1st while loop I think, because it always give me a syntax error at the first line following after the while loop, ex here:
void int_can() //Set I/O Ports
{
TRISB=(TRISB|0x08)&0xFB; //Set RB2 as CAN Tx and RB3 as CAN Rx
/*TRISBbits.TRISB2=1;
TRISBbits.TRISB3=1;*/
CANCON=0x80; //Enter CAN CONfiguration mode
while((CANSTAT&0x80)!=0); //wait to go to config mode
}
CIOCON=0x00; //Config CAN I/O ports

Whatever I have written the line CIOCON=0x00; line it reads it as a syntax error

Any suggestions?
Also, I am writing in C18 compiler should I write TRISBbits.TRISB2=1; and TRISBbits.TRISB3=1; instead of TRISB=(TRISB|0x08)&0xFB; ? Does it make any difference?

Thank you
Why did you wait till the last minute to finish a project due on Friday? You should be much further than this.


If the CIOCON=0x00; is exactly as you show above then it is outside any function. Assignments must take place inside of a function.

It is a lot more readable if you set your bits like TRISBbits.TRISB2=1;. That is why TRISbits is defined the way it is.
 
Top