util.c

Go to the documentation of this file.
00001 
00007 /*
00008        File: util.h
00009  Programmer: Ricardo Castro
00010 Description: some util stuff for comunication and other propose
00011    revision: 0.06
00012 */
00013 
00014 #include <stdlib.h> 
00015 #include <ctype.h>
00016 #include "util.h"
00017 
00018 /******************************************************************
00019 * USART_Init
00020 *
00021 * Args: unsigned int uiBaud = Baund Rate
00022 *           unsigned char ucConf = configuration mask
00023 * Description: Initialize USART, wiht baud and ucConf
00024 *
00025 *******************************************************************/
00026 void USART_Init(unsigned int uiBaud, unsigned char ucConf)
00027 {
00028   UCSRB = 0;                            //disable while setting baud rate
00029   
00030   UBRRH = (unsigned char)(uiBaud>>8);
00031   UBRRL = (unsigned char)uiBaud;
00032     
00033   UCSRC = (1<<URSEL)|ASYNC_MODE| ucConf; 
00034   // URSEL is necessary for accessing register UCSRC
00035   
00036   UCSRB = (1<<RXEN)|(1<<TXEN); // Rx enable Tx Enable
00037 }
00038 
00039 /******************************************************************
00040 * TxChar
00041 *
00042 * Args: unsigned Char ucData = data to transmit
00043 * Description: transmit(when possible) ucData
00044 ******************************************************************/
00045 void TxChar( unsigned char ucData)
00046 {
00047    while ( !( UCSRA & (1<<UDRE)) )  /* Wait for empty transmit buffer */
00048        ;
00049            
00050    UDR = ucData; /* Put data into buffer, sends the data */
00051 }
00052 
00053 /******************************************************************
00054 * TxStr
00055 *
00056 * Args: const char * strBuff = String buffer to send
00057 * Description: transmit(when possible) strBuff(end with '\0'
00058 ******************************************************************/
00059 void TxStr(const char * strBuff)
00060 {
00061         uint8_t i=0;
00062         
00063         while( '\0'!=strBuff[i] ){         // while not end of string
00064                 TxChar(strBuff[i]);   // send char  
00065                 i++;                                               // next char
00066         }
00067 }
00068 
00069 /******************************************************************
00070 * _TxStr_P
00071 * (This is a internal funcion that should not be called. 
00072 *  Please use TxStr_P macro)
00073 *
00074 * Args: const char * str_flash = pointer to flash memory where the string is stored
00075 *
00076 * Description: Transmit(when possible) str_flash (end with '\0'). The string
00077 * must be defined on Flash memory. 
00078 ******************************************************************/
00079 void _TxStr_P(const char *str_flash)
00080 {
00081   char c;
00082 
00083   while ('\0' != ( c = pgm_read_byte(str_flash++) ) )
00084                 TxChar(c);
00085 }
00086 
00087 /******************************************************************
00088 * TxUint
00089 *
00090 * Args: uint16_t number = unsigned int (16 bits) to send
00091 * Description: transmit(when possible) unsigned integer (16 bits)
00092 *******************************************************************/
00093 void TxUint(uint16_t number)
00094 {
00095   char str[8];
00096   itoa(number, &str[0],10); // the last argument is the base
00097   TxStr(&str[0]);
00098 }
00099 
00100 /******************************************************************
00101 * TxEol
00102 *
00103 * Description: Transmit end of line
00104 ******************************************************************/
00105 void TxEol(void)
00106 {
00107         TxChar('\n');
00108         TxChar(13);
00109 }
00110 
00111 /******************************************************************          
00112 * RxChar
00113 *
00114 * Description: return received byte
00115 ******************************************************************/
00116 unsigned char RxChar( void )
00117 {
00118         /* Wait for data to be received */
00119         while ( !(UCSRA & (1<<RXC)) )
00120                 ;
00121         /* Get and return received data from buffer */
00122         return UDR;
00123 }
00124 
00125 /******************************************************************          
00126 * TxHex
00127 * 
00128 * Args: unsigned char ucData = data to send
00129 * Description: Send the byte in hexadecimal format
00130 ******************************************************************/
00131 void TxHex(unsigned char ucData){
00132         unsigned char tmp;
00133         TxChar('0');
00134         TxChar('x');
00135         tmp = (ucData>>4)&0x0F;  // get the msb nibble          
00136         if(tmp < 0x0a){             // numeric value
00137                 TxUint(tmp);
00138         }
00139         else{                                           // alfa-numeric value
00140                 TxChar(0x61 + tmp - 0x0a);
00141         }
00142         tmp = ucData&0x0F;                      // get the lsb nibble   
00143         if(tmp < 0x0a){
00144                 TxUint(tmp);
00145         }
00146         else{
00147                 TxChar(0x61 + tmp - 0x0a);
00148         }
00149                 
00150 }
00151 
00152 // util stuff
00153 
00154 /******************************************************************
00155 * delay
00156 *
00157 * Args: unsigned int(16 bits) d = delay in ms
00158 * Descritpion: delay at least d miliseconds with clock frequency
00159 *              around 14.7456Mhz
00160 *
00161 ******************************************************************/
00162 void delay(uint16_t d){
00163         uint16_t i;
00164         
00165         for(i=0; i<d; i++)
00166                 _delay_loop_2(3685); // _delay loop=~ d*4+3+4cycles
00167                                                           // (3685 * 4 +7)/14745600=~1ms
00168 }
00169 /******************************************************************
00170 * delay_u
00171 *
00172 * Args: unsigned int(16 bits) d = delay in us
00173 * Descritpion: delay at least d microseconds with clock frequency
00174 *              around 14.7456Mhz
00175 *
00176 ******************************************************************/
00177 void delay_u(uint16_t d){
00178         uint16_t i;
00179         
00180         for(i=0; i<d; i++)
00181                 _delay_loop_2(2); // _delay loop=~ d*4+3+4cycles
00182                                                           // ( * 4 +7)/14745600=~1us
00183 }

Generated on Fri Jan 6 22:23:17 2006 for EtherProgs by  doxygen 1.4.5