Advertisement
Guest User

Printf example avr

a guest
Apr 27th, 2015
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.86 KB | None | 0 0
  1. #define F_CPU 16000000UL    // 1MHz
  2. #include <avr/io.h>
  3. #include <stdio.h>
  4. #include <util/delay.h>
  5.  
  6.  
  7. void init_uart(void);
  8. static int uart_putc(char data, FILE *stream);
  9.  
  10. static FILE mystdout = FDEV_SETUP_STREAM(uart_putc, NULL, _FDEV_SETUP_WRITE);
  11.  
  12.  
  13. int main(void)
  14. {
  15.      
  16.     stdout=&mystdout;
  17.     DDRB |= (1<<PB1);
  18.      
  19.     init_uart();
  20.      
  21.     while(1)
  22.     {
  23.         if (!(PINB & (1<<PB0)))
  24.         {
  25.             printf("AABBCC\r\n");
  26.             PORTB |= (1<<PB1);
  27.             _delay_ms(500);
  28.             PORTB &= ~(1<<PB1);
  29.             while (!(PINB & (1<<PB0))){}
  30.         }
  31.          
  32.     }
  33. }
  34.  
  35. void init_uart(void){
  36.     UBRR1 = 103;
  37.     UCSR1B = (1<<RXEN1)|(1<<TXEN1);
  38. }
  39.  
  40. static int uart_putc(char data, FILE *stream){
  41.     if (data == '\n') uart_putc('\r', stream);
  42. while (!(UCSR1A & (1<<UDRE1))){};
  43. UDR1 = data;
  44. return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement