Simple single-color 8x8x8 LED Cube with AVRs
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /*************************************************************************
  2. Title: Interrupt UART library with receive/transmit circular buffers
  3. Author: Peter Fleury <pfleury@gmx.ch> http://jump.to/fleury
  4. File: $Id: uart.c,v 1.6.2.2 2009/11/29 08:56:12 Peter Exp $
  5. Software: AVR-GCC 4.1, AVR Libc 1.4.6 or higher
  6. Hardware: any AVR with built-in UART,
  7. License: GNU General Public License
  8. DESCRIPTION:
  9. An interrupt is generated when the UART has finished transmitting or
  10. receiving a byte. The interrupt handling routines use circular buffers
  11. for buffering received and transmitted data.
  12. The UART_RX_BUFFER_SIZE and UART_TX_BUFFER_SIZE variables define
  13. the buffer size in bytes. Note that these variables must be a
  14. power of 2.
  15. USAGE:
  16. Refere to the header file uart.h for a description of the routines.
  17. See also example test_uart.c.
  18. NOTES:
  19. Based on Atmel Application Note AVR306
  20. LICENSE:
  21. Copyright (C) 2006 Peter Fleury
  22. This program is free software; you can redistribute it and/or modify
  23. it under the terms of the GNU General Public License as published by
  24. the Free Software Foundation; either version 2 of the License, or
  25. any later version.
  26. This program is distributed in the hope that it will be useful,
  27. but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. GNU General Public License for more details.
  30. *************************************************************************/
  31. #include <avr/io.h>
  32. #include <avr/interrupt.h>
  33. #include <avr/pgmspace.h>
  34. #include "uart.h"
  35. /*
  36. * constants and macros
  37. */
  38. /* size of RX/TX buffers */
  39. #define UART_RX_BUFFER_MASK ( UART_RX_BUFFER_SIZE - 1)
  40. #define UART_TX_BUFFER_MASK ( UART_TX_BUFFER_SIZE - 1)
  41. #if ( UART_RX_BUFFER_SIZE & UART_RX_BUFFER_MASK )
  42. #error RX buffer size is not a power of 2
  43. #endif
  44. #if ( UART_TX_BUFFER_SIZE & UART_TX_BUFFER_MASK )
  45. #error TX buffer size is not a power of 2
  46. #endif
  47. #if defined(__AVR_AT90S2313__) \
  48. || defined(__AVR_AT90S4414__) || defined(__AVR_AT90S4434__) \
  49. || defined(__AVR_AT90S8515__) || defined(__AVR_AT90S8535__) \
  50. || defined(__AVR_ATmega103__)
  51. /* old AVR classic or ATmega103 with one UART */
  52. #define AT90_UART
  53. #define UART0_RECEIVE_INTERRUPT SIG_UART_RECV
  54. #define UART0_TRANSMIT_INTERRUPT SIG_UART_DATA
  55. #define UART0_STATUS USR
  56. #define UART0_CONTROL UCR
  57. #define UART0_DATA UDR
  58. #define UART0_UDRIE UDRIE
  59. #elif defined(__AVR_AT90S2333__) || defined(__AVR_AT90S4433__)
  60. /* old AVR classic with one UART */
  61. #define AT90_UART
  62. #define UART0_RECEIVE_INTERRUPT SIG_UART_RECV
  63. #define UART0_TRANSMIT_INTERRUPT SIG_UART_DATA
  64. #define UART0_STATUS UCSRA
  65. #define UART0_CONTROL UCSRB
  66. #define UART0_DATA UDR
  67. #define UART0_UDRIE UDRIE
  68. #elif defined(__AVR_ATmega8__) || defined(__AVR_ATmega16__) || defined(__AVR_ATmega32__) \
  69. || defined(__AVR_ATmega8515__) || defined(__AVR_ATmega8535__) \
  70. || defined(__AVR_ATmega323__)
  71. /* ATmega with one USART */
  72. #define ATMEGA_USART
  73. #define UART0_RECEIVE_INTERRUPT SIG_UART_RECV
  74. #define UART0_TRANSMIT_INTERRUPT SIG_UART_DATA
  75. #define UART0_STATUS UCSRA
  76. #define UART0_CONTROL UCSRB
  77. #define UART0_DATA UDR
  78. #define UART0_UDRIE UDRIE
  79. #elif defined(__AVR_ATmega163__)
  80. /* ATmega163 with one UART */
  81. #define ATMEGA_UART
  82. #define UART0_RECEIVE_INTERRUPT SIG_UART_RECV
  83. #define UART0_TRANSMIT_INTERRUPT SIG_UART_DATA
  84. #define UART0_STATUS UCSRA
  85. #define UART0_CONTROL UCSRB
  86. #define UART0_DATA UDR
  87. #define UART0_UDRIE UDRIE
  88. #elif defined(__AVR_ATmega162__)
  89. /* ATmega with two USART */
  90. #define ATMEGA_USART0
  91. #define ATMEGA_USART1
  92. #define UART0_RECEIVE_INTERRUPT SIG_USART0_RECV
  93. #define UART1_RECEIVE_INTERRUPT SIG_USART1_RECV
  94. #define UART0_TRANSMIT_INTERRUPT SIG_USART0_DATA
  95. #define UART1_TRANSMIT_INTERRUPT SIG_USART1_DATA
  96. #define UART0_STATUS UCSR0A
  97. #define UART0_CONTROL UCSR0B
  98. #define UART0_DATA UDR0
  99. #define UART0_UDRIE UDRIE0
  100. #define UART1_STATUS UCSR1A
  101. #define UART1_CONTROL UCSR1B
  102. #define UART1_DATA UDR1
  103. #define UART1_UDRIE UDRIE1
  104. #elif defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__)
  105. /* ATmega with two USART */
  106. #define ATMEGA_USART0
  107. #define ATMEGA_USART1
  108. #define UART0_RECEIVE_INTERRUPT SIG_UART0_RECV
  109. #define UART1_RECEIVE_INTERRUPT SIG_UART1_RECV
  110. #define UART0_TRANSMIT_INTERRUPT SIG_UART0_DATA
  111. #define UART1_TRANSMIT_INTERRUPT SIG_UART1_DATA
  112. #define UART0_STATUS UCSR0A
  113. #define UART0_CONTROL UCSR0B
  114. #define UART0_DATA UDR0
  115. #define UART0_UDRIE UDRIE0
  116. #define UART1_STATUS UCSR1A
  117. #define UART1_CONTROL UCSR1B
  118. #define UART1_DATA UDR1
  119. #define UART1_UDRIE UDRIE1
  120. #elif defined(__AVR_ATmega161__)
  121. /* ATmega with UART */
  122. #error "AVR ATmega161 currently not supported by this libaray !"
  123. #elif defined(__AVR_ATmega169__)
  124. /* ATmega with one USART */
  125. #define ATMEGA_USART
  126. #define UART0_RECEIVE_INTERRUPT SIG_USART_RECV
  127. #define UART0_TRANSMIT_INTERRUPT SIG_USART_DATA
  128. #define UART0_STATUS UCSRA
  129. #define UART0_CONTROL UCSRB
  130. #define UART0_DATA UDR
  131. #define UART0_UDRIE UDRIE
  132. #elif defined(__AVR_ATmega48__) ||defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega48P__) || defined(__AVR_ATmega88P__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
  133. /* ATmega with one USART */
  134. #define ATMEGA_USART0
  135. #define UART0_RECEIVE_INTERRUPT SIG_USART_RECV
  136. #define UART0_TRANSMIT_INTERRUPT SIG_USART_DATA
  137. #define UART0_STATUS UCSR0A
  138. #define UART0_CONTROL UCSR0B
  139. #define UART0_DATA UDR0
  140. #define UART0_UDRIE UDRIE0
  141. #elif defined(__AVR_ATtiny2313__)
  142. #define ATMEGA_USART
  143. #define UART0_RECEIVE_INTERRUPT SIG_USART0_RX
  144. #define UART0_TRANSMIT_INTERRUPT SIG_USART0_UDRE
  145. #define UART0_STATUS UCSRA
  146. #define UART0_CONTROL UCSRB
  147. #define UART0_DATA UDR
  148. #define UART0_UDRIE UDRIE
  149. #elif defined(__AVR_ATmega329__) ||defined(__AVR_ATmega3290__) ||\
  150. defined(__AVR_ATmega649__) ||defined(__AVR_ATmega6490__) ||\
  151. defined(__AVR_ATmega325__) ||defined(__AVR_ATmega3250__) ||\
  152. defined(__AVR_ATmega645__) ||defined(__AVR_ATmega6450__)
  153. /* ATmega with one USART */
  154. #define ATMEGA_USART0
  155. #define UART0_RECEIVE_INTERRUPT SIG_UART_RECV
  156. #define UART0_TRANSMIT_INTERRUPT SIG_UART_DATA
  157. #define UART0_STATUS UCSR0A
  158. #define UART0_CONTROL UCSR0B
  159. #define UART0_DATA UDR0
  160. #define UART0_UDRIE UDRIE0
  161. #elif defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega640__)
  162. /* ATmega with two USART */
  163. #define ATMEGA_USART0
  164. #define ATMEGA_USART1
  165. #define UART0_RECEIVE_INTERRUPT SIG_USART0_RECV
  166. #define UART1_RECEIVE_INTERRUPT SIG_USART1_RECV
  167. #define UART0_TRANSMIT_INTERRUPT SIG_USART0_DATA
  168. #define UART1_TRANSMIT_INTERRUPT SIG_USART1_DATA
  169. #define UART0_STATUS UCSR0A
  170. #define UART0_CONTROL UCSR0B
  171. #define UART0_DATA UDR0
  172. #define UART0_UDRIE UDRIE0
  173. #define UART1_STATUS UCSR1A
  174. #define UART1_CONTROL UCSR1B
  175. #define UART1_DATA UDR1
  176. #define UART1_UDRIE UDRIE1
  177. #elif defined(__AVR_ATmega644__)
  178. /* ATmega with one USART */
  179. #define ATMEGA_USART0
  180. #define UART0_RECEIVE_INTERRUPT SIG_USART_RECV
  181. #define UART0_TRANSMIT_INTERRUPT SIG_USART_DATA
  182. #define UART0_STATUS UCSR0A
  183. #define UART0_CONTROL UCSR0B
  184. #define UART0_DATA UDR0
  185. #define UART0_UDRIE UDRIE0
  186. #elif defined(__AVR_ATmega164P__) || defined(__AVR_ATmega324P__) || defined(__AVR_ATmega644P__)
  187. /* ATmega with two USART */
  188. #define ATMEGA_USART0
  189. #define ATMEGA_USART1
  190. #define UART0_RECEIVE_INTERRUPT SIG_USART_RECV
  191. #define UART1_RECEIVE_INTERRUPT SIG_USART1_RECV
  192. #define UART0_TRANSMIT_INTERRUPT SIG_USART_DATA
  193. #define UART1_TRANSMIT_INTERRUPT SIG_USART1_DATA
  194. #define UART0_STATUS UCSR0A
  195. #define UART0_CONTROL UCSR0B
  196. #define UART0_DATA UDR0
  197. #define UART0_UDRIE UDRIE0
  198. #define UART1_STATUS UCSR1A
  199. #define UART1_CONTROL UCSR1B
  200. #define UART1_DATA UDR1
  201. #define UART1_UDRIE UDRIE1
  202. #else
  203. #error "no UART definition for MCU available"
  204. #endif
  205. /*
  206. * module global variables
  207. */
  208. static volatile unsigned char UART_TxBuf[UART_TX_BUFFER_SIZE];
  209. static volatile unsigned char UART_RxBuf[UART_RX_BUFFER_SIZE];
  210. static volatile unsigned char UART_TxHead;
  211. static volatile unsigned char UART_TxTail;
  212. static volatile unsigned char UART_RxHead;
  213. static volatile unsigned char UART_RxTail;
  214. static volatile unsigned char UART_LastRxError;
  215. #if defined( ATMEGA_USART1 )
  216. static volatile unsigned char UART1_TxBuf[UART_TX_BUFFER_SIZE];
  217. static volatile unsigned char UART1_RxBuf[UART_RX_BUFFER_SIZE];
  218. static volatile unsigned char UART1_TxHead;
  219. static volatile unsigned char UART1_TxTail;
  220. static volatile unsigned char UART1_RxHead;
  221. static volatile unsigned char UART1_RxTail;
  222. static volatile unsigned char UART1_LastRxError;
  223. #endif
  224. SIGNAL(UART0_RECEIVE_INTERRUPT)
  225. /*************************************************************************
  226. Function: UART Receive Complete interrupt
  227. Purpose: called when the UART has received a character
  228. **************************************************************************/
  229. {
  230. unsigned char tmphead;
  231. unsigned char data;
  232. unsigned char usr;
  233. unsigned char lastRxError;
  234. /* read UART status register and UART data register */
  235. usr = UART0_STATUS;
  236. data = UART0_DATA;
  237. /* */
  238. #if defined( AT90_UART )
  239. lastRxError = (usr & (_BV(FE)|_BV(DOR)) );
  240. #elif defined( ATMEGA_USART )
  241. lastRxError = (usr & (_BV(FE)|_BV(DOR)) );
  242. #elif defined( ATMEGA_USART0 )
  243. lastRxError = (usr & (_BV(FE0)|_BV(DOR0)) );
  244. #elif defined ( ATMEGA_UART )
  245. lastRxError = (usr & (_BV(FE)|_BV(DOR)) );
  246. #endif
  247. /* calculate buffer index */
  248. tmphead = ( UART_RxHead + 1) & UART_RX_BUFFER_MASK;
  249. if ( tmphead == UART_RxTail ) {
  250. /* error: receive buffer overflow */
  251. lastRxError = UART_BUFFER_OVERFLOW >> 8;
  252. }else{
  253. /* store new index */
  254. UART_RxHead = tmphead;
  255. /* store received data in buffer */
  256. UART_RxBuf[tmphead] = data;
  257. }
  258. UART_LastRxError = lastRxError;
  259. }
  260. SIGNAL(UART0_TRANSMIT_INTERRUPT)
  261. /*************************************************************************
  262. Function: UART Data Register Empty interrupt
  263. Purpose: called when the UART is ready to transmit the next byte
  264. **************************************************************************/
  265. {
  266. unsigned char tmptail;
  267. if ( UART_TxHead != UART_TxTail) {
  268. /* calculate and store new buffer index */
  269. tmptail = (UART_TxTail + 1) & UART_TX_BUFFER_MASK;
  270. UART_TxTail = tmptail;
  271. /* get one byte from buffer and write it to UART */
  272. UART0_DATA = UART_TxBuf[tmptail]; /* start transmission */
  273. }else{
  274. /* tx buffer empty, disable UDRE interrupt */
  275. UART0_CONTROL &= ~_BV(UART0_UDRIE);
  276. }
  277. }
  278. /*************************************************************************
  279. Function: uart_init()
  280. Purpose: initialize UART and set baudrate
  281. Input: baudrate using macro UART_BAUD_SELECT()
  282. Returns: none
  283. **************************************************************************/
  284. void uart_init(unsigned int baudrate)
  285. {
  286. UART_TxHead = 0;
  287. UART_TxTail = 0;
  288. UART_RxHead = 0;
  289. UART_RxTail = 0;
  290. #if defined( AT90_UART )
  291. /* set baud rate */
  292. UBRR = (unsigned char)baudrate;
  293. /* enable UART receiver and transmmitter and receive complete interrupt */
  294. UART0_CONTROL = _BV(RXCIE)|_BV(RXEN)|_BV(TXEN);
  295. #elif defined (ATMEGA_USART)
  296. /* Set baud rate */
  297. if ( baudrate & 0x8000 )
  298. {
  299. UART0_STATUS = (1<<U2X); //Enable 2x speed
  300. baudrate &= ~0x8000;
  301. }
  302. UBRRH = (unsigned char)(baudrate>>8);
  303. UBRRL = (unsigned char) baudrate;
  304. /* Enable USART receiver and transmitter and receive complete interrupt */
  305. UART0_CONTROL = _BV(RXCIE)|(1<<RXEN)|(1<<TXEN);
  306. /* Set frame format: asynchronous, 8data, no parity, 1stop bit */
  307. #ifdef URSEL
  308. UCSRC = (1<<URSEL)|(3<<UCSZ0);
  309. #else
  310. UCSRC = (3<<UCSZ0);
  311. #endif
  312. #elif defined (ATMEGA_USART0 )
  313. /* Set baud rate */
  314. if ( baudrate & 0x8000 )
  315. {
  316. UART0_STATUS = (1<<U2X0); //Enable 2x speed
  317. baudrate &= ~0x8000;
  318. }
  319. UBRR0H = (unsigned char)(baudrate>>8);
  320. UBRR0L = (unsigned char) baudrate;
  321. /* Enable USART receiver and transmitter and receive complete interrupt */
  322. UART0_CONTROL = _BV(RXCIE0)|(1<<RXEN0)|(1<<TXEN0);
  323. /* Set frame format: asynchronous, 8data, no parity, 1stop bit */
  324. #ifdef URSEL0
  325. UCSR0C = (1<<URSEL0)|(3<<UCSZ00);
  326. #else
  327. UCSR0C = (3<<UCSZ00);
  328. #endif
  329. #elif defined ( ATMEGA_UART )
  330. /* set baud rate */
  331. if ( baudrate & 0x8000 )
  332. {
  333. UART0_STATUS = (1<<U2X); //Enable 2x speed
  334. baudrate &= ~0x8000;
  335. }
  336. UBRRHI = (unsigned char)(baudrate>>8);
  337. UBRR = (unsigned char) baudrate;
  338. /* Enable UART receiver and transmitter and receive complete interrupt */
  339. UART0_CONTROL = _BV(RXCIE)|(1<<RXEN)|(1<<TXEN);
  340. #endif
  341. }/* uart_init */
  342. /*************************************************************************
  343. Function: uart_getc()
  344. Purpose: return byte from ringbuffer
  345. Returns: lower byte: received byte from ringbuffer
  346. higher byte: last receive error
  347. **************************************************************************/
  348. unsigned int uart_getc(void)
  349. {
  350. unsigned char tmptail;
  351. unsigned char data;
  352. if ( UART_RxHead == UART_RxTail ) {
  353. return UART_NO_DATA; /* no data available */
  354. }
  355. /* calculate /store buffer index */
  356. tmptail = (UART_RxTail + 1) & UART_RX_BUFFER_MASK;
  357. UART_RxTail = tmptail;
  358. /* get data from receive buffer */
  359. data = UART_RxBuf[tmptail];
  360. return (UART_LastRxError << 8) + data;
  361. }/* uart_getc */
  362. /*************************************************************************
  363. Function: uart_putc()
  364. Purpose: write byte to ringbuffer for transmitting via UART
  365. Input: byte to be transmitted
  366. Returns: none
  367. **************************************************************************/
  368. void uart_putc(unsigned char data)
  369. {
  370. unsigned char tmphead;
  371. tmphead = (UART_TxHead + 1) & UART_TX_BUFFER_MASK;
  372. while ( tmphead == UART_TxTail ){
  373. ;/* wait for free space in buffer */
  374. }
  375. UART_TxBuf[tmphead] = data;
  376. UART_TxHead = tmphead;
  377. /* enable UDRE interrupt */
  378. UART0_CONTROL |= _BV(UART0_UDRIE);
  379. }/* uart_putc */
  380. /*************************************************************************
  381. Function: uart_puts()
  382. Purpose: transmit string to UART
  383. Input: string to be transmitted
  384. Returns: none
  385. **************************************************************************/
  386. void uart_puts(const char *s )
  387. {
  388. while (*s)
  389. uart_putc(*s++);
  390. }/* uart_puts */
  391. /*************************************************************************
  392. Function: uart_puts_p()
  393. Purpose: transmit string from program memory to UART
  394. Input: program memory string to be transmitted
  395. Returns: none
  396. **************************************************************************/
  397. void uart_puts_p(const char *progmem_s )
  398. {
  399. register char c;
  400. while ( (c = pgm_read_byte(progmem_s++)) )
  401. uart_putc(c);
  402. }/* uart_puts_p */
  403. /*
  404. * these functions are only for ATmegas with two USART
  405. */
  406. #if defined( ATMEGA_USART1 )
  407. SIGNAL(UART1_RECEIVE_INTERRUPT)
  408. /*************************************************************************
  409. Function: UART1 Receive Complete interrupt
  410. Purpose: called when the UART1 has received a character
  411. **************************************************************************/
  412. {
  413. unsigned char tmphead;
  414. unsigned char data;
  415. unsigned char usr;
  416. unsigned char lastRxError;
  417. /* read UART status register and UART data register */
  418. usr = UART1_STATUS;
  419. data = UART1_DATA;
  420. /* */
  421. lastRxError = (usr & (_BV(FE1)|_BV(DOR1)) );
  422. /* calculate buffer index */
  423. tmphead = ( UART1_RxHead + 1) & UART_RX_BUFFER_MASK;
  424. if ( tmphead == UART1_RxTail ) {
  425. /* error: receive buffer overflow */
  426. lastRxError = UART_BUFFER_OVERFLOW >> 8;
  427. }else{
  428. /* store new index */
  429. UART1_RxHead = tmphead;
  430. /* store received data in buffer */
  431. UART1_RxBuf[tmphead] = data;
  432. }
  433. UART1_LastRxError = lastRxError;
  434. }
  435. SIGNAL(UART1_TRANSMIT_INTERRUPT)
  436. /*************************************************************************
  437. Function: UART1 Data Register Empty interrupt
  438. Purpose: called when the UART1 is ready to transmit the next byte
  439. **************************************************************************/
  440. {
  441. unsigned char tmptail;
  442. if ( UART1_TxHead != UART1_TxTail) {
  443. /* calculate and store new buffer index */
  444. tmptail = (UART1_TxTail + 1) & UART_TX_BUFFER_MASK;
  445. UART1_TxTail = tmptail;
  446. /* get one byte from buffer and write it to UART */
  447. UART1_DATA = UART1_TxBuf[tmptail]; /* start transmission */
  448. }else{
  449. /* tx buffer empty, disable UDRE interrupt */
  450. UART1_CONTROL &= ~_BV(UART1_UDRIE);
  451. }
  452. }
  453. /*************************************************************************
  454. Function: uart1_init()
  455. Purpose: initialize UART1 and set baudrate
  456. Input: baudrate using macro UART_BAUD_SELECT()
  457. Returns: none
  458. **************************************************************************/
  459. void uart1_init(unsigned int baudrate)
  460. {
  461. UART1_TxHead = 0;
  462. UART1_TxTail = 0;
  463. UART1_RxHead = 0;
  464. UART1_RxTail = 0;
  465. /* Set baud rate */
  466. if ( baudrate & 0x8000 )
  467. {
  468. UART1_STATUS = (1<<U2X1); //Enable 2x speed
  469. baudrate &= ~0x8000;
  470. }
  471. UBRR1H = (unsigned char)(baudrate>>8);
  472. UBRR1L = (unsigned char) baudrate;
  473. /* Enable USART receiver and transmitter and receive complete interrupt */
  474. UART1_CONTROL = _BV(RXCIE1)|(1<<RXEN1)|(1<<TXEN1);
  475. /* Set frame format: asynchronous, 8data, no parity, 1stop bit */
  476. #ifdef URSEL1
  477. UCSR1C = (1<<URSEL1)|(3<<UCSZ10);
  478. #else
  479. UCSR1C = (3<<UCSZ10);
  480. #endif
  481. }/* uart_init */
  482. /*************************************************************************
  483. Function: uart1_getc()
  484. Purpose: return byte from ringbuffer
  485. Returns: lower byte: received byte from ringbuffer
  486. higher byte: last receive error
  487. **************************************************************************/
  488. unsigned int uart1_getc(void)
  489. {
  490. unsigned char tmptail;
  491. unsigned char data;
  492. if ( UART1_RxHead == UART1_RxTail ) {
  493. return UART_NO_DATA; /* no data available */
  494. }
  495. /* calculate /store buffer index */
  496. tmptail = (UART1_RxTail + 1) & UART_RX_BUFFER_MASK;
  497. UART1_RxTail = tmptail;
  498. /* get data from receive buffer */
  499. data = UART1_RxBuf[tmptail];
  500. return (UART1_LastRxError << 8) + data;
  501. }/* uart1_getc */
  502. /*************************************************************************
  503. Function: uart1_putc()
  504. Purpose: write byte to ringbuffer for transmitting via UART
  505. Input: byte to be transmitted
  506. Returns: none
  507. **************************************************************************/
  508. void uart1_putc(unsigned char data)
  509. {
  510. unsigned char tmphead;
  511. tmphead = (UART1_TxHead + 1) & UART_TX_BUFFER_MASK;
  512. while ( tmphead == UART1_TxTail ){
  513. ;/* wait for free space in buffer */
  514. }
  515. UART1_TxBuf[tmphead] = data;
  516. UART1_TxHead = tmphead;
  517. /* enable UDRE interrupt */
  518. UART1_CONTROL |= _BV(UART1_UDRIE);
  519. }/* uart1_putc */
  520. /*************************************************************************
  521. Function: uart1_puts()
  522. Purpose: transmit string to UART1
  523. Input: string to be transmitted
  524. Returns: none
  525. **************************************************************************/
  526. void uart1_puts(const char *s )
  527. {
  528. while (*s)
  529. uart1_putc(*s++);
  530. }/* uart1_puts */
  531. /*************************************************************************
  532. Function: uart1_puts_p()
  533. Purpose: transmit string from program memory to UART1
  534. Input: program memory string to be transmitted
  535. Returns: none
  536. **************************************************************************/
  537. void uart1_puts_p(const char *progmem_s )
  538. {
  539. register char c;
  540. while ( (c = pgm_read_byte(progmem_s++)) )
  541. uart1_putc(c);
  542. }/* uart1_puts_p */
  543. #endif