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.

Using timer0

Status
Not open for further replies.

mrfunkyjay

New Member
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.
 
Last edited:
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?
 
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.
 
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!!
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top