基于51单片机的信号发生器

基于51单片机的信号发生器,第1张

目录

一、项目功能

二、仿真

三、程序


资料下载地址:基于51单片机的信号发生器

一、项目功能

1. 输出矩形波、梯形波、三角波、锯齿波、正弦波五种波形;

2. 每种波形的频率可更改;

3. 采用lcd12864中文显示屏显示波形和相关信息;

二、仿真

 

正弦波 

三角波 

方波

锯齿波

三、程序
#include 
#include 
#include 
#include 

#define DAC_OUT	P2

sbit change_wave=P3^2;               //改变波形按键
sbit change_rate=P3^3;               //改变频率按键
uchar mode=0,rate=0,delay_time=0,k,p; //为波形发生模块提供中间变量
uchar *which_wave,*which_wave2;                    
uint rate_num;

uchar code Sine_wave[64]=                                    //DA输出对应电压值对应的数字量,正弦波
{
    128,114,102,90,78,66,55,45,36,28,20,14,9,5,2,1,1,1,
    3,7,11,17,24,32,41,50,61,72,84,96,108,121,133,146,
    158,170,182,193,204,213,222,230,237,243,247,251,253,
    254,254,252,249,245,240,234,226,218,209,199,188,176,
    167,158,145,135
};

uchar code Sawtooth_Wave[64]=                                //锯齿波
{
    255,251,247,243,239,235,231,227,223,219,215,210,206,202,
    198,194,190,186,182,178,174,170,166,162,158,154,150,146,
    142,138,134,130,125,121,117,113,109,105,101,97,93,89,85,
    81,77,73,69,61,57,53,49,45,40,36,32,28,24,20,16,12,8,4,0
};

uchar code Square_wave[64]=                                  //方波
{
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,255,255,255,255,255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,
};

uchar code Triangular_Wave[64]=                              //三角波
{
    0,8,16,24,32,40,48,56,64,72,80,88,96,104,112,120,128,136,
    144,152,160,168,176,184,192,200,208,216,224,232,240,248,
    248,240,232,224,216,208,200,192,184,176,168,160,152,144,
    136,128,120,112,104,96,88,80,72,64,56,48,40,32,24,16,8,0
};

void wave_delay()                         //波形延时函数
{
    int a,b;
    for(a=1; a>0; a--)
        for(b=122; b>0; b--);
}

void wave_init()             //波形发生模块的初始化(外部中断0、1)
{
    EA=1;
    IT0=1;             //下降沿触发
    EX0=1;
    IT1=1;
    EX1=1;
}

void disp_wave(uchar *wave)   //显示波形函数
{
    uchar page,i;
    uint date;
    select(1);								//选择左屏
    for(i=32; i<64; i++)
    {
        page=7-(wave[i]/4)/8;
        date=7-(wave[i]/4)%8;
        date=pow(2,date);
        setpos(page,i);				//选择行列
        lcd_wdat(date);
    }
    select(2);								//选择右屏
    for(i=0; i<64; i++)
    {
        page=7-(wave[i]/4)/8;
        date=7-(wave[i]/4)%8;
        date=pow(2,date);
        setpos(page,i);				//选择行列
        lcd_wdat(date);
    }
}

void main()
{

    lcd_init();
    clr_screen();
    which_wave=&Sine_wave[0];
    disp_chinese();
    disp_function(chinese_sine);
    disp_rate(400);
    disp_xy();
    disp_wave(which_wave);
    wave_init();
    while(1)
    {
        delay_time=rate;            //rate=0时,12mhz下,为400hz的波形。循环一次的时间为0.00025ms
        DAC_OUT=*(which_wave+k);
        k++;
        if(k==64)
            k=0;
        while(delay_time)
            delay_time--;
    }
}

void int0() interrupt 0    //波形选择中断服务程序
{
    EX0=0;
    wave_delay();
    mode++;
    if(mode==4)
        mode=0;
    switch(mode)
    {
    case 0 :																				//显示正弦波
        which_wave=&Sine_wave[0];
        which_wave2=&chinese_sine[0];
        break;
    case 1 :																				//显示三角波
        which_wave=&Triangular_Wave[0];
        which_wave2=&chinese_triangular[0];
        break;
    case 2 :																				//显示方波
        which_wave=&Square_wave[0];
        which_wave2=&chinese_square[0];
        break;
    case 3 :																				//显示锯齿波
        which_wave=&Sawtooth_Wave[0];
        which_wave2=&chinese_sawtooth[0];
        break;
    }
    wave_delay();
    clr_screen();																		
    disp_chinese();																
    disp_rate(400/rate);
    disp_function(which_wave2);
    disp_xy();
    disp_wave(which_wave);
    while(!change_wave);
    EX0=1;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/676074.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-19
下一篇 2022-04-19

发表评论

登录后才能评论

评论列表(0条)

保存