Previous
Complete listing of DMC Code (C Version):
#include "comatos8515.c"
#include <stdio.h>
#define begin {
#define end }
//task repetition times
#define t0 30
//initial task state variables
#define NoPush 0
#define MaybePush 1
#define Pushed 2
#define DebouncePress 3
#define Waiting 4
#define Done 10
#define error 5
#define StillPressed 6
#define MeasurePrep 7
#define MeasureStart 8
#define MeasureDone 9
//Comatos constants
//Turn off debugger
#define NODebug 0
#define OSDebug 1
//Any task longer than 10 mS is an error
#define OSTimeOut 30
//mask to define that a task does not listen to messages
#define NoMessage 0xff
//mask to define that a task is not time-scheduled
#define NoTime 0
#define CapISR 0x03; //task ID for ISR
char KbdTask(void);
char MeasureTask(void);
char DisplayTask(void);
unsigned char led;
char temp;
interrupt [TIM1_CAPT] void T1CapISR(void)
{
ISRSendMess(1, 3, 0x01);
}
void main(void)
begin
DDRB = 0b11110011; //make most pins output except comparator
PORTB = 0x00; //discharge capacitor
DDRD = 0x00;
TIMSK=TIMSK | 0x08; //only use input capture interrupt
TCCR1B = 0;
TCNT1 = 0; //and zero the timer
ACSR = 0x07; //Interrupt enable, rising edge, input capture
//Set up OS constants
OSInit(OSTimeOut, OSDebug);
//Define the tasks to the OS
OSCreateTask(KbdTask, t0, NoMessage, NoPush);
OSCreateTask(MeasureTask, 0, 0x01, MeasurePrep); //waiting for KbdTask
OSCreateTask(DisplayTask, 0, 0x02, 0); //waiting for MeasureTask
//Start the system
OSStart(); //never returns
end
//blink LED 0
char KbdTask(void) //debounce state machine
begin
char pong[]="pong";
if(OSGetState()==NoPush)
{ // OSUARTTransmitBytes(pong,4);
if(~PIND==0x01)
{
OSSendMess(0, StillPressed);
OSSetState(MaybePush);
}
else OSSetState(NoPush);
}
if(OSGetState()==MaybePush) {
if(~PIND==0x01 && OSGetMess(0)==StillPressed)
OSSetState(MaybePush);
else
{
OSSendMess(0,error);
OSSetState(NoPush);
}
}
if(OSGetState()==Pushed)
{
if(~PIND==0x01 && OSGetMess(0)==StillPressed)
OSSetState(Pushed);
else
OSSetState(DebouncePress);
}
if(OSGetState()==DebouncePress)
{
OSSendMess(1,1);
OSSetState(Waiting);
OSSendMess(0,error);
}
if(OSGetState()==Waiting && OSGetAck()==0x04)
{
OSSetState(NoPush);
//OSSetState(Done);
}
return 0;
end
//blink LED 1
char MeasureTask(void)
begin
if(OSGetState()==MeasurePrep)
{
if((OSGetMessSrc & 0x01) !=0x01)
return 1; //halt if the message isn'tfrom keyboard
else //discharge capacitor
{
DDRB = 0b11110111; //set PIN2 output
PORTB = 0x00; //pull-down to discharge capacitor
OSSetTimeout(25); //wait 25 ms before we start
OSSetState(MeasureStart);
return 0;
}
}
if(OSGetState()==MeasureStart)
{
DDRB = 0b11110011; //set PIN2 input again
TCNT1 = 0; //clear timer
PORTB.0 = 1; //raise Vcc to start charging
TCCR1B = 0b01000011; //capture on rising edge, 64x clock div.
OSSetMessMask(0x08); //wait for on the completion of charging
OSSetTimeout(0);
OSSetState(MeasureDone);
// OSUARTTransmitBytes(ping,4);
return 0;
//note to self, creat a task that monitors TCNT1 and see what's going on in there.
}
if(OSGetState()==MeasureDone)
{
if( (OSGetMessSrc() & 0x08) !=0x08)
return 1; //halt if the message isn't from ISR
else
{
//fix the OSGetMessSrc problem
TCCR1B=0; //shut off timer
PORTB.0=0; //Drop Vcc to discharge capacitor
temp = ICR1L; //Grab the capacitance value
OSSendMess(2, temp); //send the value to be displayed
OSSetState(MeasurePrep); //set the state back to original
OSSetMessMask(0x01);
// OSUARTTransmitBytes(pong,4);
return 0;
}
}
end
char DisplayTask(void)
begin
char Cap[5];
char nf[]="nf";
putsf("Capacitance: \r\n");
sprintf(Cap,"%-i\n",temp);
OSUARTTransmitBytes(Cap,4);
OSUARTTransmitBytes(nf,2);
OSSendAck(0); //turn on the debounce state machine
return 0;
end
Previous