8051單片機入門十任務
====================《目錄》====================
/*任務一2017-1-13 8:42:33 P0 = x ; // x 從00-255(0x00-0xff ) 之間選10個數, // 依次下到板子里觀察現象,並總結規律。*/eg:#include<reg52.h>void main(){ P0 = 12;// 0x0c;}/*任務二2017-1-13 10:03:13 任意控制你們想亮的led燈每位同學先寫 自己學號尾號的燈亮(尾號為 0,9的 就寫倒數第二位)*/eg:#include<reg52.h>//將P0 口的第七位 取名為led7sbit led7 = P0^6; void main(){ led7 = 0;}/*任務三2017-1-13 11:32:34 寫一個流水燈 從左向右流,再從右向左流 在從兩邊向中間流 *3*/eg:見 test3/*任務四2017-1-13 19:55:36 點亮你們的第一個數碼管 顯示你們的學號最後一位*/eg:見 test4/*任務五2017-1-13 22:23:23 點亮你們的所有數碼管 顯示從0000 0000到FFFF FFFF(大概一秒變換一次)*/eg:見 test5/*任務六2017-1-14 16:34:20 用數碼管顯示你們的學號最後八位*/eg:見 test6/*任務七2017-1-14 16:35:43 用數碼管顯示LOVE YOU*/eg:見 test7/*任務八2017-1-15 15:05:40 用單片機的定時器和數碼管 做一個倒計時(精確到100毫秒) 【從9-0,到0後蜂鳴器響一下】*/eg:見 test8/*任務九2017-1-15 19:01:28 用單片機的定時器和數碼管 做一個電子錶(小時-分鐘-秒) */eg:見 test9/*任務十2017-1-15 19:01:28 用單片機的定時器、數碼管、獨立按鍵 做一個秒錶(精確到0.01秒) 一個按鍵,按下開始計時,再按停止計時。*/
所有完整代->git.oschina
====================代碼示例====================
- test3.c
/*任務三2017-1-13 19:00:59 寫一個流水燈 從左向右流,再從右向左流 在從兩邊向中間流 *3*/#include<reg52.h>#define uint unsigned int#define uchar unsigned char//延時函數void delay(int t){ uchar i; int j; for(j=0;j<t;j++) for(i=0;i<125;i++) ;}void main(){/*思路分析: 從右向左 0111 1111 >> 1 =》0011 1111 +1000 0000 1011 1111 1101 1111 ... 從左向右 1111 1110 << 1 =》1111 1100 +1 1111 1101 1111 1011 ... 從兩邊往中間 0111 1110 : 0x7e 1011 1101 : 0xbd 1101 1011 : 0xdb 1110 0111 : 0xe7*/ uchar i; uchar led; //保存8位亮燈的數據 while(1){ led = 0xfe; for(i=0;i<8;i++){ P0 = led; led = (led << 1)+1; delay(1000); } // 從右向左 led = 0x7f; for(i=0;i<8;i++){ P0 = led; led = (led >> 1)+0x80; delay(1000); } for(i=0;i<3;i++){ P0 = 0x7e;delay(1000); P0 = 0xbd;delay(1000); P0 = 0xdb;delay(1000); P0 = 0xe7;delay(1000); } }}
- Test4.c
/*任務四2017-1-13 19:55:36 點亮你們的第一個數碼管 顯示你們的學號最後一位*/#include<reg52.h>#define uchar unsigned char #define uint unsigned intuchar code shu[]={0x3f,0x06,0x5b,0x4f,0x66, 0x6d,0x7d,0x07,0x7f,0x6f /*前面是0~9的斷選碼*/,0x40/*-*/,};sbit W = P3^6;sbit D = P3^7;// 大概延時x ms的函數void Delay_xms(uint x);void main(){ while(1){ // 送位選 P0 = 0xfe; // 1111 1110 // 先把數據送到鎖存器D口(輸入口) // 再打開鎖存器 W = 1; W = 0; // 送斷選 P0 = shu[0]; D = 1; D = 0; } }// 大概延時x ms的函數void Delay_xms(uint x){ uint j; uchar i; for (j=0;j<x;j++) for (i=0;i<110;i++);}
- Test5.c
/*任務五2017-1-13 22:23:23 點亮你們的所有數碼管 顯示從0000 0000到FFFF FFFF(大概一秒變換一次)*/#include<reg52.h>#define uchar unsigned char #define uint unsigned intuchar code shu[]={0x3f,0x06,0x5b,0x4f,0x66, 0x6d,0x7d,0x07,0x7f,0x6f /*前面是0~9的斷選碼*//*,0x40:-*/, 0x77,0x7c,0x39,0x5e,0x79,0x71/*A-F*/ };sbit W = P3^6;sbit D = P3^7;// 大概延時x ms的函數void Delay_xms(uint x);// 數碼管顯示函數void Shuma_Display(uchar w,uchar d);void main(){ inti; while(1){ for(i=0;i<15;i++){ Shuma_Display(0x00,shu[i]); Delay_xms(1000); } } } // 大概延時x ms的函數void Delay_xms(uint x){ uint j; uchar i; for (j=0;j<x;j++) for (i=0;i<110;i++);} void Shuma_Display(uchar w,uchar d){ // 送位選 P0 = w; W = 1; W = 0; // 送斷選 P0 = d; D = 1; D = 0;}
- Test6.c
/*任務六2017-1-14 16:34:20 用數碼管顯示你們的學號最後八位*/#include<reg52.h>#define uchar unsignedchar #define uint unsignedintuchar code shu[]={0x3f,0x06,0x5b,0x4f,0x66, 0x6d,0x7d,0x07,0x7f,0x6f, /*前面是0~9的斷選碼*/ 0x77,0x7c,0x39,0x5e,0x79,0x71,/*A-F*/ 0x40/*:-*/ };uchar code wei[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};sbit W = P3^6;sbit D = P3^7;// 大概延時x ms的函數void Delay_xms(uint x);// 數碼管顯示函數void Shuma_Display(uchar w,uchar d);void main(){ while(1){ Shuma_Display(wei[7],shu[0]); Shuma_Display(wei[6],shu[1]); Shuma_Display(wei[5],shu[2]); Shuma_Display(wei[4],shu[0]); Shuma_Display(wei[3],shu[2]); Shuma_Display(wei[2],shu[0]); Shuma_Display(wei[1],shu[9]); Shuma_Display(wei[0],shu[0]); } } // 大概延時x ms的函數void Delay_xms(uint x){ uint j; uchar i; for (j=0;j<x;j++) for (i=0;i<110;i++);} void Shuma_Display(uchar w,uchar d){ uchar i=0; // 送位選 P0 = w; W = 1; W = 0; // 送斷選 P0 = d; D = 1; D = 0; for(;i<250;i++); }
- Test7.c
/*任務七2017-1-14 16:34:20 用數碼管顯示LOVE YOU*/#include<reg52.h>#define uchar unsigned char #define uint unsigned intuchar code wei[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};sbit W = P3^6;sbit D = P3^7;// 大概延時void Delay_xms(uint x);// 數碼管顯示函數void Shuma_Display(uchar w,uchar d);void main(){ while(1){ Shuma_Display(wei[7],0x3e); // U Shuma_Display(wei[6],0xbf); // O Shuma_Display(wei[5],0x6f); // Y Shuma_Display(wei[3],0x79); // E Shuma_Display(wei[2],0x3e); // V Shuma_Display(wei[1],0x3f); // O Shuma_Display(wei[0],0x38); // L }}// 大概延時x ms的函數void Delay_xms(uint x){ uint j; uchar i; for (j=0;j<x;j++) for (i=0;i<110;i++);} void Shuma_Display(uchar w,uchar d){ uchar i=0; // 送位選 P0 = w; W = 1; W = 0; // 送斷選 P0 = d; D = 1; D = 0; for(;i<250;i++);}
- Test8.c
/*任務八2017-1-15 15:05:40 用單片機的定時器和數碼管 做一個倒計時(精確到100毫秒)9-0 9.9 - 0.0*/#include<reg52.h>#define uchar unsigned char #define uint unsigned intuchar code shu[]={0x3f,0x06,0x5b,0x4f,0x66, 0x6d,0x7d,0x07,0x7f,0x6f, /*前面是0~9的斷選碼*/ 0x77,0x7c,0x39,0x5e,0x79,0x71,/*A-F*/ 0x40/*:-*/ };uchar code wei[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};uchar t_50ms = 0;sbit W = P3^6;sbit D = P3^7;// 大概延時x ms的函數void Delay_xms(uint x);// 數碼管顯示函數void Shuma_Display(uchar w,uchar d); void main (){ uint time = 100; TMOD=0X01; // 設置定時器0為工作方式1 TH0=(65536-45872)/256; // 裝初值45872 在11.0592MHz下為50ms TL0=(65536-45872)%256; EA=1; //開總中斷 ET0=1; //開定時器0中斷 TR0=1; //計數器開始工作 while(1){ Shuma_Display(wei[7],shu[time%10]); Shuma_Display(wei[6],shu[time/10]+0x80);//+0x80數字就帶上了小數點 if(t_50ms>=2){ t_50ms = 0; time --; if(time<=0){ TR0 = 0; } } }}void T0_50ms() interrupt 1{ TH0=(65536-45872)/256; TL0=(65536-45872)%256; t_50ms++;}// 大概延時x ms的函數void Delay_xms(uint x){ uint j; uchar i; for (j=0;j<x;j++) for (i=0;i<110;i++);}// 數碼管顯示函數void Shuma_Display(uchar w,uchar d){ uchar i=0; // 送位選 P0 = w; W = 1; W = 0; // 送斷選 P0 = d; D = 1; D = 0; for(;i<250;i++);}
- Test9.c
/*任務九2017-1-15 19:01:28 用單片機的定時器和數碼管 做一個電子錶(小時-分鐘-秒) */#include<reg52.h>#define uchar unsigned char #define uint unsigned intuchar code shu[]={0x3f,0x06,0x5b,0x4f,0x66, 0x6d,0x7d,0x07,0x7f,0x6f, /*前面是0~9的斷選碼*/ 0x77,0x7c,0x39,0x5e,0x79,0x71,/*A-F*/ 0x40/*:-*/ };uchar code wei[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};uchar t_50ms = 0;sbit W = P3^6;sbit D = P3^7;// 大概延時x ms的函數void Delay_xms(uint x);// 數碼管顯示函數void Shuma_Display(uchar w,uchar d); void main (){ uchar hour=0; uchar min=0; uchar sec=0; TMOD=0X01; // 設置定時器0為工作方式1 TH0=(65536-45872)/256; // 裝初值45872 在11.0592MHz下為50ms TL0=(65536-45872)%256; EA=1; //開總中斷 ET0=1; //開定時器0中斷 TR0=1; //計數器開始工作 while(1){ // 顯示秒 Shuma_Display(wei[7],shu[sec%10]); Shuma_Display(wei[6],shu[sec/10]); Shuma_Display(wei[5],shu[16]); //顯示「-」 // 顯示分 Shuma_Display(wei[4],shu[min%10]); Shuma_Display(wei[3],shu[min/10]); Shuma_Display(wei[2],shu[16]); //顯示「-」 // 顯示小時 Shuma_Display(wei[1],shu[hour%10]); Shuma_Display(wei[0],shu[hour/10]); if(t_50ms>=20){ // t_50ms = 20時,說明已經計時一秒了 t_50ms = 0; // t_50ms 清零 sec++; // 秒加一 if(sec>=59){ // 當秒到59時,說明已經計時一分了 sec=0; // 秒清零 min++; // 分加一 下面依次類推 if(min>=59){ min=0; hour++; if(hour>=24){ hour=0; } } } } }}void T0_50ms() interrupt 1{ TH0=(65536-45872)/256; TL0=(65536-45872)%256; t_50ms++;}// 大概延時x ms的函數void Delay_xms(uint x){ uint j; uchar i; for (j=0;j<x;j++) for (i=0;i<110;i++);}// 數碼管顯示函數void Shuma_Display(uchar w,uchar d){ uchar i=0; // 送位選 P0 = w; W = 1; W = 0; // 送斷選 P0 = d; D = 1; D = 0; for(;i<250;i++);}
=====================送上原理圖================
推薦閱讀: