?? 信號發(fā)生器程序.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的數(shù)據(jù)輸入口
#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, //波形輸出點計數(shù)
CountNum = 0; //按鍵延時計數(shù)標志位
LedModeDisp = 0x01;//發(fā)光二極管表示波形輸出模式
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; //熄滅數(shù)碼管
TMOD = 0x11; //定時器工作于方式1
TH0 = -TimerValue>>8; //取負優(yōu)先級大于右移運算
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: //正弦波發(fā)生方式(初始時N=0)
{
DAC0830 = sine[N];
N += 4 ;
break;
}
case 2: //三角波發(fā)生方式(初始時N=0)
{
DAC0830 = N;
if(RaiseFlag)
{
N += 8 ;
}
else
{
N -= 8;
}
if(N == 248)
{
RaiseFlag = 0;
}
if(N == 0)
{
RaiseFlag = 1;
}
break;
}
case 3: //鋸齒波(漸升驟降)發(fā)生方式(初始時N=0)
{
DAC0830 = N;
N += 4 ;
break;
}
case 4: //方波產(chǎn)生
{
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)//處理狀態(tài)
{
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//非處理狀態(tài)
{
if(EncodeFlag)//編碼狀態(tài)
{
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//按鍵掃描狀態(tài)
{
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()
{
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; //顯示輸出波形的狀態(tài)
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;
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
附:本程序說明
波形發(fā)生模式:
Mode
1 正弦波/余弦波——初始時N=0為正弦波,N=64為余弦波
/| /| /| /| /| /| /|
/ | / | / | / | / | / | / |
/ | / | / | / | / | / | / |…… (初始時N=0)
2 鋸齒波(1.漸升驟降)/ |/ |/ |/ |/ |/ |/ |
|\ |\ |\ |\ |\ |\ |\ |\
| \ | \ | \ | \ | \ | \ | \ | \
| \ | \ | \ | \ | \ | \ | \ | \ …… (初始時N=255)
鋸齒波(2.漸降驟升)| \| \| \| \| \| \| \| \
/\ /\ /\ /\ /\ /\ /\
/ \ / \ / \ / \ / \ / \ / \
/ \ / \ / \ / \ / \ / \ / \ ……
3 三角波 (1) / \/ \/ \/ \/ \/ \/ \
\ /\ /\ /\ /\ /\ /\ /
\ / \ / \ / \ / \ / \ / \ /
\ / \ / \ / \ / \ / \ / \ / ……
(2) \/ \/ \/ \/ \/ \/ \/
4 方波 (1) ____ ___ ___ ___
| | | | | | |
|___| |___| |___| |___……
(2) ___ ___ ___ ___
| | | | | | |
____| |___| |___| |___| ……
按鍵狀態(tài):
DealFlag 0 1
非處理狀態(tài)(99%) 處理狀態(tài)
|<----------------------->|<------->|
|
|<---掃描狀態(tài)--->|--編碼狀態(tài)-->|
98% 1%
EncodeFlag 0 1
按鍵消抖: -->對按鍵編碼(或開始計時,讓按鍵有連按功能)
__開始掃描 | ___檢測到按鍵彈起后,按鍵待處理
按 按 | |<-延時20mS進入穩(wěn)定區(qū)->|<--等待按鍵彈起-->|/ 標志位置位,鎖定按鍵掃描標志
鍵 鍵 | VCC---------|--------______________________________---|---------------- VCC
處->掃 ->檢測到 ____ | / \ | 停止按鍵掃描
理 描 按鍵按下 \| / \ |
完 解 /\ / \| /\
畢 鎖 2.7V--------/\/--\/------------------------------------\/--\/\-----------2.7V以下判定為
GND _____/\/ \/\________GND 低電平
Copyright(C) Culture@CUIT (2007)
All rights reserved
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
本驅(qū)動程序中的變量解釋:
bit變量:
UpFlag: Up鍵按下的標志位
DownFlag: Down鍵按下的標志位
ModeFlag: Mode鍵按下的標志位
EncodeFlag: 允許編碼標志位
DealFlag: 按鍵待處理標志位
RaiseFlag: 輸出電平漸升標志位
uchar變量:
Mode: 模式編碼變量
FreqValue: 當前輸出頻率值
N: 記錄波形輸出點數(shù)
CountNum: 定時器基時計數(shù)變量
Mode 1 2 3 4
正弦波 鋸齒波 三角波 方波
(漸升驟降)(頂朝上)(起始高)
Uint變量:
TimerValue: 定時器初始值變量
碼表:
sine[256]: 正弦波碼表
LedSegCode[14]: 數(shù)碼管碼表
TimerNum[99]: 1-99HZ輸出頻率定時時長碼表
編程思想:讓定時器負責定時更新輸出電平值,改變模式可以改變輸出波形,改變定時器的定時值可以
改變輸出波形的頻率,理論上講可以實現(xiàn)1-66HZ的三角波、正弦波、鋸齒波,方波。
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
Powered by Culture All rights reserved
Copyright(C) 2007
07-10-04
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -