单片机论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 126|回复: 0
打印 上一主题 下一主题
收起左侧

51单片机+DAC0832产生正弦波电路Proteus仿真+代码

[复制链接]
跳转到指定楼层
楼主
利用51单片机搭建正弦波产生电路仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)



单片机源程序如下:
  1. #include<reg52.h>  
  2. #define uint   unsigned int                                          
  3. #define uchar  unsigned char
  4. #define uchar  unsigned char
  5. sbit wr2=P3^7;  
  6. sbit rs = P2^6;              
  7. sbit rw= P2^5;
  8. sbit ep = P2^7;
  9. sbit gn=P2^4;
  10. sbit up=P2^3;
  11. sbit down=P2^2;
  12. sbit S1=P2^0;
  13. sbit S2=P2^1;
  14. sbit wr1=P3^0;
  15. sbit dir1=P3^1;
  16. sbit dir2=P3^4;  
  17. uchar j,k=39,flag,amp=0xfa;
  18. uint freq;  
  19. code uchar v[]="amplitude:";      
  20. uchar vv[5];  
  21. code uchar f[]="freqency:";  
  22. code uint fj[40]={999,744,591,490,419,366,325,292,265,243,224,208,194,181,171,
  23.                         161,153,145,138,132,126,121,116,111,107,104,100,97,94,91,85,81,74,71,67,64,60,57,53,50};  
  24. code uchar sj[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,31,33,36,38,40,42,45,48,51,55};   
  25. uchar fv[6];
  26. code uchar sin[]={0x7F,0x84,0x88,0x8D,0x91,0x96,0x9B,0x9F,0xA4,0xA8,0xAC,0xB1,0xB5,0xB9,0xBD,0xC1,0xC5,0xC9,
  27.               0xCD,0xD0,0xD4,0xD7,0xDA,0xDE,0xE1,0xE4,0xE6,0xE9,0xEB,0xEE,0xF0,0xF2  ,0xF4,0xF6,0xF7,0xF9,0xFA,0xFB,
  28.         0xFC,0xFD,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFD  ,0xFD,0xFC,0xFB,0xF9,0xF8,0xF7,0xF5,0xF3,0xF1,0xEF,0xED,
  29.         0xEA,0xE8,0xE5,0xE2,0xDF  ,0xDC,0xD9,0xD5,0xD2,0xCE,0xCB,0xC7,0xC3,0xBF,0xBB,0xB7,0xB3,0xAE,0xAA,0xA6,0xA1,
  30.         0x9D,0x98,0x94,0x8F,0x8B,0x86,0x81,0x7D,0x78,0x73,0x6F,0x6A,0x66,0x61,0x5D,0x58  ,0x54,0x50,0x4B,0x47,0x43,
  31.         0x3F,0x3B,0x37,0x33,0x30,0x2C,0x29,0x25,0x22,0x1F,0x1C  ,0x19,0x16,0x14,0x11,0x0F,0x0D,0x0B,0x09,0x07,0x06,
  32.         0x05,0x03,0x02,0x01,0x01,0x00  ,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x03,0x04,0x05,0x07,0x08,0x0A,0x0C,
  33.         0x0E,0x10,0x13,0x15,0x18,0x1A,0x1D,0x20,0x24,0x27,0x2A,0x2E,0x31,0x35,0x39,0x3D,0x41,0x45,0x49,0x4D,0x52,0x56,
  34.   0x5A,0x5F,0x63,0x68,0x6D,0x71,0x76,0x7A,0x7F};   
  35. void delay(uchar z)
  36. {   
  37. uchar x,y;   
  38. for(x=z;x>0;x--)     
  39. for(y=110;y>0;y--);
  40. }   
  41. void write_com(uchar com)
  42. {   
  43. rs=0;  
  44. P0=com;   
  45. delay(5);      //?nop????????
  46. ep=1;  
  47. delay(5);  
  48. ep=0;  
  49. }
  50. void write_data(uchar date)
  51. {
  52.         rs=1;
  53.         P0=date;
  54.         delay(5);
  55.         ep=1;
  56.         delay(5);
  57.         ep=0;
  58. }
  59. void init_1602()
  60. {   
  61. rw=0;   
  62. write_com(0x38);  
  63. delay(1);   
  64. write_com(0x0c);
  65. delay(1);   
  66. write_com(0x06);  
  67. delay(1);   
  68. write_com(0x01);  
  69. delay(1);
  70. }   
  71. void display_1602(uchar *v)
  72. {   
  73. uchar i=0;   
  74. while(v[i]!='\0')  
  75. {   
  76. write_data(v[i]);   
  77. i++;   
  78. delay(1);  
  79. }
  80. }   
  81. void dealv(uchar amp)
  82. {   
  83. int i;   
  84. i=amp*10;  
  85. i=i/50;   
  86. vv[0]=i/10+48;  
  87. vv[1]='.';   
  88. vv[2]=i%10+48;  
  89. vv[3]='V';  
  90. vv[4]='\0';
  91. }   
  92. void dealf()
  93. {   
  94. freq=fj[k];   
  95. fv[0]=freq/100+48;  
  96. fv[1]=freq%100/10+48;  
  97. fv[2]=freq%10+48;  
  98. fv[3]='H';  
  99. fv[4]='z';  
  100. fv[5]='\0';   
  101. freq=0;        //????
  102.         }   
  103. void fudu(uchar amp)
  104. {
  105. P1=amp;  
  106. wr1=0;     
  107. delay(1);  
  108. wr1=1;
  109. }   
  110. void keyscan1()
  111. {   
  112. if(gn==0)  
  113. {   
  114. delay(20);   
  115. if(gn==0);   
  116. {   
  117. while(!gn);   
  118. flag^=0x01;   
  119. }  
  120. }     
  121. if(flag)  
  122. {      
  123. dir1=0;   
  124. if(up==0)      
  125. {   
  126. delay(10);   
  127. if(up==0);   
  128. {     
  129. while(!up);     
  130. dir2^=1;     
  131. amp+=5;     
  132. if(amp>250) amp=0;  //??????     
  133. fudu(amp);     
  134. dealv(amp);     
  135. write_com(0x80+10);     
  136. display_1602(vv);   
  137. }      
  138. }  
  139. if(down==0)     
  140. {   
  141. delay(100);   
  142. if(down==0);   
  143. {   
  144. while(!down);   
  145. dir2^=1;     
  146. amp-=5;     
  147. if(amp==0)  amp=250;
  148. fudu(amp);     
  149. dealv(amp);     
  150. write_com(0x80+10);     
  151. display_1602(vv);   
  152. }     
  153. }  
  154. }  
  155. else  
  156. {      
  157. dir1=1;   
  158. if(up==0)     
  159. {   
  160. delay(100);   
  161. if(up==0);   
  162. {     
  163. while(!up);     
  164. dir2^=1;     
  165. k++;     
  166. delay(10);     
  167. if(k>39) k=0; //???????     
  168. dealf();     
  169. write_com(0xc0+10);              
  170. display_1602(fv);   
  171. }      
  172. }  
  173. if(down==0)     
  174. {   
  175. delay(80);   
  176. if(down==0);   
  177. {     
  178. while(!down);     
  179. dir2^=1;   
  180. k--;     
  181. delay(10);    //  
  182. if(k>=250)  k=39;       
  183. dealf();     
  184. write_com(0xc0+10);              
  185. display_1602(fv);   
  186. }      
  187. }  
  188. }        
  189. }
  190. void main()
  191. {   
  192. uchar i;     
  193. fudu(amp);  
  194. init_1602();   
  195. write_com(0x80);  
  196. display_1602(v);  
  197. write_com(0xc0);  
  198. display_1602(f);  
  199. dealv(amp);   
  200. write_com(0x80+10);  
  201. display_1602(vv);  
  202. dealf();   
  203. write_com(0x80+0x40+10);  
  204. display_1602(fv);         
  205. while(1)  
  206. {     
  207. keyscan1();   
  208. for(j=0;j<173;j++)   
  209. {      
  210. P1=sin[j];   
  211. wr2=0;   
  212. for(i=sj[k];i>0;i--);                              //??????????     
  213.         wr2=1;
  214. for(i=sj[k];i>0;i--);
  215. wr2=1;  
  216. }  
  217. }
  218. }
复制代码


所有资料51hei提供下载:
51 正弦波.zip (68.17 KB, 下载次数: 3)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 转播转播 分享分享 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|单片机论坛 |51黑电子论坛单片机.

Powered by 单片机教程网

快速回复 返回顶部 返回列表