Waveform generator made by 51 single chip microcomputer

Publisher:数字梦行Latest update time:2012-08-10 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
I believe that many of you may have come into contact with the production of a wave generator. Maybe you are just getting started, and the things you make are not very complicated. Maybe it is just a rectangular wave or a triangular wave. However, many online materials are misleading. In this regard, I also provide a relatively complete wave generator C51 original:

The software of this system is quite typical: including keyboard application, display application and DA converter application. In this design, there are three kinds of output waveforms: sine wave, square wave and triangle wave.

The output of square wave is the simplest, just change the output voltage according to the set period value.

The output of the triangle wave is also relatively simple. The output of the microcontroller only needs to complete the alternating increase and decrease of the digital quantity.

The output of sine wave is the most troublesome. If the voltage value of each output point is calculated in the software, a lot of CPU time will be wasted, so that the frequency requirement cannot be met. Usually the simplest method is to calculate the voltage value of each output point manually, and then give it in array form when writing the program. When needed, just output it in sequence. This method is faster than the operation method and the shape of the curve can be modified flexibly. In this design, 360 degrees is divided into 256 points, and the interval between each two points is 1.4 degrees, and then the digital value corresponding to the voltage of each point is calculated. As long as this set of data is repeatedly output to DAC0832, the desired sine wave can be obtained at the output end of the system.

The specific procedures are as follows:

#include 
#define uchar unsigned char
#define uint unsigned int
#define DAdata P0
uchar code Sinetab[256]=
{
0x80,0x82,0x84,0x86,0x88,0x8a,0x8c,0x8e,
0x90,0x92,0x94,0x96,0x98,0x9a,0x9c,0x9e,
0xa0,0xa2,0xa4,0xa6,0xa8,0xaa,0xab,0xad,
0xaf,0xb1,0xb2,0xb4,0xb6,0xb7,0xb9,0xba,
0xbc,0xbd,0xbf,0xc0,0xc1,0xc3,0xc4,0xc5,
0xc6,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,
0xce,0xcf,0xd0,0xd1,0xd1,0xd2,0xd2,0xd3,
0xd3,0xd3,0xd2,0xd2,0xd1,0xd1,0xd0,0xcf,
0xce,0xce,0xcd,0xcc,0xcb,0xca,0xc9,0xc8,
0xc6,0xc5,0xc4,0xc3,0xc1,0xc0,0xbf,0xbd,
0xbc,0xba,0xb9,0xb7,0xb6,0xb4,0xb2,0xb1,
0xaf,0xad,0xab,0xaa,0xa8,0xa6,0xa4,0xa2,
0xa0,0x9e,0x9c,0x9a,0x98,0x96,0x94,0x92,
0x90,0x8e,0x8c,0x8a,0x88,0x86,0x84,0x82,
0x80,0x7d,0x7b,0x79,0x77,0x75,0x73,0x71,
0x6f,0x6d,0x6b,0x69,0x67,0x65,0x63,0x61,
0x5f,0x5d,0x5b,0x59,0x57,0x55,0x54,0x52,
0x50,0x4e,0x4d,0x4b,0x49,0x48,0x46,0x45,
0x43,0x42,0x40,0x3f,0x3e,0x3c,0x3b,0x3a,
0x39,0x37,0x36,0x35,0x34,0x33,0x32,0x31,
0x31,0x30,0x2f,0x2e,0x2e,0x2d,0x2d,0x2c,
0x2c,0x2b,0x2b,0x2b,0x2b,0x2a,0x2a,0x2a,
0x2a,0x2a,0x2a,0x2a,0x2b,0x2b,0x2b,0x2b,
0x2c,0x2c,0x2d,0x2d,0x2e,0x2e,0x2f,0x30,
0x31,0x31,0x32,0x33,0x34,0x35,0x36,0x37,
0x39,0x3a,0x3b,0x3c,0x3e,0x3f,0x40,0x42,
0x43,0x45,0x46,0x48,0x49,0x4b,0x4d,0x4e,
0x50,0x52,0x54,0x55,0x57,0x59,0x5b,0x5d,
0x5f,0x61,0x63,0x65,0x67,0x69,0x6b,0x6d,
0x6f,0x71,0x73,0x75,0x77,0x79,0x7b,0x7d,
};

uchar code Triangletab[58]=
{
0x1a,0x21,0x28,0x2f,0x36,0x3d,0x44,0x4b,
0x52,0x59,0x60,0x67,0x6e,0x75,0x7c,0x83,
0x8a,0x91,0x98,0x9f,0xa6,0xad,0xb4,0xbb,
0xc2,0xc9,0xd0,0xd7,0xde,0xe5,
0xde,0xd7,0xd0,0xc9,0xc2,0xbb,0xb4,0xad,
0xa6,0x9f,0x98,0x91,0x8a,0x83,0x7c,0x75,
0x6e,0x67,0x60,0x59,0x52,0x4b,0x44,0x3d,
0x36,0x2f,0x28,0x21,
};

uchar code Squaretab[2]={0x56,0xaa};

uchar code disp1[]=
{
"Sine Wave"
"Triangle Wale"
"Square Wave"
};

uchar idata disp2[16]={"Frequency: Hz"};

uchar code Coef[3]={10,100,200};

uchar idata WaveFre[3]={1,1,1};

