Skip to content

Instantly share code, notes, and snippets.

@dtudury
Created June 25, 2013 04:35
Show Gist options
  • Save dtudury/5855959 to your computer and use it in GitHub Desktop.
Save dtudury/5855959 to your computer and use it in GitHub Desktop.
avr-studio C code for sparkfun's LED RingCoder Breakout - RGB https://www.sparkfun.com/products/11040
/*
* knob.c
*
* Created: 6/22/2013 9:01:18 PM
* Author: dtudury
*/
#ifndef F_CPU
#define F_CPU 8000000UL // or whatever may be your frequency
#endif
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
//C0 EN
//C1 LATCH
//C2 CLK
//C3 CLR
//C4 DAT
//C5 SW
//D3 GRN
//D6 BLU
//B1 RED
//D0 A
//D1 B
void pwmInit() {
//https://sites.google.com/site/qeewiki/books/avr-guide/pwm-on-the-atmega328
//set up red pin on timer 1, B1 (OCR1A)
TCCR1A = (1 << WGM12) | (1 << WGM10);
TCCR1A |= (1 << COM1A1) | (1 << COM1A0);
TCCR1B = (1 << CS10);
DDRB |= (1 << DDB1);
//set up green pin on timer 2, D3 (OCR2B)
TCCR2A = (1 << WGM21) | (1 << WGM20);
TCCR2A |= (1 << COM2B1) | (1 << COM2B0);
TCCR2B = (1 << CS20);
DDRD |= (1<<DDD3);
//set up blue pin on timer 0, D6 (OCR0A)
TCCR0A = (1 << WGM01) | (1 << WGM00);
TCCR0A |= (1 << COM0A1) | (1 << COM0A0);
TCCR0B = (1 << CS00);
DDRD |= (1<<DDD6);
}
void interruptInit() {
//set up A pin on D0 (PCINT16)
DDRD &= ~(1 << DDD0);
PORTD |= (1 << PORTD0);
PCMSK2 |= (1 << PCINT16);
//set up B pin on D1 (PCINT17)
DDRD &= ~(1 << DDD1);
PORTD |= (1 << PORTD1);
PCMSK2 |= (1 << PCINT17);
PCICR |= (1 << PCIE2);
//set up button (SW) on C5 (PCINT13)
DDRC &= ~(1 << DDC5);
PORTC |= (1 << PORTC5);
PCMSK1 |= (1 << PCINT13);
PCICR |= (1 << PCIE1);
sei(); // turn on interrupts
}
void writePosition(uint16_t d) {
PORTC &= ~(1 << PORTC1); //latch low
for(uint8_t i = 0; i < 16; i++) {
if(d % 16 == i) {
PORTC |= (1 << PORTC4);
} else {
PORTC &= ~(1 << PORTC4);
}
PORTC |= (1 << PORTC2); //toggle clock
PORTC &= ~(1 << PORTC2);
}
PORTC |= (1 << PORTC1); //latch high
}
void writeInit() {
DDRC |= (1 << DDC0);
DDRC |= (1 << DDC1);
DDRC |= (1 << DDC2);
DDRC |= (1 << DDC3);
DDRC |= (1 << DDC4);
PORTC &= ~(1 << PORTC0); //enable
PORTC &= ~(1 << PORTC1); //start latch low
PORTC &= ~(1 << PORTC2); //start clock low
PORTC |= (1 << PORTC3); //start clear high
PORTC &= ~(1 << PORTC4); //start data low
writePosition(0);
}
int main(void) {
pwmInit();
interruptInit();
writeInit();
while(1) {
}
}
int lastA = 0;
int lastB = 0;
int step = 0;
void inc() {
step++;
}
void dec() {
step += 63;
}
ISR (PCINT2_vect) {
int A = PIND & (1 << PIND0);
int B = PIND & (1 << PIND1);
/*
if(A) {
OCR1A = 255; //red pin
} else {
OCR1A = 0;
}
if(B) {
OCR2B = 255; //green pin
} else {
OCR2B = 0;
}
*/
if(A && B) {
if(!lastA && lastB) {
inc();
} else if(lastA && !lastB) {
dec();
}
} else if(A) {
if(lastA && lastB) {
inc();
} else if(!lastA && !lastB) {
dec();
}
} else if(B) {
if(!lastA && !lastB) {
inc();
} else if(lastA && lastB) {
dec();
}
} else {
if(lastA && !lastB) {
inc();
} else if(!lastA && lastB) {
dec();
}
}
writePosition((step + 1) / 4);
lastA = A;
lastB = B;
}
ISR (PCINT1_vect) {
if(PINC & (1 << PINC5)) {
OCR0A = 255; //blue pin
} else {
OCR0A = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment