PetrovIgor

USART test. Atmega8. Without interrupts.

Aug 12th, 2012
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.84 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <util/delay.h>
  3. #include <avr/interrupt.h>
  4.  
  5. #define XTAL 8000000L
  6. #define BAUDRATE 9600L
  7. #define BAUDDIVIDER (XTAL/(16*BAUDRATE)-1)
  8.  
  9. void init(void);
  10. void sendString(char *str);
  11. void sendByte(char byte);
  12.  
  13. char *string = "I am USART test message";
  14.  
  15. int main(void) {
  16.  
  17.     init();
  18.  
  19.     sei();
  20.  
  21.     sendString(string);
  22.  
  23.     return 0;
  24.  
  25. }
  26.  
  27. void init(void) {
  28.     DDRB = 0xff;
  29.     PORTB = 0xff;
  30.     UBRRH = BAUDDIVIDER >> 8;
  31.     UBRRL = BAUDDIVIDER & 0xFF;
  32.  
  33.     UCSRA = 0;
  34.     UCSRB = (1 << RXEN) | (1 << TXEN) | (1 << RXCIE) | (1 << TXCIE) | (0 << UDRIE);
  35.     UCSRC = (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1);
  36. }
  37.  
  38. void sendString(char *str) {
  39.     while(*str != '\0') {
  40.         sendByte(*str);
  41.         str++;
  42.     }
  43. }
  44.  
  45. void sendByte(char byte) {
  46.     while(!(UCSRA & (1 << UDRE)));
  47.     UDR = byte;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment