Created
June 16, 2013 23:02
-
-
Save dtudury/5793779 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* lights.c | |
* | |
* Created: 6/14/2013 11:30:35 PM | |
* Author: dtudury | |
*/ | |
#ifndef F_CPU | |
#define F_CPU 1000000UL // or whatever may be your frequency | |
#endif | |
#include <avr/io.h> | |
#include <util/delay.h> | |
static uint16_t meters = 5; | |
static uint16_t ledsPerMeter = 32; | |
static uint16_t numLEDs = 5 * 32; | |
int *pixels; | |
int main(void) | |
{ | |
InitADC(); | |
DDRC = 0b00000011; | |
pixels = (int *)malloc(numLEDs * 3); | |
uint16_t i; | |
while(1) | |
{ | |
for(i=0; i<numLEDs; i++) { | |
pixels[i * 3] += ReadADC(3) + 29 * i;// + ReadADC(1) + ReadADC(2) + ReadADC(3) + ReadADC(4) + ReadADC(5); | |
pixels[i * 3 + 1] += ReadADC(4) + 33 * i;// + ReadADC(1) + ReadADC(2) + ReadADC(3) + ReadADC(4) + ReadADC(5); | |
pixels[i * 3 + 2] += ReadADC(5) + 31 * i;// + ReadADC(1) + ReadADC(2) + ReadADC(3) + ReadADC(4) + ReadADC(5); | |
} | |
show(); | |
} | |
} | |
uint8_t map(uint8_t v) { | |
return v % 127 | 0b10000000; | |
} | |
void show(uint16_t r, uint16_t g, uint16_t b) { | |
uint16_t i; | |
write8(0); | |
write8(0); | |
write8(0); | |
write8(0); | |
for (i=0; i<numLEDs*3; i++ ) { | |
write8(map(pixels[i])); | |
} | |
for (i=0; i<meters; i++ ) { | |
write8(0); | |
} | |
// _delay_ms(100); | |
} | |
void write8(uint8_t d) { | |
for (uint8_t i=0; i<8; i++) { | |
writeBit(d & _BV(7-i)); | |
} | |
} | |
void writeBit(uint8_t on) { | |
if (on) PORTC = 0b00000010; | |
else PORTC = 0b00000000; | |
//toggle clock | |
PORTC = 0b00000001; | |
PORTC = 0b00000000; | |
} | |
void InitADC() { | |
// Select Vref=AVcc | |
ADMUX |= (1<<REFS0); | |
//set prescaller to 128 and enable ADC | |
ADCSRA |= (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)|(1<<ADEN); | |
} | |
int ReadADC(int ADCchannel) { | |
//select ADC channel with safety mask | |
ADMUX = (ADMUX & 0xF0) | (ADCchannel & 0x0F); | |
//single conversion mode | |
ADCSRA |= (1<<ADSC); | |
// wait until ADC conversion is complete | |
while( ADCSRA & (1<<ADSC) ); | |
return ADC; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment