PIC USART Transmission

Discussion in 'Embedded Systems and Microcontrollers' started by crazyengineer, Oct 2, 2012.

  1. crazyengineer

    Thread Starter Member

    Dec 29, 2010
    156
    2
    So I'm having some difficulty trying to transmit intergers though USART. I thought if I use sprintf it'll be able to transmit the data sucessfully. However, I'm getting this garbage

    [​IMG]

    Here's the 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. #pragma config OSC = HS
    10. #pragma config WDT = OFF
    11. #pragma config PBADEN = OFF
    12. #pragma config LVP = OFF
    13. int current_angle=15;
    14. char message[10];
    15.  
    16. void CONFIG(void);
    17.  
    18.  
    19.  
    20.  
    21.  
    22. #pragma interrupt ISR
    23. void ISR ()
    24. {   /*  Checks the current rotation */
    25.     if(INTCON3bits.INT1IF==1||INTCONbits.INT0IF==1)                                    
    26.     {
    27.         if(PORTBbits.RB0==0 && PORTBbits.RB1==0)
    28.         {
    29.             current_angle=0;
    30.         }
    31.         else if(PORTBbits.RB0==0 && PORTBbits.RB1==1)
    32.         {
    33.             current_angle=90;
    34.         }
    35.         else if(PORTBbits.RB0==1 && PORTBbits.RB1==1)
    36.         {
    37.             current_angle=180;
    38.         }
    39.         else
    40.         {
    41.             current_angle=270;
    42.         }
    43.        
    44.         /* Resets the external interrupt flags */
    45.         INTCON3bits.INT1IF=0;
    46.         INTCONbits.INT0IF=0;
    47.     }
    48. }
    49.  
    50.  
    51. #pragma code InterruptVectorHigh = 0x08             //  This function simply jumps to the ISR code shown above.
    52. void InterruptVectorHigh (void)
    53. {
    54.     _asm
    55.     goto ISR //jump to interrupt routine
    56.     _endasm
    57. }
    58. #pragma code
    59.  
    60. void main(void)
    61. {
    62.     CONFIG();
    63.  
    64.     while(1)                            //  Run the program forever and let the interrupts take over.
    65.     {
    66.         sprintf(message,"%d",current_angle);
    67.         putrsUSART(message);
    68.         Delay1KTCYx(125);
    69.     }
    70.  
    71. }
    72.  
    73.  
    74.  
    75. void CONFIG(void)
    76. {
    77.     /* Code for handling all of the interrupt */
    78.     RCONbits.IPEN=1;                    //  Enable interrupt priority
    79.     INTCONbits.GIE=1;                   //  Enable global interrupts
    80.     INTCONbits.INT0IE=1;                //  Enable INT0 interrupts
    81.     INTCONbits.INT0IF=0;                //  Set external interrupt flag to zero
    82.     INTCON2bits.RBPU=1;                 // Disable Pull up
    83.     INTCON2bits.INTEDG0=1;              //  Interrupt enable on rising edge
    84.     INTCON3bits.INT1IE=1;               //  Enable External interrupt 1
    85.     INTCON2bits.INTEDG1=1;              //  Enable External interrupt on rising edge
    86.     INTCON3bits.INT1IF=0;               //  Set flag to zero
    87.     TRISBbits.TRISB0=1;                 //  Set PORTB0 to input
    88.     TRISBbits.TRISB1=1;                 //  Set PORTB1 to input
    89.     TRISBbits.TRISB2=0;                 //  Set PORTB2 to output
    90.     PORTB=0x03;                         // Enable high on inputs
    91.  
    92.     /* USART Configuration */
    93.     TRISC=0x00;
    94.     OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE &USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, 129);
    95. }
    96.  
    97.  
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    What did you expect as output? Your terminal program take 8 and 8 bytes and print them out as ASCII characters. You may end up all kind of strange characters. That is because your micro use the binary number system not the decimal. If you want readable numbers in a simple ASCII terminal program. You have to convert that number into a string.
    By the way I have asked you several time about this. But you have ignored my request. So for the last time. Which C compiler and version do use.
     
  3. CVMichael

    Senior Member

    Aug 3, 2007
    416
    17
    Just do something like putrsUSART("test1234"); and then see if you still get invalid data.

    I am guessing the baud rate in your PIC is different than the baud rate selected in your terminal window.

    Actually I don't see in your code where you specify the baud rate.
     
  4. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Try realterm or some other terminal software that can display the actual data without converting it to ASCII text.
    Or convert the data to ASCII text before sending.
    <ed>I now realise that the %d converts to string. It might still help to have a better terminal.</ed>
     
    Last edited: Oct 2, 2012
  5. crazyengineer

    Thread Starter Member

    Dec 29, 2010
    156
    2
    It works when I submit simple text, but the error in the reading when I try to convert the number to a string. Btw, I'm sorry I did not respond to your question. I really did not see them :( . I'm using MPLAB with the C18 compiler.

    I'm pretty sure I configured it to a baud rate of 9600 as shown in the Arduino terminal.
     
  6. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    @crazyengineer Are you using a paid version of the C18 compiler? If not can you use the free CX8 from Microchip compiler instead. The C18 compiler is kind of outdated.
     
  7. crazyengineer

    Thread Starter Member

    Dec 29, 2010
    156
    2
    I'm using the free version....
     
  8. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
Loading...