📄 信号发生器程序.txt
字号:
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*
File Name:LowFrequencyGenerate.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 P1 //定义0830的数据输入口
#define Led P0
typedef unsigned char uchar;
typedef unsigned int uint;
//接口定义
sbit KeyUp = P2^1; //定义按键接口
sbit KeyDown = P2^0;
sbit KeyMode = P2^2;
sbit LedDig1 = P2^4; //定义LED位选接口Dig是Digit(位)的缩写
sbit LedDig2 = P2^6;
sbit LedDig3 = P2^5;
sbit LedDig4 = P2^7;
sbit LedDig5 = P2^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;
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*
Function:LowFreGenerate()
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()
{
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 1:LedModeDisp = 0x08;break;
case 2:LedModeDisp = 0x04;break;
case 3:LedModeDisp = 0x02;break;
case 4:LedModeDisp = 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;
}
}
}
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*
Function:LedDisplay()
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,0x30,0x78,0x67};
uchar i;
switch(Mode)
{
case 1: //显示正弦波标志SI.--->sine
{
LedDig1 = 0; //显示S
Led = LedSegCode[5];
for(i=100;i>0;i--);
LedDig1 = 1;
Led = 0x00;
LedDig2 = 0; //显示I.
Led = LedSegCode[11] | 0x80;
for(i=100;i>0;i--);
LedDig2 = 1;
Led = 0x00;
break;
}
case 2: //显示三角波标志tA.--->triangle
{
LedDig1 = 0; //显示t
Led = LedSegCode[12];
for(i=100;i>0;i--);
LedDig1 = 1;
Led = 0x00;
LedDig2 = 0; //显示A.
Led = LedSegCode[10] | 0x80;
for(i=100;i>0;i--);
LedDig2 = 1;
Led = 0x00;
break;
}
case 3: //显示锯齿波标志St.--->jag/sawtooth
{
LedDig1 = 0; //显示S
Led = LedSegCode[5];
for(i=100;i>0;i--);
LedDig1 = 1;
Led = 0x00;
LedDig2 = 0; //显示t.
Led = LedSegCode[12] | 0x80;
for(i=100;i>0;i--);
LedDig2 = 1;
Led = 0x00;
break;
}
case 4: //显示方波标志Sq.--->square wave
{
LedDig1 = 0; //显示S
Led = LedSegCode[5];
for(i=100;i>0;i--);
LedDig1 = 1;
Led = 0x00;
LedDig2 = 0; //显示q.
Led = LedSegCode[13] | 0x80;
for(i=100;i>0;i--);
LedDig2 = 1;
Led = 0x00;
break;
}
default:break;
}
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;
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
附:本程序说明
波形发生模式:
Mode
1 正弦波/余弦波——初始时N=0为正弦波,N=64为余弦波
/| /| /| /| /| /| /|
/ | / | / | / | / | / | / |
/ | / | / | / | / | / | / |…… (初始时N=0)
2 锯齿波(1.渐升骤降)/ |/ |/ |/ |/ |/ |/ |
|\ |\ |\ |\ |\ |\ |\ |\
| \ | \ | \ | \ | \ | \ | \ | \
| \ | \ | \ | \ | \ | \ | \ | \ …… (初始时N=255)
锯齿波(2.渐降骤升)| \| \| \| \| \| \| \| \
/\ /\ /\ /\ /\ /\ /\
/ \ / \ / \ / \ / \ / \ / \
/ \ / \ / \ / \ / \ / \ / \ ……
3 三角波 (1) / \/ \/ \/ \/ \/ \/ \
\ /\ /\ /\ /\ /\ /\ /
\ / \ / \ / \ / \ / \ / \ /
\ / \ / \ / \ / \ / \ / \ / ……
(2) \/ \/ \/ \/ \/ \/ \/
4 方波 (1) ____ ___ ___ ___
| | | | | | |
|___| |___| |___| |___……
(2) ___ ___ ___ ___
| | | | | | |
____| |___| |___| |___| ……
按键状态:
DealFlag 0 1
非处理状态(99%) 处理状态
|<----------------------->|<------->|
|
|<---扫描状态--->|--编码状态-->|
98% 1%
EncodeFlag 0 1
按键消抖: -->对按键编码(或开始计时,让按键有连按功能)
__开始扫描 | ___检测到按键弹起后,按键待处理
按 按 | |<-延时20mS进入稳定区->|<--等待按键弹起-->|/ 标志位置位,锁定按键扫描标志
键 键 | VCC---------|--------______________________________---|---------------- VCC
处->扫 ->检测到 ____ | / \ | 停止按键扫描
理 描 按键按下 \| / \ |
完 解 /\ / \| /\
毕 锁 2.7V--------/\/--\/------------------------------------\/--\/\-----------2.7V以下判定为
GND _____/\/ \/\________GND 低电平
Copyright(C) Culture@CUIT (2007)
All rights reserved
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
本驱动程序中的变量解释:
bit变量:
UpFlag: Up键按下的标志位
DownFlag: Down键按下的标志位
ModeFlag: Mode键按下的标志位
EncodeFlag: 允许编码标志位
DealFlag: 按键待处理标志位
RaiseFlag: 输出电平渐升标志位
uchar变量:
Mode: 模式编码变量
FreqValue: 当前输出频率值
N: 记录波形输出点数
CountNum: 定时器基时计数变量
Mode 1 2 3 4
正弦波 锯齿波 三角波 方波
(渐升骤降)(顶朝上)(起始高)
Uint变量:
TimerValue: 定时器初始值变量
码表:
sine[256]: 正弦波码表
LedSegCode[14]: 数码管码表
TimerNum[99]: 1-99HZ输出频率定时时长码表
编程思想:让定时器负责定时更新输出电平值,改变模式可以改变输出波形,改变定时器的定时值可以
改变输出波形的频率,理论上讲可以实现1-66HZ的三角波、正弦波、锯齿波,方波。
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
Powered by Culture All rights reserved
Copyright(C) 2007
07-10-04
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -