ButterflyMP3
|
00001 00032 #include <avr/io.h> 00033 #include <avr/interrupt.h> 00034 //#include <avr/signal.h>//This header file is obsolete. Use <avr/interrupt.h>. 00035 #include <avr/pgmspace.h> 00036 #include "uart.h" 00037 #include "main.h" 00038 00039 /* UART global variables */ 00040 volatile u08 UART_Ready; 00041 volatile u08 UART_ReceivedChar; 00042 volatile u08 UART_RxChar; 00043 u08* pUART_Buffer; 00044 00045 /* end-of-line string = 'Line End' + 'Line Feed' character */ 00046 //prog_char UART_pszEndOfLine[3] = {0x0d,0x0a,0}; 00047 00048 /* UART Receive Complete Interrupt Function */ 00049 SIGNAL(SIG_USART_RECV) 00050 { 00051 /* Indicate that the UART has received a character */ 00052 UART_ReceivedChar = 1; 00053 /* Store received character */ 00054 UART_RxChar = UDR; 00055 } 00056 00057 void UART_SendByte(u08 Data) 00058 { 00059 /* wait for UART to become available */ 00060 while ( (UCSRA & (1<<UDRE)) != (1<<UDRE) ) ; 00061 00062 /* Send character */ 00063 UDR = Data; 00064 } 00065 00066 u08 UART_ReceiveByte(void) 00067 { 00068 /* wait for UART to indicate that a character has been received */ 00069 // while(!UART_ReceivedChar); 00070 00071 UART_ReceivedChar = 0; 00072 /* read byte from UART data buffer */ 00073 return UART_RxChar; 00074 } 00075 00076 void UART_PrintfProgStr(const s08* pBuf) 00077 { 00078 #ifdef SERIAL_ENABLE 00079 unsigned char b; 00080 00081 pUART_Buffer = (u08 *) pBuf; 00082 00083 do { 00084 b = pgm_read_byte(pUART_Buffer); 00085 pUART_Buffer++; 00086 if (b) 00087 UART_SendByte(b); 00088 } while (b); 00089 #endif 00090 } 00091 00092 void UART_PrintfEndOfLine(void) 00093 { 00094 #ifdef SERIAL_ENABLE 00095 UART_SendByte(0x0d); 00096 UART_SendByte(0x0a); 00097 #endif 00098 } 00099 00100 void UART_PrintfU4(u08 Data) 00101 { 00102 #ifdef SERIAL_ENABLE 00103 /* Send 4-bit hex value */ 00104 u08 Character = Data&0x0f; 00105 if (Character>9) 00106 { 00107 Character+='A'-10; 00108 } 00109 else 00110 { 00111 Character+='0'; 00112 } 00113 UART_SendByte(Character); 00114 #endif 00115 } 00116 00117 void UART_Printfu08(u08 Data) 00118 { 00119 /* Send 8-bit hex value */ 00120 UART_PrintfU4(Data>>4); 00121 UART_PrintfU4(Data); 00122 } 00123 00124 void UART_Printfu16(u16 Data) 00125 { 00126 /* Send 16-bit hex value */ 00127 UART_Printfu08(Data>>8); 00128 UART_Printfu08(Data); 00129 } 00130 00131 void UART_Printfu32(u32 Data) 00132 { 00133 /* Send 32-bit hex value */ 00134 UART_Printfu16(Data>>16); 00135 UART_Printfu16(Data); 00136 } 00137 00138 void UART_Init(void) 00139 { 00140 UART_Ready = 1; 00141 UART_ReceivedChar = 0; 00142 pUART_Buffer = 0; 00143 00144 // Enable 2x speed 00145 UCSRA = (1<<U2X); 00146 00147 // Enable receiver and transmitter 00148 UCSRB = (1<<RXEN)|(1<<TXEN)|(1<<RXCIE)|(0<<TXCIE); 00149 00150 // Async. mode, 8N1 00151 UCSRC = (0<<UMSEL)|(0<<UPM0)|(0<<USBS)|(3<<UCSZ0)|(0<<UCPOL); 00152 00153 /* set baud rate */ 00154 UBRRL = UART_BAUD_SELECT; 00155 00156 00157 } 00158 00159 void UART_Shutdown(void) 00160 { 00161 // disable 2x speed 00162 UCSRA = 0; 00163 00164 // disable receiver and transmitter 00165 UCSRB =0; 00166 00167 // disable Async. mode, 8N1 00168 UCSRC = 0; 00169 } 00170 00171 unsigned char UART_HasChar(void) 00172 { 00173 return UART_ReceivedChar; 00174 } 00175 00176 00177 void UART_Puts(u08* pBuf) 00178 { 00179 while (*pBuf) 00180 { 00181 if (*pBuf == '\n') 00182 UART_SendByte('\r'); // for stupid terminal program 00183 UART_SendByte(*pBuf++); 00184 } 00185 } 00186 00187 void UART_Putsln(u08* pBuf) 00188 { 00189 UART_Puts(pBuf); 00190 UART_PrintfEndOfLine(); 00191 } 00192