This commit is contained in:
Jonathan Wyss 2024-03-05 18:19:42 +01:00
parent 7ca98cc05e
commit 2fd404f14d

109
main.c
View File

@ -1,6 +1,7 @@
#define F_CPU 8000000UL
#include <util/delay.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#define PATTERN_COUNT 4
@ -11,31 +12,68 @@ typedef struct
unsigned char b;
int time_hold;
}rgbval;
volatile uint16_t ms;
volatile int counts;
ISR(TIMER0_OVF_vect)
{
count();
}
void count(void)
{
//counts=0;//0..65535
counts++;
if(counts>=31)
{
counts=0;
if(ms<60000)//1 minute
{
ms++;
}
else
{
ms=0;
}
}
}
int main (void) { // (2)
rgbval pattern[6][PATTERN_COUNT]={ {{0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}},
{{1,1,1,1}, {1,1,153,2}, {200,50,39,1}, {204,255,153,2}},
/*rgbval pattern2[PATTERN_COUNT]=*/{{1,1,1,1}, {10,255,0,1}, {1,1,255,2}, {1,200,1,2}},
/*rgbval pattern3[PATTERN_COUNT]=*/{{1,1,5,1}, {1,1,255,1}, {1,1,5,1}, {1,1,255,1}},
{{252,102,3,1}, {161,67,47,1}, {118,120,74,1}, {83,120,74,2}},
{{87,119,122,1}, {95,87,122,1}, {52, 69, 56,1}, {83, 84, 72,1}}};
rgbval pattern[6][PATTERN_COUNT]={ {{0,0,0,10}, {0,0,0,10}, {0,0,0,10}, {0,0,0,10}},
{{1,1,1,10}, {1,1,153,20}, {200,50,39,10}, {204,255,153,20}},
{{1,1,1,10}, {10,255,0,10}, {1,1,255,20}, {1,200,1,20}},
{{1,1,5,10}, {1,1,255,10}, {1,1,5,10}, {1,1,255,10}},
{{252,102,3,10}, {161,67,47,10}, {118,120,74,10}, {83,120,74,20}},
{{87,119,122,10}, {95,87,122,10}, {52, 69, 56,10}, {83, 84, 72,10}}};
//rgbval *pattern=pattern1[0];
int i=0,i2=0,i3;
int r,g,b = 0;
int set=0;
int input=0;
//system_clock prescaler
uint16_t pre_delay=0;
ms =0;
counts = 0;
//disable prescaler
CLKPR = (1<<CLKPR);
CLKPR = 0x00;
//#######OC0A auf ausgang
DDRB = (1 << PB0 )|(1<<PB1)|(1<<PB4);
cli();
//#######Timer0-setup##### |Phase correct pwm
TCCR0A = (1<<COM0A1)|(1<<COM0B1)|(1<<WGM00); //mode
TCCR0B = (1<<CS01);//|(1<<CS02); //frequency /8
TCCR0A = (1<<COM0A1)|(1<<COM0B1)|(1<<WGM00)|(1<<WGM00); //mode
TCCR0B = (1<<CS00)|(1<<CS00); //frequency /256 = 31250Hz , 1 t0 period = 8.192ms 1 increment = 0.032ms strangly prescaler of 1 results in expected results as prescaler was 256
// PLLCSR |= (1 << PLLE) | (1 << PCKE);
//######Timer1-setup
TCCR1 = (1<<COM0A0)|(1<<COM0A1)|(1<<COM0B0)|(1<<COM0B1)|(1<<CTC1)|(1<<CS12)|(1<<CS10);//|(1<<CS11)|(1<<CS13); //frequencyi //iinverting PWM because 0 dtc not possible
TCCR1 = (1<<COM1A0)|(1<<COM1A1)|(1<<CTC1)|(1<<CS12)|(1<<CS10);//|(1<<CS11)|(1<<CS13); //frequencyi //iinverting PWM because 0 dtc not possible
GTCCR = (1<<PWM1B)|(1<<COM1B0); //mode + overrun clear, Set on compare match
OCR1C = 255; // clear timer at 255
//######PWM_VALUE$
@ -47,20 +85,25 @@ int main (void) { // (2)
//######Configure Pullups
PORTB |= (1<<PB2);
//enable global interrupts
//Interupts
TIMSK |= (1<<TOIE0);//interrupt on overflow = one timer cycle
TIFR |= (1<<TOV0);
TCNT0 = 0;
sei();
while(1)
{
for(i=0;i<PATTERN_COUNT;i++)
for(i=1;i<PATTERN_COUNT;i++)
{
r = pattern[i2][i].r;
g = pattern[i2][i].g;
b = pattern[i2][i].b;
if(i==(PATTERN_COUNT-1)) {i= -1;}
//reset timer counter
while(1)
{
//input handling
input=PINB;
if(((input & (1<<PB2)) == 0 && set==0))
{
@ -86,18 +129,29 @@ int main (void) { // (2)
{
set=0;
}
if(r < pattern[i2][i+1].r) {r++;}
if(r > pattern[i2][i+1].r) {r--;}
if(g < pattern[i2][i+1].g) {g++;}
if(g > pattern[i2][i+1].g) {g--;}
if(b < pattern[i2][i+1].b) {b++;}
if(b > pattern[i2][i+1].b) {b--;}
OCR0A = g;//green
OCR0B = r;//red
OCR1B = b;//blue
_delay_ms(10);
}
//Pattern handling
if(r < pattern[i2][i+1].r) {r++;}
if(r > pattern[i2][i+1].r) {r--;}
if(g < pattern[i2][i+1].g) {g++;}
if(g > pattern[i2][i+1].g) {g--;}
if(b < pattern[i2][i+1].b) {b++;}
if(b > pattern[i2][i+1].b) {b--;}
OCR0A = g;//green
OCR0B = r;//red
OCR1B = b;//blue
// :wq
// _delay_ms(10);
pre_delay = ms;
// :wq
while((ms-pre_delay) < pattern[i2][i+1].time_hold){};
if(r == pattern[i2][i+1].r && g == pattern[i2][i+1].g && b == pattern[i2][i+1].b) break;
}
}
@ -106,3 +160,4 @@ int main (void) { // (2)
return 0;
}