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.

timer1 n ccp problems.

Status
Not open for further replies.

plutoo

New Member
im have one question here..
Prescaler the timer was set to 8, count set to 1, f clk was 20MHz, and TMR1 set to 0xFFFF to make sure it can operate to the maximum. If TMR1 set below than 0, overflow will happened and that make the timer not work properly.
After set everything fout = 9.54Hz.

how i want to set Fout starting with 0Hz. my digital speedometer maximum is 20Hz. so if it start with 9.54Hz, it mean my speedometer will operate when half of car maximum speed.

im using c18 compiler.

code for interrupt ccp:
void InterruptHandlerHigh(void)
{
if (PIR1bits.CCP1IF == 1)
{
PIR1bits.CCP1IF = 0;




if (pwm == 1 )
{
count = ReadCapture1();
OpenCapture1 ( C1_EVERY_FALL_EDGE & CAPTURE_INT_ON );
pwm = 0;
}
else
{
countwidth = ReadCapture1()- count;
OpenCapture1 ( C1_EVERY_RISE_EDGE & CAPTURE_INT_ON );
WriteTimer1(0xFFFF);

pwm = 1;


}
}
INTCONbits.GIE=1; //enable global interrupts

}
 
im have one question here..
Prescaler the timer was set to 8, count set to 1, f clk was 20MHz, and TMR1 set to 0xFFFF to make sure it can operate to the maximum. If TMR1 set below than 0, overflow will happened and that make the timer not work properly.
After set everything fout = 9.54Hz.

how i want to set Fout starting with 0Hz. my digital speedometer maximum is 20Hz. so if it start with 9.54Hz, it mean my speedometer will operate when half of car maximum speed.

im using c18 compiler.

code for interrupt ccp:
void InterruptHandlerHigh(void)
{
if (PIR1bits.CCP1IF == 1)
{
PIR1bits.CCP1IF = 0;




if (pwm == 1 )
{
count = ReadCapture1();
OpenCapture1 ( C1_EVERY_FALL_EDGE & CAPTURE_INT_ON );
pwm = 0;
}
else
{
countwidth = ReadCapture1()- count;
OpenCapture1 ( C1_EVERY_RISE_EDGE & CAPTURE_INT_ON );
WriteTimer1(00);//For maximum pulse duration

pwm = 1;


}
}
INTCONbits.GIE=1; //enable global interrupts

}


It is difficult to understand what you have written. You say you have initiated timer one prescaler to 8, and also set it as the source of CCP1?.I take that you have initialized Timer 1 correctly. Here is an example showing the High and Low time in Microseconds on the virtual terminal, using the Microchip Libraries for C18 Capture Module and a simulation in proteus. Use the MPLab Proteus plug in to debug.
 

Attachments

  • .c
    2.1 KB · Views: 271
  • 18F1320capture..zip
    17.3 KB · Views: 134
if(PIR1bits.CCP1IF)
{
if(start==1)
{
WriteTimer1( 00);//Start timer
OpenCapture1( CAPTURE_INT_ON & //Initialize CCP for HtoL

now i know something new..

based on your program, u reset the timer before the capture happen and my program timer looping everytime. but there is still a problem because before the rise edge and fall edge, it take about 1second. sure overflow will occur. how there is a way to set a timer for 2 second to make sure overflow will not happend?
 
Hi Pluto, there is no way to measure 2 seconds using the CCP module in simple. What you can do is use CCP to detect when the pulse starts and stops, and run a separate timer your self,which generates an interrupt at 0xffff,in the interrupt reset it and increase a counter each time that interrupt is generated to count the number of times it has overflowed. you can then just multiply the delay produced by a full timer cycle, add up any residual delay from the timer and you get the total time of your pulse. But this is a sort of in efficient way to do this.
 
if im put the delay for one second, then if the application run about 20hz, sure it will give inconstant value right?


its mean ccp module is not suitable for my application.. it is have another way to count the TLL output from the sensor?
the frequency is about 0hz to the 20hz..

if im using a timer n counter, the same problem also occur because i cant set the timer for 2 second. am i right?
 
How accurate do you need to measure this signal? You could setup a 1mS interrupt and count manually in the interrupt. That would give you a reading of 2000 for 2 seconds.

Alternatively, use a 32k crystal as the timer 1 oscillator.

Mike.
 
Last edited:
Yeah like Mike said use the internal 32kHz crystal as the timer source but it will effect your for lack of a better term " time resolution", the time it takes for the count to go one value up.Assuming you use a 20Mhz crystal in HSPLL mode,you can use timer 0 with max prescale(256), max count of 65535, which gives a total delay of 0.838848 seconds. If you are using any other mode you will easily get a delay of more then 2 seconds. Your pic might even have a post scaler, which one are you using?
 
Last edited:
the accuracy is not so important, the most important is the value is stable
because im going to display the value to the lcd..


oO i see.. but, for capture modul, as i know only function with timer1 and 3 only.
am i right? im still beginner with all of this. before this im using internal clock.
so now i need to change for external clock right?

prescale max for timer 1 is 1:8. so, now, im using that timer.

if im using external clock, it mean i need another oscillator for my timer rite?
 
Last edited:
If the internal oscillator is accurate enough then you can simply slow it down during the capture period. If you set it to 500kHz (OSCCON=0x32) then you can measure periods up to 4 seconds.

Mike.
 
this is my firs time see osccon word..

so to make sure the osccon work, i need to enable T1OSCEN right.

how do u calculate the osccon?
 
No need to use the timer1 oscillator, OSCCON controls the internal oscillator and you can set it to various values. I chose 500kHz as this will allow you to measure up to 4 seconds. I still don't know which pic you are using so can't advise where to find it in the data sheet but in the 18F1320 data sheet it is on page 17.

Mike.
 
im using pic18f452... how capture module work without timer, as i know, the capture module work with timer1 or timer 3.

if there is have another way, please teach me.
im still beginner with pic microcontroller.
 
Forget everything I said. You said you were using the internal oscillator but the 452 doesn't have an internal oscillator and so you must be using an external crystal. If you are running at 20MHz then I would setup a timer to interrupt every 500uS and use the same interrupt to check the pulse pin for the start and end of the pulse. This way a pulse of 2s will give a count of 4000 and a 20Hz signal a count of 50 as the pulse is only half the wavelength.

I'm going out now but could knock together an example ISR later if you are still stuck.

Mike.
 
11.0 TIMER1 MODULE
The Timer1 module timer/counter has the following
features:
• 16-bit timer/counter
(two 8-bit registers; TMR1H and TMR1L)
• Readable and writable (both registers)
• Internal or external clock select
• Interrupt-on-overflow from FFFFh to 0000h
• RESET from CCP module special event trigger

it said pic18f452 have internal osc.

what u mean setup a timer to interrupt?
it is mean i need to setup another interrupt for timer?

i have use the interrupt for capture module.
 
The newer chips have an internal oscillator that replaces the external crystal that you currently have. This is what I thought you were referring to as the internal clock.

You now have two options,
1. use the external timer1 oscillator with a slower crystal.
2. Setup a 1mS timer and manually check the input pin and start/stop the count.

If you decide to do number two then I could post example code.

Mike.
 
Status
Not open for further replies.

Latest threads

Back
Top