Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <util/delay.h>
- #include <avr/interrupt.h>
- #define XTAL 8000000L
- #define BAUDRATE 9600L
- #define BAUDDIVIDER (XTAL/(16*BAUDRATE)-1)
- #define BUFFER_SIZE 16
- void init(void);
- void initTransmition(void);
- char buffer[BUFFER_SIZE] = "0123456789ABCDEF";
- unsigned char bufferIndex = 0;
- int main(void) {
- init();
- sei();
- while(1) {
- if(PINB & (1 << PINB0)) {
- initTransmition();
- }
- PORTB = 0b00000000;
- _delay_ms(1000);
- PORTB = 0b10000000;
- _delay_ms(1000);
- }
- return 0;
- }
- void init(void) {
- DDRB = 0b11111110;
- PORTB = 0x00;
- UBRRH = BAUDDIVIDER >> 8;
- UBRRL = BAUDDIVIDER & 0xFF;
- UCSRA = 0;
- UCSRB = (1 << RXEN) | (1 << TXEN) | (0 << RXCIE) | (0 << TXCIE) | (0 << UDRIE);
- UCSRC = (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1);
- }
- void initTransmition(void) {
- bufferIndex = 0;
- UDR = buffer[0];
- UCSRB |= (1 << UDRIE);
- }
- ISR(USART_UDRE_vect) {
- bufferIndex++;
- if(bufferIndex == BUFFER_SIZE) {
- UCSRB &= ~(1 << UDRIE);
- } else {
- UDR = buffer[bufferIndex];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment