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.

Timer0 Simple Question

Status
Not open for further replies.

mrfunkyjay

New Member
Hello Guys,

I want to know exactly how to use timer. Lets say I have a requirement like this:

A light kit system for rc car that blink a LED when no activity or system is idle for more than 10 seconds.

I want to learn how to use Timer0 in this case, but I never know how to make it because I am still a newbie in programming.

Can anyone help me?

I am using PICKIT2 Programmer, PIC18F4520 MCU, and 20Mhz External Oscillator. My program should be programmed by C Language with C18 Compiler.

**broken link removed**
 
There are many ways to use the timers, the simplest way is to simply start and stop them. To use a timer to give better resolution of your earlier code you could simply do,

Code:
int GetPulse(){
    T1CON=0b0000000;            //init Timer 1 - see data sheet
    TMR1L=0;                    //clear the timer count (low byte)
    TMR1H=0;                    //and the high byte
    Pulse_tris=1;               //make pin input
    while(Pulse_in==1);         //ensure pin is low to start with 
    while(Pulse_in==0);         //wait until pin goes high 
    T1CONbits.TMR1ON=1;         //start Timer 1
    while(Pulse_in==1);         //wait for pin to go low again
    T1CONbits.TMR1ON=0;         //stop timer 1
    return TMR1H*256+TMR1L;     //return the 16 bit timer value
}
With a 20MHz crystal the above code would return 5,000 for a 1mS pulse and 10,000 for a 2mS pulse. I used Timer 1 as Timer 0 reads the high byte in a funny way and so the above code won't work with timer 0.

I'll leave it to you to work out the other ways.:D

Mike.
 
Ah, this is exactly what my Team Leader told me ystd. the 7500 is neutral position for the steering wheel and left is 5000 and 10000 is right position.

Then the if statement goes with 5000, 7500, and 100000 instead of lower resolution of 14 15 and 16.

Is there really another way? Newbie always get lost, just like a born monkey (without parents) in a jungle. :eek:
 
**broken link removed**

okay several questions to go:

1. If I would go in 16 bits, like Pommie said, I should set this T1CONbits.RD16 = 1; right?

2. Why Pommie didnt set the Prescaler value?

3. T1OSCEN: Timer1 Oscillator Enable bit >> What does it mean?

4. T1CON=0b0000000; //init Timer 1 - see data sheet >> what kind of initialization do I need to setup?

5. What do this tell us? TMR1H*256+TMR1L >> ?? What is this?

Thanks!!!
 
Last edited:
okay several questions to go:

1. If I would go in 16 bits, like Pommie said, I should set this T1CONbits.RD16 = 1; right?
Timer 1 is always 16 bits. This bit effects how the timer is read and is only needed if you read the timer while it is still running.
2. Why Pommie didnt set the Prescaler value?
I set the prescaler to 1. Try setting it to 4 and you will see your values change to 1250 and 2500 because the incoming count is divided by 4.
3. T1OSCEN: Timer1 Oscillator Enable bit >> What does it mean?
This enables the external oscillator using a crystal on RB6 and 7.
4. T1CON=0b0000000; //init Timer 1 - see data sheet >> what kind of initialization do I need to setup?
The initialisation required just happens to be zero. If you wanted a prescaler of 4 then this would read T1CON=0b0010000;
5. What do this tell us? TMR1H*256+TMR1L >> ?? What is this?
This is just the normal way to combine two 8 bit values into a 16 bit value.
Thanks!!!

No problem. Hope it was useful.

Mike.
 
Multitasking

Hello, I would like to continue my program by doing multitasking.

As you know, after I finished with the steering, I programmed myself the throttle function.

The program simply read the PWM signal from the throttle channel pwm signal, then process it to flash several LEDs.

Two things I noticed here:

1. Both steering and throttle triggers are almost all the time pressed together, but the LEDs, although they are located at different port, A0, A1, A2, etc., the left/right blinking and throttle forward blinking signals cannot executed at the SAME TIME.

2. I have a flashing command takes around 5s to be executed until finished, I don't want to have this, I want to have a flashing command, like a loop, only when the throttle trigger is pressed. So, it comes into a conclusion that the led only switched on when there is a signal and it could be finished either at 2nd 3rd or 4th second, not necessary up to 5th second.

My colleague told me about the multitasking with software approach, hope somebody can give me clue regarding this, what do I need, how do I set those up. Thanks!
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top