找回密码
 立即注册

QQ登录

只需一步,快速开始

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

单片机波形发生器的仿真制作

[复制链接]
跳转到指定楼层
楼主
仿真图:



单片机代码:
  1. #include<reg51.h>
  2. sbit wr=P3^6;
  3. sbit rd=P3^2;
  4. sbit key0=P1^0;
  5. sbit key1=P1^1;
  6. sbit key2=P1^2;
  7. sbit key3=P1^3;
  8. sbit key4=P1^4;
  9. unsigned char flag;
  10. unsigned char const code
  11. SIN_code[256]={        
  12. 0x80,0x83,0x86,0x89,0x8d,0x90,0x93,0x96,0x99,0x9c,0x9f,0xa2,0xa5,0xa8,0xab,0xae,0xb1,0xb4,0xb7,0xba,0xbc,0xbf,0xc2,0xc5,
  13. 0xc7,0xca,0xcc,0xcf,0xd1,0xd4,0xd6,0xd8,0xda,0xdd,0xdf,0xe1,0xe3,0xe5,0xe7,0xe9,0xea,0xec,0xee,0xef,0xf1,0xf2,0xf4,0xf5,
  14. 0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfd,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfd,
  15. 0xfd,0xfc,0xfb,0xfa,0xf9,0xf8,0xf7,0xf6,0xf5,0xf4,0xf2,0xf1,0xef,0xee,0xec,0xea,0xe9,0xe7,0xe5,0xe3,0xe1,0xde,0xdd,0xda,
  16. 0xd8,0xd6,0xd4,0xd1,0xcf,0xcc,0xca,0xc7,0xc5,0xc2,0xbf,0xbc,0xba,0xb7,0xb4,0xb1,0xae,0xab,0xa8,0xa5,0xa2,0x9f,0x9c,0x99,
  17. 0x96,0x93,0x90,0x8d,0x89,0x86,0x83,0x80,0x80,0x7c,0x79,0x76,0x72,0x6f,0x6c,0x69,0x66,0x63,0x60,0x5d,0x5a,0x57,0x55,0x51,
  18. 0x4e,0x4c,0x48,0x45,0x43,0x40,0x3d,0x3a,0x38,0x35,0x33,0x30,0x2e,0x2b,0x29,0x27,0x25,0x22,0x20,0x1e,0x1c,0x1a,0x18,0x16,
  19. 0x15,0x13,0x11,0x10,0x0e,0x0d,0x0b,0x0a,0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x02,0x02,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
  20. 0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0d,0x0e,0x10,0x11,0x13,0x15,
  21. 0x16,0x18,0x1a,0x1c,0x1e,0x20,0x22,0x25,0x27,0x29,0x2b,0x2e,0x30,0x33,0x35,0x38,0x3a,0x3d,0x40,0x43,0x45,0x48,0x4c,0x4e,
  22. 0x51,0x55,0x57,0x5a,0x5d,0x60,0x63,0x66,0x69,0x6c,0x6f,0x72,0x76,0x79,0x7c,0x80};
  23. unsigned char keyscan()
  24. {
  25.       unsigned char keyscan_num,temp;
  26.           P1=0xff;
  27.           temp=P1;
  28.           if(~(temp&0xff))
  29.           {
  30.              if(key0==0)
  31.                  {
  32.                    keyscan_num=1;
  33.                 }
  34.                 else if(key1==0)
  35.                 {
  36.                    keyscan_num=2;
  37.                 }
  38.                 else if(key2==0)
  39.                 {
  40.                    keyscan_num=3;
  41.                 }
  42.                 else if(key3==0)
  43.                 {
  44.                    keyscan_num=4;
  45.                 }
  46.                 else if(key4==0)
  47.                 {
  48.                    keyscan_num=5;
  49.                 }
  50.                 else
  51.                 {
  52.                     keyscan_num=0;
  53.                 }
  54.                 return keyscan_num;
  55.                 }
  56.         }
  57. void init_DA0832()
  58. {
  59.    rd=0;
  60.    wr=0;
  61. }
  62. void SIN()
  63. {
  64.    unsigned int i;
  65.    do{
  66.        P2=SIN_code[i];
  67.            i=i+1;
  68.            }while(i<256);
  69. }
  70. void Square()
  71. {
  72.    EA=1;
  73.    ET0=1;
  74.    TMOD=1;
  75.    TH0=0xff;
  76.    TL0=0x83;
  77.    TR0=1;
  78. }
  79. void Triangle ()
  80. {
  81.    P2=0x00;
  82.    do{
  83.          P2=P2+1;
  84.          }while(P2<0xff);
  85.          P2=0xff;
  86.    do{
  87.          P2=P2-1;
  88.          }while(P2>0x00);
  89.          P2=0x00;
  90. }
  91. void Sawtooth ()
  92. {
  93.      P2=0x00;
  94.          do{
  95.               P2=P2+1;
  96.                 }while(P2<0xff);
  97. }
  98. void Trapezoidal ()
  99. {
  100.     unsigned char i;
  101.         P2=0x00;
  102.         do{
  103.             P2=P2+1;
  104.            }while(P2<0xff);
  105.            P2=0xff;
  106.            for(i=255;i>0;i--)
  107.            {
  108.               P2=0xff;
  109.                 }
  110.         do{
  111.             P2=P2-1;
  112.                 }while(P2>0x00);
  113.                 P2=0x00;
  114. }
  115. void main()
  116. {
  117.    init_DA0832();
  118.    do
  119.    {
  120.         flag=keyscan();
  121.         }while(!flag);
  122.         while(1)
  123.         {
  124.             switch(flag)
  125.                 {
  126.                   case 1:
  127.                   do{
  128.                        flag=keyscan();
  129.                            SIN();
  130.                          }
  131.                          while(flag==1);
  132.                          break;
  133.                   case 2:
  134.                   Square ();
  135.                   do{
  136.                        flag=keyscan();
  137.                          }while(flag==2);
  138.                      TR0=0;
  139.                          break;
  140.                   case 3:
  141.                   do{
  142.                        flag=keyscan();
  143.                            Triangle ();
  144.                          }while(flag==3);
  145.                          break;
  146.                   case 4:
  147.                   do{
  148.                         flag=keyscan();
  149.                                 Trapezoidal ();
  150.                         }while(flag==4);
  151.                         break;
  152.                   case 5:
  153.                   do{
  154.                         flag=keyscan();
  155.                                 Sawtooth ();
  156.                         }while(flag==5);
  157.                         break;
  158.                         default:
  159.                         flag=keyscan();
  160.                         break;
  161.                 }
  162.         }
  163. }
  164. void timer0(void) interrupt 1
  165. {
  166.    P2=~P2;
  167.    TH0=0xff;
  168.    TL0=0x83;
  169.    TR0=1;
  170. }           
复制代码
全部资料51hei下载地址:
例题11-2.zip (83.03 KB, 下载次数: 34)


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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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