开年回来后,就折腾了这个小东西
有人需要一个可调方波,很简单的花了2个晚上搭出来个用51单片机IO口输出的占空比和频率可调的方波 至于以后的完善当然是最好用单片机控制波形发生器芯片咯
废话少说
先看proteus搭的电路图
从实际上来说,单片机的IO口驱动数码管是不合适的,但是我在模拟的时候遇到了情况,假若加上了上拉三极管,仿真就会出现错误,说什么时序不对,我为了省事就没去细究就先用IO口直接驱动。
在焊实验板的时候还是老老实实加了9012三极管和限流电阻,长期稳定工作肯定是需要这样的了:)
顺便说下,自己为了方便还画了块232电路板方便51在线烧录用,以后不管做什么51板只要芯片的5V,地,P3.0,P3.1四个口接上4个排针,就可以很方便的在线烧录了省去拔插芯片的苦恼 此板还可以做供电板使用,板上有220V-12V的变压器,可以提供5V,350MA的电流,如果不方便用220V电源那么板子上还留有USB接口,我们可以用USB供电
手头上还有一些232电路-供电两用板 如果有人有兴趣的话我成本价甩了
留言即可:)
下面简述一下程序
运用51的定时器0产生1ms的中断,以1ms为基数控制某个IO口输出方波,说白了也就是高低电平的变化而已。
目前设定频率可调范围为20-100hz 以10为单位,即20,30,40,50,60,70,80,90,100这些固定值,在设定占空比为50%-80%可调,也是以10%为变化单位。调节频率和占空比都有专用的按键。要想高精度比如100KHz的得到方波,还是用专门的芯片吧,个人认为51单片机暂时只能比较粗略的得到方波。
在设定每满2ms进行IO口控制操作,第一个2ms扫描数码管1的个位,第二个2ms扫描数码管1的十位,以此类推,第4个2ms扫描数码管2的十位,第5个2ms就进入了键盘扫描程序,这样每10ms一个大循环操作 从实验板的反馈效果来看,这样设定还是比较成功的。
调试的时候还要注意一些细节,比如说一开始实验板上和232板的地并没有接在一块,这样就造成了芯片无法烧录,后咨询公司的工程师才知道需要这样。呵呵,好粗心:)
至于方波的输出,我已经用proteus的模拟工具仿真过了并且也拿公司的示波器查看了一下,感觉效果还不错,设定50hz的时候基本上真实输出的周期了19.7ms左右,反正要求也不是很高,只是作为驱动mos管用的,所以我也没在去细究程序了。
下面贴出代码
/***************************************/
/*
方波发生器软件V1.0
2009.2.5
design by su37sm
****************************************/
#include <reg52.h>
#include <intrins.h>
#include "binary.h"
#define uchar unsigned char
#define uint unsigned int
sbit a="P0"^0;
sbit b="P0"^1;
sbit c="P0"^2;
sbit d="P0"^3;
sbit e="P0"^4;
sbit f="P0"^5;
sbit g="P0"^6;
sbit D1_com2=P2^0;
sbit D0_com2=P2^1;
sbit D0_com1=P2^2;
sbit D1_com1=P2^3;
sbit key_DC=P1^0;
sbit key_fadd=P1^1;
sbit key_fdec=P1^2;
sbit FMQ="P2"^4;
sbit waver="P3"^7;
uchar freq[]={20,30,40,50,60,70,80,90,100};
uchar f_time[]={50,30,25,20,16,14,12,11,10};
uchar d_cycle[]={50,60,70,80};
uchar dispvalue[2]={0,0};
uchar settime1ms,time2ms,timechigh,timeclow;
uchar bdata flag1=0;
uchar fnum,tnum,dcnum;
uchar settimel,settimeh,setfreq,dispsetdccycle,dispsetfreq,dcycle;
uchar setfreq,settime,keydebounce=0;
uchar keynow,keypre,keysave,buztime,dispnum,disparray;
uchar dispsequence="0",dcycle_shiwei;
uchar code table[11]={0x81,0x9F,0xA4,0x94,0x9A,0xD0,0xC0,0x9D,0x80,0x90,0x88};
// 0 1 2 3 4 5 6 7 8 9 A
sbit lowf="flag1"^0;
sbit highf="flag1"^1;
sbit timef="flag1"^2;
sbit keyreadf="flag1"^3;
sbit keypressf="flag1"^4;
sbit keyf="flag1"^5;
sbit freqchangef="flag1"^6;
sbit dcchangef="flag1"^7;
void disp(); //显示模块函数
void keyscan(); //键盘扫描函数
void keyrespon(); //键盘处理函数
void settimer(); //定时器设置函数
void setdefault(); //上电默认设置函数
void freq_time_process(); //频率时间处理函数
void FMQmodule(); //蜂鸣器函数
void setdefault()
{
TMOD=0x01; //定时器0工作在方式1
TH0=0xFC; //晶振11.0592M 定时1ms中断需要置数为FC66H
TL0=0x66;
EA=1; //开总中断
ET0=1; //打开定时器0
TR0=1;
fnum=3;
tnum=3;
dcnum=0;
setfreq=freq[fnum];
settime=f_time[tnum];
dcycle=d_cycle[dcnum];
settimeh=settimel=timechigh=timeclow=10;
dispsetfreq=setfreq;
dispsetdccycle=dcycle;
FMQ=1;
highf=1;
D0_com1=0;
D0_com2=0;
D1_com1=0;
D1_com2=0;
}
void settimer()
{
if(settime1ms==1)
{
settime1ms=0;
time2ms++;
if(time2ms==2)
{
freq_time_process();
timef=1;
time2ms=0;
if(buztime!=0)
{
buztime--;
if(buztime==0)
{
FMQ=1;
}
}
/* if(keydebounce!=0)
{
keydebounce--;
}*/
if(dispsequence==0)
{
D0_com1=0;
D0_com2=0;
dispnum=dispsetfreq;
disparray=0;
disp();
D0_com2=1;
}
else if(dispsequence==1)
{
D0_com1=0;
D0_com2=0;
dispnum=dispsetfreq;
disparray=1;
disp();
D0_com1=1;
}
else if(dispsequence==2)
{
D0_com1=0;
D0_com2=0;
D1_com1=0;
D1_com2=0;
dispnum=dispsetdccycle;
disparray=0;
disp();
D1_com2=1;
}
else if(dispsequence==3)
{
D1_com1=0;
D1_com2=0;
dispnum=dispsetdccycle;
disparray=1;
disp();
D1_com1=1;
}
else
{
P0=0xff;
D0_com1=0;
D0_com2=0;
D1_com1=0;
D1_com2=0;
keyscan();
_nop_();
_nop_();
}
dispsequence++;
if(dispsequence>4)
dispsequence=0;
}
if(highf==1)
{
waver=1;
if(timechigh!=0)
{
timechigh--;
}
if(timechigh==0)
{
timeclow=settimel;
lowf=1;
highf=0;
}
}
if(lowf==1)
{
waver=0;
if(timeclow!=0)
{
timeclow--;
}
if(timeclow==0)
{
timechigh=settimeh;
lowf=0;
highf=1;
}
}
}
}
void disp()
{
uchar disptemp;
disptemp=dispnum;
dispvalue[1]=0;
while(disptemp>=10)
{
dispvalue[1]++;
disptemp=disptemp-10;
}
dispvalue[0]=disptemp;
P0=table[dispvalue[disparray]];
}
void freq_time_process()
{
dispsetfreq=setfreq;
dispsetdccycle=dcycle;
if(dcchangef|freqchangef)
{
dcycle_shiwei=dcycle/10;
settime=f_time[tnum];
settimeh=settime*dcycle_shiwei;
settimeh/=10;
settimel=settime-settimeh;
dcchangef=0;
freqchangef=0;
}
}
void FMQmodule()
{
FMQ=0;
buztime=50;
}
void keyscan()
{
_nop_();
_nop_();
keynow=P1; //将P1读取到keynow中
if(keynow==keypre)
{
keydebounce--;
if(keydebounce==0)
{
//if(keynow^0xff)
// {
uchar keycal="keynow"^0xff;
if(keycal==0x04||keycal==0x02||keycal==0x01)
{
keypressf=1;
keysave=keynow;
}
// }
}
}
else
{
keypre=keynow;
keydebounce=2;
}
}
void keyrespon()
{
if(keypressf==1)
{
FMQmodule();
keypressf=0;
switch(keysave)
{
case B11111110:
dcnum++;
if(dcnum==4)
{
dcnum=0;
}
dcycle=d_cycle[dcnum];
dcchangef=1;
break;
case B11111101:
if(fnum<8)
{
fnum++;
}
tnum=fnum;
setfreq=freq[fnum];
freqchangef=1;
break;
case B11111011:
if(fnum>0)
{
fnum--;
}
tnum=fnum;
setfreq=freq[fnum];
freqchangef=1;
break;
}
}
}
void main()
{
setdefault();
while(1)
{
settimer();
keyrespon();
}
}
void intertime0() interrupt 1
{
settime1ms=1;
TH0=0xFC;
TL0=0x66;
}
后面的是一个二进制和十六进制的定义h文件,也贴出来共享
/*------------binary.h----------------*/
#define B00000000 0x00
#define B00000001 0x01
#define B00000010 0x02
#define B00000011 0x03
#define B00000100 0x04
#define B00000101 0x05
#define B00000110 0x06
#define B00000111 0x07
#define B00001000 0x08
#define B00001001 0x09
#define B00001010 0x0A
#define B00001011 0x0B
#define B00001100 0x0C
#define B00001101 0x0D
#define B00001110 0x0E
#define B00001111 0x0F
#define B00010000 0x10
#define B00010001 0x11
#define B00010010 0x12
#define B00010011 0x13
#define B00010100 0x14
#define B00010101 0x15
#define B00010110 0x16
#define B00010111 0x17
#define B00011000 0x18
#define B00011001 0x19
#define B00011010 0x1A
#define B00011011 0x1B
#define B00011100 0x1C
#define B00011101 0x1D
#define B00011110 0x1E
#define B00011111 0x1F
#define B00100000 0x20
#define B00100001 0x21
#define B00100010 0x22
#define B00100011 0x23
#define B00100100 0x24
#define B00100101 0x25
#define B00100110 0x26
#define B00100111 0x27
#define B00101000 0x28
#define B00101001 0x29
#define B00101010 0x2A
#define B00101011 0x2B
#define B00101100 0x2C
#define B00101101 0x2D
#define B00101110 0x2E
#define B00101111 0x2F
#define B00110000 0x30
#define B00110001 0x31
#define B00110010 0x32
#define B00110011 0x33
#define B00110100 0x34
#define B00110101 0x35
#define B00110110 0x36
#define B00110111 0x37
#define B00111000 0x38
#define B00111001 0x39
#define B00111010 0x3A
#define B00111011 0x3B
#define B00111100 0x3C
#define B00111101 0x3D
#define B00111110 0x3E
#define B00111111 0x3F
#define B01000000 0x40
#define B01000001 0x41
#define B01000010 0x42
#define B01000011 0x43
#define B01000100 0x44
#define B01000101 0x45
#define B01000110 0x46
#define B01000111 0x47
#define B01001000 0x48
#define B01001001 0x49
#define B01001010 0x4A
#define B01001011 0x4B
#define B01001100 0x4C
#define B01001101 0x4D
#define B01001110 0x4E
#define B01001111 0x4F
#define B01010000 0x50
#define B01010001 0x51
#define B01010010 0x52
#define B01010011 0x53
#define B01010100 0x54
#define B01010101 0x55
#define B01010110 0x56
#define B01010111 0x57
#define B01011000 0x58
#define B01011001 0x59
#define B01011010 0x5A
#define B01011011 0x5B
#define B01011100 0x5C
#define B01011101 0x5D
#define B01011110 0x5E
#define B01011111 0x5F
#define B01100000 0x60
#define B01100001 0x61
#define B01100010 0x62
#define B01100011 0x63
#define B01100100 0x64
#define B01100101 0x65
#define B01100110 0x66
#define B01100111 0x67
#define B01101000 0x68
#define B01101001 0x69
#define B01101010 0x6A
#define B01101011 0x6B
#define B01101100 0x6C
#define B01101101 0x6D
#define B01101110 0x6E
#define B01101111 0x6F
#define B01110000 0x70
#define B01110001 0x71
#define B01110010 0x72
#define B01110011 0x73
#define B01110100 0x74
#define B01110101 0x75
#define B01110110 0x76
#define B01110111 0x77
#define B01111000 0x78
#define B01111001 0x79
#define B01111010 0x7A
#define B01111011 0x7B
#define B01111100 0x7C
#define B01111101 0x7D
#define B01111110 0x7E
#define B01111111 0x7F
#define B10000000 0x80
#define B10000001 0x81
#define B10000010 0x82
#define B10000011 0x83
#define B10000100 0x84
#define B10000101 0x85
#define B10000110 0x86
#define B10000111 0x87
#define B10001000 0x88
#define B10001001 0x89
#define B10001010 0x8A
#define B10001011 0x8B
#define B10001100 0x8C
#define B10001101 0x8D
#define B10001110 0x8E
#define B10001111 0x8F
#define B10010000 0x90
#define B10010001 0x91
#define B10010010 0x92
#define B10010011 0x93
#define B10010100 0x94
#define B10010101 0x95
#define B10010110 0x96
#define B10010111 0x97
#define B10011000 0x98
#define B10011001 0x99
#define B10011010 0x9A
#define B10011011 0x9B
#define B10011100 0x9C
#define B10011101 0x9D
#define B10011110 0x9E
#define B10011111 0x9F
#define B10100000 0xA0
#define B10100001 0xA1
#define B10100010 0xA2
#define B10100011 0xA3
#define B10100100 0xA4
#define B10100101 0xA5
#define B10100110 0xA6
#define B10100111 0xA7
#define B10101000 0xA8
#define B10101001 0xA9
#define B10101010 0xAA
#define B10101011 0xAB
#define B10101100 0xAC
#define B10101101 0xAD
#define B10101110 0xAE
#define B10101111 0xAF
#define B10110000 0xB0
#define B10110001 0xB1
#define B10110010 0xB2
#define B10110011 0xB3
#define B10110100 0xB4
#define B10110101 0xB5
#define B10110110 0xB6
#define B10110111 0xB7
#define B10111000 0xB8
#define B10111001 0xB9
#define B10111010 0xBA
#define B10111011 0xBB
#define B10111100 0xBC
#define B10111101 0xBD
#define B10111110 0xBE
#define B10111111 0xBF
#define B11000000 0xC0
#define B11000001 0xC1
#define B11000010 0xC2
#define B11000011 0xC3
#define B11000100 0xC4
#define B11000101 0xC5
#define B11000110 0xC6
#define B11000111 0xC7
#define B11001000 0xC8
#define B11001001 0xC9
#define B11001010 0xCA
#define B11001011 0xCB
#define B11001100 0xCC
#define B11001101 0xCD
#define B11001110 0xCE
#define B11001111 0xCF
#define B11010000 0xD0
#define B11010001 0xD1
#define B11010010 0xD2
#define B11010011 0xD3
#define B11010100 0xD4
#define B11010101 0xD5
#define B11010110 0xD6
#define B11010111 0xD7
#define B11011000 0xD8
#define B11011001 0xD9
#define B11011010 0xDA
#define B11011011 0xDB
#define B11011100 0xDC
#define B11011101 0xDD
#define B11011110 0xDE
#define B11011111 0xDF
#define B11100000 0xE0
#define B11100001 0xE1
#define B11100010 0xE2
#define B11100011 0xE3
#define B11100100 0xE4
#define B11100101 0xE5
#define B11100110 0xE6
#define B11100111 0xE7
#define B11101000 0xE8
#define B11101001 0xE9
#define B11101010 0xEA
#define B11101011 0xEB
#define B11101100 0xEC
#define B11101101 0xED
#define B11101110 0xEE
#define B11101111 0xEF
#define B11110000 0xF0
#define B11110001 0xF1
#define B11110010 0xF2
#define B11110011 0xF3
#define B11110100 0xF4
#define B11110101 0xF5
#define B11110110 0xF6
#define B11110111 0xF7
#define B11111000 0xF8
#define B11111001 0xF9
#define B11111010 0xFA
#define B11111011 0xFB
#define B11111100 0xFC
#define B11111101 0xFD
#define B11111110 0xFE
#define B11111111 0xFF
用户405381 2009-8-27 19:38
用户198861 2009-3-13 18:25
用户172102 2009-2-12 21:22
用户518079 2009-2-11 15:57
ilove314_323192455 2009-2-7 21:18