C code Error!! URGENT

Discussion in 'Embedded Systems and Microcontrollers' started by st98, Mar 23, 2011.

  1. st98

    Thread Starter Member

    May 27, 2010
    33
    1
    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

    Code ( (Unknown Language)):
    1.  
    2. #include<p18f248.h>
    3. #include<stdio.h>
    4.  
    5. #pragma config WDT = OFF //Disable watchdog timer
    6. #pragma config OSC = HS  // High Speed mode
    7.  
    8. unsigned i;
    9. unsigned flag;
    10.  
    11. void initialpriority()
    12. {
    13. INTCON=0x00; //Set interrupts disable
    14. ADCON1=0x07; //Set PORTA to digital mode
    15. PIE1=0; //Peripheral1 Interrupt disabled
    16. PIE2=0; //Peripheral2 Interrupt disabled
    17. PIE3=0; //Peripheral2 Interrupt disabled
    18. }
    19.  
    20. void int_can() //Set I/O Ports
    21. {
    22. TRISB=(TRISB|0x08)&0xFB; //Set RB2 as CAN Tx and RB3 as CAN Rx
    23. CANCON=0x80; //Enter CAN CONfiguration mode
    24. while((CANSTAT&0x80)==0) //CANCON not enabled then wait
    25. {
    26. ;
    27. }
    28. CIOCON=0x00; //Enable CAN I/O ports
    29.  
    30. //Baud rate control register=500kbit/sec,FOSC=20MHz, TQ=20Tq
    31. BRGCON1=0x80; //Set Synchronisation Jump Width Time=3xTQ (10)
    32. BRGCON2=0xbe; //Set Phase Segment Time1=8xTQ, bit 5-3
    33.               //Propagation time 7xTQ, bit2-0
    34. BRGCON=0x43; //Set Phase Segment Time2=4xTQ, bit 2-0,bit 5-3 read '0'
    35.  
    36. //Receive buffer 0
    37. RXB0CON=0x60; //Receive all valid messages(212)~acceptance filter 0
    38. RXB0DLC=0x08; //Data Lenght Config for 11-bit intentifier is 8 bytes    
    39. RXB0SIDH=0xfd; //Shared Implemented Data access bank high
    40. RXB0SIDL0x00; //Shared Implemented Data access bank low
    41.  
    42. //Set all receive data field byte (RXB0D0 to RXBOD7) clear;
    43. RXB0D0=0x00;
    44. RXB0D1=0x00;
    45. RXB0D2=0x00;
    46. RXB0D3=0x00;
    47. RXB0D4=0x00;
    48. RXB0D5=0x00;
    49. RXB0D6=0x00;
    50. RXB0D8=0x00;
    51.  
    52. RXF0SIDH=0xfd; //Filter Shared Implemented Data access High
    53. RXF0SIDH=0x00; //Filter Shared Implemented Data access Low
    54.  
    55. RXM0SIDH=0xff; //Mask shared Implemented Data access High
    56. RXM0SIDH=0xe0; //Mask shared Implemented Data access Low ~@~@~##~^*
    57.  
    58. CANCON=0x00; //Enter CAN CONfiguration normal mode
    59. while((CANSTAT&0xe0)!=0) //Wait to enter normal mode
    60. {
    61. ;
    62. }
    63. PIE3=0x00; //Clear all Peripheral3 Interrupts
    64. PIE3=PIE3|0x01; //Enable CAN receive Peripheral Interrupt
    65. IPR3=IPR3|0x01; //Set Interrupt Priority Register Rx high  
    66. }
    67.  
    68. void int_usart()
    69. {
    70. TRISC=0x80; ////Set R6 as CAN Tx and R7 as CAN Rx
    71. TXSTA=0x04; //Set Transmit Status and Control register to asynchronous mode
    72. RCSTA=0x80; //Enable Receive Status and control register to read bits of data
    73. SPBRG=0x0c; //Set baud rate generator to high speed mode~@~@~@#'#'***
    74. PIE1=PIE1|0x10; //Peripheral Interrupt enabled (188)
    75. TXIP=0; //Transmit Interrupt Priority bit low
    76. TXEN=1; //Transmit status enabled
    77. }
    78.  
    79. void interrupt_CANRx()
    80. {
    81. flag=1; //Set interrupt high if CAN data received
    82. RXB0IF=0; //Clear the interrupt flag
    83. RXB0FUL=0; //Set to receive new message
    84. }
    85.  
    86. void main(void)
    87. {
    88. initialpriority(); //Call function
    89. int_can(); //Call function
    90. int_usart(); //Call function
    91. IPEN=1; //Interrupt priority enabled
    92. INTCON=INTCON|0xc0; // Enable interrupts
    93. TRISA=0x00; //Set RA3, RA0 as inputs| RA5, RA4 as outputs
    94. while(1) //Wait to receive interrupt flag
    95. {
    96.  
    97.      while(flag==0) //for flag=0¬
    98. {
    99. PORTA=0x00; //Set I/O ports to 0
    100. }
    101.      while(flag==1) //for flag=1¬
    102. PORTA=0x01; //Set PORTA as output (LED_ON)
    103. flag=0; //Clear output data to initialize PORTA¬
    104. //Transmit the data bits one by one
    105. TXREG=RGB0D0; //While data bits in receiver buffer
    106.    for(i=0;i<100;i++){;} // Time delay between read byte data
    107. TXREG=RXB0D1;
    108.    for(i=0;i<100;i++){;} // Time delay between read byte data
    109. TXREG=RXB0D2;
    110.    for(i=0;i<100;i++){;} // Time delay between read byte data
    111. TXREG=RXB0D3;
    112.    for(i=0;i<100;i++){;} // Time delay between read byte data
    113. TXREG=RXB0D4;
    114.    for(i=0;i<100;i++){;} // Time delay between read byte data
    115. TXREG=RXB0D5;
    116.    for(i=0;i<100;i++){;} // Time delay between read byte data
    117. TXREG=RXB0D6;
    118.    for(i=0;i<100;i++){;} // Time delay between read byte data
    119. TXREG=RXB0D7;
    120.    for(i=0;i<100;i++){;} // Time delay between read byte data
    121. }
    122. }
    123.  
    124.  
     
    Last edited by a moderator: Mar 23, 2011
  2. atferrari

    AAC Fanatic!

    Jan 6, 2004
    2,645
    759
    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.
     
  3. st98

    Thread Starter Member

    May 27, 2010
    33
    1
    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
     
  4. DumboFixer

    Active Member

    Feb 10, 2009
    219
    34
    Can you be more specific what the erros actually are ?

    You say that
    What functions ?
     
  5. st98

    Thread Starter Member

    May 27, 2010
    33
    1
    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.
     
  6. be80be

    Senior Member

    Jul 5, 2008
    431
    57
    This whats happening
    Code ( (Unknown Language)):
    1.  
    2.  
    3. C:\can\main.c:33:Error [1105] symbol 'BRGCON' has not been defined
    4. C:\can\main.c:33:Error [1101] lvalue required
    5. C:\can\main.c:39:Error [1105] symbol 'RXB0SIDL0x00' has not been defined
    6. C:\can\main.c:49:Error [1105] symbol 'RXB0D8' has not been defined
    7. C:\can\main.c:49:Error [1101] lvalue required
    8. C:\can\main.c:74:Error [1105] symbol 'TXIP' has not been defined
    9. C:\can\main.c:74:Error [1101] lvalue required
    10. C:\can\main.c:75:Error [1105] symbol 'TXEN' has not been defined
    11. C:\can\main.c:75:Error [1101] lvalue required
    12. C:\can\main.c:81:Error [1105] symbol 'RXB0IF' has not been defined
    13. C:\can\main.c:81:Error [1101] lvalue required
    14. C:\can\main.c:82:Error [1105] symbol 'RXB0FUL' has not been defined
    15. C:\can\main.c:82:Error [1101] lvalue required
    16. C:\can\main.c:87:Warning [2058] call of function without prototype
    17. C:\can\main.c:88:Warning [2058] call of function without prototype
    18. C:\can\main.c:89:Warning [2058] call of function without prototype
    19. C:\can\main.c:90:Error [1105] symbol 'IPEN' has not been defined
    20. C:\can\main.c:90:Error [1101] lvalue required
    21. C:\can\main.c:104:Error [1105] symbol 'RGB0D0' has not been defined
    22.  
     
  7. st98

    Thread Starter Member

    May 27, 2010
    33
    1
    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
     
  8. be80be

    Senior Member

    Jul 5, 2008
    431
    57
    Some of it is miss spelling i got it down to 4 errors
    Code ( (Unknown Language)):
    1. C:\can\main.c:87:Warning [2058] call of function without prototype
    2. C:\can\main.c:88:Warning [2058] call of function without prototype
    3. C:\can\main.c:89:Warning [2058] call of function without prototype
     
  9. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    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.
     
  10. st98

    Thread Starter Member

    May 27, 2010
    33
    1
    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
     
  11. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    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.
     
Loading...