Serial

debugging interface

When I first thought of building a small kit and selling it out for educational purposes, the serial interface was not in the plans. I thought, yes its pretty much standard, but why to add the extra cost? It might be a nice thing to play with, but is it really needed? Then, the time came for me to play with the 1-wire routines and I found myself doing a lot of debugging by sending stuff to the serial port at runtime. So, I didn't want to be unfair, the serial interface is nice to use for runtime debugging. If you don't catch what I am talking about, think what "high-level" people do, they add breakpoints to their code and they inspect variables. We will do the same, we will send variables from our micro to the PC's serial port and inspect them with a terminal at runtime. Ideally, we should always know at design time what our code will do at runtime, unfortunately sometimes this is not the case. A good thing, now with the serial interface, we might also write a PC temperature logging utility.

For the serial port there are a vast amount of resources on the net, I 'll give a very quick description. Both ATMega8 and a standard PC have a USART, these modules handle communication. They are responsible for taking a byte from a register, formating it and transmitting it serially. At the same time they do the reception as well, when a byte is received serially they place it on a register. There are two wires for this story so, communication is full-duplex, transmission and reception for a party happen simultaneously. Two things to remember here, first, the voltage levels for the 1s and 0s, at the cable, are not 5V and 0V, they are about 15V and -15V, thus the chip MAX232 does the voltage level conversion. These voltage levels ensure better communication at long distances. Have a look at the schematic and the datasheet of MAX232. Notice that Tx and Rx are crossed. The second, packet format and transmission bit rate have to be agreed between the two parties, if not, expect failure.

Have a look at the USART module in the datasheet. Our clock is 7.3728MHz, how InitUSART should be called?


void InitUSART (u16 baud)
{
    // Set Baud rate - Cast High byte
    UBRRH = (u8)(baud>>8);
    // Set Baud rate - Cast Low byte                       
    UBRRL = (u8)baud;                           
    // Enable Receiver & Transmitter
    UCSRB = (1<<RXEN)|(1<<TXEN);                 
    // 0 parity, 1 stop bit
    UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);   
}
 

From the datasheet we can get the TransmitByte and ReceiveByte routines. However, know your code.


void TxByte (u8 data)
{
   // Wait for empty transmit buffer
   while ( !( UCSRA & (1<<UDRE)) );             
   // Putting data into the buffer, forces transmission
   UDR = data;                                   
}

u8 RxByte (void)
{
   // Wait for data to be received
   while ( !(UCSRA & (1<<RXC)) );               
   // Return Data
   return UDR;                                   
}
 

Please, think of some tests and perform them.

Links
http://www.beyondlogic.org/index.htm http://www.lvr.com/serport.htm http://www.epanorama.net/links/pc_interface.html#serial
 
 
Visit my other site!