adjustments for hardware differences
This commit is contained in:
143
code/src/main.c
143
code/src/main.c
@@ -2,24 +2,23 @@
|
|||||||
#include <stdint.h> // has to be added to use uint8_t
|
#include <stdint.h> // has to be added to use uint8_t
|
||||||
#include <avr/interrupt.h> // Needed to use interrupts
|
#include <avr/interrupt.h> // Needed to use interrupts
|
||||||
#include <avr/sleep.h>
|
#include <avr/sleep.h>
|
||||||
|
#include <avr/eeprom.h>
|
||||||
|
|
||||||
|
uint8_t EEMEM on_off_state;
|
||||||
|
uint8_t anim_nr = 0;
|
||||||
|
|
||||||
unsigned long NextVal(void);
|
unsigned long NextVal(void);
|
||||||
unsigned long InitSeed();
|
unsigned long InitSeed(void);
|
||||||
void random(void);
|
void random(void);
|
||||||
void link_rechts(void);
|
void links_rechts(void);
|
||||||
|
|
||||||
int (*animation)(void) = &link_rechts;
|
void (*animation[4])(void);
|
||||||
|
|
||||||
volatile uint8_t leds = 0x00;
|
volatile uint32_t leds = 0x00;
|
||||||
volatile uint8_t leds_active = 0x00;
|
|
||||||
|
|
||||||
volatile uint32_t active_count = 0;
|
volatile uint32_t active_count = 0;
|
||||||
volatile uint32_t active_count_max = 13733; // 1 count = 0,065536 s
|
volatile uint32_t active_count_max = 13733; // 1 count = 0,065536 s
|
||||||
|
|
||||||
volatile uint8_t debounce_lock = 0;
|
|
||||||
volatile uint8_t debounce_count = 0;
|
|
||||||
volatile uint8_t debounce_count_max = 10;
|
|
||||||
|
|
||||||
volatile uint16_t count = 0;
|
volatile uint16_t count = 0;
|
||||||
volatile uint16_t count_max = 3;
|
volatile uint16_t count_max = 3;
|
||||||
|
|
||||||
@@ -42,89 +41,110 @@ void random(){
|
|||||||
int temp;
|
int temp;
|
||||||
do{
|
do{
|
||||||
temp=NextVal();
|
temp=NextVal();
|
||||||
temp=1 << (temp%8);
|
temp=1 << (temp%18);
|
||||||
}while(temp==leds);
|
}while(temp==leds);
|
||||||
leds=temp;
|
leds=temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
void link_rechts(){
|
void links_rechts(){
|
||||||
if(dir)
|
if(dir)
|
||||||
leds = leds << 1;
|
leds = leds << 1;
|
||||||
else
|
else
|
||||||
leds = leds >> 1;
|
leds = leds >> 1;
|
||||||
|
|
||||||
if(!(leds & 0x7F)){
|
if(!(leds & 0x1FFFF)){
|
||||||
leds = 0x08;
|
leds = 0x08;
|
||||||
dir^=1;
|
dir^=1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void write_leds(){
|
void write_leds(void){
|
||||||
if(leds_active){
|
PORTD = leds & 0xFF;
|
||||||
PORTC = leds;
|
PORTC = (leds>>8) & 0x3F;
|
||||||
PORTD = (leds>>1)&((1<<PD1)|(1<<PD2)|(1<<PD3));
|
PORTB = (leds>>14) & 0x07;
|
||||||
PORTD = (leds)&((1<<PD5)|(1<<PD6));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
PORTC = 0;
|
|
||||||
PORTD = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
|
animation[0] = links_rechts;
|
||||||
|
animation[1] = random;
|
||||||
|
|
||||||
|
// reset switch as on/off switch
|
||||||
|
if(eeprom_read_byte(&on_off_state)){
|
||||||
|
eeprom_write_byte(&on_off_state, 0);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
eeprom_write_byte(&on_off_state, 1);
|
||||||
|
set_sleep_mode(SLEEP_MODE_PWR_SAVE);
|
||||||
|
sleep_mode();
|
||||||
|
// power consumption is below 1uA
|
||||||
|
}
|
||||||
|
|
||||||
InitSeed();
|
InitSeed();
|
||||||
DDRC = 0xFF;
|
DDRC = 0xFF;
|
||||||
DDRD = 0xFF;
|
DDRD = 0xFF;
|
||||||
|
DDRB = 0x07;
|
||||||
|
|
||||||
DDRB &= ~(1 << DDB0); // Clear the PB0, PB1, PB2 pin
|
//DDRB &= ~(1 << DDB0); // Clear the PB0, PB1, PB2 pin
|
||||||
// PB0,PB1,PB2 (PCINT0, PCINT1, PCINT2 pin) are now inputs
|
// PB0,PB1,PB2 (PCINT0, PCINT1, PCINT2 pin) are now inputs
|
||||||
|
|
||||||
PORTB |= ((1 << PORTB0) | (1 << PORTB1) | (1 << PORTB2)); // turn On the Pull-up
|
//PORTB |= ((1 << PORTB0) | (1 << PORTB1) | (1 << PORTB2)); // turn On the Pull-up
|
||||||
// PB0, PB1 and PB2 are now inputs with pull-up enabled
|
// PB0, PB1 and PB2 are now inputs with pull-up enabled
|
||||||
|
|
||||||
TIMSK1 |= 1 << TOIE1;
|
//TIMSK1 |= 1 << TOIE1;
|
||||||
|
|
||||||
TCCR0B |= (1<<CS01)|(1<<CS00);
|
TCCR0B |= (1<<CS01)|(1<<CS00);
|
||||||
TIMSK0 |= 1 << TOIE0;
|
TIMSK0 |= 1 << TOIE0;
|
||||||
|
|
||||||
PCICR |= (1 << PCIE0); // set PCIE0 to enable PCMSK0 scan
|
//PCICR |= (1 << PCIE0); // set PCIE0 to enable PCMSK0 scan
|
||||||
PCMSK0 |= (1 << PCINT0); // set PCINT0 to trigger an interrupt on state change
|
//PCMSK0 |= (1 << PCINT0); // set PCINT0 to trigger an interrupt on state change
|
||||||
|
|
||||||
|
ADCSRA |= (1<<ADEN) | (1<<ADPS1) | (1<<ADPS0);
|
||||||
|
ADMUX = 1<<REFS0;
|
||||||
|
ADMUX |= 6;
|
||||||
|
|
||||||
sei(); // turn on interrupts
|
sei(); // turn on interrupts
|
||||||
|
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
write_leds();
|
write_leds();
|
||||||
|
ADCSRA |= 1<<ADSC;
|
||||||
|
while(ADCSRA & (1<<ADSC));
|
||||||
|
if(ADC < 512){
|
||||||
|
if(ADMUX & 0x01)
|
||||||
|
anim_nr = 0;
|
||||||
|
else
|
||||||
|
anim_nr = 1;
|
||||||
|
}
|
||||||
|
ADMUX ^= 0x01;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ISR (PCINT0_vect)
|
//ISR (PCINT0_vect)
|
||||||
{
|
//{
|
||||||
if(!debounce_lock){
|
// if(!debounce_lock){
|
||||||
TCCR1B |= (1<<CS00);
|
// TCCR1B |= (1<<CS00);
|
||||||
debounce_lock=1;
|
// debounce_lock=1;
|
||||||
if((~PINB & 0x01))
|
// if((~PINB & 0x01))
|
||||||
{
|
// {
|
||||||
leds_active ^= 0x01;
|
// leds_active ^= 0x01;
|
||||||
active_count=0;
|
// active_count=0;
|
||||||
if(!leds_active){
|
// if(!leds_active){
|
||||||
write_leds();
|
// write_leds();
|
||||||
TCCR0B &= ~((1<<CS01)|(1<<CS00));
|
// TCCR0B &= ~((1<<CS01)|(1<<CS00));
|
||||||
set_sleep_mode(SLEEP_MODE_PWR_SAVE);
|
// set_sleep_mode(SLEEP_MODE_PWR_SAVE);
|
||||||
sleep_mode();
|
// sleep_mode();
|
||||||
TCCR0B |= (1<<CS01)|(1<<CS00);
|
// TCCR0B |= (1<<CS01)|(1<<CS00);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|
||||||
ISR(TIMER0_OVF_vect){
|
ISR(TIMER0_OVF_vect){
|
||||||
active_count++;
|
active_count++;
|
||||||
if(count>=count_max){
|
if(count>=count_max){
|
||||||
count=0;
|
count=0;
|
||||||
animation();
|
(*animation[anim_nr])();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -132,20 +152,21 @@ ISR(TIMER0_OVF_vect){
|
|||||||
}
|
}
|
||||||
if(active_count >= active_count_max){
|
if(active_count >= active_count_max){
|
||||||
active_count=0;
|
active_count=0;
|
||||||
leds_active = 0;
|
//leds_active = 0;
|
||||||
|
//TODO maybe sleep here
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ISR(TIMER1_OVF_vect){
|
//ISR(TIMER1_OVF_vect){
|
||||||
if(debounce_count >= debounce_count_max)
|
// if(debounce_count >= debounce_count_max)
|
||||||
{
|
// {
|
||||||
TCCR1B &= ~(1<<CS00);
|
// TCCR1B &= ~(1<<CS00);
|
||||||
debounce_lock=0;
|
// debounce_lock=0;
|
||||||
debounce_count=0;
|
// debounce_count=0;
|
||||||
}
|
// }
|
||||||
else
|
// else
|
||||||
{
|
// {
|
||||||
debounce_count++;
|
// debounce_count++;
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user