Help with UART Funtionality for 8052 Family

Help
2007-05-22
2013-03-12
  • Stephen Blair
    Stephen Blair
    2007-05-22

    I am using an ST Electronics uPSD3354D microprocessor and am trying to get UART0 to output data.  At first I was trying to use TIMER2 as a baud rate generator, but have now switched to a (simpler?) TIMER1.  With the following code I get LED1 and LED2 alternatively flashing but nothing comes out of UART0 (pin 77, P3.1/TxD).

    Any help is greatly appreciated.  Here's my source code:

    //----------------------------------------------------------------------------------
    // UART0.c
    //
    #define PSD_CSIOP    0x7F00
    #include <uPSD33XX.h>

    #include <upsd3300_timer.h>
    #include <stdio.h>

    #define LED1_ON_LED2_ON        0xF9
    #define LED1_OFF_LED2_OFF    0xFF
    #define LED1_ON_LED2_OFF    0xFB
    #define LED1_OFF_LED2_ON    0xFD

    #define LONG    100
    #define MEDIUM     50
    #define SHORT     10

    #define YES 1
    #define NO  0

    void InitSerial(void);
    unsigned char RxdSerial( unsigned char *c );
    unsigned char TxdSerial( unsigned char c );

    main()
    {
        static char blink_delay, blink_leds_together, use_timer0;
        static long x, y;

        blink_delay = LONG;
        blink_leds_together = NO;

        timer0_init();

        PSD_PMMR0|=0x08;
        PSD_PMMR2|=0x3C;

        PSD_DIRECTION_D|=0x06;
        PSD_DATAOUT_D = LED1_OFF_LED2_OFF;

        //timer2_init();
        InitSerial();

        while(1)
        {
            PSD_DATAOUT_D = LED1_OFF_LED2_OFF;
            timer0_delay(blink_delay);

            TxdSerial(0x65);
            PSD_DATAOUT_D = LED1_ON_LED2_OFF;
            timer0_delay(blink_delay);

            PSD_DATAOUT_D = LED1_OFF_LED2_OFF;
            timer0_delay(blink_delay);

            TxdSerial('B');
            PSD_DATAOUT_D = LED1_OFF_LED2_ON;
            timer0_delay(blink_delay);
        }
    }

    void InitSerial(void)
    {
        SCON0  = 0x50;            /* SCON: mode 1, 8-bit UART, enable rcvr      */
        PCON  |= 0x80;
        TMOD  |= 0x20;              /* TMOD: timer 1, mode 2, 8-bit reload        */
        TH1    = 0xF5;              /* TH1:  reload value for 19200 baud @ 40MHz  */
        T2CON &= 0xCF;            /* Clear RCLK and TCLK              */
        TR1    = 1;                 /* TR1:  timer 1 run                          */
    //    ES0    = 1;            /* Enable UART0 Interrupts                    */
    }

    unsigned char RxdSerial( unsigned char *c )
    {
    *c='\0';
    while(!(RI));
    *c = SBUF0;
    return *c;
    }

    unsigned char TxdSerial( unsigned char c )
    {
    TI=0;
    SBUF0=c;
    while(!(TI));
    return c;
    }
    //----------------------------------------------------------------------------------

    //----------------------------------------------------------------------------------
    // upsd3300_timer.c
    //
    #define PSD_CSIOP    0x7F00
    #include "upsd33XX.h"
    #include "upsd3300_timer.h"

    /*------------------------------------------------------------------------------
                  Global Variable Declarations
    ------------------------------------------------------------------------------*/
    static unsigned int idata timer0_tick;
    static unsigned int idata timer0_value;

    #define FREQ_OSC    40000
    /*------------------------------------------------------------------------------
    timer0_isr()

    This ISR stops timer0, adjusts the counter so that another interrupt occurs in
    10ms, and then restarts the timer.
    ------------------------------------------------------------------------------*/
    void timer0_isr (void) interrupt TIMER0_INTERRUPT using 1
    {
        TR0 = 0;                      /* stop timer 0 */
        TL0 = (timer0_value & 0x00FF);
        TH0 = (timer0_value >> 8);
        TR0 = 1;                      /* start timer 0 */
        timer0_tick++;                // Increment global var timer_tick (number of 10ms ticks)
    }

    /*------------------------------------------------------------------------------
    timer0_init();

    This function enables TIMER 0.  TIMER 0 will generate a synchronous interrupt
    once every 100Hz (10ms).
    ------------------------------------------------------------------------------*/
    void timer0_init (void)
    {
        EA = 0;                 /* disable interrupts                      */
        timer0_tick = 0;
        TR0 = 0;                /* stop timer 0                            */
        TMOD &= 0xF0;           /* clear timer 0 mode bits - bottom 4 bits */
        TMOD |= 0x01;           /* put timer 0 into 16-bit no prescale     */

        // Calculate timer rollover based on FREQ_OSC to be 10ms periods (100hz)
        timer0_value = 0x10000 - ( ((FREQ_OSC * 5L) / 6L) - 17L);
        TL0 = (timer0_value & 0x00FF);
        TH0 = (timer0_value >> 8);

        ET0 = 1;                /* enable timer 0 interrupt                */
        TR0 = 1;                /* start timer 0                           */
        EA = 1;                 /* enable interrupts                       */
    }

    /*------------------------------------------------------------------------------
    timer0_count ();

    This function returns the current Timer 0 tick count.
    ------------------------------------------------------------------------------*/
    unsigned int timer0_count (void)
    {
        unsigned int t;

        EA = 0;               // disable interrupts to read a non-changing value
        t = timer0_tick;
        EA = 1;               // enable interrupts
        return(t);
    }

    /*------------------------------------------------------------------------------
    timer0_delay (count);

    This is a delay function that waits for the specified number of timer 0 ticks to
    pass before returning.

    count   - unsigned int
            - the number of timer ticks to wait before returning from function.
    ------------------------------------------------------------------------------*/
    void timer0_delay (unsigned int count)
    {
        unsigned int start_count;

        start_count = timer0_count();             /* get the start count    */

        while ((timer0_count() - start_count) <= count)    /* wait for count "ticks" */
        {
            PCON |= 0x01;    // Idle MCU to wait for timer tick
        }
    }
    //----------------------------------------------------------------------------------

    //----------------------------------------------------------------------------------
    // upsd3300_timer.h
    //

    void timer0_isr (void) interrupt TIMER0_INTERRUPT using 1;
    void timer0_init (void);
    unsigned int timer0_count (void);
    void timer0_delay (unsigned int count);
    //----------------------------------------------------------------------------------