单片机让T0定时1s有什么办法? 51单片机T0定时器4种方式的定时时长分别是多少
\u5229\u7528\u5b9a\u65f6\u5668T0\u7684\u5de5\u4f5c\u65b9\u5f0f1\u5b9e\u73b01\u79d2\u949f\u5ef6\u65f6\u7a0b\u5e8f(\u5355\u7247\u673a)#include
#defineuchar unsigned char
sbit LED=P1^0;
uchar time;
void initT0( )
{
TMOD = 0x01; //\u5b9a\u65f6\u56680\uff0c\u5de5\u4f5c\u65b9\u5f0f1
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256; //50ms\u4e2d\u65ad\u4e00\u6b21
EA = 1;
ET0 = 1;
TR0 = 1;
}
void main(void)
{
initT0();
while(1)
if(time==20) //\u4e2d\u65ad20\u6b21\uff0c1s
{
time=0;
LED=\uff01LED; //P1.0\u4e0a\u7684LED\uff0c1s\u4eae\u6216\u706d\u4e00\u6b21
}
}
void T0int( ) interrupt 1
{
TH0 = (65536-65536)/256;
TL0 = (65536-65536)%256;
time++;
}
\u8fd9\u4e2a\u53d6\u51b3\u4e8e\u7cfb\u7edf\u65f6\u949f\u548c\u5b9a\u65f6\u5668\u4f4d\u6570\u3002
\u4f8b\u5982\u4f20\u7edf89c51\u5355\u7247\u673a\uff08\u5728\u4e0d\u8003\u8651\u65b0\u578b\u5355\u7247\u673a\u6709\u5206\u9891\u8bbe\u7f6e\u7684\u60c5\u51b5\u4e0b\uff09\uff0c12MHz\u7cfb\u7edf\u65f6\u949f\uff0c\u91c7\u75288\u4f4d\u5b9a\u65f6\u5668\uff0c\u6700\u5927\u5b9a\u65f6\u65f6\u95f4\u4e3a256us\uff1b\u5982\u679c\u91c7\u752816\u4f4d\u5b9a\u65f6\u5668\uff0c\u6700\u5927\u5b9a\u65f6\u65f6\u95f4\u4e3a65536us\u3002
精确定时,那么就必须用到硬件定时器了,而不能用网上那些实验室教学delay函数;
所以说,你得学习研究你用的单片机硬件定时器的配置方法;
硬件定时器学会配置使用后。比如,你配置1ms基础时间定时器进入中断服务函数1次,
然后在中断服务函数timer_ISR里面,再定义一个static静态变量cnt,
每进入1次中断服务函数timer_ISR,变量cnt++,当cnt = 1000了,是不是就意味着1s时间到
了。
打个比方,你很快,每过1ms你钻进屋里打个手枪,当你打了1000次手枪 ,是不是就意味着1s时间就过去了!
绛旓細棣栧厛锛绮剧‘瀹氭椂锛岄偅涔堝氨蹇呴』鐢ㄥ埌纭欢瀹氭椂鍣ㄤ簡锛岃屼笉鑳界敤缃戜笂閭d簺瀹為獙瀹ゆ暀瀛elay鍑芥暟锛涙墍浠ヨ锛屼綘寰楀涔犵爺绌朵綘鐢ㄧ殑鍗曠墖鏈虹‖浠跺畾鏃跺櫒鐨勯厤缃柟娉曪紱纭欢瀹氭椂鍣ㄥ浼氶厤缃娇鐢ㄥ悗銆傛瘮濡傦紝浣犻厤缃1ms鍩虹鏃堕棿瀹氭椂鍣ㄨ繘鍏ヤ腑鏂湇鍔″嚱鏁1娆★紝鐒跺悗鍦ㄤ腑鏂湇鍔″嚱鏁皌imer_ISR閲岄潰锛屽啀瀹氫箟涓涓猻tatic闈欐佸彉閲廲nt锛屾瘡杩涘叆1娆′腑鏂...
绛旓細5銆乿oid LED_Flash() interrupt 1{TH0 = (65535-50000)/256;TL0 = (65535-50000)%256;if(++T_Count == 20){LED = !LED;T_Count = 0;}} 6銆佽繖涓▼搴忔槸1绉掗挓LED鐏細浜笌鐏樉绀恒1鍗曠墖鏈锛屾嫢鏈変袱涓瀹氭椂鍣紝鐢ㄦ潵涓柇璁℃暟锛屽垎鍒槸T0鍜孴1銆傝52鍗曠墖鏈哄拰51鍗曠墖鏈虹殑瀹氭椂鍣ㄦ槸涓鏍风殑锛屽彧鏄5...
绛旓細銆1銆戝惊鐜幏鍙栨槸鍚︽寚浠庡綋鍓嶆椂鍒荤畻璧凤紝鍓嶄竴绉掑唴T1璁″緱鐨勬柟娉㈡暟銆傘2銆戣繖閲岀己灏戜竴涓弬鏁帮紝灏辨槸寰幆鑾峰彇鐨勯噰鏍峰懆鏈焧锛圫锛夛紝濡0.1S銆傘3銆戞牴鎹畉锛岃瀹T0涓柇銆傛诲叡搴旇鏈1/t涓噰鏍风墖娈礟ulseNum[1/t]锛屾瘡娆0涓柇锛屽~鍏匬ulseNum[i++%(1/t)]銆傚苟娓呴櫎T1鍒濆笺傘4銆戜富绋嬪簭姹傚拰PulseNum[1/t]鍗冲彲銆
绛旓細銆0銆戝惎鍔T0鏃讹紙瀹氭椂妯″紡锛20-50mS涓柇锛夛紝鍚姩T1銆傘1銆戝湪T0涓柇涓紝璁℃椂锛堝锛20mS璁50娆″悗锛夛紝鍒拌揪1S锛岃绉掓爣蹇椼傜洿鎺ヨ鍙朤H1锛孴L1鐨勫间繚瀛樺埌鍙橀噺x銆傚苟娓呴櫎T1璁℃暟鍊笺傘2銆戝湪涓荤▼搴忎腑娴嬪埌绉掓爣蹇楋紝娓呴櫎锛屽皢x閫佹樉绀哄崟鍏冦傘3銆戞牴鎹叿浣撶殑LCD妯″潡锛岀粍缁囧浘褰㈡暟鎹紝閫丩CD妯″潡銆=== 銆0銆-鍒濆...
绛旓細瀹氭椂 7.5ms锛屽嵆鍙傜▼搴忓涓嬶細include<reg51.h> sbit P10=P1^0;unsigned int counter = 200;void main(){ TMOD = 0x00;TH0 = (8192 - 7500) / 32; //0X63;TL0 = (8192 - 7500) % 32; //0X18;TR0 = 1;ET0 = 1;EA = 1;while(1) { ;} } void isr_time0(void) ...
绛旓細ACC POP PSW RETI END 娉細浠ヤ笂璁捐涓12M鏅舵尟锛屼豢鐪熼氳繃銆傛鏂规硶骞舵湭杈惧埌绮剧‘鐨瀹氭椂锛屽洜涓8浣嶅畾鏃跺櫒锛岃鍋氬ソ澶4000娆℃墠瀹屾垚1绉掔殑瀹氭椂銆傚畠鐨勬椂闂翠細寤堕暱浜嗕竴鐐圭偣锛屾瘡涓绉掔鏈0銆01绉掔殑璇樊宸﹀彸锛岃鐪嬩綘鐢ㄧ殑鏄浠涔堝崟鐗囨満銆傝嫢瑕佺簿纭畾鏃跺缓璁敤16浣嶅畾鏃舵柟寮忥紝杩欐牱璇樊浼氭洿灏忎竴鐐广傛垜鐖辨姊呭ご ...
绛旓細浣犳槸鐢8051绯诲垪鍗曠墖鏈鍚э紝灏辫繖鐐瑰皬绋嬪簭锛屼簩妤煎お澶嶆潅浜嗗惂锛岃繕鏈1姣澶皯浜嗭紝鎭愭曟病鎰熻锛岄檮浠朵紶涓婏紝鏀瑰悗搴廰sm姹囩紪<BR/>闄勪欢锛歠oriask.txt
绛旓細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...
绛旓細CNTH EQU 30H CNTL EQU 31H ORG 0000H LJMP MAIN ORG 000BH LJMP T0ISR ORG 0030H MAIN:MOV TMOD,#51H MOV TH0,#HIGH(65536-50000)MOV TL0,#LOW(65536-50000)MOV TH1,#0 MOV TL1,#0 SETB TR0 SETB TR1 SETB ET0 SETB EA MOV R2,#20 SJMP T0ISR:CLR TR0 MOV TH0,#HIGH(...
绛旓細second/10];P2=dispcode[second%10];TR0=1;while(1){ if( second ==60){ TR0=0;P1=0x5F;delay();P1=0xFF;delay();} } } void t0(void) interrupt 1 using 0 { tcnt++;if(tcnt==4000){ tcnt=0;second++;P0=dispcode[second/10];P2=dispcode[second%10];} } ...