tannenbaum code as base
This commit is contained in:
151
code/src/main.c
Normal file
151
code/src/main.c
Normal file
@@ -0,0 +1,151 @@
|
||||
#include <avr/io.h>
|
||||
#include <stdint.h> // has to be added to use uint8_t
|
||||
#include <avr/interrupt.h> // Needed to use interrupts
|
||||
#include <avr/sleep.h>
|
||||
|
||||
unsigned long NextVal(void);
|
||||
unsigned long InitSeed();
|
||||
void random(void);
|
||||
void link_rechts(void);
|
||||
|
||||
int (*animation)(void) = &link_rechts;
|
||||
|
||||
volatile uint8_t leds = 0x00;
|
||||
volatile uint8_t leds_active = 0x00;
|
||||
|
||||
volatile uint32_t active_count = 0;
|
||||
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_max = 3;
|
||||
|
||||
volatile uint8_t dir = 0;
|
||||
|
||||
static unsigned long Seed;
|
||||
|
||||
unsigned long InitSeed()
|
||||
{
|
||||
return NextVal();
|
||||
}
|
||||
|
||||
unsigned long NextVal()
|
||||
{
|
||||
Seed=Seed*1632125L+1013904223L;
|
||||
return Seed;
|
||||
}
|
||||
|
||||
void random(){
|
||||
int temp;
|
||||
do{
|
||||
temp=NextVal();
|
||||
temp=1 << (temp%8);
|
||||
}while(temp==leds);
|
||||
leds=temp;
|
||||
}
|
||||
|
||||
void link_rechts(){
|
||||
if(dir)
|
||||
leds = leds << 1;
|
||||
else
|
||||
leds = leds >> 1;
|
||||
|
||||
if(!(leds & 0x7F)){
|
||||
leds = 0x08;
|
||||
dir^=1;
|
||||
}
|
||||
}
|
||||
|
||||
void write_leds(){
|
||||
if(leds_active){
|
||||
PORTC = leds;
|
||||
PORTD = (leds>>1)&((1<<PD1)|(1<<PD2)|(1<<PD3));
|
||||
PORTD = (leds)&((1<<PD5)|(1<<PD6));
|
||||
}
|
||||
else
|
||||
{
|
||||
PORTC = 0;
|
||||
PORTD = 0;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
InitSeed();
|
||||
DDRC = 0xFF;
|
||||
DDRD = 0xFF;
|
||||
|
||||
DDRB &= ~(1 << DDB0); // Clear the PB0, PB1, PB2 pin
|
||||
// PB0,PB1,PB2 (PCINT0, PCINT1, PCINT2 pin) are now inputs
|
||||
|
||||
PORTB |= ((1 << PORTB0) | (1 << PORTB1) | (1 << PORTB2)); // turn On the Pull-up
|
||||
// PB0, PB1 and PB2 are now inputs with pull-up enabled
|
||||
|
||||
TIMSK1 |= 1 << TOIE1;
|
||||
|
||||
TCCR0B |= (1<<CS01)|(1<<CS00);
|
||||
TIMSK0 |= 1 << TOIE0;
|
||||
|
||||
PCICR |= (1 << PCIE0); // set PCIE0 to enable PCMSK0 scan
|
||||
PCMSK0 |= (1 << PCINT0); // set PCINT0 to trigger an interrupt on state change
|
||||
|
||||
sei(); // turn on interrupts
|
||||
|
||||
while(1)
|
||||
{
|
||||
write_leds();
|
||||
}
|
||||
}
|
||||
|
||||
ISR (PCINT0_vect)
|
||||
{
|
||||
if(!debounce_lock){
|
||||
TCCR1B |= (1<<CS00);
|
||||
debounce_lock=1;
|
||||
if((~PINB & 0x01))
|
||||
{
|
||||
leds_active ^= 0x01;
|
||||
active_count=0;
|
||||
if(!leds_active){
|
||||
write_leds();
|
||||
TCCR0B &= ~((1<<CS01)|(1<<CS00));
|
||||
set_sleep_mode(SLEEP_MODE_PWR_SAVE);
|
||||
sleep_mode();
|
||||
TCCR0B |= (1<<CS01)|(1<<CS00);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ISR(TIMER0_OVF_vect){
|
||||
active_count++;
|
||||
if(count>=count_max){
|
||||
count=0;
|
||||
animation();
|
||||
}
|
||||
else
|
||||
{
|
||||
count++;
|
||||
}
|
||||
if(active_count >= active_count_max){
|
||||
active_count=0;
|
||||
leds_active = 0;
|
||||
}
|
||||
}
|
||||
|
||||
ISR(TIMER1_OVF_vect){
|
||||
if(debounce_count >= debounce_count_max)
|
||||
{
|
||||
TCCR1B &= ~(1<<CS00);
|
||||
debounce_lock=0;
|
||||
debounce_count=0;
|
||||
}
|
||||
else
|
||||
{
|
||||
debounce_count++;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user