用单片机汇编语言编写一秒的定时 用定时器T0 方式2 急求!! 用汇编语言编写程序,用定时器T1中断方式实现在P1.2管脚上...
\u600e\u6837\u7528\u5355\u7247\u673a\u5b9a\u65f6\u5668T0\u7684\u65b9\u5f0f2\u5b9e\u73b0P0\u53e3LED\u706f\u4eae\u4e00\u79d2\u706d\u4e00\u79d2\uff1f\u6c47\u7f16\u8bed\u8a00\uff0c\u8c22\u8c22\uff01#include
unsigned char timer;
//\u7cfb\u7edf\u65f6\u949f: 12Mhz
//\u9ad8\u7535\u5e73\u70b9\u4eae.
void delay(unsigned int delaytime)
{ timer = delaytime;
while(timer!=0)
{}
}
void main()
{
unsigned light=1;
TMOD = 0X11; //timer1 mode 1,timer0 mode 1
PCON = 0x00; //SMOD = 0
TH0=(0xffff-10000)/256;
TL0=(0xffff-10000)%256;
TR0=1;
ET0=1;
EA =1;
while(1)
{P1=light;
delay(1000);//\u5ef6\u65f61\u79d2.
light <<=1;
if(light ==0)
light =1;
}
}
void T0_service(void) interrupt 1 using 1
{//1\u6beb\u79d2\u4e2d\u65ad1\u6b21.
TH0=(0xffff-1000)/256;
TL0=(0xffff-1000)%256;
if(timer>0)timer--;
}
ORG 0000H SJMP START ORG 0013H CPL P1.2 MOV TH1, #0D8H MOV TL1, #0F0H RETISTART: MOV TMOD, #10H MOV TH1, #0D8H MOV TL1, #0F0H SETB TR1 MOV IE, #84H SJMP $END
TIME1S EQU 30HTIME50ms EQU 31H
ORG 0000H ;程序入口
AJMP main
ORG 000BH ;TO中断入口
AJMP INT_T0
ORG 100H
main: MOV TIME1S,#0
MOV TIME50ms,#0
MOV TMOD ,#02H ;写控制字,将T0设为方式2( 8位自动重装)
MOV TH0, #06H ;THO和TL0的计算为:2的16次方-(晶振*定时时间 /12)
MOV TL0, #06H ;TC =0FFH-06H= 6H =6===》延时时间为(2^8-TC)*12/16 =250us 定时时间为250us
SETB TR0 ;启动T0
SETB ET0 ;允许T0中断
SETB EA ;开放CPU中断
LOOP: NOP
AJMP LOOP
INT_T0:PUSH PSW
PUSH ACC
INC TIME50ms
MOV A,TIME50mS
CJNE A,#200,INT_01 ;200次为TIME1S加1,此处时间为200*250us=50ms
MOV TIME50ms,#0
INC TIME1s
MOV A,TIME1S
CJNE A,#20,INT_01 ;此处再做20次即为1S
2020
MOV TIME1S,#0
;;;;;;;;;;;此处你可以编你要的程序,我在这里将P3。1脚取反,每秒变一次电平
CPL P3.1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
INT_01:POP ACC
POP PSW
RETI
END
注:以上设计为12M晶振,仿真通过。此方法并未达到精确的定时,因为8位定时器,要做好多4000次才完成1秒的定时。它的时间会延长了一点点,每一秒种有0。01秒的误差左右,要看你用的是什么单片机。若要精确定时建议用16位定时方式,这样误差会更小一点。
**********我爱梅梅头
#include
#define
uchar
unsigned
char
sbit
led=p1^0;
uchar
time;
void
inittimer0(void)
{
tmod
=
0x01;
//定时器0,工作方式1
th0
=
0x3c;
tl0
=
0xb0;
//50ms中断一次
ea
=
1;
et0
=
1;
tr0
=
1;
}
void
main(void)
{
inittimer0();
while(1);
//
分号,表示在此一直循环执行,等待中断。相当于汇编的
ajmp
$
}
void
timer0interrupt(void)
interrupt
1
{
th0
=
0x3c;
tl0
=
0xb0;
time++;
if(time==20)
//中断20次,1s
{
time=0;
led=!led;
//p1.0上的led1s亮灭一次
}
}
MOV TMOD,#02H
MOV TL0,#06H
MOV TH0,#06H
MOV R5,#40
MOV R6,#100
SETB TR0
DELAY: SETB TR0
JBC TF0,DEL1
SJMP DELAY
DEL1: DJNZ R5,DELAY
DJNZ R6,DELAY
RET
是51单片机吧!R5是八位的,最大255,你给的是400。
DEL1: DJNZ R5,DELAY
DJNZ R6,DELAY
该成
DEL1: DJNZ R5,DEL1
DJNZ R6, DEL1
定时器T0延时1s的是吧,你邮箱是多少我给你传一个
绛旓細DELAY: MOV R3,#10;寤舵椂瀛绋嬪簭锛屽欢鏃1绉 D1: MOV R4,#200 D2: MOV R5,#250 D3: DJNZ R5,D3 DJNZ R4,D2 DJNZ R3,D1 RET END 绗簩绉嶆柟娉曪細鐢ㄥ畾鏃鍣ㄧ殑鏂规硶瀹炵幇 鈥斺斺擬OV R1锛#20 鈥斺斺擠LY: MOV TMOD,#00000001B锛涘欢鏃跺瓙绋嬪簭锛岃瀹氬畾鏃跺櫒鐨勫伐浣滄柟寮 D1: SETB ...
绛旓細TIME1S EQU 30H TIME50ms EQU 31H ORG 0000H ;绋嬪簭鍏ュ彛 AJMP main ORG 000BH ;TO涓柇鍏ュ彛 AJMP INT_T0 ORG 100H main: MOV TIME1S,#0 MOV TIME50ms,#0 MOV TMOD ,#02H ;鍐欐帶鍒跺瓧锛屽皢T0璁句负鏂瑰紡2( 8浣嶈嚜鍔ㄩ噸瑁咃級MOV TH0, #06H ;THO鍜孴L0鐨...
绛旓細瑙o細閫夌敤瀹氭椂璁℃暟鍣═1宸ヤ綔涓庡畾鏃舵柟寮2锛屽畾鏃250us锛坒osc锛12MHz锛夋満鍣ㄥ懆鏈熶负 1us锛氬畾鏃跺櫒鍒濆糡C锛6 鎵浠ワ細TH1锛6锛孴L1锛6 ORG 00H LJMP START ORG 1BH LJMP INT_T1 ORG 0100H START:MOV TMOD,#20H MOV TH1,#6H MOV TL1,#6H SETB ET1 SETB EA MOV R7,...
绛旓細鍗曠墖鏈甯哥敤鐨勭紪绋嬭瑷鏈夋眹缂栬瑷銆丆璇█绛夈備互涓嬫槸浣跨敤姹囩紪璇█瀹炵幇寤舵椂1s鐨勭▼搴忥細浠ヤ笂绋嬪簭涓紝浣跨敤浜嗕竴涓8浣嶈鏁板櫒鏉ュ疄鐜板欢鏃跺姛鑳姐傜敱浜庡崟鐗囨満鏅舵尟棰戠巼涓12MHz锛屾瘡涓寚浠ら渶瑕佹墽琛12/4=3涓満鍣ㄥ懆鏈燂紙鍏朵腑4浠h〃鎸囦护鍛ㄦ湡锛夛紝鍥犳鍙互閫氳繃璋冩暣璁℃暟鍣ㄥ垵鍊兼潵瀹炵幇涓嶅悓鐨勫欢鏃舵椂闂淬傚叿浣撹绠楄繃绋嬪涓嬶細姣忎釜鎸囦护闇瑕佹墽琛...
绛旓細CJNE R1,#20,MAIN杩欓噷鏄笉瀵圭殑锛屾病鏈夎繖鏉¤鍙ワ紝鍙互鏀规垚锛歁OV A,R1 CJNE A,#29,MAIN 鍏朵粬鍦版柟涔熸湁闂锛屼慨鏀瑰涓 MOV TMOD,#01H MOV R1,#00H MAIN:MOV TH0,#3CH MOV TL0,#0B0H SETB TR0 JNB TF0,CLR TF0 INC R1 MOV A,R1 CJNE R1,#20,MAIN ...
绛旓細11.0592 M 鐢ㄦ垜杩欎釜绋嬪簭鍚: 鍑嗙‘瀹氭椂鏄仛涓嶅埌鐨勶紝浣嗘槸鏈夎В鍐冲姙娉曘備綘鍙互浣跨敤琛ュ伩娉曘傛瘮濡10鍒嗛挓鍚庡樊1绉銆備綘灏辫蒋浠惰ˉ鍋1S杩涘幓灏卞ソ浜嗐傛垜鏄繖涔堝共鐨勩傚綋鐒朵綘杩樺彲浠ヤ娇鐢ㄥ叾浠栨洿濂界殑鍔炴硶銆俤elay1s: mov r7,#2 delay1s1: mov r6,#200 delay1s2: mov r5,#250 delay1s3: nop nop nop...
绛旓細鍗曠墖鏈銆鐢ㄦ眹缂栬瑷缂栫▼銆 璁捐涓杞欢寤舵椂1s鐨勫瓙绋嬪簭銆傝鏅舵尟棰戠巼涓12MHz銆 瑙f斁瀹氬悜 | 娴忚2043 娆 |涓炬姤 鎴戞湁鏇村ソ鐨勭瓟妗堟帹鑽愪簬2017-12-16 10:52:13 鏈浣崇瓟妗 涓嶅悓鍗曠墖鏈 鎸囦护鍛ㄦ湡鏈夋墍涓嶅悓 涓嬮潰DJNZ鎸囦护3涓櫠鎸懆鏈烡ELAY1S:MOV R7,#000HMOV R6,#00AHMOV R5,#03EHLOOP: DJNZ R7,LOOP NOP DJNZ R6...
绛旓細delay1000ms:锛圚T46R65鐨鍗曠墖鏈锛夛紙娉ㄦ剰涓嶈寮鍚鏃朵腑鏂厑璁镐綅锛塵ov a,10000111b 锛堣缃鐜囧拰璁版暟鏂瑰紡锛夛紙涓婂崌璁版暟杩樻槸涓嬮檷璁版暟锛塵ov tmr0c,a 锛坱mr0c 璁℃暟鍣ㄧ殑鎺у埗瀵勫瓨鍣級mov a,low(65536-31250) (璁剧疆鐨勯鐜囧氨鏄1S涓婂崌31250)鍥犳鏄疍ELAY1S mov TMR0L,a 锛堣鏁板櫒鐨勮祴浜堝垵濮嬪肩殑...
绛旓細涓嶇煡浣犱滑鑰佸笀鏄庝箞鎯崇殑锛孯7涓嶄簨鍏堣祴鍊间細閫犳垚绋嬪簭鍚姩鐘舵佷笉瀹氾紝鏈夊彲鑳藉欢杩熷弽搴斻侻OV R7锛#14涓嶈兘鍑嗙‘瀹氭椂1绉銆備篃璁告槸MOV R7锛#14H锛屼綘璐薄浜"H"銆傚亣瀹氭槸14H杩欑绉嶆儏鍐碉細T0_INT: MOV TL0, #58H;T0瀹氭椂鏃堕棿50姣 MOV TH0, #9EH DJNZ R7, NEXT;娌″埌1绉掞紝閫鍑轰腑鏂紝杩欓噷鏄鍑猴紝娌℃湁寰幆 ...
绛旓細鐢瀹氭椂鍣═0鏉ュ畬鎴 閭d箞鎴戜护T0鐨勬孩鍑烘椂闂翠负200us 閭d箞T0鐨勫垵鍊间负56 1锛坰锛=200*250*2锛坲s锛変笅闈㈡槸绋嬪簭 ORG 0000H LJMP MAIN ORG 000BH LJMP CTC0 ORG 100H MIAN:MOV SP,#5FH MOV R7,#250;杞欢璁℃暟绗竴绾 MOV R6,#2;杞欢璁℃暟绗簩绾 MOV TL0,#56;閫佸垵鍊 MOV TH0,#56;ANL TOMD,#...