141 lines
2.5 KiB
C
141 lines
2.5 KiB
C
#include <avr/interrupt.h>
|
|
#include <avr/io.h>
|
|
|
|
|
|
|
|
|
|
#define OUTPUT_PORT PORTB
|
|
#define OUTPUT_DDR DDRB
|
|
|
|
#define LIGHT_FRONT 0
|
|
#define LIGHT_FOG 1
|
|
#define LIGHT_FAR 2
|
|
#define LIGHT_SIGN_L 3
|
|
#define LIGHT_SIGN_R 4
|
|
#define LIGHT_BACK 5
|
|
#define LIGHT_BRAKE 6
|
|
#define LIGHT_BRAKE_M 7
|
|
|
|
|
|
#define MODE_OFF (0<<0)
|
|
#define MODE_ON (1<<0)
|
|
#define MODE_BLINK (1<<2)
|
|
|
|
|
|
|
|
#define BLINK_SPEED 32 //lower is faster: range 0..255 //where 0 is longer than 255!!!
|
|
|
|
|
|
volatile uint8_t lightModes[8] = {MODE_OFF, MODE_OFF, MODE_OFF, MODE_OFF | MODE_BLINK, MODE_OFF | MODE_BLINK, MODE_OFF, MODE_OFF, MODE_OFF};
|
|
volatile uint8_t lightValue[8] = {255, 255, 255, 255, 255, 255, 32, 255};
|
|
volatile uint8_t timeCounter = 0;
|
|
volatile uint8_t blinkCounter = 0;
|
|
volatile uint8_t blinker = 0;
|
|
|
|
|
|
void updateOutput()
|
|
{
|
|
uint8_t outputState = 0;
|
|
|
|
for(uint8_t i = 0; i < 8; i++) //go through all 8 lights
|
|
{
|
|
if(lightModes[i] & MODE_ON)
|
|
{
|
|
if(lightModes[i] & MODE_BLINK)
|
|
{
|
|
if(blinker)
|
|
{
|
|
outputState |= (lightValue[i] >= timeCounter) << i;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
outputState |= (lightValue[i] >= timeCounter) << i;
|
|
}
|
|
}
|
|
}
|
|
|
|
OUTPUT_PORT = outputState;
|
|
//OUTPUT_PORT = 1;
|
|
}
|
|
|
|
|
|
|
|
ISR(TIM0_OVF_vect)
|
|
{
|
|
timeCounter++;
|
|
if(timeCounter == 0) //overflow
|
|
{
|
|
blinkCounter++;
|
|
if(blinkCounter == BLINK_SPEED) //when blink-speed steps are reached
|
|
{
|
|
blinker = 1-blinker; //toggle blinker-state
|
|
blinkCounter = 0;
|
|
}
|
|
}
|
|
|
|
updateOutput();
|
|
}
|
|
|
|
|
|
|
|
void setUpDimmer()
|
|
{
|
|
OUTPUT_DDR = 255; //set all bits to output
|
|
OUTPUT_PORT = 0; //set all zero
|
|
|
|
//TCCR0B |= (1<<CS02) | (1<<CS00); //set 1024 as prescaler for timer0
|
|
TCCR0B |= (1<<CS00); //set no prescaler for timer0
|
|
|
|
TIMSK |= (1<<TOIE0); //enable timer/counter0 overflow interrupt
|
|
sei();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(void)
|
|
{
|
|
setUpDimmer();
|
|
|
|
//set lighs:
|
|
//lightModes[LIGHT_FRONT] = MODE_ON;
|
|
//lightModes[LIGHT_SIGN_L] = MODE_ON | MODE_BLINK;
|
|
|
|
|
|
//Test-Environment, use PORTA/D for 8bit input
|
|
DDRD = 0; //input
|
|
DDRA = 0;
|
|
PORTD = 0b00111111; //pullup
|
|
PORTA = 0b00000011;
|
|
|
|
|
|
uint8_t curInput;
|
|
while(1)
|
|
{
|
|
//push input from both ports into one byte
|
|
curInput = PIND & 0b00111111;
|
|
curInput |= (PINA << 6);
|
|
|
|
//thread this as input
|
|
for(uint8_t i = 0; i < 8; i++)
|
|
{
|
|
if(curInput & (1<<i))
|
|
{
|
|
lightModes[i] &= (0xFF-MODE_ON); //switch light off
|
|
}
|
|
else
|
|
{
|
|
lightModes[i] |= MODE_ON; //switch light on
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
return 0;
|
|
}
|