You are on page 1of 4

PIC Vietnam > Microchip PIC > Cc ngn ng lp trnh khc (CCS C, HT PIC,...

) > ngt timer


CCS
PDA
View Full Version : ngt timer CCS
hunter800
22-03-2010, 02:37 PM
hix ai gii thch dm mnh chng trnh ngt ny vi!Mnh thc s rt c gng c nhng chu
hiu lm!thanks
#include <16F877A.h>
#fuses NOWDT,PUT,XT,NOPROTECT
#use delay(clock=4000000)
#byte PORTB = 0x06
int16 count;
int8 a;
//Chuong trinh ngat TMR0
#int_timer0
void interrupt_timer0()
{
set_timer0(6);
++count;
if(count == 2000) // 2000*500us = 500000us = 1s
{
count=0;
rotate_left(&a,1);
}
}
//Chuong trinh chinh
void main(void)
{
set_tris_b(0);
enable_interrupts(int_timer0);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2);
enable_interrupts(global);
set_timer0(6);// T_dinhthi = 2*(256 - 6)*1us = 500us
a = 0x01;
while(true)
{
PORTB = a;
}
}
trai_xq

22-03-2010, 05:00 PM
hix ai gii thch dm mnh chng trnh ngt ny vi!Mnh thc s rt c gng c nhng chu
hiu lm!thanks
#include <16F877A.h>
#fuses NOWDT,PUT,XT,NOPROTECT
#use delay(clock=4000000)
#byte PORTB = 0x06
int16 count;
int8 a;
//Chuong trinh ngat TMR0
#int_timer0
void interrupt_timer0()
{
set_timer0(6);
++count;
if(count == 2000) // 2000*500us = 500000us = 1s
{
count=0;
rotate_left(&a,1);
}
}
//Chuong trinh chinh
void main(void)
{
set_tris_b(0);
enable_interrupts(int_timer0);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2);
enable_interrupts(global);
set_timer0(6);// T_dinhthi = 2*(256 - 6)*1us = 500us
a = 0x01;
while(true)
{
PORTB = a;
}
}
+set_tris_b(0);//nh ngha portb l u ra s(nu nh vng lp while bn khai bo
output_b(a); th cu lnh set_tris_b(0); l khng cn thit.
+enable_interrupts(int_timer0);//mi bt u chng trnh, bn cho timer0 lm vic(bt u tnh
+setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2); //ch l chia tn s
timer0 8bit, m t 0-->255, v sau trng m li t 0. Mi ln trn nh vy l c bn tnh
thi gian nh sau:
-bn dng thch anh 4M=4000000hz. nh vy mi ln trn th c thi gian l

Ttimer0=255*4/(4.000.000) (s)=255micro s. Nhn 4 bi v mt chu k lnh ca VDk=4 chu k


xung ca thch anh. Mc ch ca chng ta l to ra mt giy, nh th th phi lm sao?
+setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2); //lnh ny s nng gii tn ca timer0 ln 2
ln na. ngha l khi c ln ny ttimer0 s c tnh li sau mi ln trn nh sau:
Ttimer0=( 255*4/(4.000.000) )* 2(s) = 510micro s
+enable_interrupts(global);//cho ngt ton b(lnh ny khng cn nu nh bn ch c ngt
timer0)
+set_timer0(6);//t gi tr u cho timer0 l 6, ngha l timer by gi tng t 6 ch khng phi
l 0 na. Ch ny thi ti ngh c s nhm ln l set_timer0(5); l ng hn, v sao phi lm vy
bn bit khng? tt ca cng ch v cho d tnh v chn s thi. Nu t nh vy th thi gian
timer0 s c tnh li nh sau:
Ttimer0=( (255-5)*4/(4.000.000) )* 2(s) = 500micro s
+nh vy bn phi cn my ln trn mi c 1s? c phi l 2000 ln khng
v 2000*500=1000000micro s=1s ok!
+on chng trnh ny s th vn :
#int_timer0
void interrupt_timer0()
{
set_timer0(5);
++count;
if(count == 2000) // 2000*500us = 500000us = 1s
{
count=0;//nu c 2000 nghn ln trn( c 1s) th cho trn li t u
rotate_left(&a,1);//tng ng vi lnh a=a<<1; ngh l dc tri mt bit, sau 1s
//bi v bn cho a=0x01<=> a=0b00000001; trong chng trnh main
}
}
//-----------------S d ti gip bn v ngy xa ti cng c gp kh khn ny ri.
v1t2m3
17-02-2011, 08:18 PM
Cm n bc trai_xp, bc vit qu cn k, khng th no khng hiu tr phi khng mun hiu.
babadada
01-03-2011, 09:49 PM
may wa gp bi ny cu bc em hiu tha bc ...thank bc
iVoVa
13-03-2011, 04:15 AM
Theo mnh tnh c gi tr cn np vo thanh ghi TMRO th mnh tnh theo cng thc ny .
T_out = ((4*Prescaler)*(256-TMR0)x Count)/4000000. ( Timer 16bit th thay 256 = 65536 )
Mnh nn cho Prescaler l 256 .
Cho Count l 1 trc. T_out l thi gian tr mnh cn to ( v d T_out = 0.5s).
Thay vo tnh c TMR0.
Nu TMR0 ko biu din gi tr va tnh ra ( vt qu 8 bit ) th ta xem mc ln hn
nh th no tng bin count ln cho ph hp v tnh li t u.
( Hin nay cc PIC18 timer 0 c th s dng n 16 bit hoc 8 bit , trong CCS C Complier khi s
dng hm set_timer0(xxx) th mnh xem m ASM thy n mc nh s dng l timer 16 bit.

Delfy_Coltech
14-03-2011, 08:34 PM
setup_timer1(TMR_INTERNAL | TMR_DIV_BY_8, 4096);
Th cn dng setup Timer1 th ny cc bn gii thch gim?
Thanks!
vBulletin v3.7.2, Copyright 2000-2011, Jelsoft Enterprises Ltd.