UART with PIC24F and Parallax 28040

Discussion in 'Embedded Systems and Microcontrollers' started by milesguidon, Dec 20, 2010.

  1. milesguidon

    Thread Starter New Member

    Dec 4, 2010
    11
    0
    I am using the UART TX and RX ports on my PIC24F04KA200 to send and receive data to/from the bi-directional signal pin on a Parallax 28040 temperature sensor module. Is anybody familiar enough with programming UART on the PIC in C to glance at these code snippets and give me some advice on where I'm going wrong so far? Thanks:

    Code ( (Unknown Language)):
    1.  
    2.  
    3. const char Txdata[] = "0!TEMR$5A$07";
    4. char Rxdata[2]="00";
    5.  
    6. int main(void)
    7. {    
    8. init_U1MODE();
    9. init_U1STA();
    10. //Set Baud rate to 9600
    11. U1BRG=25;
    12.  
    13. //Write Txdata[] to U1TX1REG
    14. int k;
    15. for(k=13; k>0; k--)
    16. {
    17. _UTXEN=0; //make sure TX is disabled
    18. U1TXREG=Txdata[k]; //load the word, one char at a time
    19. _UTXEN=1; //enable TX, char gets sent
    20. }
    21. _UTXEN=0; //disable TX--we're done sending this word
    22.  
    23. //Read U1RX1REG only if receive buffer isn't overflown
    24. char lowByte, highByte;
    25. if(_OERR==0) //if we dont have an overflow in the RX buffer
    26. {
    27. blink_LED1();
    28. lowByte = U1RXREG;
    29. pause_1();
    30. highByte = U1RXREG;
    31. }
    32. //Fill Rxdata[] with the low and high bytes
    33. Rxdata[0]=lowByte;
    34. Rxdata[1]=highByte;
    35. .......
    36.  
    37. void init_U1MODE()
    38. {
    39. //U1MODE configuration: auto Baud enable, 8-bit data, no parity bit, 1 stop bit, normal baud rate
    40. U1MODE=0x8210;    
    41. }
    42.  
    43. void init_U1STA()
    44. {
    45. //Initializes the U1 status -- transmit off, etc.
    46. _UTXISEL1=0;
    47. _UTXISEL0=1;
    48. _UTXINV=1;
    49. _UTXBRK=1;
    50. _UTXEN=0;
    51. _URXISEL1=1;
    52. _URXISEL0=0;
    53. _ADDEN=0;
    54. }
    55.  
    56.  
    I took the main chunks that I'm concerned with from my code, which all compiles fine. The character array in the beginning is what I am sending to the Parallax module -- I built this string based on what I think is the correct command syntax to send to the 28040 to get it to reply with output. I found the commands in the Parallax 28040 datasheet, which is here:

    http://www.parallax.com/Portals/0/Do...meter-v2.0.pdf

    Command !TEMr is described on Page 4.

    I'd love any guidance at all! I'm doing this from scratch, and it's my 6th day working with any sort of microprocessor -or- UART communication protocol...and to top it all off, C isn't my strong suit.
     
    Last edited by a moderator: Dec 20, 2010
Loading...