Thursday, November 1, 2012

Firmware for uC | Communication Interface

In the last post, we had little introduction of how the firmware is going to act. With that picture in mind, now we will go a little deeper.

Android <==> BtBee <==> USART <==> uC

USART Communication
    Atmega16 contains one USART device. In this section we'll learn how to use it for receiving information. For using any of the peripherals in the uC, we need to do 

           + Switch ON the peripheral.
           + Configure the peripheral.
           + Use it.

Switching ON the USART
    Switching ON the USART involves, setting the TXEN and RXEN bits.
UCSRB |= _(RXEN) | _(TXEN);    /*enable the device */

Configuring the USART
   Configuring involves setting the Baud-rate of the USART device. In Atmega16, it can be done by writing the calculated Prescaler value into USART Baud Rate Register(UBRR). UBRR is a 16 bit register. UBRRH and UBRRL represents the higher and lower bytes of the UBRR.
UCSRC |= _(USBS);               /* use 2 STOP bits */
UCSRC |= _(UCSZ0) | _(UCSZ1)    /* use 8 DATA bit mode */

int baud_prescale = F_CPU/(16*baud) - 1;
UBRRL = baud_prescale & 0x00FF;
UBRRH = RSHIFT(baud_prescale, 8) & 0x00FF; 

Using USART
   USART device provides a data register named USART Data Register(UDR), which act as a buffer. There are two separate register for transmission and reception. UDR refers to one when used for transmission and another for reception.
unsigned char received_data = UDR ;
UDR = data;
Interrupt 
   We have seen the snippets for transmission and reception of data through USART. Using Interrupt mechanism in conjunction with USART provides a way for efficient use of the micro-controller time. For you to realize how useful the interrupt mechanism, here is an example. Here our purpose is to receive a byte of information through USART.

   You can just copy the contents of the UDR register at any time you want. If you do so, mostly the copied content will not the correct information, but some garbage value. We have wait for the USART device to complete reception and then copy the data. It is done as follows.
unsigned char USART_receive()
{
   while(UCSRA&(1<<RXC) );
   return UDR;
}

    USART indicates the reception of data by setting the RXC bit of UCSRA register to 1. We have to continuously monitor the RXC bit, and when it becomes 1, we return the data, i.e contents of UDR.

    By enabling the Interrupt mechanism, we can make the USART to interrupt the processor when, RXC becomes one i.e. when the reception is complete. This means that, the micro-controller can do some useful job without wasting its time in monitoring a single bit.And when the processor is interrupted, it will halt the operation, whatever it does and serves the Interrupt, and after serving the interrupt it can continues is operation where is was left.

    And of course, we have code the Interrupt Service Routine, a special function which is called when the interrupt occurs. There can be several interrupts, with several priorities. In avr-gcc, the interrupt service routines are coded as follows.
ISR(USART_RXC_vect)
{
   UCSRA &= ~(1<<RXC);  /* clears the RXC bit */
   return UDR;
}
    where the ISR and USART_RXC_vect is a predefined macros by avr-gcc. Here is the list of predefined interrupt vector names specific to a controller.
   List of Interrupt Vectors

0 comments:

Post a Comment

 

Copyright © Project Athena Design by O Pregador | Blogger Theme by Blogger Template de luxo | Powered by Blogger