怎么写代码使单片机引脚电压在几秒钟内从5v降到0v

发布网友 发布时间:2022-04-23 13:46

我来回答

3个回答

热心网友 时间:2023-10-16 10:22

#include//包含头文件#include#defineucharunsignedchar//宏定义#defineuintunsignedintsbits1=P3^5;//定义按键的接口sbits2=P3^6;sbits3=P3^7;sbits4=P3^4;sbits5=P2^3;sbitled0=P3^0;//定义四个LED,分别表示不同的波形sbitled1=P3^1;sbitled2=P3^2;sbitled3=P3^3;sbitlcdrs=P2^7;//液晶控制引脚,还有一个控制脚是RW,因为我们只需要向液晶里写数据系那是就好了,所以,我们直接将RW引脚接地sbitlcden=P2^6;charnum,boxing,u;//定义全局变量ucharpinlv=100,bujin=1,bujin1=1;//频率初始值是10Hz,步进值默认是0.1,显示步进值变量ucharcodetable[]="01234567";//定义显示的数组ucharcodetable1[]="Fout=Waveform:";//初始化显示字符unsignedintm,pwm=50;//定义变量minta,b,h,num1;//定义全局变量//自定义字符ucharcodezifu[]={//此数组内数据为液晶上显示波形符号的自定义字符0x0e,0x11,0x11,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x11,0x11,0x0e,0x00,//正弦波010x00,0x07,0x04,0x04,0x04,0x04,0x1c,0x00,0x00,0x1c,0x04,0x04,0x04,0x04,0x07,0x00,//矩形波230x00,0x01,0x02,0x04,0x08,0x10,0x00,0x00,0x00,0x10,0x08,0x04,0x02,0x01,0x00,0x00,//三角波450x00,0x01,0x03,0x05,0x09,0x11,0x00,0x00,//锯齿波6};ucharcodesin[]={//此数组内的数据为,da输出对应电压值对应的数字量,0是0V,255是5V135,145,158,167,176,188,199,209,218,226,234,240,245,249,252,254,254,253,251,247,243,237,230,222,213,204,193,182,170,158,146,133,121,108,96,84,72,61,50,41,32,24,17,11,7,3,1,0,0,2,5,9,14,20,28,36,45,55,66,78,90,102,114,128};//正弦波取码ucharcodejuxing[]={//一个周期是采样个点,所以数组内是个数据255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//矩形波取码ucharcodesanjiao[]={0,8,16,24,32,40,48,56,,72,80,88,96,104,112,120,128,136,144,152,160,168,176,184,192,200,208,216,224,232,240,248,248,240,232,224,216,208,200,192,184,176,168,160,152,144,136,128,120,112,104,96,88,80,72,,56,48,40,32,24,16,8,0};//三角波取码ucharcodejuchi[]={0,4,8,12,16,20,24,28,32,36,40,45,49,53,57,61,65,69,73,77,81,85,,93,97,101,105,109,113,117,121,125,130,134,138,142,146,150,154,158,162,166,170,174,178,182,186,190,194,198,202,206,210,215,219,223,227,231,235,239,243,247,251,255};//锯齿波取码voiddelay(uintxms)//延时函数{inta,b;for(a=xms;a>0;a--)for(b=110;b>0;b--);}voidwrite_com(ucharcom)//写命令函数{lcdrs=0;P0=com;delay(1);lcden=0;delay(1);lcden=1;}voidwrite_date(uchardate)//写数据函数{lcdrs=0;P0=date;delay(1);lcden=0;delay(1);lcden=1;}//自定义字符集voidLcd_ram(){uinti,j,k=0,temp=0x04;for(i=0;i1000)//最大加到100Hz{pinlv=100;//100Hz}display();//显示函数m=65536-(15000/pinlv);//计算频率/*频率值最小是10Hz,pinlv的值是100(因为要显示小数点后一位),150000/100=1500,这个1500就是定时器需要计时的,单位是us,65536-1500得到的是定时器的初值,先不管初值,先看定时时间,1500us,一个波形的周期是由个定时组成的,所以,一个波形周期就是*1500us=96000,也就是96ms,约等于100ms,也就是10Hz的频率*/a=m/256;//将定时器的初值赋值给变量b=m%256;EA=1;//打开中断总开关}if(s2==0)//减按键按下{EA=0;while(!s2);pinlv-=bujin;//频率以步进值减if(pinlv=4)//4种波形{boxing=0;}display();EA=1;}if(s5==0)//PWM切换按键{EA=0;while(!s5);pwm+=10;if(pwm>90){pwm=10;}//display();EA=1;}}voidbujindisplay()//步进值设置界面显示程序{uint,shi,ge;//定义步进值百十个位=bujin1/100;//将步进值除以100得到百位,也就是频率值的十位,因为有一个小数位shi=bujin1%100/10;//将步进值除以100的余数除以十得到十位ge=bujin1%100%10;//取余10后得到个位,也就是频率步进值的小数点后一位write_com(0x80+11);//选中液晶第一行第十一列if(==0)//百位是否为0write_date('');//百位不显示else//百位不为0write_date(table[]);//显示百位数据write_date(table[shi]);//显示十位数据write_date('.');//显示小数点write_date(table[ge]);//显示个位,也就是小数点后一位}voidbujinjiance()//步进值设置键盘程序{if(s4==0)//步进设置按键按下{delay(5);//延时去抖if(s4==0)//再次判断按键{while(!s4);//按键释放,按键松开才继续向下执行h++;//变量加if(h==1)//进入设置状态时{write_com(0x01);//清屏write_com(0x80);//初始化显示步进设置界面write_date('S');delay(1);//stepvaluewrite_date('t');delay(1);write_date('e');delay(1);write_date('p');delay(1);write_date('');delay(1);write_date('v');delay(1);write_date('a');delay(1);write_date('l');delay(1);write_date('u');delay(1);write_date('e');delay(1);write_date(':');delay(1);bujin1=bujin;//步进值赋值给临时变量bujindisplay();//显示步进值}if(h==2)//退出设置{h=0;//清零bujin=bujin1;//设置好的临时步进值赋值给步进变量init_lcd();//初始化液晶显示initclock();//定时器初始化display();//调用显示程序}}}if(h==1)//设置步进值时{if(s1==0)//加按键按下{delay(5);//延时去抖if(s1==0)//再次判断{while(!s1);//按键释放bujin1++;//步进值加1if(bujin1>=101)//步进值最大100,也就是10.0Hz{bujin1=1;//超过最大值就恢复到0.1Hz}bujindisplay();//步进显示}}if(s2==0)//减按键,注释同上{delay(5);if(s2==0){while(!s2);bujin1--;//步进减if(bujin1<=0){bujin1=100;}bujindisplay();}}}}voidmain()//主函数{init_lcd();//调用初始化程序m=65536-(15000/pinlv);//定时器初值a=m/256;b=m%256;initclock();//定时器初始化led0=0;//点亮第一个波形指示灯while(1)//进入while循环,括号内为1,一直成立,所以也叫死循环,程序不会跳出,一直在内执行{if(h==0)//正常模式不是步进调节{keyscan();//扫描按键//display();}bujinjiance();//扫描步进调节程序switch(boxing)//选择波形{case0:P1=sin[u];break;//正弦波case1://矩形波if(u=)//一个周期采样个点,所以加到就清零u=0;//u清零//根据不同的初值,定时器定时时间不同,达到不同频率的目的}

