点击此处下载 ourdev_468235.rar(文件大小:20K) (原文件名:WRQ.rar)
请高手帮个忙由于学校让做个综合设计,而本人考研日期在即,没有时间再搞这些东西了,指自己做好了板子,程序没空写了,请高手帮我写一下程序完成以下两个功能,仿真图在附件里面,函数发生器的程序只需要帮我修改一下(代码已付出),谢谢,感激不尽
低频函数波形发生器
基本要求:
设计并制作一个具有高频率稳定度和高相位稳定度的低频函数发生器,频率可调,为1HZ-1KHz;
波形种类:三角波、正弦波、方波、锯齿波
没有明显的波形失真
具有频率、波形种类显示和设置功能,即能通过按键设置指定频率,指定种类的波形输出,并在数码管上显示频率值及波形种类。
输出电压:0V~+5V
远程监控器
设计基本要求:
通过RS232串行通信,将PC机与单片机连接,实现远程开关量监测和控制
单片机上有四个LED数码管,并设有若干个选择按键开关 。可以通过按键设置数据,传输到计算机进行显示
显示PC机上超级终端发送的字符编码,左边第一个LED显示接收字符数,第二个显示‘-’,右边两个LED显示编码,如发送‘1’时显示1-31,发送‘A’时显示2-41
扩展要求:
通过选择开关,可以选择在单片机上显示PC机上的时间,分别是“年月”、“日时”、“分秒”、循环模式
PC机要求工作在Windows9X、Window2000或DOS下,用C语言或vb编程
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*
File NameowFrequencyGenerate.c
Function:Can generate a low frequency signal form 1HZ to 99HZ.
Author:Culture
Revision:1.0
Date:07/10/04
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*/
//头文件与宏定义
#include <REG52.H>
#define DAC0830 1 //定义0830的数据输入口
#define Led 0
typedef unsigned char uchar;
typedef unsigned int uint;
//接口定义
sbit KeyUp = 2^1; //定义按键接口
sbit KeyDown = 2^0;
sbit KeyMode = 2^2;
sbit LedDig1 = 2^4; //定义LED位选接口Dig是Digit(位)的缩写
sbit LedDig2 = 2^6;
sbit LedDig3 = 2^5;
sbit LedDig4 = 2^7;
sbit LedDig5 = 2^3;
//变量声明
bit UpFlag = 0, //Up键按下标志位
DownFlag = 0, //Down键按下标志位
ModeFlag = 0, //Mode键按下标志位
AddFlag = 0, //连加标志
EncodeFlag = 0, //使能编码标志位
DealFlag = 0, //使能按键处理标志位
RaiseFlag = 1; //输出电平升降标志位
uchar Mode = 1, //当前输出模式,取值1-4,分别代表正弦波、三角波、锯齿波、方波
FreqValue = 1,//当前输出频率值
N = 128, //波形输出点计数
CountNum = 0; //按键延时计数标志位
LedModeDisp = 0x01;//发光二极管表示波形输出模式
uint TimerValue = 28800;//输出1HZ时定时器初始值
extern uchar code sine[256];
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*
Function:SystemInit()
Description:A Initiation Program of system
Parameters: None
Returns:None
Side Effects: Will change most Parameters of system
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*/
void SystemInit()
{
DAC0830 = 0x00; //DAC0830输出电平为0
Led = 0x00; //熄灭数码管
TMOD = 0x11; //定时器工作于方式1
TH0 = -TimerValue>>8; //取负优先级大于右移运算
TL0 = -TimerValue;
TH1 = -500>>8;
TL1 = -500;
ET0 = 1;
ET1 = 1;
EA = 1;
TR0 = 1;
TR1 = 1;
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*
FunctionowFreGenerate()
Description:Generate sine wave,sawtooth,triangle wave,square wave.
Parameters: None
Returns:None
Side Effects: Will change the value of N and the state of RaiseFlag.
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*/
void LowFreGenerate()
{
switch(Mode)
{
case 1: //正弦波发生方式(初始时N=0)
{
DAC0830 = sine[N];
N += 4 ;
break;
}
case 2: //三角波发生方式(初始时N=0)
{
DAC0830 = N;
if(RaiseFlag)
{
N += 8 ;
}
else
{
N -= 8;
}
if(N == 248)
{
RaiseFlag = 0;
}
if(N == 0)
{
RaiseFlag = 1;
}
break;
}
case 3: //锯齿波(渐升骤降)发生方式(初始时N=0)
{
DAC0830 = N;
N += 4 ;
break;
}
case 4: //方波产生
{
if(N < 128)
{
DAC0830 = 0xff;
}
else
{
DAC0830 = 0x00;
}
N += 4 ;
break;
}
default:break;
}
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*
Function:T0_SVR(void) interrupt 1
Description:The drive program of the timer 0,to realize different time-delay for
getting different frequency wave.
Parameters: None
Returns:None
Side Effects:Will change the state of UpdateFlag,
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*/
void T0_SVR(void) interrupt 1
{
TR0 = 0;
TH0 = -(TimerValue -65)>>8; //重新定时
TL0 = -(TimerValue -65);
LowFreGenerate();
TR0 = 1;
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*
Function:TimeInit()
Description:Initiation of the timer
Parameters: None
Returns:None
Side Effects: None,it's safe for outside program.
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*/
void TimeInit()
{ sinsum = 128*sin((i/80)*6.28) + 127; //计算正弦函数的值对应的二进制码表
outsum[j++] = sinsum;
uint code TimerNum[99]={28800,14400,9600,7200,5760,4800,4114,3600,3200,2880,2618,
2400,2215,2057,1920,1800,1694,1600,1516,1440,1371,1309,
1252,1200,1152,1108,1067,1029,993,960,929,900,873,847,823,
800,778,758,738,720,702,686,670,655,640,626,613,600,588,
576,565,554,543,533,524,514,505,497,488,480,472,465,457,
450,443,436,430,424,417,411,406,400,395,389,384,379,374,
369,365,360,356,351,347,343,339,335,331,327,324,320,316,
313,310,306,303,300,297,294,291}; //1-99HZ
TimerValue = TimerNum[FreqValue-1];
TR0 = 0;
TH0 = -TimerValue>>8; //重新定时
TL0 = -TimerValue;
TR0 = 1;
}
void LedModeDisplay(uchar Mod)
{
switch(Mod)
{
case 1edModeDisp = 0x08;break;
case 2edModeDisp = 0x04;break;
case 3edModeDisp = 0x02;break;
case 4edModeDisp = 0x01;break;
default:break;
}
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*
Function:ButtonProcess()
Description:Scan the state of button.
Parameters: None
Returns:None
Side Effects: Will change the value of CountNum, and the state of EncodeFlag,
DealFlag,DownFlag,UpFlag,ModeFlag.
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*/
void ButtonProcess()
{
CountNum++;
if(DealFlag)//处理状态
{
if(KeyUp && KeyDown && KeyMode)
{
if(UpFlag)
{
FreqValue++; //改变频率值实际上是改变计时器的定时值
if(FreqValue == 100)
{
FreqValue = 1;
}
TimeInit();
UpFlag = 0;
}
else if(DownFlag)
{
FreqValue--;
if(FreqValue == 0)
{
FreqValue = 99;
}
TimeInit();
DownFlag = 0;
}
else if(ModeFlag)
{
Mode++;
if(Mode == 5)
{
Mode = 1;
}
if(Mode == 1)
{
N = 128;
}
else
{
N = 0;
}
LedModeDisplay(Mode);
ModeFlag = 0;
}
DealFlag = 0;
EncodeFlag = 0;
}
}
else//非处理状态
{
if(EncodeFlag)//编码状态
{
if(CountNum == 40)
{
if(!KeyUp)
{
UpFlag = 1;
DealFlag = 1;
}
else if(!KeyDown)
{
DownFlag = 1;
DealFlag = 1;
}
else if(!KeyMode)
{
ModeFlag = 1;
DealFlag = 1;
}
CountNum = 0;
}
}
else//按键扫描状态
{
if(!KeyUp | !KeyDown | !KeyMode)
{
CountNum = 0;
EncodeFlag = 1;
}
}
}
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*
FunctionedDisplay()
Description:make the led display the information of the state of system.
Parameters: None
Returns:None
Side Effects: None,it's safe for outside program.
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*/
void LedDisplay()
{
uchar code LedSegCode[14]={0x3f,0x06,0x5b,0x4f,0x66,/*0-9的码值,A,I,t,q共阴*/
0x6d,0x7d,0x07,0x7f,0x6f,0x77,0xff,0x39};
uchar i;
switch(Mode)
{
case 1: //显示正弦波标志A
{
LedDig1 = 0; //显示A
Led = LedSegCode[10];
for(i=100;i>0;i--);
LedDig1 = 1;
Led = 0x00;
break;
}
case 2: //显示三角波标志B
{
LedDig1 = 0; //显示B
Led = LedSegCode[11];
for(i=100;i>0;i--);
LedDig1 = 1;
Led = 0x00;
break;
}
case 3: //显示锯齿波标志C
{
LedDig1 = 0; //显示C
Led = LedSegCode[12];
for(i=100;i>0;i--);
LedDig1 = 1;
Led = 0x00;
break;
}
case 4: //显示方波标志D
{
LedDig1 = 0; //显示D
Led = LedSegCode[5];
for(i=100;i>0;i--);
LedDig1 = 1;
Led = 0x00;
break;
}
default:break;
}
LedDig2 = 0; //显示百位频率值
Led = LedSegCode[FreqValue/100];
for(i=100;i>0;i--);
LedDig2 = 1;
Led = 0x00;
LedDig3 = 0; //显示频率值的十位
Led = LedSegCode[FreqValue/10];
for(i=100;i>0;i--);
LedDig3 = 1;
Led = 0x00;
LedDig4 = 0; //显示频率值的个位
Led = LedSegCode[FreqValue%10];
for(i=100;i>0;i--);
LedDig4 = 1;
Led = 0x00;
LedDig5 = 0; //显示输出波形的状态
Led = LedModeDisp;
for(i=100;i>0;i--);
LedDig5 = 1;
Led = 0x00;
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*
Function:main()
Description:The start program of the system and organize all the program in the system
Parameters: None
Returns:None
Side Effects: It's the boss of the system.
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*/
void main()
{
SystemInit();
while(1)
{
LedDisplay();
}
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*
Function:T1_SVR(void) interrupt 3
Description:The drive program of the timer 1,
Parameters: None
Returns:None
Side Effects: Will change the state of LedFlag,call the function ButtonScan().
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*/
void T1_SVR(void) interrupt 3
{
TR1 = 0;
TH1 = -500>>8;
TL1 = -500;
ButtonProcess();
TR1 = 1;
}
//本来程序在keil中显示很正常的,特别是对齐方面,但是在这里显示的不是很好,特别是文字的对其方面,如果拷到kerl中显示就应该正常了,大家就凑合的看吧,程序是完全正确的,已经调试成功,并应用于相对应的硬件板子上了。
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
File Name:sine.c
Function:A sheet of sine wave code with 256 dots.
Author:Culture
Revision:1.0
Date:07/10/04
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
unsigned char code sine[256]=
{
0x80,0x83,0x86,0x89,0x8D,0x90,0x93,0x96,
0x99,0x9C,0x9F,0xA2,0xA5,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