Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
LinkBack Thread Tools Display Modes
Old 15th January 2005, 10:36 PM   (permalink)
Default Check my Timer code

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
__________________
All Electronic components run on smoke. Let the smoke out and it no longer works.
~Tim Baker (Electronics Instructor at John A. Logan College)
MrMikey83 is offline  
Old 16th January 2005, 05:20 AM   (permalink)
Default

I would answer your post but i dont understand ' C ' ops:
williB is offline  
Old 16th January 2005, 10:43 AM   (permalink)
Default

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.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is online now  
Old 16th January 2005, 03:42 PM   (permalink)
Default

Aha! Thanks Nigel, I hadn't thought of doing it that way.
~Mike
__________________
All Electronic components run on smoke. Let the smoke out and it no longer works.
~Tim Baker (Electronics Instructor at John A. Logan College)
MrMikey83 is offline  
Old 16th January 2005, 06:48 PM   (permalink)
Default

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.
__________________
All Electronic components run on smoke. Let the smoke out and it no longer works.
~Tim Baker (Electronics Instructor at John A. Logan College)
MrMikey83 is offline  
Old 17th January 2005, 01:31 AM   (permalink)
Default

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..
williB is offline  
Old 17th January 2005, 01:59 AM   (permalink)
Default

Well, I meant that I had tried it with and without the delay.
__________________
All Electronic components run on smoke. Let the smoke out and it no longer works.
~Tim Baker (Electronics Instructor at John A. Logan College)
MrMikey83 is offline  
Old 17th January 2005, 07:56 AM   (permalink)
Default

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.
__________________
let me try!
gas is offline  
Reply

Bookmarks

Thread Tools
Display Modes





All times are GMT. The time now is 03:34 PM.


Electronic Circuits  |  Learning Electronics
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.

eXTReMe Tracker