/********************************
工程名:连续频率产生器
原作者:亓先军
修改者:付江云
说 明:将25M的频率通过82C54
连续分频,产生各段连续
频率信号。
原地址:http://www.dadwj.cn/post/371.html
修改处:数码管显示方式,输出连续频率时采用
循环往返输出。
********************************/
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
//按键宏定义
#define key1 0x0fa
#define key2 0x0f9
#define key3 0x0f3
#define key4 0x0eb
#define key5 0x0db
#define key6 0x0bb
#define key7 0x07b
//显示数字数组定义
uchar code num[]={0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9};
/*
#define num0 0xf0
#define num1 0xf1
#define num2 0xf2
#define num3 0xf3
#define num4 0xf4
#define num5 0xf5
#define num6 0xf6
#define num7 0xf7
#define num8 0xf8
#define num9 0xf9
*/
//8254管脚定义
sbit wr=P2^4;
sbit rd=P2^3;
sbit cs=P2^2;
sbit a1=P2^1;
sbit a0=P2^0;
//4051地址位定义
sbit _4051a0=P2^5;
sbit _4051a1=P2^6;
sbit _4051a2=P2^7;
//4个数码管位选定义,4511选通位
sbit wei1=P1^4;
sbit wei2=P1^5;
sbit wei3=P1^6;
sbit wei4=P1^7;
//1毫秒延时函数
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x–)
for(y=124;y>0;y–);
}
/********8254模块函数*********/
void wr_c(uchar com) //8254写控制字函数
{
uint i;
a1=1;
a0=1;
P0=com;
wr=0;
for(i=0;i<1;i++);
wr=1;
}
void wr_d(uchar dat,bit aa1,bit aa0) //8254写数据函数
{
uint i;
a1=aa1;
a0=aa0;
P0=dat;
wr=0;
for(i=0;i<2;i++);
wr=1;
}
/**********8254模块函数结束**********/
/*****************各段分频子函数****************/
void fre100k_400k() //100k到400K的连续分频
{
uint cnt;
uchar th,tl;
_4051a0=0;
_4051a1=0;
_4051a2=0;
for(cnt=0x00fa;cnt>0x003d;cnt–)
{
tl=cnt%0x100;
th=cnt/0x100;
wr_d(tl,0,0);
wr_d(th,0,0);
}
for(cnt=0x003d;cnt<0x00fa;cnt++)
{
tl=cnt%0x100;
th=cnt/0x100;
wr_d(tl,0,0);
wr_d(th,0,0);
}
}
void fre10k_100k() //10k到100K的连续分频
{
uint cnt;
uchar th,tl;
_4051a0=0;
_4051a1=0;
_4051a2=0;
for(cnt=0x09c4;cnt>0x00fa;cnt–)
{
tl=cnt%0x100;
th=cnt/0x100;
wr_d(tl,0,0);
wr_d(th,0,0);
}
for(cnt=0x00fa;cnt<0x09c4;cnt++)
{
tl=cnt%0x100;
th=cnt/0x100;
wr_d(tl,0,0);
wr_d(th,0,0);
}
}
void fre1k_10k() //1k到10K的连续分频
{
uint cnt;
uchar th,tl;
_4051a0=0;
_4051a1=0;
_4051a2=0;
for(cnt=0x61a6;cnt>0x09c4;cnt–)
{
tl=cnt%0x100;
th=cnt/0x100;
wr_d(tl,0,0);
wr_d(th,0,0);
}
for(cnt=0x09c4;cnt<0x61a6;cnt++)
{
tl=cnt%0x100;
th=cnt/0x100;
wr_d(tl,0,0);
wr_d(th,0,0);
}
}
void fre500_1k() //500到1K的连续分频
{
uint cnt;
uchar th,tl;
_4051a0=0;
_4051a1=0;
_4051a2=0; //开通4051通道0
wr_c(0x36); //通道0读写8位,工作方式3BCD计数
for(cnt=0xc34e;cnt>0x61a6;cnt–)
{
tl=cnt%0x100;
th=cnt/0x100;
wr_d(tl,0,0);
wr_d(th,0,0);
}
for(cnt=0x61a6;cnt<0xc34e;cnt++)
{
tl=cnt%0x100;
th=cnt/0x100;
wr_d(tl,0,0);
wr_d(th,0,0);
}
}
void fre100_500() //100到500的连续分频
{
uint cnt;
uchar th,tl;
wr_d(0x3d,0,0);
wr_d(0x00,0,0);
wr_c(0x76); //通道1读写8位,工作方式3BCD计数
_4051a0=1;
_4051a1=0;
_4051a2=0; //接通4051通道1
for(cnt=0x1001;cnt>0x0332;cnt–)
{
tl=cnt%0x100;
th=cnt/0x100;
wr_d(tl,0,1);
wr_d(th,0,1);
}
for(cnt=0x0332;cnt<0x1001;cnt++)
{
tl=cnt%0x100;
th=cnt/0x100;
wr_d(tl,0,1);
wr_d(th,0,1);
}
}
void fre1_100() //1到100的连续分频
{
uint cnt;
uchar th,tl;
wr_d(0xa0,0,0);
wr_d(0x01,0,0);
wr_c(0x76); //通道1读写8位,工作方式3BCD计数
_4051a0=1;
_4051a1=0;
_4051a2=0; //接通4051通道1
for(cnt=0xeab8;cnt>0x257;cnt–)
{
tl=cnt%0x100;
th=cnt/0x100;
wr_d(tl,0,1);
wr_d(th,0,1);
}
for(cnt=0x0257;cnt<0xeab8;cnt++)
{
tl=cnt%0x100;
th=cnt/0x100;
wr_d(tl,0,1);
wr_d(th,0,1);
}
}
void fre1_400k() //1到400K的连续分频
{
fre100k_400k();
fre10k_100k();
fre1k_10k();
fre500_1k();
fre100_500();
fre1_100();
fre1_100();
fre100_500();
fre500_1k();
fre1k_10k();
fre10k_100k();
fre100k_400k();
}
/*****************各段分频函数结束**************/
/***************数码管显示模块函数**************/
void display(uchar dat)
{
P1=num[dat];
wei1=0;
delay(500);
wei2=0;
delay(500);
wei3=0;
delay(500);
wei4=0;
}
/*************数码管显示模块函数结束************/
void main() //主函数入口
{
cs=0;
wr_c(0x36);
_4051a0=0;
_4051a1=0;
_4051a2=0;
EA=1;
EX0=1;
display(4);
while(1)
fre500_1k();
}
void inter() interrupt 0
{
uint i;
EX0=0;
delay(5);
switch(P3)
{
case key1:
display(1);
for(i=0;i<20;i++)
fre100k_400k();
break;
case key2:
display(2);
for(i=0;i<20;i++)
fre10k_100k();
break;
case key3:
display(3);
for(i=0;i<20;i++)
fre1k_10k();
break;
case key4:
display(4);
for(i=0;i<20;i++)
fre500_1k();
break;
case key5:
display(5);
for(i=0;i<20;i++)
fre100_500();
break;
case key6:
display(6);
for(i=0;i<20;i++)
fre1_100();
break;
case key7:
display(7);
for(i=0;i<10;i++)
fre1_400k();
break;
}
EX0=1;
display(4);
}
发表回复