Forum Discussion
Trigger and counter.
Hi,
I'm trying to implement a counter that counts down when it recieves a trigger pulse from the hardware (in this case, it's a short burst of AC, in the order of a few hundred µs), which keeps an LED on. I have no idea why my code isn't working, I'm pretty new to C programming. Any help greatly appreciated.if (trigger_reg == 1){ //goes to 1 when the AC signal reaches the trigger module threshold
int countdown = 10000000; //arbitrary value
while (countdown > 0){ //use this loop to keep the LED on longer than the trigger pulse, so the user can see it
green_leds=1;
countdown--;
}
} else {
green_leds=0;
} -N13 Replies
- Altera_Forum
Honored Contributor
I assume this code is placed in a while loop, and you are polling the trigger_reg. However, if your burst is short, then your polling is simply missing the trigger. You should be using interrupts.
Better yet, you should implement this circuit in hardware - verilog or vhdl. - Altera_Forum
Honored Contributor
You might be reading a cached value of trigger_reg if you have data cache turned on.
- Altera_Forum
Honored Contributor
Hi,
Probably may need to simplify to basic functional code test first, if the basic one work then can implemented your target purpose. Example you can place more printF and print all the related condition if and else entry/ variable (trigger_reg[11]) to have a closer look how your program runs. - Altera_Forum
Honored Contributor
How would I implement an interrupt?
- Altera_Forum
Honored Contributor
I wouldn't debug with printf. This takes a long time and may result in missing the pulse.
- Altera_Forum
Honored Contributor
erm... printf is for basic way, any better suggestion which is more easier and faster?
- Altera_Forum
Honored Contributor
Leds? This could be done in a few processor instructions, but you need them in your project...
- Altera_Forum
Honored Contributor
i guess, when we do not know where goes wrong, probably print f in every occasion is the only option you can try already...
- Altera_Forum
Honored Contributor
Here is some information from altera on interrupts and exception handling
https://www.altera.com/ja_jp/pdfs/literature/hb/nios2/n2sw_nii52006.pdf - Altera_Forum
Honored Contributor
first of all is this a Nios II or SOC? as the interrupt procedure may works differerenly