|
下面这段程序中如何加入输出波形并可改变频率和占空比
[复制链接]
本帖最后由 paulhyde 于 2014-9-15 09:20 编辑
高手指点啊!!!!!
单 片 机 (MCU type):STC89C52RC
注意事项(Attention):
修改时间:
-------------------------------------------------------------------------------------------------------------------*/
#include
/*--------------------------------------------------------------------------------------------------------------------
-----------------变量定义----------------------
-------------------------------------------------------------------------------------------------------------------*/
#define uchar unsigned char
#define uint unsigned int
sbit key1=P1^0; //端口定义
sbit key2=P1^1;
sbit key3=P1^2;
sbit key4=P1^3;
sbit key03=P2^0;
sbit key04=P2^1;
sbit key05=P2^2;
sbit key06=P2^3;
sbit key07=P2^4;
sbit key08=P2^5;
sbit key09=P2^6;
sbit rs=P1^4;
sbit en=P1^5;
sbit DAC_CS=P3^2;
sbit DAC_WR=P3^6;
uchar code table1[]=" guoqiang do"; //初始化显示
uchar code table2[]=" dianya :0.0V "; //初始化电源
uchar s1,s2,keynum,volt;
/*--------------------------------------------------------------------------------------------------------------------
函数全称: void delay(uint z)
函数功能: 延时函数 (毫秒级)
输 入:unit z
返 回:
调用函数:
注意事项:
提示说明:
-------------------------------------------------------------------------------------------------------------------*/void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/*--------------------------------------------------------------------------------------------------------------------
函数全称: void write_com()
函数功能: 写指令到LCD1602
输 入:uchar com
返 回:
调用函数:
注意事项:
提示说明:
-------------------------------------------------------------------------------------------------------------------*/void write_com(uchar com)
{
rs=0;
en=0;
P0=com;
delay(5);
en=1;
delay(5);
en=0;
}
/*--------------------------------------------------------------------------------------------------------------------
函数全称: void write_date()
函数功能: 写数据到LCD1602
输 入:uchar data
返 回:无
调用函数: 无
注意事项:
提示说明:
-------------------------------------------------------------------------------------------------------------------*/void write_date(uchar date)
{
rs=1;
en=0;
P0=date;
delay(5);
en=1;
delay(5);
en=0;
}
/*--------------------------------------------------------------------------------------------------------------------
函数全称: void write_date()
函数功能: lcd1602初始化子程序
输 入:无
返 回:无
调用函数: 无
注意事项:
提示说明:
-------------------------------------------------------------------------------------------------------------------*/void Init()
{
uchar num;
en=0;
write_com(0x38); //置地址
write_com(0x0c);
write_com(0x06); //光标右移
write_com(0x01); //清显示
write_com(0x80); //lcd DDRAM设置
for(num=0;num<16;num++)
{
write_date(table1[num]);
delay(50);
}
write_com(0x80+0x40); //lcd第二行
for(num=0;num<14;num++)
{
write_date(table2[num]);
delay(50);
}
}
/*--------------------------------------------------------------------------------------------------------------------
函数全称: void write_voltage()
函数功能: 写电压到lcd1602
输 入:uchar add,uchar dat
返 回:无
调用函数: 无
注意事项:
提示说明:
-------------------------------------------------------------------------------------------------------------------*/void write_voltage(uchar add,uchar dat)
{
write_com(0x80+0x40+add); //确定第二行位置
write_date(0x30+dat); //写数据
}
/*--------------------------------------------------------------------------------------------------------------------
函数全称: void da0832out()
函数功能: 写电压数据到DAC0832
输 入?char dadata
返 回:无
调用函数: 无
注意事项:
提示说明:
-------------------------------------------------------------------------------------------------------------------*/void da0832out(uchar dadata)
{
DAC_CS=0;
P0=dadata;
DAC_WR=0;
delay(5);
DAC_WR=1;
DAC_CS=1;
}
/*--------------------------------------------------------------------------------------------------------------------
函数全称: void keyscan()
函数功能: 键盘扫描子程序
输 入?char dadata
返 回:无
调用函数:write_com();write_voltage(); da0832out();
注意事项:
提示说明:
-------------------------------------------------------------------------------------------------------------------*/void keyscan()
{
if(key1==0)
{
delay(5);
if(key1==0)
{
keynum++;
while(!key1); //按住置一直到放手
if(keynum==1) //等于 1
{
write_com(0x80+0x40+12);
write_com(0x0f); //置数据显示地址
}
}
if(keynum==2) //deng=2,ce jia 1
{
write_com(0x80+0x40+10); //置数据显示地址
write_com(0x0f);
}
if(keynum==3) //若等于 3,
{
keynum=0;
write_com(0x0c); //测无关标
}
}
if(keynum!=0)
{
if(key2==0)
{
delay(5);
if(key2==0)
{
while(!key2);
if(keynum==1)
{
s1=1;
write_com(0x80+0x40+11); //个位加0.1
write_com(0x0c); // 无光标
write_voltage(12,s1); //写电压
}
if(keynum==2) //操作个位
{
s2=1;
write_com(0x80+0x40+9); //写个位
write_com(0x0c); //无光标
write_voltage(10,s2); //写电源
}
}
}
if(key3==0)
{
delay(5);
if(key3==0)
{
while(!key3);
if(keynum==1)
{
s1=2;
write_com(0x80+0x40+11);
write_com(0x0c);
write_voltage(12,s1);
}
if(keynum==2)
{
s2=2;
write_com(0x80+0x40+9);
write_com(0x0c);
write_voltage(10,s2);
}
}
}
if(key03==0)
{
delay(5);
if(key03==0)
{
while(!key03);
if(keynum==1)
{
s1=3;
write_com(0x80+0x40+11); //操作十分位 write_com(0x0c); // 无光标
write_voltage(12,s1); //写电压
}
if(keynum==2) //操作个位
{
s2=3;
write_com(0x80+0x40+9); //写个位
write_com(0x0c); //无光标
write_voltage(10,s2); //写电源
}
}
}
if(key04==0)
{
delay(5);
if(key04==0)
{
while(!key04);
if(keynum==1)
{
s1=4;
write_com(0x80+0x40+11); //操作十分位
write_com(0x0c); // 无光标
write_voltage(12,s1); //写电压
}
if(keynum==2) //操作个位
{
s2=4;
write_com(0x80+0x40+9); //写个位
write_com(0x0c); //无光标
write_voltage(10,s2); //写电源
}
}
}
if(key05==0)
{
delay(5);
if(key05==0)
{
while(!key05);
if(keynum==1)
{
s1=5;
write_com(0x80+0x40+11); //操作十分位 write_com(0x0c); // 无光标
write_voltage(12,s1); //写电压
}
if(keynum==2) //操作个位
{
s2=5;
write_com(0x80+0x40+9); //写个位
write_com(0x0c); //无光标
write_voltage(10,s2); //写电源
}
}
}
if(key06==0)
{
delay(5);
if(key06==0)
{
while(!key06);
if(keynum==1) //操作十分位
{
s1=6;
write_com(0x80+0x40+11);
write_com(0x0c); // 无光标
write_voltage(12,s1); //写电压
}
if(keynum==2) //操作个位
{
s2=6;
write_com(0x80+0x40+9); //写个位
write_com(0x0c); //无光标
write_voltage(10,s2); //写电源
}
}
}
if(key07==0)
{
delay(5);
if(key07==0)
{
while(!key07);
if(keynum==1) //操作十分位
{
s1=7;
write_com(0x80+0x40+11);
write_com(0x0c); // 无光标
write_voltage(12,s1); //写电压
}
if(keynum==2) //操作个位
{
s2=7;
write_com(0x80+0x40+9); //写个位
write_com(0x0c); //无光标
write_voltage(10,s2); //写电源
}
}
}
if(key08==0)
{
delay(5);
if(key08==0)
{
while(!key08);
if(keynum==1) //操作十分位
{
s1=8;
write_com(0x80+0x40+11);
write_com(0x0c); // 无光标
write_voltage(12,s1); //写电压
}
if(keynum==2) //操作个位
{
s2=8;
write_com(0x80+0x40+9); //写个位
write_com(0x0c); //无光标
write_voltage(10,s2); //写电源
}
}
}
if(key09==0)
{
delay(5);
if(key09==0)
{
while(!key09);
if(keynum==1) //操作十分位
{
s1=9;
write_com(0x80+0x40+11);
write_com(0x0c); // 无光标
write_voltage(12,s1); //写电压
}
if(keynum==2) //操作个位
{
s2=9;
write_com(0x80+0x40+9); //写个位
write_com(0x0c); //无光标
write_voltage(10,s2); //写电源
}
}
}
if(key4==0)
{
delay(5);
if(key4==0)
{
while(!key4);
volt=10*s2+s1;
da0832out(volt); //输出电源值
}
}
}
}
/*--------------------------------------------------------------------------------------------------------------------
函数全称: void main()
函数功能: 主函数
输 入?
返 回:无
调用函数: Init();keyscan();
注意事项:
提示说明:
-------------------------------------------------------------------------------------------------------------------*/void main()
{
Init(); //初始化化
while(1)
{
keyscan(); //按键扫描
}
while(1);
}
|
|