Skip to content

Instantly share code, notes, and snippets.

@fivdi
Created October 1, 2015 20:42
Show Gist options
  • Save fivdi/adaa00fa3e7eccfaa4cd to your computer and use it in GitHub Desktop.
Save fivdi/adaa00fa3e7eccfaa4cd to your computer and use it in GitHub Desktop.
#include <avr/io.h>
#include <avr/power.h>
#include <util/delay.h>
#define LED PB0
#define LED_DDR DDRB
#define LED_PORT PORTB
#define USART_BAUDRATE 115200
#define UBRR_VALUE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
#define set_bit(sfr, bit) (_SFR_BYTE(sfr) |= (1 << bit))
#define clear_bit(sfr, bit) (_SFR_BYTE(sfr) &= ~(1 << bit))
#define toggle_bit(sfr, bit) (_SFR_BYTE(sfr) ^= (1 << bit))
void usart_init() {
UBRR0 = UBRR_VALUE;
UCSR0B = (1 << RXEN0) | (1 << TXEN0);
UCSR0C = (1 << UCSZ00) | (1 << UCSZ01); // 8 bits, no parity, 1 stop bit
}
void usart_send(uint8_t ch) {
set_bit(LED_PORT, LED);
while (!(UCSR0A & (1 << UDRE0)));
UDR0 = ch;
clear_bit(LED_PORT, LED);
}
uint8_t usart_receive(void) {
while(!(UCSR0A & (1 << RXC0)));
return UDR0;
}
void send_chars(uint16_t count) {
uint8_t ch = 'a';
while (count != 0) {
usart_send(ch);
if (ch == 'z') {
ch = 'a';
} else {
++ch;
}
--count;
}
}
int main(void) {
clock_prescale_set(clock_div_1); // 8Mhz clock
OSCCAL = 45;
usart_init();
set_bit(LED_DDR, LED); // set LED pin for output
while (1) {
uint8_t command = usart_receive();
switch (command) {
case 'o':
send_chars(1024);
break;
case 't':
send_chars(10240);
break;
case 'b':
default:
usart_send(command);
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment