单片机c语言程序设计实训报告,(整理)单片机C语言程序设计实训100例.doc
(整理)單片機C語言程序設計實訓100例.doc
.單片機C語言程序設計實訓100例基于8051Proteus仿真案例第 01 篇 基礎程序設計01閃爍的LED/* 名稱閃爍的LED說明LED按設定的時間間隔閃爍*/includereg51.hdefine uchar unsigned chardefine uint unsigned intsbit LEDP10;延時void DelayMSuint xuchar i;whilexfori0;i120;i;主程序void mainwhile1LEDLED;DelayMS150;02 從左到右的流水燈/* 名稱從左到右的流水燈說明接在P0口的8個LED從左到右循環依次點亮,產生走馬燈效果*/includereg51.hincludeintrins.hdefine uchar unsigned chardefine uint unsigned int延時void DelayMSuint xuchar i;whilexfori0;i120;i;主程序void mainP00 xfe;while1P0_crol_P0,1; P0的值向左循環移動DelayMS150;03 8只LED左右來回點亮/* 名稱8只LED左右來回點亮說明程序利用循環移位函數_crol_和_cror_形成來回滾動的效果*/includereg51.hincludeintrins.hdefine uchar unsigned chardefine uint unsigned int延時void DelayMSuint xuchar i;whilexfori0;i120;i;主程序void mainuchar i;P20 x01;while1fori0;i7;iP2_crol_P2,1; P2的值向左循環移動DelayMS150;fori0;i7;iP2_cror_P2,1; P2的值向右循環移動DelayMS150;04 花樣流水燈/* 名稱花樣流水燈說明16只LED分兩組按預設的多種花樣變換顯示*/includereg51.hdefine uchar unsigned chardefine uint unsigned intuchar code Pattern_P0 0 xfc,0 xf9,0 xf3,0 xe7,0 xcf,0 x9f,0 x3f,0 x7f,0 xff,0 xff,0 xff,0 xff,0 xff,0 xff,0 xff,0 xff, 0 xe7,0 xdb,0 xbd,0 x7e,0 xbd,0 xdb,0 xe7,0 xff,0 xe7,0 xc3,0 x81,0 x00,0 x81,0 xc3,0 xe7,0 xff, 0 xaa,0 x55,0 x18,0 xff,0 xf0,0 x0f,0 x00,0 xff,0 xf8,0 xf1,0 xe3,0 xc7,0 x8f,0 x1f,0 x3f,0 x7f, 0 x7f,0 x3f,0 x1f,0 x8f,0 xc7,0 xe3,0 xf1,0 xf8,0 xff,0 x00,0 x00,0 xff,0 xff,0 x0f,0 xf0,0 xff, 0 xfe,0 xfd,0 xfb,0 xf7,0 xef,0 xdf,0 xbf,0 x7f,0 xff,0 xff,0 xff,0 xff,0 xff,0 xff,0 xff,0 xff, 0 xff,0 xff,0 xff,0 xff,0 xff,0 xff,0 xff,0 xff,0 x7f,0 xbf,0 xdf,0 xef,0 xf7,0 xfb,0 xfd,0 xfe, 0 xfe,0 xfc,0 xf8,0 xf0,0 xe0,0 xc0,0 x80,0 x00,0 x00,0 x00,0 x00,0 x00,0 x00,0 x00,0 x00,0 x00, 0 x00,0 x00,0 x00,0 x00,0 x00,0 x00,0 x00,0 x00,0 x00,0 x80,0 xc0,0 xe0,0 xf0,0 xf8,0 xfc,0 xfe, 0 x00,0 xff,0 x00,0 xff,0 x00,0 xff,0 x00,0 xff;uchar code Pattern_P2 0 xff,0 xff,0 xff,0 xff,0 xff,0 xff,0 xff,0 xfe,0 xfc,0 xf9,0 xf3,0 xe7,0 xcf,0 x9f,0 x3f,0 xff, 0 xe7,0 xdb,0 xbd,0 x7e,0 xbd,0 xdb,0 xe7,0 xff,0 xe7,0 xc3,0 x81,0 x00,0 x81,0 xc3,0 xe7,0 xff, 0 xaa,0 x55,0 x18,0 xff,0 xf0,0 x0f,0 x00,0 xff,0 xf8,0 xf1,0 xe3,0 xc7,0 x8f,0 x1f,0 x3f,0 x7f, 0 x7f,0 x3f,0 x1f,0 x8f,0 xc7,0 xe3,0 xf1,0 xf8,0 xff,0 x00,0 x00,0 xff,0 xff,0 x0f,0 xf0,0 xff, 0 xff,0 xff,0 xff,0 xff,0 xff,0 xff,0 xff,0 xff,0 xfe,0 xfd,0 xfb,0 xf7,0 xef,0 xdf,0 xbf,0 x7f, 0 x7f,0 xbf,0 xdf,0 xef,0 xf7,0 xfb,0 xfd,0 xfe,0 xff,0 xff,0 xff,0 xff,0 xff,0 xff,0 xff,0 xff, 0 xff,0 xff,0 xff,0 xff,0 xff,0 xff,0 xff,0 xff,0 xfe,0 xfc,0 xf8,0 xf0,0 xe0,0 xc0,0 x80,0 x00, 0 x00,0 x80,0 xc0,0 xe0,0 xf0,0 xf8,0 xfc,0 xfe,0 xff,0 xff,0 xff,0 xff,0 xff,0 xff,0 xff,0 xff, 0 x00,0 xff,0 x00,0 xff,0 x00,0 xff,0 x00,0 xff;延時void DelayMSuint xuchar i;whilexfori0;i120;i;主程序void mainuchar i;while1從數組中讀取數據送至P0和P2口顯示fori0;i136;iP0Pattern_P0i;P2Pattern_P2i;DelayMS100;05 LED模擬交通燈/*名稱LED模擬交通燈說明東西向綠燈亮若干秒,黃燈閃爍5次后紅燈亮, 紅燈亮后,南北向由紅燈變為綠燈,若干秒后南北向黃燈閃爍5此后變紅燈,東西向變綠燈,如此重復。*/includereg51.hdefine uchar unsigned chardefine uint unsigned intsbit RED_AP00; 東西向燈sbit YELLOW_AP01;sbit GREEN_AP02;sbit RED_BP03; 南北向燈sbit YELLOW_BP04;sbit GREEN_BP05;uchar Flash_Count0,Operation_Type1; 閃爍次數,操作類型變量延時void DelayMSuint xuchar i;whilex fori0;i120;i;交通燈切換void Traffic_LightswitchOperation_Typecase 1東西向綠燈與南北向紅燈亮RED_A1;YELLOW_A1;GREEN_A0;RED_B0;YELLOW_B1;GREEN_B1;DelayMS2000;Operation_Type2;break;case 2東西向黃燈閃爍,綠燈關閉DelayMS300;YELLOW_AYELLOW_A;GREEN_A1;ifFlash_Count10 return; 閃爍5次Flash_Count0;Operation_Type3;break;case 3東西向紅燈,南北向綠燈亮RED_A0;YELLOW_A1;GREEN_A1;RED_B1;YELLOW_B1;GREEN_B0;DelayMS2000;Operation_Type4;break;case 4南北向黃燈閃爍5次DelayMS300;YELLOW_BYELLOW_B;GREEN_B1;ifFlash_Count10 return;Flash_Count0;Operation_Type1;主程序void mainwhile1 Traffic_Light;06 單只數碼管循環顯示09/*名稱單只數碼管循環顯示09說明主程序中的循環語句反復將09的段碼送至P0口,使數字09循環顯示*/includereg51.hincludeintrins.hdefine uchar unsigned chardefine uint unsigned intuchar code DSY_CODE0 xc0,0 xf9,0 xa4,0 xb0,0 x99,0 x92,0 x82,0 xf8,0 x80,0 x90,0 xff;延時void DelayMSuint xuchar t;whilex fort0;t120;t;主程序void mainuchar i0;P00 x00;while1 /* for;i11;i P0DSY_CODEi; DelayMS300; 注另一方案 */P0DSY_CODEi;ii110;DelayMS300;07 8只數碼管滾動顯示單個數字/*名稱8只數碼管滾動顯示單個數字說明數碼管從左到右依次滾動顯示07,程序通過每次僅循環選通一只數碼管*/includereg51.hincludeintrins.hdefine uchar unsigned chardefine uint unsigned intuchar code DSY_CODE0 xc0,0 xf9,0 xa4,0 xb0,0 x99,0 x92,0 x82,0 xf8,0 x80,0 x90;延時void DelayMSuint xuchar t;whilex fort0;t120;t;主程序void mainuchar i,wei0 x80;while1fori0;i8;iP20 xff; 關閉顯示wei_crol_wei,1;P0DSY_CODEi; 發送數字段碼P2wei; 發送位碼DelayMS300;08 8只數碼管動態顯示多個不同字符電路如上圖/*名稱8只數碼管動態顯示多個不同字符說明數碼管動態掃描顯示07。*/includereg51.hincludeintrins.hdefine uchar unsigned chardefine uint unsigned intuchar code DSY_CODE0 xc0,0 xf9,0 xa4,0 xb0,0 x99,0 x92,0 x82,0 xf8,0 x80,0 x90;延時void DelayMSuint xuchar t;whilex fort0;t120;t;主程序void mainuchar i,wei0 x80;while1fori0;i8;iP20 xff;P0DSY_CODEi; 發送段碼wei_crol_wei,1;P2wei; 發送位碼DelayMS2;09 8只數碼管閃爍顯示數字串電路如上圖/*名稱8只數碼管閃爍顯示數字串說明數碼管閃爍顯示由07構成的一串數字 本例用動態刷新法顯示一串數字,在停止刷新時所有數字顯示消失。*/includereg51.hdefine uchar unsigned chardefine uint unsigned int段碼表uchar code DSY_CODE0 xc0,0 xf9,0 xa4,0 xb0,0 x99,0 x92,0 x82,0 xf8,0 x80,0 x90;位碼表uchar code DSY_IDX0 x01,0 x02,0 x04,0 x08,0 x10,0 x20,0 x40,0 x80;延時void DelayMSuint xuchar t;whilex fort0;t120;t;主程序void mainuchar i,j;while1fori0;i30;iforj0;j8;jP00 xff;P0DSY_CODEj; 發送段碼P2DSY_IDXj; 發送位碼DelayMS2; P20 x00; 關閉所有數碼管并延時DelayMS1000;10 8只數碼管滾動顯示數字串電路如上圖/*名稱8只數碼管滾動顯示數字串說明數碼管向左滾動顯示3個字符構成的數字串*/includereg51.hincludeintrins.hdefine uchar unsigned chardefine uint unsigned int段碼表uchar code DSY_CODE0 xc0,0 xf9,0 xa4,0 xb0,0 x99,0 x92,0 x82,0 xf8,0 x80,0 x90,0 xff;下面數組看作環形隊列,顯示從某個數開始的8個數(10表示黑屏)uchar Num10,10,10,10,10,10,10,10,2,9,8;延時void DelayMSuint xuchar t;whilex fort0;t120;t;主程序void mainuchar i,j,k0,m0 x80;while1刷新若干次,保持一段時間的穩定顯示fori0;i15;iforj0;j8;j發送段碼,采用環形取法,從第k個開始取第j個P00 xff;P0DSY_CODENumkj11; m_crol_m,1;P2m; 發送位碼DelayMS2; kk111;環形隊列首支針k遞增,Num下標范圍010,故對11取余11 K1-K4 控制LED移位/*名稱K1-K4 控制LED移位說明按下K1時,P0口LED上移一位; 按下K2時,P0口LED下移一位; 按下K3時,P2口LED上移一位; 按下K4時,P2口LED下移一位;*/includereg51.hincludeintrins.hdefine uchar unsigned chardefine uint unsigned int延時void DelayMSuint xuchar i;whilex fori0;i120;i;根據P1口的按鍵移動LEDvoid Move_LEDif P1 K1else ifP1K2else ifP1 K3else ifP1K4主程序void mainuchar Recent_Key;最近按鍵P00 xfe;P20 xfe;P10 xff;Recent_Key0 xff;while1ifRecent_KeyP1Recent_KeyP1; 保存最近按鍵Move_LED;DelayMS10;12 K1-K4 按鍵狀態顯示/*名稱K1-K4 按鍵狀態顯示說明K1、K2按下時LED點亮,松開時熄滅, K3、K4按下并釋放時LED點亮,再次按下并釋放時熄滅;*/includereg51.hdefine uchar unsigned chardefine uint unsigned intsbit LED1P00;sbit LED2P01;sbit LED3P02;sbit LED4P03;sbit K1P10;sbit K2P11;sbit K3P12;sbit K4P13;延時void DelayMSuint xuchar i;whilex fori0;i120;i;主程序void mainP00 xff;P10 xff;while1LED1K1;LED2K2;ifK30whileK30;LED3LED3;ifK40whileK40;LED4LED4;DelayMS10;13 K1-K4 分組控制LED/*名稱K1-K4 分組控制LED說明每次按下K1時遞增點亮一只LED,全亮時再次按下則再次循環開始, K2按下后點亮上面4只LED,K3按下后點亮下面4只LED,K4按下后關閉所有LED*/includereg51.hdefine uchar unsigned chardefine uint unsigned int延時void DelayMSuint xuchar i;whilex fori0;i120;i;主程序void mainuchar k,t,Key_State;P00 xff;P10 xff;while1tP1;ift0 xffDelayMS10;iftP1 continue;取得4位按鍵值,由模式XXXX1111X中有一位為0,其他均為1變為模式0000XXXXX中有一位為1,其他均為0Key_Statet4;k0;檢查1所在位置,累加獲取按鍵號kwhileKey_State0k;Key_State1;根據按鍵號k進行4種處理switchkcase 1ifP00 x00 P00 xff;P01;DelayMS200;break;case 2P00 xf0;break;case 3P00 x0f;break;case 4P00 xff;14 K1-K4 控制數碼管移位顯示/*名稱K1-K4 控制數碼管移位顯示說明按下K1時加1計數并增加顯示位, 按下K2時減1計數并減少顯示位, 按下K3時清零。*/includereg51.hdefine uchar unsigned chardefine uint unsigned int段碼uchar code DSY_CODE0 xc0,0 xf9,0 xa4,0 xb0,0 x99,0 x92,0 x82,0 xf8,0 x80,0 x90,0 xff;位碼uchar code DSY_Index0 x80,0 x40,0 x20,0 x10,0 x08,0 x04,0 x02,0 x01;待顯示到各數碼管的數字緩沖(開始僅在0位顯示0,其他黑屏)uchar Display_Buffer0,10,10,10,10,10,10,10;延時void DelayMSuint xuchar i;whilex fori0;i120;i;void Show_Count_ON_DSYuchar i;fori0;i8;iP00 xff;P0DSY_CODEDisplay_Bufferi;P2DSY_Indexi;DelayMS2;主程序void mainuchar i,Key_NO,Key_Counts0;P00 xff;P10 xff;P20 x00;while1Show_Count_ON_DSY;P10 xff;Key_NOP1;P1口按鍵狀態分別為K1-0 xfe,K2-0 xfd,K3-0 xfbswitchKey_NOcase 0 xfeKey_Counts;ifKey_Counts8 Key_Counts8;Display_BufferKey_Counts-1Key_Counts;break;case 0 xfdifKey_Counts0Display_BufferKey_Counts10;break;case 0 xfbDisplay_Buffer00;fori1;i8;i Display_Bufferi10;Key_Counts0;若鍵未釋放則僅刷新顯示,不進行鍵掃描whileP10 xff Show_Count_ON_DSY;15 K1-K4 控制數碼管加減演示/*名稱K1-K4 控制數碼管加減演示說明按下K1后加1計數,按下K2后減1計數,按下K3后清零。*/includereg51.hincludeintrins.hdefine uchar unsigned chardefine uint unsigned int段碼uchar code DSY_CODE0 xc0,0 xf9,0 xa4,0 xb0,0 x99,0 x92,0 x82,0 xf8,0 x80,0 x90,0 xff;待顯示的3位緩沖uchar Num_Buffer0,0,0;按鍵代碼,按鍵計數uchar Key_Code,Key_Counts0;延時void DelayMSuint xuchar i;whilex fori0;i120;i;顯示函數void Show_Counts_ON_DSYuchar i,j0 x01;Num_Buffer2Key_Counts/100;Num_Buffer1Key_Counts/1010;Num_Buffer0Key_Counts10;fori0;i3;ij_cror_j,1;P00 xff;P0DSY_CODENum_Bufferi;P2j;DelayMS1;主程序void mainuchar i;P00 xff;P10 xff;P20 x00;Key_Code0 xff;while1Show_Counts_ON_DSY;P10 xff;Key_CodeP1;有鍵按下時,數碼管刷新顯示30次,該行代碼同時起到延時作用ifKey_Code0 xfffori0;i30;i Show_Counts_ON_DSY;switchKey_Codecase 0 xfeifKey_Counts255 Key_Counts;break;case 0 xfdifKey_Counts0 Key_Counts;break;case 0 xfbKey_Counts0;Key_Code0 xff;16 4X4矩陣鍵盤控制條形LED顯示/*名稱4X4矩陣鍵盤控制條形LED顯示說明運行本例時,按下的按鍵值越大點亮的LED越多。*/includereg51.hincludeintrins.hdefine uchar unsigned chardefine uint unsigned int矩陣鍵盤按鍵特征碼表uchar code KeyCodeTable0 x11,0 x12,0 x14,0 x18,0 x21,0 x22,0 x24,0 x28,0 x41,0 x42,0 x44,0 x48,0 x81,0 x82,0 x84,0 x88;延時void DelayMSuint xuchar i;whilex fori0;i120;i;鍵盤掃描uchar Keys_Scanuchar sCode,kCode,i,k;低4位置0,放入4行P10 xf0;若高4位出現0,則有鍵按下ifP1ifP1行掃描碼初值fork0;k4;k對4行分別進行掃描P1sCode;ifP1fori0;i16;i查表得到按鍵序號并返回ifkCodeKeyCodeTableireturni;elsesCode_crol_sCode,1; return-1;主程序void mainuchar i,P2_LED,P3_LED;uchar KeyNo-1;按鍵序號,-1表示無按鍵while1KeyNoKeys_Scan;掃描鍵盤獲取按鍵序號KeyNo ifKeyNo-1P2_LED0 xff;P3_LED0 xff;fori0;iKeyNo;i鍵值越大,點亮的LED越多ifi8P3_LED1;elseP2_LED1;P3P3_LED; 點亮條形LEDP2P2_LED;17 數碼管顯示4X4矩陣鍵盤按鍵號/*名稱數碼管顯示4X4矩陣鍵盤按鍵號說明按下任意鍵時,數碼管都會顯示其鍵的序號,掃描程序首先判斷按鍵發生在哪一列,然后根據所發生的行附加不同的值,從而得到按鍵的序號。*/includereg51.hdefine uchar unsigned chardefine uint unsigned int段碼uchar code DSY_CODE0 xc0,0 xf9,0 xa4,0 xb0,0 x99,0 x92,0 x82,0 xf8,0 x80,0 x90,0 x88,0 x83,0 xc6,0 xa1,0 x86,0 x8e,0 x00;sbit BEEPP37;上次按鍵和當前按鍵的序號,該矩陣中序號范圍015,16表示無按鍵uchar Pre_KeyNo16,KeyNo16;延時void DelayMSuint xuchar i;whilex fori0;i120;i;矩陣鍵盤掃描void Keys_Scanuchar Tmp;P10 x0f;高4位置0,放入4行DelayMS1;TmpP10 x0f;按鍵后0f變成0000XXXX,X中一個為0,3個仍為1,通過異或把3個1變為0,唯一的0變為1switchTmp判斷按鍵發生于03列的哪一列case 1KeyNo0;break;case 2KeyNo1;break;case 4 KeyNo2;break;case 8KeyNo3;break;defaultKeyNo16;無鍵按下P10 xf0; 低4位置0,放入4列DelayMS1;TmpP140 x0f;按鍵后f0變成XXXX0000,X中有1個為0,三個仍為1;高4位轉移到低4位并異或得到改變的值switchTmp對03行分別附加起始值0,4,8,12case 1KeyNo0;break;case 2KeyNo4;break;case 4KeyNo8;break;case 8KeyNo12;蜂鳴器void Beepuchar i;fori0;i100;iDelayMS1;BEEPBEEP;BEEP0;主程序void mainP00 x00;BEEP0;while1P10 xf0;ifP10 xf0 Keys_Scan; 獲取鍵序號ifPre_KeyNoKeyNoP0DSY_CODEKeyNo;Beep;Pre_KeyNoKeyNo;DelayMS100;18 開關控制LED/*名稱開關控制LED說明開關S1和S2分別控制LED1和LED2。*/includereg51.hsbit S1P10;sbit S2P11;sbit LED1P00;sbit LED2P01;主程序void mainwhile1LED1S1;LED2S2;19 繼電器控制照明設備/*名稱繼電器控制照明設備說明按下K1燈點亮,再次按下時燈熄滅*/includereg51.hdefine uchar unsigned chardefine uint unsigned intsbit K1P10;sbit RELAYP24;延時void De
總結
以上是生活随笔為你收集整理的单片机c语言程序设计实训报告,(整理)单片机C语言程序设计实训100例.doc的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: questasim linux安装包,U
- 下一篇: C语言复杂的学生成绩管理系统,哭诉、拜求