热心网友 时间:2023-10-16 10:22

做一个pwm就行了

热心网友 时间:2023-10-16 10:23

可以时候用DAC慢慢的使引脚电压在几秒内从5V降到0V

热心网友 时间:2023-10-16 10:22

#include//包含头文件#include#defineucharunsignedchar//宏定义#defineuintunsignedintsbits1=P3^5;//定义按键的接口sbits2=P3^6;sbits3=P3^7;sbits4=P3^4;sbits5=P2^3;sbitled0=P3^0;//定义四个LED,分别表示不同的波形sbitled1=P3^1;sbitled2=P3^2;sbitled3=P3^3;sbitlcdrs=P2^7;//液晶控制引脚,还有一个控制脚是RW,因为我们只需要向液晶里写数据系那是就好了,所以,我们直接将RW引脚接地sbitlcden=P2^6;charnum,boxing,u;//定义全局变量ucharpinlv=100,bujin=1,bujin1=1;//频率初始值是10Hz,步进值默认是0.1,显示步进值变量ucharcodetable[]="01234567";//定义显示的数组ucharcodetable1[]="Fout=Waveform:";//初始化显示字符unsignedintm,pwm=50;//定义变量minta,b,h,num1;//定义全局变量//自定义字符ucharcodezifu[]={//此数组内数据为液晶上显示波形符号的自定义字符0x0e,0x11,0x11,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x11,0x11,0x0e,0x00,//正弦波010x00,0x07,0x04,0x04,0x04,0x04,0x1c,0x00,0x00,0x1c,0x04,0x04,0x04,0x04,0x07,0x00,//矩形波230x00,0x01,0x02,0x04,0x08,0x10,0x00,0x00,0x00,0x10,0x08,0x04,0x02,0x01,0x00,0x00,//三角波450x00,0x01,0x03,0x05,0x09,0x11,0x00,0x00,//锯齿波6};ucharcodesin[]={//此数组内的数据为,da输出对应电压值对应的数字量,0是0V,255是5V135,145,158,167,176,188,199,209,218,226,234,240,245,249,252,254,254,253,251,247,243,237,230,222,213,204,193,182,170,158,146,133,121,108,96,84,72,61,50,41,32,24,17,11,7,3,1,0,0,2,5,9,14,20,28,36,45,55,66,78,90,102,114,128};//正弦波取码ucharcodejuxing[]={//一个周期是采样个点,所以数组内是个数据255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//矩形波取码ucharcodesanjiao[]={0,8,16,24,32,40,48,56,,72,80,88,96,104,112,120,128,136,144,152,160,168,176,184,192,200,208,216,224,232,240,248,248,240,232,224,216,208,200,192,184,176,168,160,152,144,136,128,120,112,104,96,88,80,72,,56,48,40,32,24,16,8,0};//三角波取码ucharcodejuchi[]={0,4,8,12,16,20,24,28,32,36,40,45,49,53,57,61,65,69,73,77,81,85,,93,97,101,105,109,113,117,121,125,130,134,138,142,146,150,154,158,162,166,170,174,178,182,186,190,194,198,202,206,210,215,219,223,227,231,235,239,243,247,251,255};//锯齿波取码voiddelay(uintxms)//延时函数{inta,b;for(a=xms;a>0;a--)for(b=110;b>0;b--);}voidwrite_com(ucharcom)//写命令函数{lcdrs=0;P0=com;delay(1);lcden=0;delay(1);lcden=1;}voidwrite_date(uchardate)//写数据函数{lcdrs=0;P0=date;delay(1);lcden=0;delay(1);lcden=1;}//自定义字符集voidLcd_ram(){uinti,j,k=0,temp=0x04;for(i=0;i1000)//最大加到100Hz{pinlv=100;//100Hz}display();//显示函数m=65536-(15000/pinlv);//计算频率/*频率值最小是10Hz,pinlv的值是100(因为要显示小数点后一位),150000/100=1500,这个1500就是定时器需要计时的,单位是us,65536-1500得到的是定时器的初值,先不管初值,先看定时时间,1500us,一个波形的周期是由个定时组成的,所以,一个波形周期就是*1500us=96000,也就是96ms,约等于100ms,也就是10Hz的频率*/a=m/256;//将定时器的初值赋值给变量b=m%256;EA=1;//打开中断总开关}if(s2==0)//减按键按下{EA=0;while(!s2);pinlv-=bujin;//频率以步进值减if(pinlv=4)//4种波形{boxing=0;}display();EA=1;}if(s5==0)//PWM切换按键{EA=0;while(!s5);pwm+=10;if(pwm>90){pwm=10;}//display();EA=1;}}voidbujindisplay()//步进值设置界面显示程序{uint,shi,ge;//定义步进值百十个位=bujin1/100;//将步进值除以100得到百位,也就是频率值的十位,因为有一个小数位shi=bujin1%100/10;//将步进值除以100的余数除以十得到十位ge=bujin1%100%10;//取余10后得到个位,也就是频率步进值的小数点后一位write_com(0x80+11);//选中液晶第一行第十一列if(==0)//百位是否为0write_date('');//百位不显示else//百位不为0write_date(table[]);//显示百位数据write_date(table[shi]);//显示十位数据write_date('.');//显示小数点write_date(table[ge]);//显示个位,也就是小数点后一位}voidbujinjiance()//步进值设置键盘程序{if(s4==0)//步进设置按键按下{delay(5);//延时去抖if(s4==0)//再次判断按键{while(!s4);//按键释放,按键松开才继续向下执行h++;//变量加if(h==1)//进入设置状态时{write_com(0x01);//清屏write_com(0x80);//初始化显示步进设置界面write_date('S');delay(1);//stepvaluewrite_date('t');delay(1);write_date('e');delay(1);write_date('p');delay(1);write_date('');delay(1);write_date('v');delay(1);write_date('a');delay(1);write_date('l');delay(1);write_date('u');delay(1);write_date('e');delay(1);write_date(':');delay(1);bujin1=bujin;//步进值赋值给临时变量bujindisplay();//显示步进值}if(h==2)//退出设置{h=0;//清零bujin=bujin1;//设置好的临时步进值赋值给步进变量init_lcd();//初始化液晶显示initclock();//定时器初始化display();//调用显示程序}}}if(h==1)//设置步进值时{if(s1==0)//加按键按下{delay(5);//延时去抖if(s1==0)//再次判断{while(!s1);//按键释放bujin1++;//步进值加1if(bujin1>=101)//步进值最大100,也就是10.0Hz{bujin1=1;//超过最大值就恢复到0.1Hz}bujindisplay();//步进显示}}if(s2==0)//减按键,注释同上{delay(5);if(s2==0){while(!s2);bujin1--;//步进减if(bujin1<=0){bujin1=100;}bujindisplay();}}}}voidmain()//主函数{init_lcd();//调用初始化程序m=65536-(15000/pinlv);//定时器初值a=m/256;b=m%256;initclock();//定时器初始化led0=0;//点亮第一个波形指示灯while(1)//进入while循环,括号内为1,一直成立,所以也叫死循环,程序不会跳出,一直在内执行{if(h==0)//正常模式不是步进调节{keyscan();//扫描按键//display();}bujinjiance();//扫描步进调节程序switch(boxing)//选择波形{case0:P1=sin[u];break;//正弦波case1://矩形波if(u=)//一个周期采样个点,所以加到就清零u=0;//u清零//根据不同的初值,定时器定时时间不同,达到不同频率的目的}

热心网友 时间:2023-10-16 10:22

做一个pwm就行了

热心网友 时间:2023-10-16 10:23

可以时候用DAC慢慢的使引脚电压在几秒内从5V降到0V

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com