USART Receive Problems

Discussion in 'Embedded Systems and Microcontrollers' started by crazyengineer, Nov 21, 2012.

  1. crazyengineer

    Thread Starter Member

    Dec 29, 2010
    156
    2
    Hello everyone. I'm having difficulty trying to get the PIC18F2420 to receive the messages that I send it using the Arduino terminal. I can get the PIC to send messages to the terminal fine, but it seems like it cannot receive anything.

    Code ( (Unknown Language)):
    1.  
    2. /*  Includes */
    3. #include <p18F2420.h>
    4. #include <delays.h>
    5. #include <portb.h>
    6. #include <usart.h>
    7. #include <stdio.h>
    8. #include <stdlib.h>
    9. #include <math.h>
    10. /*  PIC Configuratings */
    11. #pragma config OSC = HS
    12. #pragma config WDT = OFF
    13. #pragma config PBADEN = OFF
    14. #pragma config LVP = OFF
    15. #pragma config PWRT = ON
    16. #pragma config DEBUG= OFF
    17. //#pragma config BOREN = OFF
    18. char data;
    19. char message[100];
    20.  
    21.  
    22.  
    23. void main(void)
    24. {
    25.     OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE &USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, 10);
    26.     while(1)
    27.     {
    28.        
    29.         if(DataRdyUSART())
    30.         {
    31.             data=ReadUSART();
    32.             sprintf(message,"Hello %c \r\n",data);
    33.             putsUSART(message);
    34.             while(BusyUSART());
    35.         }
    36.  
    37.     }
    38.     CloseUSART();
    39. }
    40.  
    The above code should just print hello and the character you type in the display. However, whenever I type the message, I don't even see the message.
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    try commenting out the sprintf and see it you can use the UART functions... this may not be printing to the proper output stream...
     
  3. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,867
    992
    Are you certain data is indeed being sent to the Audrino? Do you have a scope that you can put on your input pin?

    Have you tried debugging your code? Is it going past DataRdyUSART()
     
  4. crazyengineer

    Thread Starter Member

    Dec 29, 2010
    156
    2
    IDK why, but it seems like it's working now. However,I'm really doing this for a research. I'm trying to write a pic code which outputs the encoder velocity, acceleration, and position a certain number of times.Bassically, if I send "c100", it should print the velocity, acceleration, and position 100 times. However, it seems like the code works, when I comment out the timer configuration code

    Code ( (Unknown Language)):
    1.  
    2.  
    3. #include <p18F2420.h>
    4. #include <delays.h>
    5. #include <portb.h>
    6. #include <usart.h>
    7. #include <stdio.h>
    8. #include <stdlib.h>
    9.  
    10. /*  PIC Configuratings */
    11. #pragma config OSC = HS
    12. #pragma config WDT = OFF
    13. #pragma config PBADEN = OFF
    14. #pragma config LVP = OFF
    15. #pragma config PWRT = ON
    16. #pragma config DEBUG= OFF
    17. //#pragma config BOREN = OFF
    18.  
    19.  
    20. /*  Global Variables Encoder*/
    21. int encoder_previous_pos=0;
    22. int encoder_previous_state=0;
    23. int encoder_current_state=0;
    24. int encoder_state_ticks=0;
    25. int encoder_ticks=0;
    26. int encoder_previous_ticks=0;
    27. int encoder_current_velocity=0;
    28. int encoder_pos=0;
    29. int encoder_previous_velocity=0;
    30. int timer_counts=0;
    31. int acceleration=0;
    32. int time_stamp=0;
    33. /*  Global Variables for USART */
    34. char message[100];
    35. char command[4];
    36. char test[3];
    37. int v=0;
    38. int index=0;
    39.  
    40. void GET_ENCODER_TICKS(void);
    41. void GET_ENCODER_DATA(void);
    42. void PIC_CONFIG(void);
    43. void PIC_EXT_CONFIG(void);
    44. void PIC_TIMER_CONFIG(void);
    45.  
    46. #pragma interrupt ISR
    47. void ISR (void)
    48. {
    49.     if(INTCONbits.INT0IF==1 || INTCON3bits.INT1IF==1)
    50.     {
    51.         GET_ENCODER_TICKS();
    52.     }
    53.     if(INTCONbits.TMR0IF==1)
    54.     {
    55.         if(timer_counts==40)                        //  Checks to see if .02 seconds passed
    56.         {
    57.             timer_counts=0;                         //  Resets timer counts
    58.             GET_ENCODER_DATA();                     //  Retrieve encoder velocity
    59.             time_stamp+=5;
    60.         }
    61.         else
    62.         {
    63.             timer_counts+=1;
    64.         }
    65.         INTCONbits.TMR0IF=0;
    66.     }
    67.  
    68. }
    69. #pragma code InterruptVectorHigh = 0x08             //  This function simply jumps to the ISR code shown above.
    70. void InterruptVectorHigh (void)
    71. {
    72.     _asm
    73.     goto ISR //jump to interrupt routine
    74.     _endasm
    75. }
    76. #pragma code
    77.  
    78. void main(void)
    79. {
    80. //  PIC_CONFIG();
    81.     OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE &USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, 10);
    82.     while(1)
    83.     {  
    84.        
    85.         if(DataRdyUSART())
    86.         {
    87.             getsUSART(command,4);
    88.             if(command[0]=='c')
    89.             {
    90.                 sprintf(test,"%c%c%c",command[1],command[2],command[3]);
    91.                 v=atoi(&test);
    92.                 for(index;index<v;index++)
    93.                 {
    94.                     sprintf(message,"Hello World \r\n",v);
    95.                     putsUSART(message);
    96.                     while(BusyUSART());
    97.                 }
    98.                 index=0;
    99.             }
    100.         }
    101.                    
    102.            
    103.            
    104.     }
    105.  
    106.     CloseUSART();
    107. }
    108.  
    109. void PIC_CONFIG(void)
    110. {
    111.     /*  Setting PIC to use interrupts   */
    112.     RCONbits.IPEN=1;                    //  Enable interrupt priority
    113.     INTCONbits.GIE=1;                   //  Enable global interrupts
    114.    
    115.     //  Call the external interrupt configuration function
    116.     PIC_EXT_CONFIG();
    117.    
    118.     //  Call the timer interrupt configuration function
    119.     PIC_TIMER_CONFIG();
    120.    
    121.     //  USART Configuration
    122.     TRISC=0x00;
    123. }
    124.  
    125. void PIC_EXT_CONFIG(void)
    126. {
    127.         /*  External Interrupt configuration for INT0 */
    128.     INTCONbits.INT0IE=1;                //  Enables INT0
    129.     INTCONbits.INT0IF=0;                //  Clears the flag
    130.     INTCON2bits.INTEDG0=1;              //  Activates on rising edge
    131.    
    132.     /*  External Interrupt configuration for INT1 */
    133.     INTCON3bits.INT1IE=1;               //  Enables INT1
    134.     INTCON3bits.INT1IF=0;               //  Clears the flag
    135.     INTCON2bits.INTEDG1=1;              //  Activates on rising edge
    136.    
    137.     //  External Interrupts final configurations
    138.     INTCON2bits.RBPU=1;                 // Disable Pull up
    139.     TRISB=0x03;                         //  Sets RB0 and RB1 as inputs
    140.     PORTB=0x03;                         // Enable high on inputs
    141. }
    142.  
    143. void PIC_TIMER_CONFIG(void)
    144. {
    145.     //  Setup Timer 0 for interrupts
    146.     INTCONbits.TMR0IE=1;
    147.     INTCON2bits.TMR0IP=1;
    148.     INTCONbits.TMR0IF=0;
    149.     T0CONbits.T08BIT=1;
    150.     T0CONbits.T0CS=0;
    151.     T0CONbits.PSA=1;
    152.     TMR0L=0;
    153.     T0CONbits.TMR0ON=1;
    154. }
    155.  
    156.  
    157.  
    158. void GET_ENCODER_TICKS(void)
    159. {
    160.     //  Checks to see if INT0 has been activated
    161.         if(INTCONbits.INT0IF==1)
    162.         {
    163.        
    164.             INTCON2bits.INTEDG0=~(INTCON2bits.INTEDG0);         //  Inverts the INTEDG0 bit
    165.             INTCONbits.INT0IF=0;                                //  Clears interrupt flag
    166.         }
    167.        
    168.         //  Checks to see if INT1 has been activiated
    169.         if(INTCON3bits.INT1IF==1)
    170.         {
    171.            
    172.             INTCON2bits.INTEDG1=~(INTCON2bits.INTEDG1);             //  Inverts the INTEDG1 bit
    173.             INTCON3bits.INT1IF=0;                                   //  Clears the flag
    174.         }
    175.        
    176.         if(PORTBbits.RB0==1)
    177.         {
    178.             encoder_current_state+=1;
    179.         }
    180.         if(PORTBbits.RB1==1)
    181.         {
    182.             encoder_current_state+=2;
    183.         }
    184.        
    185.         //  Checks to see if a new state has occured
    186.         if(encoder_previous_state != encoder_current_state)
    187.         {
    188.             //  Checks the different states
    189.             switch(encoder_current_state)
    190.             {
    191.                 case 0:
    192.                     //  Checks to see if the previous state is equal to 2). If it did, then increase ticks, otherwise it'll be decreased
    193.                     if(encoder_previous_state==2)
    194.                     {
    195.                         encoder_state_ticks+=1;
    196.                     }
    197.                     else
    198.                     {
    199.                         encoder_state_ticks-=1;
    200.                     }
    201.                     break;
    202.                 case 1:
    203.                     //  Checks to see if the previous state is equal to 0). If it did, then increase ticks, otherwise it'll be decreased
    204.                     if(encoder_previous_state==0)
    205.                     {
    206.                         encoder_state_ticks+=1;
    207.                     }
    208.                     else
    209.                     {
    210.                         encoder_state_ticks-=1;
    211.                     }
    212.                     break;
    213.                 case 3:
    214.                     //  Checks to see if the previous state is equal to 1). If it did, then increase ticks, otherwise it'll be decreased
    215.                     if(encoder_previous_state==1)
    216.                     {
    217.                         encoder_state_ticks+=1;
    218.                     }
    219.                     else
    220.                     {
    221.                         encoder_state_ticks-=1;
    222.                     }
    223.                     break;
    224.                 case 2:
    225.                     //  Checks to see if the previous state is equal to 3). If it did, then increase ticks, otherwise it'll be decreased
    226.                     if(encoder_previous_state==3)
    227.                     {
    228.                         encoder_state_ticks+=1;
    229.                     }
    230.                     else
    231.                     {
    232.                         encoder_state_ticks-=1;
    233.                     }
    234.                     break;
    235.                 //  This will do nothing
    236.                 default:
    237.                     break;
    238.             }
    239.            
    240.             //  Checks to see if the encoder ticks is greater than or equal to 4
    241.             if(encoder_state_ticks>=4)
    242.             {
    243.                 encoder_ticks+=1;       //  Increase the encoder pos
    244.                 encoder_state_ticks=0;
    245.             }
    246.             //  Checks to see if the encoder ticks is less than or equal to -4
    247.             if(encoder_state_ticks<=-4)
    248.             {
    249.                 encoder_ticks-=1;       //  Decrease the encoder pos
    250.                 encoder_state_ticks=0;
    251.             }
    252.            
    253.             //  Sets the current state as the new previous state.
    254.             encoder_previous_state=encoder_current_state;
    255.             encoder_current_state=0;
    256.         }
    257. }
    258.  
    259. void GET_ENCODER_DATA(void)
    260. {
    261.     encoder_current_velocity=(encoder_ticks-encoder_previous_ticks)*500;
    262.     acceleration=(encoder_current_velocity-encoder_previous_velocity)*500;
    263.     encoder_pos=encoder_ticks/2;
    264.     encoder_previous_ticks=encoder_ticks;
    265.     encoder_previous_velocity=encoder_current_velocity;
    266.  
    267. }
    268.  
     
  5. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,867
    992
    Because your ISR is huge. Just have the ISR clear the interrupt flag and have it set your own flag, process it in your main loop when the flag is set.
     
  6. crazyengineer

    Thread Starter Member

    Dec 29, 2010
    156
    2
    Okay. So I edited the code so that the main function updates the encoder data. So when I tried to have the usart transmit the data whenever it detects c in the string I send it, it will output the encoder data. However, when I ran it in terminal, it just outputs the encoder data without waiting for the string first.


    Code ( (Unknown Language)):
    1.  
    2. /* Includes */
    3. #include <p18F2420.h>
    4. #include <delays.h>
    5. #include <portb.h>
    6. #include <usart.h>
    7. #include <stdio.h>
    8. #include <stdlib.h>
    9.  
    10. /*  PIC Configuratings */
    11. #pragma config OSC = HS
    12. #pragma config WDT = OFF
    13. #pragma config PBADEN = OFF
    14. #pragma config LVP = OFF
    15. #pragma config PWRT = ON
    16. #pragma config DEBUG= OFF
    17. //#pragma config BOREN = OFF
    18.  
    19. /*  Global Variables Encoder*/
    20. int encoder_previous_pos=0;
    21. int encoder_previous_state=0;
    22. int encoder_current_state=0;
    23. int encoder_state_ticks=0;
    24. int encoder_ticks=0;
    25. int encoder_previous_ticks=0;
    26. int encoder_current_velocity=0;
    27. int encoder_pos=0;
    28. int encoder_previous_velocity=0;
    29. int timer_counts=0;
    30. int acceleration=0;
    31. int time_stamp=0;
    32.  
    33. /*  Global Variables for USART */
    34. char message[100];
    35. char command[4];
    36. int index=0;
    37.  
    38. /* Interrupt Handling */
    39. unsigned int timer_goal=0;
    40.  
    41. void GET_ENCODER_TICKS(void);
    42. void GET_ENCODER_DATA(void);
    43. void PIC_CONFIG(void);
    44. void PIC_EXT_CONFIG(void);
    45. void PIC_TIMER_CONFIG(void);
    46.  
    47. #pragma interrupt ISR
    48. void ISR (void)
    49. {
    50.     if(INTCONbits.INT0IF==1 || INTCON3bits.INT1IF==1)
    51.     {
    52.         GET_ENCODER_TICKS();
    53.     }
    54.     if(INTCONbits.TMR0IF==1)
    55.     {
    56.         if(timer_counts==40)                        //  Checks to see if .02 seconds passed
    57.         {
    58.             timer_counts=0;                         //  Resets timer counts
    59.             timer_goal=1;                       //  Retrieve encoder velocity
    60.             time_stamp+=5;
    61.         }
    62.         else
    63.         {
    64.             timer_counts+=1;
    65.         }
    66.         INTCONbits.TMR0IF=0;
    67.     }
    68.  
    69. }
    70. #pragma code InterruptVectorHigh = 0x08             //  This function simply jumps to the ISR code shown above.
    71. void InterruptVectorHigh (void)
    72. {
    73.     _asm
    74.     goto ISR //jump to interrupt routine
    75.     _endasm
    76. }
    77. #pragma code
    78.  
    79. void main(void)
    80. {
    81.     /* Configure the PIC microcontroller */
    82.     PIC_CONFIG();
    83.     while(1)
    84.     {  
    85.         /* Check to see if it's time to get new encoder data */
    86.         if(timer_goal==1)
    87.         {
    88.             GET_ENCODER_DATA();
    89.             timer_goal=0;
    90.         }
    91.         //  Checks to see if the USART is ready to recieve data
    92.         if(DataRdyUSART())
    93.         {
    94.             command[index]=ReadUSART();
    95.             index+=1;
    96.            
    97.         }
    98.         if(index==4)
    99.         {
    100.             index=0;
    101.         }
    102.         if(command[0]=='c')
    103.             {
    104.                 sprintf(message,"%d %d %d \r\n",encoder_pos,encoder_current_velocity,acceleration);
    105.                 putsUSART(message);
    106.             }
    107.        
    108.     }
    109.     CloseUSART();
    110. }
    111.  
    112. void PIC_CONFIG(void)
    113. {
    114.     /*  Setting PIC to use interrupts   */
    115.     RCONbits.IPEN=1;                    //  Enable interrupt priority
    116.     INTCONbits.GIE=1;                   //  Enable global interrupts
    117.    
    118.     //  Call the external interrupt configuration function
    119.     PIC_EXT_CONFIG();
    120.    
    121.     //  Call the timer interrupt configuration function
    122.     PIC_TIMER_CONFIG();
    123.    
    124.     //  USART Configuration
    125.     TRISC=0x00;
    126.     OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE &USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, 10);
    127.  
    128. }
    129.  
    130.  
    131. void PIC_EXT_CONFIG(void)
    132. {
    133.         /*  External Interrupt configuration for INT0 */
    134.     INTCONbits.INT0IE=1;                //  Enables INT0
    135.     INTCONbits.INT0IF=0;                //  Clears the flag
    136.     INTCON2bits.INTEDG0=1;              //  Activates on rising edge
    137.    
    138.     /*  External Interrupt configuration for INT1 */
    139.     INTCON3bits.INT1IE=1;               //  Enables INT1
    140.     INTCON3bits.INT1IF=0;               //  Clears the flag
    141.     INTCON2bits.INTEDG1=1;              //  Activates on rising edge
    142.    
    143.     //  External Interrupts final configurations
    144.     INTCON2bits.RBPU=1;                 // Disable Pull up
    145.     TRISB=0x03;                         //  Sets RB0 and RB1 as inputs
    146.     PORTB=0x03;                         // Enable high on inputs
    147. }
    148.  
    149.  
    150.  
    151.  
    152. void PIC_TIMER_CONFIG(void)
    153. {
    154.     //  Setup Timer 0 for interrupts
    155.     INTCONbits.TMR0IE=1;
    156.     INTCON2bits.TMR0IP=1;
    157.     INTCONbits.TMR0IF=0;
    158.     T0CONbits.T08BIT=1;
    159.     T0CONbits.T0CS=0;
    160.     T0CONbits.PSA=1;
    161.     TMR0L=0;
    162.     T0CONbits.TMR0ON=1;
    163. }
    164.  
    165.  
    166.  
    167. void GET_ENCODER_TICKS(void)
    168. {
    169.     //  Checks to see if INT0 has been activated
    170.         if(INTCONbits.INT0IF==1)
    171.         {
    172.        
    173.             INTCON2bits.INTEDG0=~(INTCON2bits.INTEDG0);         //  Inverts the INTEDG0 bit
    174.             INTCONbits.INT0IF=0;                                //  Clears interrupt flag
    175.         }
    176.        
    177.         //  Checks to see if INT1 has been activiated
    178.         if(INTCON3bits.INT1IF==1)
    179.         {
    180.            
    181.             INTCON2bits.INTEDG1=~(INTCON2bits.INTEDG1);             //  Inverts the INTEDG1 bit
    182.             INTCON3bits.INT1IF=0;                                   //  Clears the flag
    183.         }
    184.        
    185.         if(PORTBbits.RB0==1)
    186.         {
    187.             encoder_current_state+=1;
    188.         }
    189.         if(PORTBbits.RB1==1)
    190.         {
    191.             encoder_current_state+=2;
    192.         }
    193.        
    194.         //  Checks to see if a new state has occured
    195.         if(encoder_previous_state != encoder_current_state)
    196.         {
    197.             //  Checks the different states
    198.             switch(encoder_current_state)
    199.             {
    200.                 case 0:
    201.                     //  Checks to see if the previous state is equal to 2). If it did, then increase ticks, otherwise it'll be decreased
    202.                     if(encoder_previous_state==2)
    203.                     {
    204.                         encoder_state_ticks+=1;
    205.                     }
    206.                     else
    207.                     {
    208.                         encoder_state_ticks-=1;
    209.                     }
    210.                     break;
    211.                 case 1:
    212.                     //  Checks to see if the previous state is equal to 0). If it did, then increase ticks, otherwise it'll be decreased
    213.                     if(encoder_previous_state==0)
    214.                     {
    215.                         encoder_state_ticks+=1;
    216.                     }
    217.                     else
    218.                     {
    219.                         encoder_state_ticks-=1;
    220.                     }
    221.                     break;
    222.                 case 3:
    223.                     //  Checks to see if the previous state is equal to 1). If it did, then increase ticks, otherwise it'll be decreased
    224.                     if(encoder_previous_state==1)
    225.                     {
    226.                         encoder_state_ticks+=1;
    227.                     }
    228.                     else
    229.                     {
    230.                         encoder_state_ticks-=1;
    231.                     }
    232.                     break;
    233.                 case 2:
    234.                     //  Checks to see if the previous state is equal to 3). If it did, then increase ticks, otherwise it'll be decreased
    235.                     if(encoder_previous_state==3)
    236.                     {
    237.                         encoder_state_ticks+=1;
    238.                     }
    239.                     else
    240.                     {
    241.                         encoder_state_ticks-=1;
    242.                     }
    243.                     break;
    244.                 //  This will do nothing
    245.                 default:
    246.                     break;
    247.             }
    248.            
    249.             //  Checks to see if the encoder ticks is greater than or equal to 4
    250.             if(encoder_state_ticks>=4)
    251.             {
    252.                 encoder_ticks+=1;       //  Increase the encoder pos
    253.                 encoder_state_ticks=0;
    254.             }
    255.             //  Checks to see if the encoder ticks is less than or equal to -4
    256.             if(encoder_state_ticks<=-4)
    257.             {
    258.                 encoder_ticks-=1;       //  Decrease the encoder pos
    259.                 encoder_state_ticks=0;
    260.             }
    261.            
    262.             //  Sets the current state as the new previous state.
    263.             encoder_previous_state=encoder_current_state;
    264.             encoder_current_state=0;
    265.         }
    266. }
    267.  
    268. void GET_ENCODER_DATA(void)
    269. {
    270.     encoder_current_velocity=(encoder_ticks-encoder_previous_ticks)*500;
    271.     acceleration=(encoder_current_velocity-encoder_previous_velocity)*500;
    272.     encoder_pos=encoder_ticks/2;
    273.     encoder_previous_ticks=encoder_ticks;
    274.     encoder_previous_velocity=encoder_current_velocity;
    275. }
    276.  
    277.  
     
  7. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,867
    992
    And what have you done to debug your own code?
     
  8. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    What have you done to debug this. As I remember you have a PICKIT 2 or 3. And both of those support in circuit debugging for your chip. You can not work with embedded systems. And at the same time be allergic to all kind of debugging
     
  9. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,867
    992
    What I said. :) But more concise. :)
     
  10. crazyengineer

    Thread Starter Member

    Dec 29, 2010
    156
    2
    I've used the PICKit3 as my debugger. When I debugged it, it works fine reading a single character, but when I used a getsUSART command, it will not print the message.
     
    Last edited: Nov 22, 2012
  11. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    That is fine. But what did you do in your debugging process. Like where did place your break points.
     
  12. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,867
    992
    And what where your results? What did you find? At the very least narrow the problem down to a few lines of code for us.
     
Loading...