volatile ktok = 0;
/* Zero-cross detector*/
volatile char zok = 0;
-unsigned long ztime;
+volatile char ztime = 0;
/* Triac */
char trstate = 0;
char tron = 0;
-unsigned long trtime;
-unsigned short trdelay = 0;
+volatile char trtime;
+volatile char trdelay = 0;
void init(void)
{
- /* Timer init */
+ /* Timer init
+ * Timer 0 cycles timing sensitive subsystems
+ * Timer 1 is used for global timing
+ */
+ OCR0A = 100;
+ TCCR0A = 2;
+ TCCR0B = 1;
+ TIMSK0 = 2;
TCCR1A = 0;
TCCR1B = 1;
TIMSK1 = 1;
} else {
c = d = 0;
}
+ cli();
PORTC = c;
PORTD = (PORTD & 0x0f) | d;
+ sei();
}
}
{
if(tstate == 0) {
if((PIND & 8) && (tlock == 0)) {
+ cli();
PORTD |= 2;
+ sei();
tstart = mnow;
tstate = 1;
}
} else if(tstate == 1) {
if(mnow - tstart > 1000) {
+ cli();
PORTD &= ~2;
+ sei();
tstate = 0;
tstart = mnow;
}
}
}
-void triaccycle(void)
-{
- if(trstate == 0) {
- if(tron && zok && (mnow > ztime + trdelay)) {
- PORTD |= 1;
- zok = 0;
- trstate = 1;
- trtime = mnow;
- }
- } else if(trstate == 1) {
- if(mnow > trtime + 500) {
- PORTD &= ~1;
- trstate = 0;
- }
- }
-}
-
int main(void)
{
int state, cur;
tempcycle();
calcavg();
convcycle();
- triaccycle();
#if 1
/*
if(cur > 99)
cur = 99;
display(cur);
- trdelay = 10000 - ((unsigned short)cur * 100);
+ trdelay = 99 - cur;
utime = mnow;
}
if(mnow - utime > 1000000) {
if(cur > 99)
cur = 99;
display(cur);
- trdelay = 10000 - ((unsigned short)cur * 100);
+ trdelay = 99 - cur;
pval = 0;
}
if(sstate == 2) {
ISR(SIG_INTERRUPT0)
{
- ztime = getticks();
+ ztime = 0;
zok = 1;
}
}
}
+ISR(SIG_OUTPUT_COMPARE0A)
+{
+ if(trstate == 0) {
+ ztime++;
+ if(tron && (ztime >= trdelay)) {
+ PORTD |= 1;
+ trstate = 1;
+ trtime = 0;
+ }
+ } else if(trstate == 1) {
+ trtime++;
+ if(trtime >= 5) {
+ PORTD &= ~1;
+ trstate = 0;
+ }
+ }
+}
+
ISR(SIG_OVERFLOW1)
{
of = 1;