void main() { auto char s[40]; auto char display[80]; auto int key, reading, channel; brdInit(); //Display user instructions and channel headings DispStr(8, 1, " <<< Digital inputs: >>>"); DispStr(8, 2, "IN0\tIN1\tIN2\tIN3\tIN4\tIN5\tIN6\tIN7"); DispStr(8, 3, "---\t---\t---\t---\t---\t---\t---\t---"); DispStr(8, 6, "IN8\tIN9\tIN10"); DispStr(8, 7, "---\t---\t----"); DispStr(8, 12, "Connect the Demo Bd. switches to the inputs that you what to toggle."); DispStr(8, 13, "(See instructions in sample program for complete details)"); DispStr(8, 15, "<-PRESS 'Q' TO QUIT->"); //loop until user presses the "Q" key for(;;) { // display the input status for all channels display[0] = '\0'; //initialize for strcat function for(channel = 0; channel < 8; channel++) //read channels 0 - 7 { reading = digIn(channel); //read channel sprintf(s, "%d\t", reading); //format reading in memory strcat(display,s); //append reading to display string } DispStr(8, 4, display); //update input status display[0] = '\0'; for(channel = 8; channel < 11; channel++) //read channels 8 - 10 { reading = digIn(channel); sprintf(s, "%d\t", reading); strcat(display,s); } DispStr(8, 8, display); if(kbhit()) { key = getchar(); if (key == 'Q' || key == 'q') // check if it's the q or Q key { while(kbhit()) getchar(); exit(0); } } } }
/* * Check the status of switch 3 */ cofunc void CheckSwitch3() { if (digIn(3)) abort; // if button not down skip out waitfor(DelayMs(50)); // wait 50 ms if (digIn(3)) abort; // if button not still down exit SendMail(3); // send email since button was down 50 ms while (1) { waitfor(digIn(3)); // wait for button to go up waitfor(DelayMs(200)); // wait additional 200 ms if (digIn(3)) break; // if button still up break out of while loop } }
/********************************************************************************* This function reads/writes the IO and fills the information in the my_inouts array, for example: - Analog Inputs read the channel then fill the my_inouts->ain array with the new data - Digital Inputs: read the channel then fill the my_inouts->din array with the new data - Digital Outputs: read the my_inouts->dout array then set the correct digital channel LOW or HIGH depending what is in the array. ********************************************************************************/ void My_IO_Ctrl(My_InOuts_Type *my_inouts) { static int channel; // ANALOG INPUTS: this is where we read the analog inputs............ for(channel=0;channel<ANALOG_INPUTS;channel++) { my_inouts->ain[channel] = anaInVolts(channel,GAIN_X2); if (my_inouts->ain[channel] > 10 ) my_inouts->ain[channel] = 10.000; if (my_inouts->ain[channel] < 0 ) my_inouts->ain[channel] = 0.000; // if(my_inouts->ain[channel] < 10) // DEBUGGING // my_inouts->ain[channel]++; // DEBUGGING // else // DEBUGGING // my_inouts->ain[channel] = 0; // DEBUGGING } // DIGITAL INPUTS: this is where we read the digital inputs.......... for(channel=0;channel<DIGITAL_INPUTS;channel++) { my_inouts->din[channel] = LOW_HIGH[digIn(channel)]; //if(!strcmp(my_inouts->din[channel],LOW_HIGH[1]))// DEBUGGING // my_inouts->din[channel] = LOW_HIGH[0]; // DEBUGGING //else // DEBUGGING // my_inouts->din[channel] = LOW_HIGH[1]; // DEBUGGING } // DIGITAL OUTPUTS: this is where we set the digital outputs......... for(channel=0;channel<DIGITAL_OUTPUTS;channel++) { if(!strcmp(my_inouts->dout[channel],LOW_HIGH[0])) { //my_inouts->dout[channel] = LOW_HIGH[0]; // DEBUGGING digOut(channel,0); // at this point the data in the my_inouts->dout array is high so we // need to set the digital output to high, for example: // see_digital_output(channel,1); } else { //my_inouts->dout[channel] = LOW_HIGH[1]; // DEBUGGING digOut(channel,1); // at this point the data in the my_inouts->dout array is low so we // need to set the digital output to low, for example: // see_digital_output(channel,0); } } }
/** * This task polls the switches and then * posts a message to the message queue * if an alarm needs to be activated */ void switchTask(void *data) { // Error Referene INT8U err; // Local zone status auto int localZone0; auto int localZone1; auto int localZone2; auto int localZone3; // Local switch state auto int sw0State; auto int sw1State; auto int sw2State; auto int sw3State; // Result for message queue static char result; // Initialize zones localZone0 = ON; localZone1 = ON; localZone2 = ON; localZone3 = ON; // Initialize switch state sw0State = OFF; sw1State = OFF; sw2State = OFF; sw3State = OFF; // Loop forever while(1) { // Check switches sw0State = digIn(ID_SWITCH_1); sw1State = digIn(ID_SWITCH_2); sw2State = digIn(ID_SWITCH_3); sw3State = digIn(ID_SWITCH_4); // Check to see if we have any switches if(!sw0State || !sw1State || !sw2State || !sw3State ) { // Take a semaphore and get the current zone states OSSemPend(zoneSem, 0, &err); localZone0 = zone0State; localZone1 = zone1State; localZone2 = zone2State; localZone3 = zone3State; OSSemPost(zoneSem); // Check to see which zones (if any) have been triggered if(!sw0State && localZone0 == ON) { // Msg Queue Zone 0 result = '0'; } else if(!sw1State && localZone1 == ON) { // Msg Queue Zone 1 result = '1'; } else if(!sw2State && localZone2 == ON) { // Msg Queue Zone 2 result = '2'; } else if(!sw3State && localZone3 == ON) { // Msg Queue Zone 3 result = '3'; } else { //Nope result = 'A'; } // Post the message printf("Posting alarm message \n"); OSQPost(msgQueuePtr, (void *)&result); } // Update Alarm State (in case web bypassed/disarmed) OSSemPend(alarmSem, 0, &err); if(alarming == OFF) { digOut(ID_BUZZER, OFF); digOut(LED_0_ID, OFF); digOut(LED_1_ID, OFF); digOut(LED_2_ID, OFF); digOut(LED_3_ID, OFF); } OSSemPost(alarmSem); //Done OSSemPost(switchToHTTP); // Try to take a semaphore, this will block us OSSemPend(httpToSwitch, 0, &err); } }
void main() { char s[40]; int encoder_state; word count; // Initialize the controller brdInit(); // Configure digital outputs to simulate quadrature encoder output setDigOut(LED1, 1); setDigOut(LED2, 1); // Configure quadrature encoder input setDecoder(QUAD_I, QUAD_Q, -1, 0); // Configure digital inputs for switches setDigIn(SW1); setDigIn(SW2); setDigIn(SW3); // initialize outputs to low encoder_state = 0; simulate_encoder(encoder_state); // reset quadrature decoder counter resetCounter(QUAD_I); DispStr(2, 1, "<<< Simulating a Quadrature Encoder with button presses >>>"); DispStr(1, 3, "Press Button SW1 to decrement counter"); DispStr(1, 4, "Press Button SW2 to increment counter"); DispStr(1, 5, "Press Button SW3 to reset counter"); while (1) { costate { // Display the counter value getCounter(QUAD_I, &count); sprintf(s, "Quadrature Decoder Count = %6u", count); DispStr(1, 7, s); } costate { // decrement counter waitfor(!digIn(SW1)); // wait for switch 1 to be pressed waitfor(DelayMs(50)); // debounce if (!digIn(SW1)) { --encoder_state; if (encoder_state < 0) { encoder_state = 3; } simulate_encoder(encoder_state); waitfor(DelayMs(150)); // repeat when switch held down } } costate { // increment counter waitfor(!digIn(SW2)); // wait for switch 2 to be pressed waitfor(DelayMs(50)); // debounce if (!digIn(SW2)) { ++encoder_state; if (encoder_state > 3) { encoder_state = 0; } simulate_encoder(encoder_state); waitfor(DelayMs(150)); // repeat when switch held down } } costate { // reset counter waitfor(!digIn(SW3)); // wait for switch 3 to be pressed waitfor(DelayMs(50)); // debounce if (!digIn(SW3)) { resetCounter(QUAD_I); // reset quadrature decoder counter waitfor(digIn(SW3)); // wait for switch 3 to be released } } } }
int debounce_key(void) { auto int status; static int state; static unsigned long debounce_period; // initialize state machine during system initialization #GLOBAL_INIT {state=0;} status = !KEY_PRESSED; switch(state) { case 0: // wait for the switch to be pressed if(!digIn(0)) { //set debounce period state++; debounce_period = MS_TIMER + 50; // debounce period = 50ms } break; case 1: // check if switch is still being pressed, if not reset state machine if((long) (MS_TIMER-debounce_period) >= 0 ) { if(!digIn(0)) { state++; status = KEY_PRESSED; } else { state = 0; } } break; case 2: // wait for the switch to be released if(digIn(0)) { state++; debounce_period = MS_TIMER + 100; // debounce period = 100ms } break; case 3: // verify that the switch is still released after debounce period if((long)(MS_TIMER-debounce_period) >= 0 ) { if(digIn(0)) { state = 0; } else { debounce_period = MS_TIMER + 20; // debounce period = 20ms } } break; default: // should never get here! state = 0; break; } return(status); }