Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++

Trigger and counter.

Altera_Forum
명예로운 기여자 II
2,581 조회수

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 포인트
13 응답
Altera_Forum
명예로운 기여자 II
1,333 조회수

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.
0 포인트
Altera_Forum
명예로운 기여자 II
1,333 조회수

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

0 포인트
Altera_Forum
명예로운 기여자 II
1,333 조회수

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.
0 포인트
Altera_Forum
명예로운 기여자 II
1,333 조회수

How would I implement an interrupt?

0 포인트
Altera_Forum
명예로운 기여자 II
1,333 조회수

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

0 포인트
Altera_Forum
명예로운 기여자 II
1,333 조회수

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

0 포인트
Altera_Forum
명예로운 기여자 II
1,333 조회수

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

0 포인트
Altera_Forum
명예로운 기여자 II
1,333 조회수

i guess, when we do not know where goes wrong, probably print f in every occasion is the only option you can try already...

0 포인트
Altera_Forum
명예로운 기여자 II
1,333 조회수

Here is some information from altera on interrupts and exception handling 

https://www.altera.com/ja_jp/pdfs/literature/hb/nios2/n2sw_nii52006.pdf
0 포인트
Altera_Forum
명예로운 기여자 II
1,333 조회수

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

0 포인트
Altera_Forum
명예로운 기여자 II
1,333 조회수

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.
0 포인트
Altera_Forum
명예로운 기여자 II
1,333 조회수

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

0 포인트
Altera_Forum
명예로운 기여자 II
1,333 조회수

 

--- 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.
0 포인트
응답