Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Altera_Forum
Honored Contributor I
1,286 Views

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; } 

 

-N
0 Kudos
13 Replies
Altera_Forum
Honored Contributor I
38 Views

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 I
38 Views

You might be reading a cached value of trigger_reg if you have data cache turned on.

Altera_Forum
Honored Contributor I
38 Views

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 I
38 Views

How would I implement an interrupt?

Altera_Forum
Honored Contributor I
38 Views

I wouldn't debug with printf. This takes a long time and may result in missing the pulse.

Altera_Forum
Honored Contributor I
38 Views

erm... printf is for basic way, any better suggestion which is more easier and faster?

Altera_Forum
Honored Contributor I
38 Views

Leds? This could be done in a few processor instructions, but you need them in your project...

Altera_Forum
Honored Contributor I
38 Views

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 I
38 Views

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 I
38 Views

first of all is this a Nios II or SOC? as the interrupt procedure may works differerenly

Altera_Forum
Honored Contributor I
38 Views

A wild guess here. 

Compile with no optimization at all. 

The optimizer may be shrinking all the code to almost nothing if the variables are not declared volatile.
Altera_Forum
Honored Contributor I
38 Views

You should implement this circuit in hardware - verilog or vhdl.

Altera_Forum
Honored Contributor I
38 Views

 

--- Quote Start ---  

You should implement this circuit in hardware - verilog or vhdl. 

--- Quote End ---  

 

 

What if the OP wants to make a bigger design and this is just a first step? Yes a HDL solution would work, but you need to be able to understand the languages and hardware design, which not everyone does or wants to invest in.
Reply