I want to build a program in C that enables / disables a passive matrix display. But my problem occurs before I can even implement one function of this display.
I have a simple switch connected to Port C pin 24, which is configured as INPUT with a pull-up resistor and debouncing input filter. When I toggle the switch the code enters an interrupt and toggles the "display" on or of depending on the state of the switch.
Everything works fine until I introduce a simple while(1)
loop, which should handle everything I want to do. But every time I write the function called run
, which only contains one while(1)
loop and a single dummy operation __asm__("nop")
, the while loop prevents the code from entering the interrupt.
This is my code:
void display_init(void) {
sysclk_init();
delay_init(sysclk_get_cpu_hz());
pmc_set_writeprotect(false);
pmc_enable_periph_clk(ID_PIOC);
NVIC_EnableIRQ(PIOC_IRQn);
REG_PIOC_PER = SWITCH;
REG_PIOC_ODR = SWITCH;
REG_PIOC_OWDR = SWITCH;
REG_PIOC_PUER = SWITCH;
REG_PIOC_IFER = SWITCH;
REG_PIOC_DIFSR = SWITCH;
REG_PIOC_LSR = SWITCH;
REG_PIOC_REHLSR = SWITCH;
//Clear input change register to avoid immediate interrupt
REG_PIOC_ISR;
REG_PIOC_IER = SWITCH;
run();
}
void run() {
while(1) {
__asm__("nop"); //Dummy operation for debugging
}
}
void PIOC_Handler(void) {
REG_PIOC_ISR;
if(REG_PIOC_PDSR & PIO_PC24) {
__asm__("nop"); //Breakpoint
} else {
__asm__("nop"); //Breakpoint
}
}
As far as I know, an interrupt stops everything executed at the current time and starts working on the interrupt code. My question is now: Why is the interrupt not stopping the while loop?
Before I recognized this problem, I had a simple condition in the while loop that checks whether 1 or 0 to equals 1 when I set this state to 0, so the condition was 1 == 0
which should equal false
. The loop did not stop and continued to run even if the condition for this loop was false... Is it possible that while loops do not like to be used in parallel with interrupts?
EDIT:
void main(void) {
board_init();
display_init();
}
I don't have an Arduino setup() function because I'm using Atmel Studio to ensure working at low Level, to avoid unnecessary code and to keep memory usage as low as possible.
while
loops? You may as well not usefor
loops. It is impossible to answer without seeing all of your code. Please make a minimal example that demonstrates what you are talking about.