PIC12F1572 UART communication problem.

Discussion in 'Embedded Systems and Microcontrollers' started by FPerez, Jul 11, 2014.

  1. FPerez

    Thread Starter New Member

    Jul 11, 2014
    4
    0
    Hi everyone! First post here, great to join this community.*

    I'm trying to build a program for a PIC12F1572 using Mplab X Ide v.2.15 and XC8 compiler. It would listen for serial data and when '0' is received it sets RA4 high. In case of receiving '1' it sets RA4 low, and if it receives an 's' it sends out the status of RA4 (sends '1' if RA4 is high and '0' if low).
    Thought it would be quite straight forward but it's turned out pretty challenging!
    Circuitry is quite simple, the pic12f1572 is connected to a max232 chip, which is connected to a computer running a terminal program. Checked both the pic and the max232 and both are working just right. There must be some problem in my code for it just doesn't work, RA4 state doesn't never change at all, and sending an 's' doesn't return anything on terminal screen.
    Here's the code:

    Code ( (Unknown Language)):
    1.  
    2.  
    3. #include <xc.h>
    4.  
    5.  
    6. #pragma config WDTE       =    OFF // watch dog timer off
    7. #pragma config MCLRE      =    ON
    8. #pragma config CP         =    OFF
    9. #pragma config LVP        =    OFF // Low voltage program off
    10. #pragma config FOSC       =    INTOSC
    11.  
    12. unsigned char cUART_char;
    13. unsigned char cUART_data_flg;
    14. unsigned char valor_rele;
    15.  
    16. void init_uart(void);
    17. void UART_putc(unsigned char c);
    18. void interrupt InterruptHandlerLow ();
    19.  
    20. void main()
    21. {
    22.  
    23.     OSCCONbits.SCS = 0;
    24.     OSCCONbits.IRCF = 0x0F; // 16Mhz
    25.     OSCCONbits.SPLLEN = 0;
    26.     TRISAbits.TRISA4 = 0; // R4 es output
    27.     LATAbits.LATA4 = 0; // R4 a GND
    28.  
    29.     init_uart(); // init UART module
    30.     while (1) // infinite loop which handles ncoming data as they arrive
    31.     {
    32.         if (cUART_data_flg==1)// if new data available, send it back through USART tx line (echo it)
    33.         {
    34.             if (cUART_char == '1'){
    35.                 LATAbits.LATA4 = 0;
    36.             }
    37.             else if (cUART_char == '0'){
    38.                 LATAbits.LATA4 = 1;
    39.             }
    40.             else if (cUART_char == 's'){
    41.                 valor_rele = PORTAbits.RA4;
    42.                  UART_putc(valor_rele); // Mandas el valor del relé
    43.             }
    44.             cUART_data_flg=0; // clear new data flag so one charactor will echoed once
    45.         }
    46.     }
    47. }
    48.  
    49.  
    50. void interrupt InterruptHandlerLow ()
    51. {
    52.   if (PIR1bits.RCIF==1)//is interrupt occured by EUSART receive?,
    53.                         //then RCREG is full we have new data (cleared when RCREG is read)
    54.   {
    55.   // if(RCSTA&0x06) //more efficient way than following commented method to check for reception error
    56.     if(RCSTAbits.FERR==1 || RCSTAbits.OERR==1 )
    57.     {
    58.       RCSTAbits.CREN=0;    //Overrun error (can be cleared by clearing bit CREN)
    59.       cUART_char=RCREG;    //clear Framing error
    60.       RCSTAbits.CREN=1;
    61.     }
    62.     else
    63.     {
    64.        cUART_char = RCREG; // read new data into variable
    65.        cUART_data_flg = 1; // new data received. so enable flg
    66.     }
    67.   }
    68. }
    69.  
    70. void init_uart(void) // init UART module for 9600bps boud, start bit 1, stopbit 1, parity NONE
    71. {
    72.  
    73.     TXCKSEL = 0;
    74.     RXDTSEL = 0;
    75.     cUART_data_flg=0;   // init data receive flag to zero (no data)
    76.     TRISAbits.TRISA1=1; //Make UART RX pin input
    77.     TRISAbits.TRISA0=0; //Make UART TX pin output
    78.     SPBRGH  = 0x01;     //9600bps 16MHz Osc
    79.     SPBRGL  = 0xA0;
    80.  
    81.     RCSTAbits.CREN=1;   //1 = Enables receiver
    82.     RCSTAbits.SPEN=1;   //1 = Serial port enabled (configures RX/DT and TX/CK pins as serial port pins)
    83.     BAUDCONbits.BRG16=1;//1 = 16-bit Baud Rate Generator ? SPBRGH and SPBRG
    84.  
    85.     TXSTAbits.SYNC=0;  //0 = Asynchronous mode
    86.     TXSTAbits.BRGH=1;  //1 = High speed
    87.     TXSTAbits.TXEN=1;  //1 = Transmit enabled
    88.  
    89.     PIE1bits.RCIE=1;     // 1 = Enables the EUSART receive interrupt
    90.     INTCONbits.GIE = 1;  //enable interrupts
    91.     INTCONbits.PEIE = 1;
    92. }
    93.  
    94. void UART_putc(unsigned char c)
    95. {
    96.   TXSTAbits.TXEN=0;// disable transmission
    97.   TXREG=c;            // load txreg with data
    98.   TXSTAbits.TXEN=1;    // enable transmission
    99.   while(TXSTAbits.TRMT==0) // wait here till transmit complete
    100.   {
    101.     NOP();
    102.   }
    103. }
    104.  
    It'll be great if you could take a look at it, as I'm kinda stuck here...*
    I use a pickit3 to load the program into the micro controller, and the code compiles and loads just right.*

    Thank you very much indeed!!
    Kind regards,
    Fran
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Avoid claiming everything works when you haven't validated it (or, at least you haven't shown that you have).

    Try making the PIC output data to the terminal and verify your serial communication is fine for transmitting from PIC to PC.

    Since you neglected to mention it, I'll ask: do you have a pullup on MCLR?

    Have you tried wrap-around tests on your devices (connect RX and TX on your UART interfaces) to verify they are configured properly?
     
  3. FPerez

    Thread Starter New Member

    Jul 11, 2014
    4
    0
    Thanks for your time tshuck :)

    I performed a loopback test and MAX232 outputs the data I send from my computer, I guess this part is working ok then. As per the PIC, data transmission from PIC to PC doesn't work. I modified the main function code to just test data transmission, and didn't work. ( I also added a led+resistor in RA2 to see it blink, so I could verify the PIC works).
    About the pull-up resistor, yes, I placed 10k resistor from VDD to MCLR

    Code ( (Unknown Language)):
    1.  
    2. void main()
    3. {
    4.     OSCCONbits.SCS = 0;
    5.     OSCCONbits.IRCF = 0x0F; // 16Mhz
    6.     OSCCONbits.SPLLEN = 0;
    7.     TRISAbits.TRISA2 = 0; // R4 es output
    8.     LATAbits.LATA2 = 0; // R4 a GND
    9.  
    10.     init_uart(); // init UART module
    11.     while (1) // infinite loop which handles ncoming data as they arrive
    12.     {
    13.              LATAbits.LATA2 = 0;
    14.              _delay(1000000);
    15.              LATAbits.LATA2 = 1;
    16.              _delay(1000000);
    17.  
    18.                 TXSTAbits.TXEN=0;// disable transmission
    19.                 TXREG=0xFF;            // load txreg with data
    20.                 TXSTAbits.TXEN=1;    // enable transmission
    21.                 while(TXSTAbits.TRMT==0) // wait here till transmit complete
    22.                 {
    23.                   NOP();
    24.                 }
    25.     }
    26. }
    27.  
    28.  
    Seems like something's wrong/missing in my uart_init() function. :(
     
  4. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    One thing I don't see is any specification for the ANSELA register (page 105)- this is responsible for setting the pins to digital I/O functionality.

    Also, you don't need to turn the transmitter module on and off - it will only transmit while something is in the transmit buffer.
     
  5. FPerez

    Thread Starter New Member

    Jul 11, 2014
    4
    0
    Thanks tshuck!
    You're right, I forgot to set the ANSELA register bits to 0, they're set now.
    Also commented the two lines to turn the transmitter module on/off.
    Unfortunately, there must be something else, as it's still not working :(
     
  6. FPerez

    Thread Starter New Member

    Jul 11, 2014
    4
    0
    Hi again.
    Fixed it !!
    I had not set the ansela register properly. Now it's working!
    You saved my day tshuck, thank you!!

    Cheers,
    Fran
     
  7. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    I've been bitten by the ANSEL register enough times that it's one of the first 5 things I check for...

    Glad to hear you got it! :)
     
Loading...