//timerchute.c
#include <stmduino.h>
uint time=0;
uint delaytimer=0;
#define buttondelay 200
#define minimumtime 1000
#define beep pwmout(t2c1,62)
#define nobeep pwmout(t2c1,0)
uint binterval=3000/8;
uint bcounter=2800/8;
uchar beepstate=0xff;
uint setting=0;
uchar state=0;
/*
states
0 wait
1 setting
3 starting
4 triggered(wire broken, wait for certain time.
5 chute(noreturn unless button.
*/
void tick()
{
bcounter++;
bcounter%=binterval;
if(bcounter==0||(bcounter==2980/8&&state==0)){
if(beepstate){
//beep;
timer2(COUNT);
}else{
//nobeep;
timer2(STOP);
}
if(bcounter==2980/8&&state==0)
{
beepstate=0x0;
}
else
{
beepstate=!beepstate;
}
}
if(delaytimer!=0){
delaytimer--;
}
else
{
switch(state)
{
case 0:
binterval=3000/8;
if(pinread(A2))
{
//if high: button not pusht
if(pinread(A3)){
//if a3 is high:not connected
}
else
{
//if a3 is conn to gnd:start waiting for line break
state=3;
delaytimer=buttondelay*5/8;
}
}
else
{
//if low: button pusht,enter setting mode
state=1;
time=0;
delaytimer=500/8;
binterval=50/8;
}
break;
case 1:
binterval=500/8;
time++;
if(pinread(A2)){
//button is not pushed
}
else
{
//button is pusht
setting=time;
eeuint(600)=setting;
time=0;
state=0;
binterval=50/8;
delaytimer=1000/8;
}
break;
case 3:
binterval=200/8;
if(pinread(A3)){
//if a3 is not connected
state=4;//enter timing mode
time=0;
}
else
{//dont do anyting.
}
break;
case 4://wire broken. timing...
binterval=50/8;
time++;
if(time>setting)
{
//triggered, please chute!
state=5;
delaytimer=100/8;
}
else
{
}
break;
case 5://execute chute operation
beepstate=0xff;
pinhigh(A1);
time--;
if(time==0){
state=0;
delaytimer=500/8;
pinlow(A1);
binterval=50/8;
}
break;
}
}
}
void main()
{
clock(2M);
cpudivider(2);
timer4clock(64,249);//8ms interrput
pinMode(A3,INPUT);
//a3 is wire,shall connected to gnd
pinMode(A2,INPUT);
//a2 is button,connected to gnd
pinMode(A1,OUTPUT);
//a1 is chute ctrl
timer2clock(2,124);
unlock();
CLK_PeripheralClockConfig(CLK_PERIPHERAL_I2C,DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1,DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_SPI,DISABLE);
pwmset(t2c1);
pwmout(t2c1,62);
pinlow(A1);
//load settings
if(eeuint(600)<minimumtime){
setting=minimumtime;
}
else
{
setting=eeuint(600);
}
job mstick=employ(tick);
linkjob (timer4tick,mstick);
interrupts();
while(1){sleep();}
}