AT89C51单片机利用定时器|计数器T0产生1秒钟的定时,每当一秒定时到来时,更换指示灯点亮,依次循环点亮 用at89c51单片机的定时/计数器T0产生1秒的定时
\u7528AT89C51\u7684\u5b9a\u65f6/\u8ba1\u6570\u5668T0\u4ea7\u751f2\u79d2\u949f\u7684\u5b9a\u65f6\uff0c\u6bcf\u5f532\u79d2\u5b9a\u65f6\u5230\u6765\u65f6\uff0c\u66f4\u6362\u6307\u793a\u706f\u95ea\u70c1\uff0c\u6bcf\u4e2a\u6307\u793a\u95ea\u70c1\u7684\u9891\u7387\u4e3a0.2//\u6211\u5012\uff0c\u5199\u5b8c\u8c03\u8bd5\u597d\uff0c\u624d\u53d1\u73b0\u4f60\u8981\u7684\u662f\u6c47\u7f16\uff0c\u7b97\u4e86\uff0c\u4e0d\u5199\u4e86\uff0c\u5206\u7559\u7ed9\u522b\u4eba\u5427\uff0c\u4e0d\u662f\u592a\u96be\u7684\u9898\u3002
#include
#include
#define uchar unsigned char
#define L1 PD0
#define L2 PD1
uchar light_change=0x01;
void MCU_init(void)
{
DDRD=0xff;
PORTD=0xff;
}
void timer0_init(void)
{
TCNT0=158;
TCCR0=0x05;
TIMSK|=(1<<TOIE0);
}
ISR(TIMER0_OVF_vect)
{
static uchar cnt=0;
TCNT0=158;
if(++cnt>=20)
{
cnt=0;
light_change^=0x01;
PORTD=0xff;
}
if(light_change)PORTD^=(1<<L1);
else PORTD^=(1<<L2);
}
int main(void)
{
MCU_init();
timer0_init();
sei();
while(1);
}
\u53c2\u8003\u8fd9\u4e2a\uff1a
#include
#include
#define uchar unsigned char
#define uint unsigned int
uchar Count;
sbit Dot = P0^7;
uchar code DSY_CODE[]=
{
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f
};
uchar Digits_of_6DSY[]={0,0,0,0,0,0};
void DelayMS(uint x)
{
uchar i;
while(--x)
{
for(i=0;i<120;i++);
}
}
void main()
{
uchar i,j;
P0 = 0x00;
P3 = 0xff;
Count =0;
TMOD = 0x01;
TH0 = (65535-50000)/256;
TL0 = (65535-50000)%256;
IE = 0x82;
TR0 = 1;
while(1)
{
j = 0x7f;
for(i=5;i!=-1;i--)
{
j=_crol_(j,1);
P3 = j;
P0 = DSY_CODE[Digits_of_6DSY[i]];
if(i==1) P0 |= 0x80;
DelayMS(2);
}
}
}
void Time0() interrupt 1
{
uchar i;
TH0 = (65535-50000)/256;
TL0 = (65535-50000)%256;
if(++Count !=2) return;
Count = 0;
Digits_of_6DSY[0]++;
for(i=0;i<=5;i++)
{
if(Digits_of_6DSY[i] == 10)
{
Digits_of_6DSY[i] = 0;
if(i != 5) Digits_of_6DSY[i+1]++;
}
else break;
}
}
;仿照例7.11
;TC=M-T/T计数=9E58H(其中T=25ms)
ORG 0000H
LJMP MAIN
ORG 000BH
AJMP BRT0
ORG 0030H
MAIN:
CLR P1
MOV TMOD,#01H;T0为方式1
SETB EA
SETB ET0
SETB TR0
MOV TH0,#9EH
MOV TL0,#58H
MOV R0,#04H
MOV A,#01H
LOOP:SJMP $;等待中断
ORG 0080H
BRT0:
DJNZ R0,NEXT
MOV P1,A
RL A
MOV R0,#04H
NEXT:MOV TH0,#9EH
MOV TL0,#58H
RETI
END
初始化T0定时50MS,设个计数变量,每定时到了进中断一次+1,加到20次时就是1S,然后更换指示灯点亮,下1S 依次循环点亮。
void main(void)
{
TMOD=0x01; //16位定时模式
TH0=(65536-50000) >> 8;
TL0=(65536-50000) & 255;
TR0=1;
ET0=1;
EA=1;
while(1) ;
}
void timer0() interrupt 1
{
static unsigned char t ,Light = 1;
TF0=0;
if(++t==20)
{
t=0;
//这里加入流水灯程序
P0 = Light;
if(Light > 0x80)Light = 1;
else Light << 1;
}
//重装定时器
TH0=(65536-50000) >> 8;
TL0=(65536-50000) & 255;
}
绛旓細绋嬪簭璁捐鎬濊矾锛1.鍒╃敤鍗曠墖鏈鍐呴儴瀹氭椂鍣浣滀负璁℃椂鍣紝璋冪敤timer鍑芥暟锛2銆佽璁′竴濂楁寜閿瓙绋嬪簭锛涳紙3锛夎璁′竴濂楁暟鐮佹樉绀虹瀛愮▼搴忋備笅鍒楁彁渚涗竴濂楃▼搴忎緵鍙傝冦俰nclude<iom16v.h> include <macros.h> define uchar unsigned char define uint unsigned int const uchar table[10]={0x3f,0x06,0x5b,0x4f,0x...
绛旓細TMOD=0x01; //璁剧疆瀹氭椂鍣0宸ヤ綔鏂瑰紡01 TH0=(65536-45872)/256; //瑁呭垵鍊11.0592鏅舵尟瀹氭椂50ms TL0=(65536-45872)%256;EA=1;ET0=1;TR0=1;} void TO_time() interrupt 1 { TH0=(65536-45872)/256; //瑁呭垵鍊11.0592鏅舵尟瀹氭椂50ms TL0=(65536-45872)%256;num0++;if(40==num0) ...
绛旓細include <reg51.H> sbit P3_5 =P3^5;void delay(){ unsigned char i,j;for(j=20;j>0;j--)for( i= 250;i>0;i--);} unsigned char code dispcode[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0x00};unsigned char second...
绛旓細锛3锛夛紟 鎶娾鍗曠墖鏈绯荤粺鈥濆尯鍩熶腑鐨凱3.4锛圱0锛夌瀛愮敤瀵肩嚎杩炴帴鍒扳滈鐜囦骇鐢熷櫒鈥濆尯鍩熶腑鐨刉AVE绔瓙涓娿4锛 绋嬪簭璁捐鍐呭 锛1锛夛紟 瀹氭椂/璁℃暟鍣═0鍜孴1鐨勫伐浣滄柟寮忚缃紝鐢卞浘鍙煡锛孴0鏄伐浣滃湪璁℃暟鐘舵佷笅锛屽杈撳叆鐨勯鐜囦俊鍙疯繘琛岃鏁帮紝浣嗗宸ヤ綔鍦ㄨ鏁扮姸鎬佷笅鐨凾0锛屾渶澶ц鏁板间负fOSC/24锛岀敱浜巉OSC锛...
绛旓細浣犲ソ锛佽繖涓竴涓椂閽熺殑浠跨湡鏁堟灉锛屽彲浠ヨ皟鏁存椂闂达紝鍚屾椂鍏锋湁闂归挓鍔熻兘
绛旓細AT89C51鍗曠墖鏈 鍒╃敤瀹氭椂鍣T0鏂瑰紡1 ORG 0000H AJMP MAIN ORG 000BH AJMP T0INT ORG 0100H MAIN: MOV TMOD , #01H MOV TH0 , #0FFH MOV TL0 , #06H MOV IE , #82H SETB TR0 SJMP T0INT: CPL P1.0 MOV TH0 , #0FFH MOV TL0 , #06H RETI END ...
绛旓細sbit P1_0=P1^0;//浣嶅畾涔 unsigned char i; //瀹氫箟鍙橀噺 void main(void){ P1_0=0;TMOD=0x01; //T0瀹氭椂鍣锛屽伐浣滄柟寮1 TH0=(65536-500)/256;TL0=(65536-500)%256; //瑁呭垵鍊 ET0=1;EA=1;TR0=1;while (1) ;} void time0 () interrupt 1 //浣跨敤涓柇鏂瑰紡1 { TH0=(...
绛旓細;LED1浜,鍏朵綑鐏->LED2浜,鍏朵綑鐏︹︼紝濡傛寰幆5娆″悗锛;杩涜鍏ㄧ伃->鍏ㄤ寒闂儊锛屾椂闂撮棿闅斾负500ms鐢p0鍙f帶鍒 涓嬪垪绋嬪簭宸茬粡娴嬭瘯閫氳繃 ;--- ORG 0000H LJMP MAIN ORG 000BH LJMP T0_INT MAIN:SETB TR0 ;T0瀹氭椂鏂瑰紡0 MOV SP, #60H MOV IE, #82H MOV B, #61 ;瀹...
绛旓細鐢T0瀹氭椂鍣瀹氭椂50ms锛岄噰鐢ㄤ腑鏂柟寮忥紝瀵逛腑鏂鏁20娆★紝灏辨槸1绉掞紝鍐嶅1绉掕鏁板氨鍙互浜嗐傚叾娆℃槸鏄剧ず鍑鸿鏃剁殑绉掑嵆鍙傝繖涓彁闂湁杩欎箞澶氱亴姘寸殑鍥炵瓟锛岃屼笖涔熺綉鍙嬮噰绾充簡銆備綘鑳介噰绾充簡锛岀敤绉佷俊鍙戠粰浣犵▼搴忋備豢鐪熷浘濡備笅
绛旓細妤间笂鐨勪唬鐮佽鐢6M鐨勬櫠鎸紝杩樻湁瀹氭椂鍣娌℃湁閲嶆柊寮鍚紱浠ヤ笅浠g爜鍊熺敤妤间笂鐨勶紝鐢12M鏅舵尟 锛屽垎缁欐ゼ涓婂惂 include<reg51.h>sbit pulse=P1^0;//鑴夊啿杈撳嚭寮曡剼P1^0;void t0isr() interrupt 1 //瀹氭椂鍣0涓柇鏈嶅姟鍑芥暟{TH0=(65536-1000)/256;//閲嶆柊璧嬪糡L0=(65536-1000)%256;pulse=~pulse;//鐢靛钩鍙栧弽 ...