??? 11/06/04 20:52 Read: times |
#80553 - Int running on rising edge? |
Ok, on my little 'teach myself 8051 C' project, I'm running into something of a road block. The program runs fine, but on thing is wierd - Instead of the blink happening the moment the button is pressed, it blinks when it's released. I know there are simpler ways to do this, but in the first real project I'm doing with this, the main loop performs a bunch of other stuff and it is critical that button-to-activity time be as miniscule as possible - so, I need to put it on an interrupt and return from that interrupt as quickly as possible.
Anyway, quick run-down of what I'm doing in this simplified test program: The program simply initializes all i/o & registers, then loops doing nothing until the button is pressed. When the button is pressed, the interrupt routine (which is super-short so that the timer and/or int0 will delay each other as little as possible in the final project) just swaps the LED status and disables itself. When it returns to the main loop, the fact that the button will be held (unless this superman can push and release in about 15-20 microseconds), the debounce routine will trigger, which basically just sticks until the button has been in a released state for a while (I'm figuring the loop at around 10 instruction cycles, so about 10,000 iterations, or around 10 ms.) Once the debounce routine returns, the loop turns int0 back on and we're back where we started. Can anyone spot anything in the following code which would cause the int to happen only when the button is released? I did stick a meter on the pin, and it's 0v when pushed, 5v when released, and I don't know of anything which would cause it to delay interrupt operation like this... /* Test program by Richard Slaughter. SDCC compiler. Teaching myself to apply C code in Microprocessor applications Should simply blink the LED each time the button is pressed. Processor: Atmel AT89S8252 Speed: 12mhz Instruction rate: 1mhz */ #include<at89s8252.h> #define button P3_2 #define LEDc P3_4 void debounce(void); void initInts(void); void main(void); /* above would all be in a header file */ void _sdcc__external__startup() //Init all IO bits high { P1=255; P2=255; P3=255; } void debounce() { unsigned int DBTmr=5000; // initialize stability counter while(DBTmr!=0) { DBTmr=1000; while(button && (DBTmr != 0)) // Each time the button is tested as being pressed, { // The while loop will repeat instead of exiting DBTmr--; // If the button goes for around 10000 inst cycles (10ms) } // Then it will countdown and the while loop will end } } void initInts() { TL1=0; // Timer 1 counter as slow as possible TH1=0; // 65 and a half milliseconds TMOD=17; // Tmr0/TMR1 both 16 bit no prescaler. 0001 0001 TCON=67; // timer 1 active, timer 0 not, int0 on & falling edge 0100 0011 IP=0; // all priorities default 0000 0000 IE=138; // both timers, enabled - int0 not enabled just yet 1000 1010 } void button() interrupt 0 //button { IE=(IE & 0xFE); //disable int0 if(LEDc==1) LEDc=0; else // Swap status of LED LEDc=1; } void timer() interrupt 3 //empty timer { } void main() { initInts(); // Just initialize once, then while(!button); // Wait til the button stabilizes high IE=(IE | 0x01); //Enable int1.. was EX0=1, this didn't work... while(1) // Loop eternally { if(!button) // If the button is being pressed.. { debounce(); //run the debounce routine. IE=(IE | 0x01); //then re-enable int0 after button stabilizes } } } |
Topic | Author | Date |
Int running on rising edge? | 01/01/70 00:00 | |
RE: Int running on rising edge? | 01/01/70 00:00 | |
RE: Int running on rising edge? | 01/01/70 00:00 | |
RE: Int running on rising edge? | 01/01/70 00:00 | |
RE: Int running on rising edge? | 01/01/70 00:00 | |
RE: Int running on rising edge? | 01/01/70 00:00 | |
RE: Int running on rising edge?![]() | 01/01/70 00:00 | |
RE: Int running on rising edge? | 01/01/70 00:00 | |
RE: Int running on rising edge? | 01/01/70 00:00 |