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
 
Tools
Old 10th July 2008, 06:03 PM   #1
Default Using timer0

Hi all, I have PIC18F1330.

It has two timer, namely timer0 and timer1. I have no problem with timer1. Here are my codes:

Code:
int GetPulse2(){
	T1CON=0b00000000;   /*Init timer1*/
	TMR1L=0;
	TMR1H=0;
	TRISAbits.TRISA1=1;   /*Make this pin as an input*/
	while(PORTAbits.RA1==1); /*Make pin low*/
	while(PORTAbits.RA1==0); /*While pin is high*/
	T1CONbits.TMR1ON=1;   /*Start timer1*/
	while(PORTAbits.RA1==1); /*When pin is low*/
	T1CONbits.TMR1ON=0;   /*Stop timer1*/
	return TMR1H*256+TMR1L;  /*return 16bit timer value*/
}
I have two Pulses. Namely Pulse1 and Pulse2. I need to use this microcontroller for sake of designing etc. I need to get my timer0 working at the same principal with the code above. Is it possible to do that with timer0? Or anyone has better idea/solution to this? Thank you very much.

PS: I am going to count the Pulse Width of PWM if this info useful.
__________________
When NewBee asks...

Last edited by mrfunkyjay; 10th July 2008 at 06:03 PM.
mrfunkyjay is offline  
Old 10th July 2008, 06:08 PM   #2
Default

Quote:
Originally Posted by mrfunkyjay View Post
Or anyone has better idea/solution to this? Thank you very much.

PS: I am going to count the Pulse Width of PWM if this info useful.
Try looking at the datasheet and series reference manual, look up the CCP module.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Old 10th July 2008, 07:48 PM   #3
Default

there is no ccp module by the way, is there anything else?
__________________
When NewBee asks...
mrfunkyjay is offline  
Old 10th July 2008, 08:08 PM   #4
Default

Quote:
Originally Posted by mrfunkyjay View Post
there is no ccp module by the way, is there anything else?
That's a shame!.

Are you trying to read radio control pulses, or does it vary over a wider range than that?.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Old 10th July 2008, 08:10 PM   #5
Default

yes radio control pulse from receiver module. it is between 1ms-1.5ms-2ms.

Do you have an idea? Timer is used to make resolution better, and I used it with timer1 before. Now I need another one with timer0. Is it possible? If not, why?
__________________
When NewBee asks...
mrfunkyjay is offline  
Old 10th July 2008, 08:19 PM   #6
Default

Quote:
Originally Posted by mrfunkyjay View Post
yes radio control pulse from receiver module. it is between 1ms-1.5ms-2ms.

Do you have an idea? Timer is used to make resolution better, and I used it with timer1 before. Now I need another one with timer0. Is it possible? If not, why?
You should be able to do it with timer0 if you want, or a simple software loop is all that's required, it's really a very simple thing to measure, as it's a nice fixed width variation.

I wouldn't get too excited about trying to get massive resolution, I don't think there's that much resolution in the original pulses.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Old 11th July 2008, 10:43 AM   #7
Default Check please...

Hi please check this for me, whether I made a mistake or not.

To count up to 2ms Pulse Width, I use 8bit timer/counter from timer0 (since PCB is made and timer1 is already used in this PIC18F1330).

-8bit timer0

-Prescaler of 8

-FOSC/4 = 1Mhz

-frequency input to timer0 would be 1Mhz/8 = 125kHz or T (period) = 8us

-since, 8 bit, 0-255 then overflow, 256x8us = 2.048ms which is enough and useable in this case.

CALCULATION:

2ms = (1/1Mhz) x 8 x ins.cycle

ins.cycle = 0.002s / 0.000008s = 250

RESULT:

250 decimal would be 2ms Pulse Width.

divide by two:

125 decimal would be 1ms Pulse Width.

center position of servo:

125/2 + 125 = 187.5 ~ 188.

then my code would be:

Code:
int GetPulse1(){
	T0CON=0b01000010;   /*Init timer0 8 bit, PSA assigned, 1:8 prescale value, reset timer0*/
	TMR0L=0;
	TMR0H=0;
	TRISAbits.TRISA0=1;   /*Make this pin as an input*/
	while(PORTAbits.RA0==1); /*Make pin low*/
	while(PORTAbits.RA0==0); /*While pin is high*/
	T0CONbits.TMR0ON=1;   /*Start timer0*/
	while(PORTAbits.RA0==1); /*When pin is low*/
	T0CONbits.TMR0ON=0;   /*Stop timer0*/
}
question is: will this work??

Later I will use 20 gap in between 188.

168 or less as turning left (servo)

169-207 as middle position

more than 208 as turning right.

Thanks for ur comments!!
__________________
When NewBee asks...
mrfunkyjay is offline  
Reply

Tags
timer0

Thread Tools
Display Modes


Similar
Title Starter Forum Replies Latest
Timer0 Prescale lord loh. Micro Controllers 10 8th October 2008 06:14 PM
TIMER0 - Have I got this right? UTMonkey Micro Controllers 8 14th December 2007 11:08 PM
PIC Timer0 Hesam Kamalan Micro Controllers 7 29th October 2007 03:32 PM
Timer0 on PIC's Hesam Kamalan Micro Controllers 4 18th July 2006 02:38 PM
Using the Timer0 module wonbinbk Micro Controllers 2 14th September 2004 07:06 AM



All times are GMT. The time now is 09:35 PM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker