C51单片机程序 单片机c51程序
c51\u5355\u7247\u673a\u7a0b\u5e8f\u5b9e\u4f8b#include#define uchar unsigned charuchar tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00}; //0\u52309uchar num,cnt,disn;uchar keyval,disk;uchar led[]={1,2,3,4};void dealdat(uchar a){led[0]=0;led[1]=0;led[2]=0;led[3]=0;led[a]=disk;}void delay(unsigned int a){unsigned int i,j;for(i=0;i100){cnt=0;disn++;disn%=4;dealdat(disn);}}uchar kbscan(void){unsigned char sccode,recode;P3=0x0f; //\u53d10\u626b\u63cf,\u5217\u7ebf\u8f93\u5165if ((P3 & 0x0f) != 0x0f) //\u6709\u952e\u6309\u4e0b{//delay(20); //\u5ef6\u65f6\u53bb\u6296\u52a8if ((P3&0x0f)!= 0x0f){sccode = 0xef; //\u9010\u884c\u626b\u63cf\u521d\u503cwhile((sccode&0x01)!=0){ P3=sccode;if((P3&0x0f)!=0x0f){ recode=(P3&0x0f)|0xf0;return((~sccode)+(~recode)); } elsesccode=(sccode<<1)|0x01;}}}return 0; //\u65e0\u952e\u6309\u4e0b\uff0c\u8fd4\u56de0}void getkey(void){unsigned char key;key=kbscan();if(key==0){keyval=0xff;return;}switch(key){case 0x11:keyval=7;break;case 0x12:keyval=4;break;case 0x14:keyval=1;break;case 0x18:keyval=10;break;case 0x21:keyval=8;break;case 0x22:keyval=5;break;case 0x24:keyval=2;break;case 0x28:keyval=0;break;case 0x41:keyval=9;break;case 0x42:keyval=6;break;case 0x44:keyval=3;break;case 0x48:keyval=11;break;case 0x81:keyval=12;break;case 0x82:keyval=13;break;case 0x84:keyval=14;break;case 0x88:keyval=15;break;default:keyval=0xff;break;}}main(){TMOD=0x11;TH0=(65536-5000)/256;TL0=(65536-5000)%256;TR0=1;ET0=1;EA=1;while(1){getkey();if(keyval!=0xff)disk=keyval;delay(10);}}
\u5982\u679c\u4e0d\u662f\u201c\u5929\u7965\u201d\u7684\u677f\u5b50\uff0c\u8fd9\u7a0b\u5e8f\u662f\u4e0d\u884c\u7684\u3002
-------------
\u5982\u679c\u662f\u201c\u5929\u7965\u201d\u7684\uff0c\u90a3\u4e48\u53ef\u4ee5\u8bd5\u8bd5\u4e0b\u9762\u7684\u663e\u793a\u7a0b\u5e8f\uff0c\u6211\u589e\u52a0\u4e86\u4e24\u884c\u6d88\u9690\u7684\u8bed\u53e5\uff0c\u7ecf\u8fc7\u5b9e\u9a8c\uff0c\u53ef\u4ee5\u6b63\u5e38\u663e\u793a\u201c\u6309\u4e00\u4e0b\u952e\uff0c\u6570\u5b57\u9012\u589e\u201d\u7684\u6548\u679c\u3002
void display(a, b)
{
num = t;
a = num / 10;
b = num % 10;
dula = 1; P0 = table[a]; dula = 0;
wela = 1; P0 = 0xfe; wela = 0;
delay(10);
wela = 1; P0 = 0xff; wela = 0; //\u5173\u95ed\u663e\u793a.
dula = 1; P0 = table[b]; dula = 0;
wela = 1; P0 = 0xfd; wela = 0;
delay(10);
wela = 1; P0 = 0xff; wela = 0; //\u5173\u95ed\u663e\u793a.
}
TH0 = 0xf6;
TL0 = 0xff;
上两行是定时器/计数器T0的初值,因为T0是16位的,所以用TH0表示高八位,TL0表示低八位
TH1 = 0xfd;
TL1 = 0xfd;
T1初值
TMOD = 0x21;
定时器/计数器模式控制寄存器 如图
GATE=1时,由外部中断引脚INT0、INT1来启动定时器T0、T1
GATE=0时,仅由TR0,TR1置位分别启动定时器T0、T1。
C/T=0时为定时功能,C/T=1时为计数功能
M0、M1——方式选择功能
M1M0 工作方式 计数器模式 TMOD(设置定时器模式)
0 0 方式0 13位计数器 TMOD=0x00
0 1 方式1 16位计数器 TMOD=0x01
1 0 方式2 自动重装8位计数器 TMOD=0x02
1 1 方式3 T0分为2个8位独立计数器,T1为无中断重装8位计数器 TMOD=0x03
PCON = 0x00;
PCON主要是为CHMOS型单片机的电源控制而设置的专用寄存器
WDCON = 0x00;清狗
SCON = 0x50;
SCON1 = 0x40;
串口控制寄存器
TH0 = 0xf6; //T0定时器初值 -2303
TL0 = 0xff;
TH1 = 0xfd; //波特率9600
TL1 = 0xfd;
TMOD = 0x21; //定时器0位模式1,定时器2位模式2
PCON = 0x00; //波特率不加倍
WDCON = 0x00; //清狗
SCON = 0x50; //串口0工作与模式1,启动接收
SCON1 = 0x40; //串口1工作与模式1,用于发送
这些是单片机寄存器的初始化 ,要学习,必须根据每个寄存器每一位代表什么具体意思来判断,TH0 TL0 是定时器0 的高8位 和低8位,其他的同理,至于为什么要那么写,是要看整个程序要实现什么具体的功能来判断,需要用到单片机的什么资源,就必须得线初始化,慢慢学把,51系列单片机还是挺简单的。
定时/计数器,TH高八位,TL低八位,T0是计时,T1是串口通讯
绛旓細include <REG51.H> include <intrins.h> define uchar unsigned char void delay(uchar ms);void main(){ uchar i,LED;//瀹氫箟涓や釜鍙橀噺 P2 = 0xFF;//鍒濆鍖朠2鍙 LED = 0x7F;//鍒濆鍖栧彉閲 while(1){ for(i=0;i<8;i++){ P2 = LED;//LX鐏寒鍏跺畠鐏 delay(100);//绋冲畾鏄剧ず100姣...
绛旓細include <reg51.h> define uchar unsigned char define uint unsigned int main(){ uint xdata *xp;//瀹氫箟鎸囧悜鐗囧RAM鐨勬寚閽 uint data *p;//瀹氫箟鎸囧悜鐗囧唴RAM鐨勬寚閽 uchar i;//瀹氫箟寰幆鍙橀噺 xp=0x100;//鎸囧悜鐗囧RAM鐨100H鍗曞厓 p=0x30;//鎸囧悜鐗囧唴RAM鐨30H鍗曞厓 for(i=0;i<10;i++){//...
绛旓細MAIN: MOV P1, #11111110B ; P1 鍙 鐐逛寒涓涓彂鍏変簩鏋佺 ACALL DELAY ; 璋冪敤寤舵椂瀛绋嬪簭 MOV P1, #11111101B ; 浠庡乏鍚戝彸绉诲姩 涓浣 ACALL DELAY MOV P1, #11111011B ACALL DELAY MOV P1, #11110111B ACALL DELAY MOV P1, #11101111B ACALL DEL...
绛旓細//鍋囪鏈8涓狶ED閫氳繃涓婃媺鐢甸樆鎺ュ埌P1绔彛, 楂樼數骞崇伅鐏,浣庣數骞崇伅浜,閫愪釜寰幆鐐逛寒 include <reg51.h> unsigned char data tab1[9]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00};void delay(){ unsigned int data n;for (n=0;n<40000;n++){ ;} } void main(){ unsigned char...
绛旓細涓嬪垪绋嬪簭璋冭瘯鎴愬姛銆備娇鐢ㄦ櫠鎸负12M銆//杞欢寤舵椂锛屼娇P1鍙e鎺ョ殑8涓彂鍏変簩鏋佺锛屼互姣忕1浣嶇殑閫熷害锛岃疆娴佸彂鍏夈//鍙戝厜椤哄簭锛0123锛1234锛2345锛3456锛4567锛5670锛6701锛7012銆傚惊鐜//--- include<reg52.h> include<intrins.h> unsigned char a;void main(){ unsigned char i, j, k;a = 0x0f...
绛旓細{ TMOD=0x01; //瀹氭椂鍣═O宸ヤ綔鍦ㄦ柟寮1 TH0=(65536-5000)/256; //瑁呭垵鍊硷紝12M鏅舵尟 1涓1us; 5000涓5000us=5ms锛汿L0=(65536-5000)%256;EA =1; //寮鎬讳腑鏂 ET0=1; //寮瀹氭椂鍣═O涓柇 TR0=1; //鍚姩瀹氭椂鍣 P1=0; //鍒濆鍖朠1鍙 while(1) ; //绋嬪簭鍦ㄨ繖閲岀瓑寰...
绛旓細璁捐涓涓鍗曠墖鏈篊51绋嬪簭,瀹炵幇鍔熻兘鏄,1銆佹寜KEY1鍙湁绾㈢伅浜,KEY2鍙湁榛勭伅浜,KEY3鍙湁缁跨伅浜2銆佸紑濮嬪彸鏁扮爜绠℃樉绀0,姣忔寜KEY1涓娆℃暟鐮佺鏄剧ず鍔1,鑷9鍚庡啀寰幆銆傚厛宸茬粡缂栧ソ绋嬪簭,绗竴涓疄鐜,浣嗙浜屼釜鎬诲紕涓嶅銆傛劅瑙夐棶棰樺緢绠鍗,灏辨槸宸竴鐐,楂樻墜甯垜鐪嬩竴涓嬨... 灞曞紑 #...
绛旓細void main(void )//涓绋嬪簭 { TMOD=0x01;// 瀹氭椂鍣0宸ヤ綔鏂瑰紡涓锛屾柟寮2涓娆″彧鑳藉畾鏃256us,涓柇澶绻 锛屼笉寤鸿浣跨敤 ET0=1;EA=1;TH0=(65536-50000)/256;//瀹氭椂50ms TL0=(65536-50000)%256;TR0=1;while(1){ dislpay();}//鏄剧ず瀛愮▼搴忥紝涓庣‖浠惰繛鎺ユ湁鍏 } //涓柇瀛愮▼搴 void Timer0(...
绛旓細鎵撳紑C51绋嬪簭涔﹀啓杞欢锛屾柊寤哄伐绋嬶紝杩欎釜鏄繀椤荤殑鍝︺傚湪寮瑰嚭鐨勫璇濇缂栬緫宸ョ▼鍚嶅苟淇濆瓨鍒拌淇濆瓨鐨勫湴鏂广備繚瀛樹箣鍚庝細寮瑰嚭濡備笅鐨勭獥鍙o紝鐐瑰嚮Atmel鐨勫姞鍙凤紝閫夋嫨AT89s52骞剁‘瀹氥備細缁х画寮瑰嚭杩欎釜绐楀彛锛岄夋嫨鍚﹀嵆鍙紝涓嶇敤绠′簡銆傚叿浣撳共浠涔堢殑鎴戜篃涓嶆噦銆傛鏃跺氨瑕佸紑濮嬪缓绔嬬▼搴忎簡锛岀偣鍑诲鍥剧殑鍥炬爣锛屾柊寤虹▼搴忥紝娉ㄦ剰淇濆瓨鐨勬椂鍊 ...
绛旓細鍘熺悊锛氫綅瀵诲潃鏄竴绉嶄娇鐢鍗曠墖鏈瀹炵幇绋嬪簭鎺у埗浣嶉昏緫鎿嶄綔鐨勬柟娉曪紝閫氳繃涓绯诲垪鎸囦护鍦ㄥ崟瀛楄妭鐨勫唴瀛樼┖闂翠腑鐨勪竴涓壒瀹氱殑浣 (bit 浣) 涓婅繘琛屾搷浣溿傚畠鐢ㄤ簬鎺у埗鍙湁涓ょ姸鎬佺殑I/O绔彛銆傜紪绋嬫柟娉曪細1. 鐢 ORL A, #data 鍛戒护灏嗘煇涓寚瀹氱殑浣嶇疆浣嶏紝鍗虫妸鍗曞瓧鑺傚唴閮ㄦ煇涓寚瀹氱殑浣嶇疆1銆2. 鐢 ANL A, #data 鍛戒护娓呴櫎...