您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 单片机C语言程序设计实训100例--基于8051+PROTEUS仿真1
13210s的秒表“按键部分有问题”/*名称:10s的秒表说明:首次按键计时开始,再次按键暂停,第三次按键清零。*/#includereg51.h#defineucharunsignedchar#defineuintunsignedintsbitK1=P3^7;uchari,Second_Counts,Key_Flag_Idx;bitKey_State;ucharDSY_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//延时voidDelayMS(uintms){uchart;while(ms--)for(t=0;t120;t++);}//处理按键事件voidKey_Event_Handle(){if(Key_State==0){Key_Flag_Idx=(Key_Flag_Idx+1)%3;switch(Key_Flag_Idx){case1:EA=1;ET0=1;TR0=1;break;case2:EA=0;ET0=0;TR0=0;break;case0:P0=0x3f;P2=0x3f;i=0;Second_Counts=0;}}}//主程序voidmain(){P0=0x3f;//显示00P2=0x3f;i=0;Second_Counts=0;Key_Flag_Idx=0;//按键次数(取值0,1,2,3)Key_State=1;//按键状态2TMOD=0x01;//定时器0方式1TH0=(65536-50000)/256;//定时器0:15msTL0=(65536-50000)%256;while(1){if(Key_State!=K1){DelayMS(10);Key_State=K1;Key_Event_Handle();}}}//T0中断函数voidDSY_Refresh()interrupt1{TH0=(65536-50000)/256;//恢复定时器0初值TL0=(65536-50000)%256;if(++i==2)//50ms*2=0.1s转换状态{i=0;Second_Counts++;P0=DSY_CODE[Second_Counts/10];P2=DSY_CODE[Second_Counts%10];if(Second_Counts==100)Second_Counts=0;//满100(10s)后显示00}}33用计数器中断实现100以内的按键计数/*名称:用计数器中断实现100以内的按键计数说明:本例用T0计数器中断实现按键技术,由于计数寄存器初值为1,因此P3.4引脚的每次负跳变都会触发T0中断,实现计数值累加。计数器的清零用外部中断0控制。*/#includereg51.h#defineucharunsignedchar#defineuintunsignedint//段码ucharcode3DSY_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};ucharCount=0;//主程序voidmain(){P0=0x00;P2=0x00;TMOD=0x06;//计数器T0方式2TH0=TL0=256-1;//计数值为1ET0=1;//允许T0中断EX0=1;//允许INT0中断EA=1;//允许CPU中断IP=0x02;//设置优先级,T0高于INT0IT0=1;//INT0中断触发方式为下降沿触发TR0=1;//启动T0while(1){P0=DSY_CODE[Count/10];P2=DSY_CODE[Count%10];}}//T0计数器中断函数voidKey_Counter()interrupt1{Count=(Count+1)%100;//因为只有两位数码管,计数控制在100以内(00~99)}//INT0中断函数voidClear_Counter()interrupt0{Count=0;}34100000s以内的计时程序/*名称:100000s以内的计时程序说明:在6只数码管上完成0~99999.9s。*/#includereg51.h#includeintrins.h#defineucharunsignedchar#defineuintunsignedint//段码ucharcode4DSY_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//6只数码管上显示的数字ucharDigits_of_6DSY[]={0,0,0,0,0,0};ucharCount;sbitDot=P0^7;//延时voidDelayMS(uintms){uchart;while(ms--)for(t=0;t120;t++);}//主程序voidmain(){uchari,j;P0=0x00;P3=0xff;Count=0;TMOD=0x01;//计数器T0方式1TH0=(65536-50000)/256;//50ms定时TL0=(65536-50000)%256;IE=0x82;TR0=1;//启动T0while(1){j=0x7f;//显示Digits_of_6DSY[5]~Digits_of_6DSY[0]的内容//前面高位,后面低位,循环中i!=-1亦可写成i!=0xfffor(i=5;i!=-1;i--){j=_crol_(j,1);P3=j;P0=DSY_CODE[Digits_of_6DSY[i]];if(i==1)Dot=1;//加小数点DelayMS(2);}}}//T0中断函数voidTimer0()interrupt1{uchari;TH0=(65536-50000)/256;//恢复初值TL0=(65536-50000)%256;if(++Count!=2)return;5Count=0;Digits_of_6DSY[0]++;//0.1s位累加for(i=0;i=5;i++)//进位处理{if(Digits_of_6DSY[i]==10){Digits_of_6DSY[i]=0;if(i!=5)Digits_of_6DSY[i+1]++;//如果0~4位则分别向高一位进位}elsebreak;//若某低位没有进位,怎循环提前结束}}35定时器控制数码管动态显示/*名称:定时器控制数码管动态显示说明:8个数码管上分两组动态显示年月日与时分秒,本例的位显示延时用定时器实现。*/#includereg51.h#includeintrins.h#defineucharunsignedchar#defineuintunsignedint//段码,最后一位是“-”的段码ucharcodeDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};//待显示的数据:09-12-25与23-59-58(分两组显示)ucharcodeTable_of_Digits[][8]={{0,9,10,1,2,10,2,5},{2,3,10,5,9,10,5,8}};uchari,j=0;uintt=0;//主程序voidmain(){P3=0x80;//位码初值TMOD=0x00;//计数器T0方式0TH0=(8192-4000)/32;//4ms定时TL0=(8192-4000)%32;IE=0x82;TR0=1;//启动T0while(1);}//T0中断函数控制数码管刷新显示voidDSY_Show()interrupt16{TH0=(8192-4000)/32;//恢复初值TL0=(8192-4000)%32;P0=0xff;//输出位码和段码P0=DSY_CODE[Table_of_Digits[i][j]];P3=_crol_(P3,1);j=(j+1)%8;//数组第i行的下一字节索引if(++t!=350)return;//保持刷新一段时间t=0;i=(i+1)%2;//数组行i=0时显示年月日,i=1时显示时分秒}368X8LED点阵显示数字/*名称:8X8LED点阵显示数字说明:8X8LED点阵屏循环显示数字0~9,刷新过程由定时器中断完成。*/#includereg51.h#includeintrins.h#defineucharunsignedchar#defineuintunsignedintucharcodeTable_of_Digits[]={0x00,0x3e,0x41,0x41,0x41,0x3e,0x00,0x00,//00x00,0x00,0x00,0x21,0x7f,0x01,0x00,0x00,//10x00,0x27,0x45,0x45,0x45,0x39,0x00,0x00,//20x00,0x22,0x49,0x49,0x49,0x36,0x00,0x00,//30x00,0x0c,0x14,0x24,0x7f,0x04,0x00,0x00,//40x00,0x72,0x51,0x51,0x51,0x4e,0x00,0x00,//50x00,0x3e,0x49,0x49,0x49,0x26,0x00,0x00,//60x00,0x40,0x40,0x40,0x4f,0x70,0x00,0x00,//70x00,0x36,0x49,0x49,0x49,0x36,0x00,0x00,//80x00,0x32,0x49,0x49,0x49,0x3e,0x00,0x00//9};uchari=0,t=0,Num_Index;//主程序voidmain(){P3=0x80;Num_Index=0;//从0开始显示TMOD=0x00;//T0方式0TH0=(8192-2000)/32;//2ms定时TL0=(8192-2000)%32;7IE=0x82;TR0=1;//启动T0while(1);}//T0中断函数voidLED_Screen_Display()interrupt1{TH0=(8192-2000)/32;//恢复初值TL0=(8192-2000)%32;P0=0xff;//输出位码和段码P0=~Table_of_Digits[Num_Index*8+i];P3=_crol_(P3,1);if(++i==8)i=0;//每屏一个数字由8个字节构成if(++t==250)//每个数字刷新显示一段时间{t=0;if(++Num_Index==10)Num_Index=0;//显示下一个数字}}37按键控制8X8LED点阵屏显示图形/*名称:按键控制8X8LED点阵屏显示图形说明:每次按下K1时,会使8X8LED点阵屏循环显示不同图形。本例同时使用外部中断和定时中断。*/#includereg51.h#includeintrins.h#defineucharunsignedchar#defineuintunsignedint//待显示图形编码ucharcodeM[][8]={{0x00,0x7e,0x7e,0x7e,0x7e,0x7e,0x7e,0x00},//图1{0x00,0x38,0x44,0x54,0x44,0x38,0x00,0x00},//图2{0x00,0x20,0x30,0x38,0x3c,0x3e,0x00,0x00}//图3};uchari,j;//主程序voidmain(){P0=0xff;8P1=0xff;TMOD=0x01;//T0方式1TH0=(65536-2000)/256;//2ms定时TL0=(65536-2000)%256;IT0=1;//下降沿触发IE=0x83;//允许定时器0、外部0中断i=0xff;//i的初值设为0xff,加1后将从0开始while(1);}//T0中断控制点阵屏显示voidShow_Dot_Matrix()interrupt1{TH0=(65536
本文标题:单片机C语言程序设计实训100例--基于8051+PROTEUS仿真1
链接地址:https://www.777doc.com/doc-4436274 .html