任务名称:基于单片机的固定频率、占空比的方波发生器。
功能要求:产生固定频率和占空比的方波电压。
硬件要求:单片机芯片采用AT89C51,D/A转换器采用DAC0832,显示器使用LCD1602,运放使用LM324。
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include "reg52.h"
- #include "intrins.h"
- #include "absacc.h"
- #define DAC0832 XBYTE[0x7fff]
- #define LCD_DATA P1
- unsigned char LcdBuf[2][16]={{"Square F: 100Hz"},{"Duty Cycle:50%"}};
- unsigned int Fr=100,D=50;
- unsigned int low,high;
- sbit LCD_RS = P2^0;
- sbit LCD_RW = P2^1;
- sbit LCD_E = P2^2;
- void dellay(unsigned int h)
- {
- while(h--); //0.01MS
- }
- void WriteDataLcd(unsigned char wdata)
- {
- LCD_DATA=wdata;
- LCD_RS=1;
- LCD_RW=0;
- LCD_E=0;
- dellay(1000);
- LCD_E=1;
- }
- void WriteCommandLcd(unsigned char wdata)
- {
- LCD_DATA=wdata;
- LCD_RS=0;
- LCD_RW=0;
- LCD_E=0;
- dellay(1000);
- LCD_E=1;
- }
- void lcd_init(void)
- {
- LCD_DATA=0;
- WriteCommandLcd(0x38);
- dellay(1000);
- WriteCommandLcd(0x38);
- dellay(1000);
- WriteCommandLcd(0x01);
- WriteCommandLcd(0x0c);
- }
- void display_xy(unsigned char x,unsigned char y)
- {
- if(y==1)
- x+=0x40;
- x+=0x80;
- WriteCommandLcd(x);
- }
- void display_string(unsigned char x,unsigned char y,unsigned char *s)
- {
- display_xy(x,y);
- while(*s)
- {
- WriteDataLcd(*s);
- s++;
- }
- }
- void delayhus(unsigned int hus)
- {
- unsigned int i,j;
- for(i=hus;i>0;i--)
- for(j=11;j>0;j--);
- }
复制代码
Keil代码与Proteus8.13 Proteus7.5版本的仿真下载:(按键调节功能暂未加入,大家可以在此基础上改进)
固定频率、占空比方波发生器.rar
(100.27 KB, 下载次数: 32)
|