找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 706|回复: 1
收起左侧

问一下单片机定时器里面的写的东西,它为什么就能输出一个完整的波形,我看那个c...

[复制链接]
ID:1076450 发表于 2023-5-12 00:11 | 显示全部楼层 |阅读模式
  1. #include "reg52.h"
  2. #include "LCD1602.h"
  3. #include "key.h"


  4. #define DAPort P2
  5. sbit IO = P2^0;
  6. unsigned int count;


  7. static unsigned char code TableSin[256]={
  8. 128,131,134,137,140,143,146,149,152,155,158,162,165,167,170,173,
  9. 176,179,182,185,188,190,193,196,198,201,203,206,208,211,213,215,
  10. 218,220,222,224,226,228,230,232,234,235,237,238,240,241,243,244,
  11. 245,246,248,249,250,250,251,252,253,253,254,254,254,255,255,255,
  12. 255,255,255,255,254,254,254,253,253,252,155,250,250,249,248,246,
  13. 245,244,243,241,240,238,237,235,234,232,230,228,226,224,222,220,
  14. 218,215,213,211,208,206,203,155,198,196,193,190,188,185,182,179,
  15. 176,173,170,167,165,162,158,155,152,149,146,143,140,137,134,131,
  16. 127,124,121,118,115,112,109,106,103,100, 97, 93, 90, 88, 85, 82,
  17. 79, 76, 73, 70, 67, 65, 62, 59, 57, 54, 52, 49, 47, 44, 42, 40,
  18. 37, 35, 33, 31, 29, 27, 25, 23, 21, 20, 18, 17, 15, 14, 12, 11,
  19. 10,  9,  7,  6,  5,  5,  4,  3,  2,  2,  1,  1,  1,  0,  0,  0,
  20.   0,  0,  0,  0,  1,  1,  1,  2,  2,  3,  4,  5,  5,  6,  7,  9,
  21. 10, 11, 12, 14, 15, 17, 18, 20, 21, 23, 25, 27, 29, 31, 33, 35,
  22. 37, 40, 42, 44, 47, 49, 52, 54, 57, 59, 62, 65, 67, 70, 73, 76,
  23. 79, 82, 85, 88, 90, 93, 97,100,103,106,109,112,115,118,121,124
  24. };

  25. static unsigned char code TableTriangle[256]={
  26. 128,130,132,134,136,138,140,142,144,146,148,150,152,154,156,158,
  27. 160,162,164,166,168,170,172,174,176,178,180,182,184,186,188,190,
  28. 192,194,196,198,200,202,204,206,208,210,212,214,216,218,220,222,
  29. 224,226,228,230,232,234,236,238,240,242,244,246,248,250,252,254,
  30. 255,254,252,250,248,246,244,242,240,238,236,234,232,230,228,226,
  31. 224,222,220,218,216,214,212,210,208,206,204,202,200,198,196,194,
  32. 192,190,188,186,184,182,180,178,155,174,172,170,168,166,164,162,
  33. 160,158,156,154,152,150,148,146,144,142,140,138,136,134,132,130,
  34. 128,126,124,122,120,118,116,114,112,110,108,106,104,102,100, 98,
  35. 96, 94, 92, 90, 88, 86, 84, 82, 80, 78, 76, 74, 72, 70, 68, 66,
  36. 64, 62, 60, 58, 56, 54, 52, 50, 48, 46, 44, 42, 40, 38, 36, 34,
  37. 32, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10,  8,  6,  4,  2,
  38.   0,  2,  4,  6,  8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30,
  39. 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62,
  40. 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94,
  41. 96, 98,100,102,104,106,108,110,112,114,116,118,120,122,124,126
  42. };

  43. static unsigned char code TableSaw[256]={
  44.   0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
  45. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  46. 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  47. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  48. 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  49. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
  50. 96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111,
  51. 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,
  52. 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
  53. 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
  54. 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
  55. 176,177,178,179,180,181,182,183,155,185,186,187,188,189,190,191,
  56. 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
  57. 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
  58. 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
  59. 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255
  60. };

  61. void main(void)
  62. {
  63.         unsigned char i;
  64.         fword = (unsigned int)SaveTable[0]*10000+(unsigned int)SaveTable[1]*1000+(unsigned int)SaveTable[2]*100+(unsigned int)SaveTable[3]*10+(unsigned int)SaveTable[4];
  65.         fword = (unsigned int)(fword*1.048);
  66.         for(i=0;i<5;i++)
  67.                 FreqNum[i] = SaveTable[i];
  68.         AmPort = (unsigned char)(((unsigned char )SaveTable[5] *10 +(unsigned char )SaveTable[6])*5.1);        
  69.         AmpNum[0] = SaveTable[5];AmpNum[1] = SaveTable[6];
  70.         Duty = (unsigned char)SaveTable[7]*10   +(unsigned char)SaveTable[8];        
  71.         LCDInit();
  72.         TMOD= 0x02;               
  73.         TH0 = 0xf0;
  74.     TL0 = 0xf0;
  75.         TR0=1;                    
  76.         ET0=1;                    
  77.         switch(gType)
  78.         {
  79.                 case 0: LCDDispString(5,1,"Sin");break;
  80.                 case 1: LCDDispString(5,1,"Squ");break;
  81.                 case 2: LCDDispString(5,1,"Tri");break;
  82.                 case 3: LCDDispString(5,1,"Saw");break;
  83.         }
  84.         LCDDispNum(12,1,SaveTable[5]);LCDDispNum(14,1,SaveTable[6]);
  85.         for(i=0;i<5;i++)
  86.                 LCDDispNum(3+i,2,SaveTable[i]);
  87.         if(gType == 1)
  88.         {
  89.                 LCDDispNum(13,2,SaveTable[7]);
  90.                 LCDDispNum(14,2,SaveTable[8]);
  91.         }
  92.         else
  93.         {
  94.                 LCDDispChar(13,2,'-');
  95.                 LCDDispChar(14,2,'-');
  96.         }

  97.         EA=1;                     
  98.          
  99.         while(1)
  100.         {
  101.                 key_scan();
  102.         }
  103. }      




  104. void timer0(void) interrupt 1
  105. {
  106.         count = count + fword;
  107.         switch(gType)
  108.         {
  109.                 case 0:        DAPort = TableSin[count>>8];
  110.                                 break;         
  111.                 case 1: if(count>Duty*655)
  112.                                         DAPort = 0xff;
  113.                                 else
  114.                                         DAPort = 0x00;
  115.                                 break;
  116.                 case 2:        DAPort = TableTriangle[count>>8];
  117.                                 break;
  118.                 case 3:        DAPort = TableSaw[256-(count>>8)];
  119.                                 break;
  120.                 default:DAPort = TableSin[count>>8];
  121.         }
  122.                                 
  123. }        
复制代码


回复

使用道具 举报

ID:94031 发表于 2023-5-12 06:32 | 显示全部楼层
把显示器坐标搞清楚,把要显示图形坐标搞清楚,用程序写入0和1.
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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