Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

pic16F877A USART

2009-06-14
2013-03-12
  • hi all,

    i have been successfully using sdcc since 2 years for 8051 micros.  now i am shifting focus to pic devices. my first trial was on pic 16F877A. i have no problems in compiling and working for pic. there are some problems in UART at runtime. i can not monitor the TXIF flag bit to check wether the transmission has been complete. the same goes for the reception too. can not monitor the RCIF flag. transmission and reception works when used with delays instead of polling the flag bits. i tried many approaches including disabling and enabling interrupts. pls help, urgent.  the code is given below. 

    #define _16f877a
    #include <pic/pic16f877a.h>

    static __code unsigned short __at(0x2007) _conf =
    _WDT_OFF & _PWRTE_ON &_HS_OSC &_LVP_OFF;

    void uart_init(void);

    void  main()
    {

    uart_init();

    while(1)
    {
    TXREG='s';
    while(!TXIF);
    }
       
    }

    void uart_init(void)
    {
    // Configure UART serial transmit
            // Configured for:
        //  2400 Baud
        //  8N1
                                    
        // SPBRG - Baud Rate Generator Register
        SPBRG = 25; // 4MHz => 2400 baud (BRGH = 0)
       
        // BRGH - High Baud Rate Select Bit
        BRGH = 0; // (0 = low speed)
       
        // SYNC - USART Mode select Bit
        SYNC = 0; // (0 = asynchronous)
       
       
        TRISC7 = 1; // (1 = pin set as input) RX of USART

        TRISC6 = 1; // (0 = pin set as output)  TX of USART
       
        // SPEN - Serial Port Enable Bit
        SPEN = 1; // (1 = serial port enabled)

        // TXIE - USART Transmit Interupt Enable Bit
        TXIE = 1; // (1 = enabled)

        // RCIE - USART Receive Interupt Enable Bit
        RCIE = 1; // (1 = enabled)
       
        // TX9 - 9-bit Transmit Enable Bit
        TX9 = 1; // (0 = 8-bit transmit)
       
        // RX9 - 9-bit Receive Enable Bit
        RX9 = 1; // (0 = 8-bit reception)
       
        // CREN - Continuous Receive Enable Bit
        CREN = 1; // (1 = Enables receiver)
       
        // TXEN - Trasmit Enable Bit
        TXEN = 1; // (1 = transmit enabled)

       
        // GIE - Global Interrupt Enable Bit
        GIE = 1; // (1 = Enable all unmasked interrupts)
       
        // PEIE - Peripheral Interrupt Enable Bit
        PEIE = 1; // (1 = Enable all unmasked peripheral interrupts)

    }

    any thing is not being recieved on terminal. works well when i give a delay inside the while(1)

     
    • OldETC
      OldETC
      2009-08-30

      Check the datasheet, I believe you will find that one cycle is required for the data to transition from the latch to the output, and during that time the flag is not changed.