找回密码
 立即注册

QQ登录

只需一步,快速开始

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

单片机实现简易波形发生器

[复制链接]
跳转到指定楼层
楼主
单片机实现简易波形发生器


单片机源程序如下:
  1. #include<reg51.h>
  2. #include<absacc.h>
  3. #define DAC0832 XBYTE[0xfeff]
  4. #define uchar unsigned char

  5. uchar  num = 0;
  6. uchar  tl = 0xe0;
  7. uchar  th = 0xff;
  8. uchar  interrupt_flagl = 0x00;

  9. uchar code sin[256] = {  0x7f,0x82,0x85,0x88,0x8b,0x8f,0x92,0x95,0x98,0x9b,0x9e,0xa1,0xa4,0xa7,0xaa,
  10.                                                  0xad,0xb0,0xb3,0xb6,0xb8,0xbb,0xbe,0xc1,0xc3,0xc6,0xc8,0xcb,0xcd,0xd0,
  11.                                                  0xd2,0xd5,0xd7,0xd9,0xdb,0xdd,0xe0,0xe2,0xe4,0xe5,0xe7,0xe9,0xeb,0xec,
  12.                                                  0xee,0xef,0xf1,0xf2,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfb,0xfc,0xfd,0xfd,
  13.                                                  0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfd,0xfd,0xfc,0xfb,0xfb,
  14.                                                  0xfa,0xf9,0xf8,0xf7,0xf6,0xf5,0xf4,0xf2,0xf1,0xef,0xee,0xec,0xeb,0xe9,0xe7,
  15.                                                  0xe5,0xe4,0xe2,0xe0,0xdd,0xdb,0xd9,0xd7,0xd5,0xd2,0xd0,0xcd,0xcb,0xc8,0xc6,0xc3,
  16.                                                  0xc1,0xbe,0xbb,0xb8,0xb6,0xb3,0xb0,0xad,0xaa,0xa7,0xa4,0xa1,0x9e,0x9b,0x98,0x95,
  17.                                                  0x92,0x8f,0x8b,0x88,0x85,0x82,0x7f,0x7c,0x79,0x76,0x73,0x6f,0x6c,0x69,0x66,0x63,
  18.                                                  0x60,0x5d,0x5a,0x57,0x54,0x51,0x4e,0x4b,0x48,0x46,0x43,0x40,0x3d,0x3b,0x38,0x36,
  19.                                                  0x33,0x31,0x2e,0x2c,0x29,0x27,0x25,0x23,0x21,0x1e,0x1c,0x1a,0x19,0x17,0x15,0x13,
  20.                                                  0x12,0x10,0x0f,0x0d,0x0c,0x0a,0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x03,0x02,0x01,0x01,0x00,0x00,
  21.                                                  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x05,
  22.                                                  0x06,0x07,0x08,0x09,0x0a,0x0c,0x0d,0x0f,0x10,0x12,0x13,0x15,0x17,0x19,0x1a,0x1c,
  23.                                                  0x1e,0x21,0x23,0x25,0x27,0x29,0x2c,0x2e,0x31,0x33,0x36,0x38,0x3b,0x3d,0x40,0x43,0x46,
  24.                                                  0x48,0x4b,0x4e,0x51,0x54,0x57,0x5a,0x5d,0x60,0x63,0x66,0x69,0x6c,0x6f,0x73,0x76,
  25.                                                  0x79,0x7c
  26.                                                  
  27.                                         };

  28. void case1(void)
  29. {
  30.         tl -= 0x05;

  31.         if(tl <= 0xb0)
  32.         {
  33.                 tl = 0xe0;
  34.         }
  35. }
  36.          
  37. void case2(void)
  38. {
  39.         tl += 0x05;

  40.         if(tl >= 0xff)
  41.         {
  42.                 tl = 0xe0;
  43.         }
  44. }

  45. void case3(void)
  46. {
  47.           interrupt_flagl= interrupt_flagl + 0x01;

  48.         if(interrupt_flagl > 0x04)
  49.         {
  50.                 interrupt_flagl = 0x01;
  51.         }
  52. }

  53. int0_key() interrupt 0
  54. {
  55.         if(P2 == 0xdf)
  56.         {
  57.                 case1();
  58.         }
  59.         else if(P2 == 0xbf)
  60.         {
  61.                 case2();       
  62.         }
  63.         else if(P2 == 0x7f)
  64.         {
  65.                 case3();
  66.         }
  67. }

  68. timer0_int(void) interrupt 1
  69. {
  70.         TR0 = 0;
  71.         if(interrupt_flagl == 0x01)
  72.         {
  73.                 if(num < 64)
  74.                 {
  75.                         DAC0832 = 0x00;
  76.                 }
  77.                 else if((num >= 64) && (num <= 128))       
  78.                 {
  79.                         DAC0832 = 0x85;
  80.                 }
  81.                 else if((num > 128) &&(num <= 255))
  82.                 {
  83.                         DAC0832 = 0;
  84.                 }
  85.                 else if(num >255)
  86.                 {
  87.                         num =0;
  88.                 }
  89.         }
  90.         else if(interrupt_flagl == 0x02)
  91.         {
  92.           if( num <= 85)
  93.           {
  94.                    DAC0832  = num;
  95.           
  96.           }
  97.           else if(num > 85 && num < 170)
  98.           {
  99.                   DAC0832 = 85;

  100.           }
  101.           else if((num >= 170) &&(num < 255 ))
  102.           {
  103.                  DAC0832 = 255 - num;
  104.           
  105.           }
  106.           else if(num >= 255)
  107.           {
  108.                   num = 0;
  109.           }
  110.         }

  111.         else if(interrupt_flagl == 0x03)
  112.         {
  113.            if( num <= 0x128)
  114.            {
  115.                            DAC0832 = num;
  116.            }
  117.            else if((num >= 0x128) && (num < 0x255))       
  118.            {
  119.                         DAC0832 = 255 - num;
  120.            }
  121.            else if(num >= 255)
  122.            {
  123.                         num = 0;
  124.            }
  125.         }

  126.         else if(interrupt_flagl == 0x04)
  127.         {
  128.        
  129.                         DAC0832 = sin[num];
  130.                
  131.                         if(num > 255)
  132.                         {
  133.                                 num = 0;
  134.                         }
  135.         }
  136.         num++;
  137.         TR0 =1;
  138.         TH0 = th;
  139.         TL0 = tl;
  140. }


  141. void main()
  142. {
  143.         TMOD = 0x01;
  144.         TH0 = 0xff;
  145.         TL0 = 0xe0;
  146.         ET0= 1;
  147.         TR0 = 1;
  148.         TF0= 1;
  149.         PX0 =1;
  150.         EX0 =1;
  151.         EA =1;
  152.                
  153.         while(1)
  154.         {       
  155.          ;
  156.         }

  157. }
复制代码

所有资料51hei提供下载:
简易波形发生器.rar (24.19 KB, 下载次数: 32)


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

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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