89C51 and ad0832 output sine wave, triangle wave, rectangular wave, sawtooth wave

Publisher:qin199099Latest update time:2018-05-13 Source: eefocusKeywords:89C51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Proteus simulation circuit diagram



C language program

  1. #include   

  2. #include   

  3.   

  4. #define DA0832 XBYTE[0xfffe]  

  5.   

  6. #define uchar unsigned char  

  7. #define uint unsigned int  

  8.   

  9. sbit S1 = P1^0;  

  10. sbit S2 = P1^1;  

  11. //00 sawtooth wave; 01 square wave; 10 triangle wave; 11 sine wave  

  12.   

  13. uchar code sin_tab[] = //sine wave output table  

  14. {  

  15.      0x80,0x83,0x86,0x89,0x8D,0x90,0x93,0x96,0x99,0x9C,0x9F,0xA2,0xA5,0xA8,0xAB,0xAE,  

  16.      0xB1,0xB4,0xB7,0xBA,0xBC,0xBF,0xC2,0xC5,0xC7,0xCA,0xCC,0xCF,0xD1,0xD4,0xD6,0xD8,  

  17.      0xDA,0xDD,0xDF,0xE1,0xE3,0xE5,0xE7,0xE9,0xEA,0xEC,0xEE,0xEF,0xF1,0xF2,0xF4,0xF5,  

  18.      0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFD,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,  

  19.      0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xFD,0xFD,0xFC,0xFB,0xFA,0xF9,0xF8,0xF7,0xF6,  

  20.      0xF5,0xF4,0xF2,0xF1,0xEF,0xEE,0xEC,0xEA,0xE9,0xE7,0xE5,0xE3,0xE1,0xDF,0xDD,0xDA,  

  21.      0xD8,0xD6,0xD4,0xD1,0xCF,0xCC,0xCA,0xC7,0xC5,0xC2,0xBF,0xBC,0xBA,0xB7,0xB4,0xB1,  

  22.      0xAE,0xAB,0xA8,0xA5,0xA2,0x9F,0x9C,0x99,0x96,0x93,0x90,0x8D,0x89,0x86,0x83,0x80,  

  23.      0x80,0x7C,0x79,0x76,0x72,0x6F,0x6C,0x69,0x66,0x63,0x60,0x5D,0x5A,0x57,0x55,0x51,  

  24.      0x4E,0x4C,0x48,0x45,0x43,0x40,0x3D,0x3A,0x38,0x35,0x33,0x30,0x2E,0x2B,0x29,0x27,  

  25.      0x25,0x22,0x20,0x1E,0x1C,0x1A,0x18,0x16,0x15,0x13,0x11,0x10,0x0E,0x0D,0x0B,0x0A,  

  26.      0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x02,0x02,0x01,0x00,0x00,0x00,0x00,0x00,0x00,  

  27.      0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,  

  28.      0x0A,0x0B,0x0D,0x0E,0x10,0x11,0x13,0x15,0x16,0x18,0x1A,0x1C,0x1E,0x20,0x22,0x25,  

  29.      0x27,0x29,0x2B,0x2E,0x30,0x33,0x35,0x38,0x3A,0x3D,0x40,0x43,0x45,0x48,0x4C,0x4E,  

  30.      0x51,0x55,0x57,0x5A,0x5D,0x60,0x63,0x66,0x69,0x6C,0x6F,0x72,0x76,0x79,0x7C,0x7E  

  31. };  

  32.   

  33. void Delay_MS(uint);// delay   

  34. void stair(uchar AMP);//sawtooth wave   

  35. void square(uchar AMP, uchar THL,uchar TLL);//方波   

  36. void trian(uchar AMP); //triangle wave   

  37. void sin(); //sine wave   

  38. void scan(); //scan function   

  39.   

  40.   

  41. void main()  

  42. {     

  43.     while(1)  

  44.     {  

  45.         //Test one by one   

  46.         //stair(200);  

  47.         //square(200,10,10);  

  48.         //trian(200);  

  49.         //sin();  

  50.         scan();  

  51.     }  

  52. }  

  53.   

  54. void scan()  

  55. {  

  56.     if((0 == S1) &&(0 == S2))  

  57.         stair(200);  

  58.     else if((1 == S1) && (0 == S2))  

  59.         square(200,10,10);  

  60.     else if((0 == S1) && (1 == S2))  

  61.         trian(200);  

  62.     else  

  63.         sin();  

  64.           

  65.           

  66.       

  67. }  

  68. void Delay_MS(uint n)  

  69. {  

  70.     uint k;  

  71.     for(n; n  >0 ;n--)  

  72.         for(k = 10; k > 0 ;k--);  

  73. }  

  74.   

  75. void stair(uchar AMP)  

  76. {  

  77.     flying i;  

  78.     for(i = 0 ;i < 255; i++)  

  79.     {  

  80.         DA0832 = i;  

  81.     }  

  82.        

  83. }  

  84.   

  85. void square(uchar AMP, uchar THL, uchar TLL)  

  86. {  

  87.     DA0832 = 255 - AMP;  

  88.     Delay_MS(THL);  

  89.     DA0832 = 255;  

  90.     Delay_MS(TLL);  

  91. }  

  92.   

  93. void trian(uchar AMP)  

  94. {  

  95.     flying i;  

  96.     for(i = 255 - AMP ;i < 255; i++)  

  97.     {  

  98.         DA0832 = i;  

  99.     }  

  100.       

  101.     for(i-1 ;i > 255 - AMP; i--)  

  102.     {  

  103.         DA0832 = i;  

  104.     }  

  105. }  

  106.   

  107. void sin()  

  108. {  

  109.     flying i;  

  110.     for(i = 0; i < 255; i++)  

  111.     {  

  112.         DA0832 = sin_tab[i];  

  113.     }  

  114. }   

Output waveform


Keywords:89C51 Reference address:89C51 and ad0832 output sine wave, triangle wave, rectangular wave, sawtooth wave

Previous article:Microcontroller Experiment - Using Timer to Generate Square Wave
Next article:General purpose timer (interrupt function and PWM output)

Recommended ReadingLatest update time:2024-11-16 17:33

Design of power supply anti-interference circuit based on 89C51/2 microcontroller and MAX638 chip
In recent years, various low-power microcontrollers have been widely used in various types of instruments, especially the 89C51/2 microcontroller due to its excellent performance, low price and standard power consumption reduction characteristics, as well as the fast erasability of on-chip memory. Sex and so on have w
[Microcontroller]
Design of power supply anti-interference circuit based on 89C51/2 microcontroller and MAX638 chip
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号