Skip to content

Instantly share code, notes, and snippets.

@naquad
Created March 1, 2014 22:37
Show Gist options
  • Save naquad/9298583 to your computer and use it in GitHub Desktop.
Save naquad/9298583 to your computer and use it in GitHub Desktop.
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include <util/delay.h>
// bit twiddling: enable_bit(s), disable_bit(s), toggle_bit(s), set_bit(s),
// basically wrappers for bitwise operations to make code more readable
#include "bitwit.h"
// USART stuff: initUSART, printString, transmitByte, receiveByte
#include "USART.h"
static uint16_t read16(void);
ISR(TIMER1_OVF_vect){
disable_bit(DDRB, PB1);
}
int main(void){
initUSART();
// timer1 mode 14 (Fast PWM with top at ICR1),
// toggle OC1A (aka PB1) and prescaler = 1
enable_bits(TCCR1A, WGM11, COM1A1);
enable_bits(TCCR1B, WGM13, WGM12, CS10);
enable_bit(TIMSK1, TOIE1);
// cycle length with prescale = 1 and CPU frequency = 1 000 000 Hz (1MHz)
// is 20000us = 20ms, or 50Hz
ICR1 = 20000;
sei();
while(1){
printString("ready for input\r\n");
OCR1A = read16();
enable_bit(DDRB, PB1);
}
return 0;
}
static uint16_t read16(void){
uint16_t ret = 0;
while(1){
char c = receiveByte();
if(c == '\r' || c == '\n')
break;
if(c == 127){
transmitByte('\b');
ret /= 10;
}else if(c >= '0' && c <= '9'){
transmitByte(c);
ret = ret * 10 + (c - '0');
}
}
transmitByte('\r');
transmitByte('\n');
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment