Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Check my Timer code

Status
Not open for further replies.

MrMikey83

New Member
This code is supposed to sense the input from a radio receiver. I wish I had an O'scope so I could see the receiver output and know for certain what kind of wavefor it is, but I dont feel like spending $1000 on one right now.
The receiver outputs a 1ms to 2ms signal. The ATmega32 needs to be able to read this and store the result.

I tried Timer0 instead of Timer1 and finally was able to get the timer running, but when I use it in my code, I dont get anything.

PWMin is where I have the receiver connected. timeUP, timeDOWN and pulse are variables. The printf lines are for me to see what is hapenning when I run it. The Terminal output looks like this...
|13|0|0|
|128|0|0|
|231|0|0|
|19|0|0|
|132|0|0|
|251|0|0|
...

The first number is the timer. The second is my "pulse" output. The third is to monitor the overflow. As you can see, the "pulse" code didn't work, but also, the overflow isn't counting up either. I have an overflow interupt which increments the variable ov_counter.


Code:
while (1) 
      { 
            
            if(PWMin == 1) 
            { 
                  timeUP = TCNT0; 
            } 
            else 
            { 
                  timeDOWN = TCNT0; 
            } 
            pulse = (timeDOWN - timeUP) / 500; 
            
            printf("|%d|", TCNT0); 
            printf("%d|", pulse); 
            printf("%d|", ov_counter); 
            putchar(13); 
            delay_ms(300); 
      }; 
}


I have altered the code and I get a slightly better result...

Code:
while (1)
      {
            
            if(PWMin == 1)
            {
                  timeUP = TCNT0;
            }
            if(PWMin == 0)
            {
                  timeDOWN = TCNT0;
            }
            pulse = (timeDOWN - timeUP);
            
            printf("|%d|", TCNT0);
            printf("%d|", timeUP);
            printf("%d|", timeDOWN);
            printf("%d|", pulse);
            printf("%d|", ov_counter);
            putchar(13);
            delay_ms(300);
            
      };

...and heres the output...

|82|0|80|80|0|
|175|0|173|173|0|
|35|0|33|33|0|
|128|0|126|126|0|
|244|0|242|242|0|
...

Key:
|TCNT0|timeUP|timeDOWN|pulse|ov_counter|

As you can see, it too is not doing the job I wish it to do.
It doesn't seem to be storing the time for the timeUP.

The overflow counter also doesn't seem to work. Isn't it supposed to trigger the interrupt (not shown above) when the counter reaches 255 and rolls over?

Thanks
~Mike
 
Likewise, I don't understand C either, plus the code section assumes an understanding of how Atmel processors work.

For a start though, why are you trying to use hardware timers?.

Use a simple software loop, there's far less demand on knowing what the hardware is doing.

Something like this

Zero counter
Wait until pin is high
Loop:
Increment Count
If pin high then goto Loop

You will probably need Count to be a multi-byte counter, or introduce a delay in the counting process.
 
Aha! Thanks Nigel, I hadn't thought of doing it that way.
~Mike
 
OK, I tried it that way and still no good.

Code:
            count = 0;
            timeUP = count;
            while (input == 0)
            {
                  //delay_us(1);
                  ++count;
            }
            timeDOWN = count;

I tried it with and without the 1us delay. When it runs, timeDOWN gets 2314 and does not change when I move the control stick. 2056 when the delay is uncommented.
 
what would comments have to do with it?.
unless it is interpreted C?
anyway i bought a scope used for $ 125 I'm sure you can find one , you just gotta look..
 
Well, I meant that I had tried it with and without the delay.
 
which software do you use?
i use the Keil C, when press 'Debug'\'Start/Stop Debug Session', then into the debug window, on the left of the window, i can see the time.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top