uchar code WaveTH[]=
{
0xfc,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xfc,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,

};

uchar code WaveTL[]=
{
0xf2,0x78,0xfb,0x3c,0x63,0x7d,0x8f,0x9d,0xa8,0xb1,
0x17,0x0b,0xb2,0x05,0x37,0x58,0x70,0x82,0x90,0x9b,
0x4d,0xa7,0xc4,0xd3,0xdc,0xe2,0xe6,0xea,0xec,0xee
};

uchar Wavecount,THtemp,TLtemp;
uchar Waveform;
sbit rs=P2^5;
sbit rw=P2^6;
sbit e=P2^7;
sbit DA=P2^0;
sbit KEY=P3^2;
void delay(uchar i)
{
uchar j;
for(;i>0;i--)
   for(j=20;j>0;j--);
}

void busy()
{
uchar temp;
temp=0x00;
rs=0;
rw=1;
while((temp&0x80)==0x80)
   {
    P0=0xff;
    e=1;
    temp=P0;
    e=0;
   }
}

void WR_Com(uchar temp)
{
busy();
rs=0;
rw=0;
P0=temp;
e=1;
e=0;
}

void WR_Data(uchar num)
{
busy();
rs=1;
rw=0;
P0=num;
e=1;
e=0;
}

void disp_lcd(uchar addr,uchar *temp1)
{
uchar i;
WR_Com(addr);
delay(100);
for(i=0;i<16;i++)
   {
    WR_Data(temp1[i]);
    delay(100);
   }
}

void lcd_ini()
{
char i;
for(i=3;i>0;i--)
   {
    P0=0x30;
    rs=0;
    rw=0;
    e=1;
    e=0;
    delay(100);
   }
P0=0x38;
rs=0;
rw=0;
e=1;
e=0;
delay(100);
}

void lcd_Reset()
{
WR_Com(0x01);
delay(100);
WR_Com(0x06);
delay(100);
WR_Com(0x0c);
delay(100);
}

void SineOUT(uchar Wavecount)
{
DAdata=Sinetab[Wavecount++];
Wavecount=0;
DA=0;
DA=1;
}

void TriangleOUT(uchar Wavecount)
{
DAdata=Triangletab[Wavecount++];
if(Wavecount>57)
   Wavecount=0;
DA=0;
DA=1;
}

void SquareOUT(uchar Wavecount)
{
DAdata=Squaretab[Wavecount++];
if(Wavecount>1)
Wavecount=0;
DA=0;
DA=1;
}

void timer() interrupt 1
{
TH0=THtemp;
TL0=THtemp;
if(Waveform==0)
   SineOUT(Wavecount);
else if(Waveform==1)
   TriangleOUT(Wavecount);
else if(Waveform==2)
   SquareOUT(Wavecount);
}

void key_int() interrupt 0
{
uchar keytemp,keytemp1;
uint WaveCoef;
EA=0;
TR0=0;
keytemp1=0;
delay(10);
while(!KEY);
keytemp=~P2&0x1e;
keytemp>>=1;
while(keytemp!=8)
   {
    keytemp=~P2&0x1e;
    keytemp>>=1;
    if(keytemp!=keytemp1)
     {
      keytemp1=keytemp;
      switch(keytemp)
       {
        case 1:
         if(++Waveform==3)
          Waveform=0;
         break;
        case 2:
         if(++WaveFre[Waveform]==11)
          WaveFre[Waveform]=1;
         break;
        case 4:
         if (--WaveFre[Waveform]==0)
          WaveFre[Waveform]=10;
         break;
       }
    THtemp=WaveTH[Waveform*16+(WaveFre[Waveform]-1)];
    TLtemp=WaveTL[Waveform*16+(WaveFre[Waveform]-1)];
    WaveCoef=WaveFre[Waveform]*Coef[Waveform];
    disp2[13]=WaveCoef%10+0x30;
    WaveCoef/=10;
    disp2[12]=WaveCoef%10+0x30;
    WaveCoef/=10;
    disp2[11]=WaveCoef%10+0x30;
    WaveCoef/=10;
    disp2[10]=WaveCoef%10+0x30;
    WaveCoef/=10;
    disp_lcd(0x80,&disp1[Waveform*16]);
    disp_lcd(0xc0,disp2);
     }
   }
   TH0=THtemp;
   TL0=THtemp;
   Wavecount=0;
   TR0=1;
}

void main()
{
uint WaveCoef;
uchar i;
lcd_ini();
lcd_Reset();
WaveCoef=WaveFre[Waveform]*Coef[Waveform];
disp2[13]=WaveCoef%10+0x30;
WaveCoef/=10;
disp2[12]=WaveCoef%10+0x30;
WaveCoef/=10;
disp2[11]=WaveCoef%10+0x30;
WaveCoef/=10;
disp2[10]=WaveCoef%10+0x30;
WaveCoef/=10;
disp_lcd(0x80,&disp1[Waveform*16]);
disp_lcd(0xc0,disp2);
i=0;
DAdata=0x00;
DA=0;
TMOD=0x01;
IT0=1;
ET0=1;
EX0=1;
EA=1;
while(1);
}
Reference address:Waveform generator made by 51 single chip microcomputer

Previous article:Design of 12864 LCD animation effect based on single chip microcomputer
Next article:About C51 assembly ADD and SUBB instructions

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号