Need help on a small interrupt project using Freescale FRDM-K64F demo board.
The goal is: (1) when power is on, an external LED will blink (2) Push button SW2, LED will be constantly on (3) Push button SW3, LED will be off (4) Push button SW2 and SW3, LED will be constantly on
I wrote code to achieve goal (1), (2), and (3). But after one button is pushed, it sometimes takes two pushes of the other button to achieve the function of that button. Second question is how to achieve goal (4).
//main.c program //Start of main.c #include "MK64F12.h"
void DelayFunction (void);
int main(void) { SIM_SCGC5 |= SIM_SCGC5_PORTB_MASK; /*Enable Port B Clock Gate Control*/ SIM_SCGC5 |= SIM_SCGC5_PORTC_MASK; /*Enable Port C Clock Gate Control*/ SIM_SCGC5 |= SIM_SCGC5_PORTA_MASK; /*Enable Port A Clock Gate Control*/
PORTB_PCR23 = 0x100; PORTC_PCR6 = 0x90100; //Push button SW2 PORTA_PCR4 = 0x90100; //Push button SW3
GPIOB_PDDR |= (1 << 23); //external LED connected to PTB-23 pin GPIOC_PDDR |= (0 << 6); //Push button SW2 connected to PORTC-6 GPIOA_PDDR |= (0 << 4); //Push button SW3 connected to PORTA-4
PORTC_ISFR = PORT_ISFR_ISF(0x40); /* Clear interrupt status flag */ PORTA_ISFR = PORT_ISFR_ISF(0x10); NVIC_EnableIRQ(PORTC_IRQn); /*Enable the PORTC interrupt*/ NVIC_EnableIRQ(PORTA_IRQn);
for (;;) {
GPIOB_PTOR |= (1 << 23); //Toggle external LED DelayFunction(); } return 0; }
void PORTC_IRQHandler(void) { PORTA_ISFR = PORT_ISFR_ISF(0x10); GPIOB_PSOR |= (1 <<23); //Turn on external LED }
void PORTA_IRQHandler(void) { PORTC_ISFR = PORT_ISFR_ISF(0x40); GPIOB_PCOR |= (1 <<23); //Turn off external LED }
void DelayFunction(void) { int cnt; for(cnt=0; cnt<1000000; cnt++) { } } //end of main.c