Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. #define F_CPU 16000000UL
  4. #define BAUD 9600
  5. #define BDIV (F_CPU/16/BAUD)-1
  6. #define tByte unsigned char
  7. #define tWord unsigned int
  8. #define LED0 PB0
  9. #define LED1 PB1
  10. #define BUF_SIZE 30 // Buffer size
  11. volatile tByte buf[BUF_SIZE]; // Buffer
  12. volatile tByte head=0; // Put
  13. volatile tByte tail=0; // Get
  14. tByte fBufIn(tByte d) { // Put data into buffer
  15. if (((head+1)%BUF_SIZE)==tail) return 0xFF;
  16. buf[head]=d;
  17. head = ++head % BUF_SIZE;
  18. UCSR0B |= (1<<UDRIE0); // Data empty intr enabled
  19. return 0;
  20. }
  21. tByte fBufOut(void) { // Get data from buffer
  22. tByte d;
  23. if (head==tail) return 0xFF;
  24. d=buf[tail];
  25. tail = ++tail % BUF_SIZE;
  26. return d;
  27. }
  28. void fUART_Init(void) {
  29. //- Set baud rate
  30. UBRR0H = (tByte)(BDIV>>8);
  31. UBRR0L = (tByte)BDIV;
  32. //- Transmitter & receiver enabled
  33. UCSR0B = (1<<RXEN0)|(1<<TXEN0);
  34. //- UART, 1 stop, 8-bit data, non-parity
  35. UCSR0C = (3<<UCSZ00);
  36. UCSR0B |= (1<<RXCIE0); // Rx intr enabled
  37. }
  38. void fPrintf(tByte *s){
  39. int i=0;
  40. while (s[i] != "\0"){
  41. fBufIn(s[i]);
  42. i++;
  43. }
  44. }
  45. int main(void) {
  46. fUART_Init();
  47. DDRB |= (1<<LED1)|(1<<LED0);
  48. PORTB |= (1<<LED1)|(0<<LED0);
  49. sei();
  50. fPrintf("hell");
  51. while (1) {}
  52. }
  53. ISR(USART_UDRE_vect) { // Data empty's ISR
  54. tByte dat;
  55. dat=fBufOut(); // Get data from buf
  56. if (dat!=0xFF) { // Check valid data
  57. UDR0 = dat; // Transmit data
  58. } else UCSR0B &= ~(1<<UDRIE0); // Data empty intr disabled
  59. PORTB ^= (1<<LED1)|(1<<LED0);
  60. }
  61. ISR(USART_RX_vect) { // Rx complete's ISR
  62. tByte dat;
  63. dat = UDR0; // Data received
  64. fBufIn(dat); // Put data into buf
  65. PORTB ^= (1<<LED1)|(1<<LED0);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement