完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
51单片机最小系统 按键调方波周期及占空比、PWM波周期 1602LCD显示周期、占空比 系统初始化 方波周期、占空比设置 PWM波高电平最大宽度设置 源码 #include #include #define uint unsigned int #define uchar unsigned char //--------------------------1602液晶 ***it RS = P1^0; //定义端口 ***it RW = P1^1; ***it EN = P2^5; #define RS_CLR RS=0 #define RS_SET RS=1 #define RW_CLR RW=0 #define RW_SET RW=1 #define EN_CLR EN=0 #define EN_SET EN=1 #define DataPort P0 //--------------------------按键 ***it s3=P2^2;//加 ***it s4=P2^3;//减 ***it s5=P2^4;//功能切换键 //--------------------------存储波形参数 uchar o; uchar p; uchar q1,q2; uchar r1,r2,r3; //--------------------------波形选择 uchar wave;//选择波形 uchar wei; bit keyscan_done; uchar s5num=0; ***it led1=P1^6;//功能按键指示灯 ***it led2=P1^7;//加减按键指示灯 //--------------------------方波 ***it fang=P1^4;//方波输出端口 uchar T=0; uchar D=0; //--------------------------PWM波 ***it pwm=P1^5;//PWM波输出端口nvvv uint C=0; uchar pwm_high=0,pwm_low=0; //--------------------------//延时函数 void delayus(uint t) { while(--t); } void delay(uint z) { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } //--------------------------------1602液晶函数 void LCD_Write_Com(unsigned char com)//写指令函数 { delay(5); RS_CLR; RW_CLR; EN_SET; DataPort= com; _nop_(); EN_CLR; } void LCD_Write_Data(unsigned char Data)//写数据函数 { delay(5); RS_SET; RW_CLR; EN_SET; DataPort= Data; _nop_(); EN_CLR; } void LCD_Clear(void) //清屏函数 { LCD_Write_Com(0x01); delay(5); } void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data)//写字符函数 { if (y == 0) { LCD_Write_Com(0x80 + x); } else { LCD_Write_Com(0xC0 + x); } LCD_Write_Data( Data); } void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)//写字符串函数 { if (y == 0) { LCD_Write_Com(0x80 + x); //表示第一行 } else { LCD_Write_Com(0xC0 + x); //表示第二行 } while (*s) { LCD_Write_Data( *s); s ++; } } void LCD_Init(void)//初始化函数 { LCD_Write_Com(0x38); /*显示模式设置*/ delay(5); LCD_Write_Com(0x38); delay(5); LCD_Write_Com(0x38); delay(5); LCD_Write_Com(0x38); LCD_Write_Com(0x08); /*显示关闭*/ LCD_Write_Com(0x01); /*显示清屏*/ LCD_Write_Com(0x06); /*显示光标移动设置*/ delay(5); LCD_Write_Com(0x0C); /*显示开及光标设置*/ } //-------------------------------------------方波函数 void fangbo() { fang=1; delay(T*D/100); fang=0; delay(T*(1-D/100)); } //----------------------------------------PWM函数 void PWM() { pwm=1;//低电平逐渐升为高电平 for(pwm_high=1;pwm_high pwm=0; delayus(pwm_high); //PWM函数 pwm=1; delayus(C-pwm_high); } pwm=0;//高电平逐渐降为低电平 for(pwm_low=C-1;pwm_low>0;pwm_low--) { pwm=0; delayus(pwm_low); pwm=1; delayus(C-pwm_low); } pwm=1; } //-------------------------------------------按键函数 void keyscan1() { if(s5==0)//波形选择及参数设置按键 { delay(10); if(s5==0)//确认按键被按下 { s5num+=1;//记录按键被按下的次数 led1=0;//切换键指示灯 delay(30); while(!s5);//确认释放 led1=1; switch(s5num) { case 1:LCD_Clear();LCD_Write_Com(0x80+0x40+2);LCD_Write_Com(0x0f);break; case 2:LCD_Write_Com(0x80+0x40+13);LCD_Write_Com(0x0f);break; case 3:wave=1;break; case 4:LCD_Clear();wave=0;wei=0;LCD_Write_Com(0x80+0x40+2);LCD_Write_Com(0x0f);break; case 5:wave=2;s5num=0;break; } } //keyscan_done=1;//按键处理完毕 } if(s5num!=0) { if(s3==0) { delay(10); if(s3==0)//s3被按下 { led2=0; delay(30); while(!s3);//确认按键释放 led2=1; switch(s5num) { case 1:T=T+10; if(T>90){T=10;} o=T/10; r1=(1000/T)/100; r2=((1000/T)/10)%10; r3=(1000/T)%10; wei=1; //LCD_Write_Data(2,1,t1); break; case 2:D+=10; if(D>90){D=10;} p=D/10; wei=2; //LCD_Write_Data(13,1,d1); break; case 4:C+=50; if(C>250){C=50;} q1=C/100; q2=(C/10)%10; wei=3; //LCD_Write_Data(3,1,c1); break; } } } if(s4==0) { delay(10); if(s4==0) { led2=0; delay(30); while(!s4); led2=1; switch(s5num) { case 1:T-=10; if(T<10){T=90;} o=T/10; r1=(1000/T)/100; r2=((1000/T)/10)%10; r3=(1000/T)%10; wei=1; //LCD_Write_Data(2,1,t1); break; case 2:D-=10; if(D<10){D=90;} p=D/10; wei=2; //LCD_Write_Data(13,1,d1); break; case 4:C-=50; if(C<50){C=250;} q1=C/100; q2=(C/10)%10; wei=3; //LCD_Write_Data(3,1,c1); break; } } } //keyscan_done=1;//按键处理完毕 } keyscan_done=1;//按键处理完毕 } //------------------------------------------- void choosewave() { switch(wei) { case 1:LCD_Write_String(0,0,"DSF FB F:");LCD_Write_String(14,0,"Hz"); LCD_Write_String(0,1,"T:");LCD_Write_String(4,1,"Ms");LCD_Write_String(9,1,"D/T:");LCD_Write_Char(15,1,'%'); LCD_Write_Char(2,1,0x30+o);LCD_Write_Char(3,1,'0'); LCD_Write_Char(12,0,0x30+r2);LCD_Write_Char(13,0,0x30+r3);LCD_Write_Com(0x0c);break; case 2:LCD_Write_Char(13,1,0x30+p);LCD_Write_Char(14,1,'0');LCD_Write_Com(0x0c);break; case 3:LCD_Write_String(0,0,"DSF PWM "); LCD_Write_String(0,1,"C:");LCD_Write_String(5,1,"Us "); LCD_Write_Char(2,1,0x30+q1);LCD_Write_Char(3,1,0x30+q2); LCD_Write_String(4,1,"0");LCD_Write_Com(0x0c);break; default:break; } switch(wave) { case 1:pwm=1;fangbo(); break; case 2:fang=1;PWM(); break; default:break; } keyscan_done=0; } //------------------------------------------- void main() { s3=1; s4=1;//初始换按键 s5=1; led1=1;//初始化按键指示灯 led2=1; LCD_Init(); //初始化液晶 delay(20); //延时有助于稳定 LCD_Write_String(0,0,"0123456789ABCDEF"); LCD_Write_String(0,1,"0123456789ABCDEF"); delay(5000); LCD_Clear();//清屏 while(1) { keyscan1(); if(keyscan_done) choosewave(); } } |
|
相关推荐
1 个讨论
|
|
751 浏览 0 评论
使用Keil建立完整的工程,并使用外部中断0触发数码管显示903
1330 浏览 0 评论
嵌入式学习-飞凌嵌入式ElfBoard ELF 1板卡-使用AHT20进行环境监测之AHT20传感器介绍
1184 浏览 0 评论
899 浏览 0 评论
947 浏览 1 评论
【youyeetoo X1 windows 开发板体验】少儿AI智能STEAM积木平台
11865 浏览 31 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-3 19:53 , Processed in 0.680870 second(s), Total 39, Slave 31 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号