Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
01/10/08 23:15
Read: times


 
#149286 - Here is a sample code that is working ...
Responding to: ???'s previous message
with an AVR ;)

However, this should be fairly easy to adopt to essentially any microcontroller that has a decent serial device implanted.

The complete source code (this is a stripped version for shortness) id zippen in here (4Kb).

You are free to use and sell this one. I don't mind.


// -----------------------------------------------------------------------
// Receive/Transmit buffers
// -----------------------------------------------------------------------
volatile    uint8_t    SIOLIB_rx_buffer    [SIO_BUFFER_SIZE];
volatile    uint8_t    SIOLIB_tx_buffer    [SIO_BUFFER_SIZE];
volatile    uint8_t    SIOLIB_rx_w_ptr;
volatile    uint8_t    SIOLIB_rx_r_ptr;
volatile    uint8_t    SIOLIB_tx_w_ptr;
volatile    uint8_t    SIOLIB_tx_r_ptr;

// -----------------------------------------------------------------------
// UART Rx complete interrupt
// -----------------------------------------------------------------------
SIGNAL( SIG_UART_RECV ) {
    uint8_t    rxbyte;

    rxbyte = UDR;
    // Is there room in the receive buffer ?
    if    ( ((SIOLIB_rx_w_ptr + 1) & (SIO_BUFFER_SIZE - 1)) != SIOLIB_rx_r_ptr ) {
        SIOLIB_rx_buffer[SIOLIB_rx_w_ptr] = rxbyte;
        SIOLIB_rx_w_ptr = (SIOLIB_rx_w_ptr + 1) & (SIO_BUFFER_SIZE - 1);
    }
}

// -----------------------------------------------------------------------
// UART Data Register Empty Interrupt

// This interrupt assumes that there is always some data present in TX buffer.

// If the byte to transmit next is the last one from the buffer - this

// interrupt will be disabled until enabled again by the write function.

// -----------------------------------------------------------------------
SIGNAL( SIG_UART_DATA ) {
    uint8_t    txbyte;

    // Get the byte to transmit
    txbyte = SIOLIB_tx_buffer[SIOLIB_tx_r_ptr];

    // Increment pointer
    SIOLIB_tx_r_ptr = (SIOLIB_tx_r_ptr + 1) & (SIO_BUFFER_SIZE - 1);

    UDR = txbyte;

    // Did we drain out the TX buffer ?
    // Disable this type of interrupt if we did.
    if    ( SIOLIB_tx_r_ptr == SIOLIB_tx_w_ptr ) {
        UCR &= (_BV(UDRIE) ^ 0xFF);
    }
}

// -----------------------------------------------------------------------
// public:    Initialize the serial interface
// -----------------------------------------------------------------------
extern    void        Rs232Init        ( uint8_t    aBaudRate,
                                      uint8_t    aMode ) {
    SIOLIB_rx_w_ptr = 0;
    SIOLIB_rx_r_ptr = 0;
    SIOLIB_tx_w_ptr = 0;
    SIOLIB_tx_r_ptr = 0;

    // Set the baudrate
    UBRR = aBaudRate;

    // Initially disable everything in the controller
    UCR = 0x00;
    
    // Set interface data mode
    SIOLIB_data_mode = aMode;
    switch    ( aMode ) {
        case    SER_MODE_7O1:
        case    SER_MODE_7E1:
        case    SER_MODE_7N2:
        case    SER_MODE_8N1:
            UCR = _BV(RXCIE) | _BV(RXEN) | _BV(TXEN);
            break;

        default:
            UCR = _BV(CHR9) | _BV(RXCIE) | _BV(RXEN) | _BV(TXEN);
            break;
    }

    // Read any carbage out of the controller
    while    ( UDR != 0 );
}

// -----------------------------------------------------------------------
// EOF: Rs232Init.c
// -----------------------------------------------------------------------


List of 26 messages in thread
TopicAuthorDate
Serial Problem            01/01/70 00:00      
   Can you post more information ?            01/01/70 00:00      
      more information            01/01/70 00:00      
         Too much information!            01/01/70 00:00      
         Some advice            01/01/70 00:00      
         while loop            01/01/70 00:00      
            Bad indenting trap!            01/01/70 00:00      
               Andu, Andy, how dare you ..            01/01/70 00:00      
            I don't think that's the problem.            01/01/70 00:00      
               you're right            01/01/70 00:00      
                  So, have you got it working yet?            01/01/70 00:00      
                     can't receive the 16 bytes            01/01/70 00:00      
                        Have you dealt with the atomicity issues ?            01/01/70 00:00      
                        Debugging            01/01/70 00:00      
                           details            01/01/70 00:00      
   Misleading comment            01/01/70 00:00      
      I too dislike the placement, even worse it is WRON            01/01/70 00:00      
   RI and TI are not mutually exclusive            01/01/70 00:00      
      ... but this is not a cause it will not work...            01/01/70 00:00      
         Yes...            01/01/70 00:00      
   Here is a sample code that is working ...            01/01/70 00:00      
      ... and here for the right processor            01/01/70 00:00      
         Wheel reinvention is -            01/01/70 00:00      
         I have a doubt about that KEIL code.            01/01/70 00:00      
            The Question is what do you want to loose?            01/01/70 00:00      
               No - I was just wondering the hardware            01/01/70 00:00      

Back to Subject List