#define THRESHOLD 10 main() { static int j, d; #asm ld a,0x51 ioi ld (WDTTR),a ld a,0x54 ioi ld (WDTTR),a #endasm #nodebug WrPortI(SPCR, &SPCRShadow, 0x84); WrPortI(PEFR, &PEFRShadow, 0x00); WrPortI(PEDDR, &PEDDRShadow, 0x00); BitWrPortI(PADR, &PADRShadow, 1, 1); // Preset the gate output high while(1) { while(1) { /* Bring PA0 low, then high again, leaving PA1 high. Making this inline assembly reduces the time this takes from 15 us to 1 us. */ #asm ld a, 0x03 ioi ld (PADR), a ld (PADRShadow), a ld a, 0x02 ioi ld (PADR), a ld (PADRShadow), a nop nop nop nop nop nop #endasm d = RdPortI(PEDR); j = 0; // delay if(d > THRESHOLD) { break; } } /* If the count goes over the threshold for two consecutive cycles, then trip the gate. For speed, the second test is inlined instead of being made into some sort of loop with a variable check. */ #asm ld a, 0x03 ioi ld (PADR), a ld (PADRShadow), a ld a, 0x02 ioi ld (PADR), a ld (PADRShadow), a nop nop nop nop nop nop #endasm d = RdPortI(PEDR); j = 0; if(d > THRESHOLD) { //BitWrPortI(PADR, &PADRShadow, 0, 1); // Set the gate output low #asm xor a, a ioi ld (PADR), a ld (PADRShadow), a #endasm while(1) { /* check to see if the reset switch has been pressed */ d = RdPortI(PBDR); if(!(d & 0x04)) { BitWrPortI(PADR, &PADRShadow, 1, 1); break; } } } } }