找回密码
 立即注册

QQ登录

只需一步,快速开始

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

基于msp430的简易电子示波器

[复制链接]
跳转到指定楼层
楼主
求大神分享一个完整的简易电子示波器项目,
顺便求大神帮看一下自己写的示波器程序是哪儿出来问题,编译没有问题,应该是逻辑上的错误,12864连提示词都不能显示 ,现在只求出来一个波形,细节问题再调

所有资料下载:
示波器程序.7z (34.2 KB, 下载次数: 22)

单片机主程序:
  1. #include<msp430f169.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4. #define uchar unsigned char
  5. #define uint  unsigned int
  6. #define Num_of_Results 100
  7. static uint results[Num_of_Results];
  8. uchar index=0;
  9. #define S1 0
  10. #define S2 1
  11. #define S3 2
  12. #define S4 3



  13. static unsigned char font3x6[] = {
  14. 0x00,0x00,0x00,            //space
  15. 0x00,0x04,0x00,            //.
  16. 0x20,0x10,0x08,            ///
  17. 0x7c,0x44,0x7c,            //0
  18. 0x00,0x7C,0x00,            //1
  19. 0x5C,0x54,0x74,            //2
  20. 0x54,0x54,0x7C,            //3
  21. 0x70,0x10,0x7C,            //4
  22. 0x74,0x54,0x5C,            //5  
  23. 0x7C,0x54,0x5C,            //6
  24. 0x40,0x40,0x7C,            //7
  25. 0x7C,0x54,0x7C,            //8
  26. 0x74,0x54,0x7C,            //9
  27. };

  28. const unsigned char tab[]=
  29. {

  30. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF8,0x00,0x00,0x00,
  31. 0x80,0x40,0x10,0x04,0x01,0x00,0x40,0x10,0x04,0x01,0x00,0x40,0x08,0x00,0x00,0x00,
  32. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,
  33. 0x80,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x08,0xE3,0xB8,0x55,
  34. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0xA2,0x20,0x45,
  35. 0x80,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x08,0xA3,0xB8,0xD5,
  36. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0xA0,0x89,0x55,
  37. 0x80,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x08,0xEB,0xB8,0xD2,
  38. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,
  39. 0x80,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,
  40. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,
  41. 0xC0,0x40,0x10,0x04,0x01,0x00,0x40,0x10,0x04,0x01,0x00,0x40,0x18,0x00,0x05,0x25,
  42. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x07,0x55,
  43. 0x80,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x05,0x72,
  44. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x05,0x55,
  45. 0x80,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x05,0x55,
  46. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,
  47. 0x80,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,
  48. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x01,0x57,
  49. 0x80,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x01,0xC5,
  50. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x01,0x55,
  51. 0xC0,0x40,0x10,0x04,0x01,0x00,0x40,0x10,0x04,0x01,0x00,0x40,0x18,0x00,0x01,0x55,
  52. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x01,0x55,
  53. 0x80,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,
  54. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,
  55. 0x80,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x05,0x66,
  56. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x05,0x55,
  57. 0x80,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x05,0x66,
  58. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x05,0x44,
  59. 0x80,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x02,0x44,
  60. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,
  61. 0xD5,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x58,0x00,0x00,0x00,
  62. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,
  63. 0x80,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,
  64. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,
  65. 0x80,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x09,0x00,0x00,0x00,
  66. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0A,0xA0,0x00,0x00,
  67. 0x80,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x08,0x40,0x00,0x00,
  68. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,
  69. 0x80,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,
  70. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,
  71. 0xC0,0x40,0x10,0x04,0x01,0x00,0x40,0x10,0x04,0x01,0x00,0x40,0x18,0x00,0x00,0x00,
  72. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,
  73. 0x80,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x0A,0xA0,0x00,0x00,
  74. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,
  75. 0x80,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x0B,0xE0,0x00,0x00,
  76. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,
  77. 0x80,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,
  78. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,
  79. 0x80,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,
  80. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09,0xD9,0xCC,0x00,
  81. 0xC0,0x40,0x10,0x04,0x01,0x00,0x40,0x10,0x04,0x01,0x00,0x40,0x19,0x15,0x12,0x00,
  82. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09,0x99,0x92,0x00,
  83. 0x80,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x09,0x15,0x12,0x00,
  84. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09,0x15,0xCE,0x00,
  85. 0x80,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,
  86. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,
  87. 0x80,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,
  88. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x01,0x40,
  89. 0x80,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x01,0x5C,
  90. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x01,0xC4,
  91. 0x80,0x40,0x10,0x04,0x01,0x00,0x40,0x10,0x04,0x01,0x00,0x40,0x08,0x00,0x01,0x48,
  92. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x01,0x5C,
  93. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF8,0x00,0x00,0x00,
  94. };
  95. /**************************************************
  96. ****************lcd12864管脚定义*******************
  97. **************************************************/
  98. #define LCD_DataIn  P4DIR=0X00
  99. #define LCD_DataOut  P4DIR=0Xff
  100. #define LCD2MCU_Data  P4IN
  101. #define MCU2LCD_Data  P4OUT
  102. #define LCD_RS_H P5OUT &=~BIT5
  103. #define LCD_RS_L P5OUT |= BIT5
  104. #define LCD_RW_H P5OUT &=~BIT6
  105. #define LCD_RW_L P5OUT |= BIT6
  106. #define LCD_EN_H P5OUT &=~BIT7
  107. #define LCD_EN_L P5OUT |= BIT7
  108. #define LCD_PSB_H P5OUT &=~BIT0
  109. #define LCD_PSB_L P5OUT |= BIT0
  110. #define LCD_RST_H P5OUT &=~BIT1
  111. #define LCD_RST_L P5OUT |= BIT1

  112. /**************************************************
  113. 函数名称:Delay_1ms
  114. 功    能:延时1ms
  115. 参    数:无
  116. 返回值  :无
  117. **************************************************/
  118. void Delay_1ms(void)
  119. {
  120.   uchar i;
  121.   for(i=150;i>0;i--) _NOP();
  122. }

  123. void Delay_Nms(uint n)
  124. {
  125.   uint i;
  126.   for(i=n;i>0;i--) Delay_1ms();
  127. }
  128. /**************************************************
  129. 函数名称:Write_Cmd
  130. 功    能:向液晶中写控制命令
  131. 参    数:cmd--控制命令
  132. 返回值  :无
  133. **************************************************/
  134. void Write_Cmd(uchar cmd)
  135. {
  136.   uchar lcdtemp = 0;
  137.   LCD_RS_L;
  138.   LCD_RW_H;
  139.   LCD_DataIn;
  140.   do
  141.   {
  142.     LCD_EN_H;
  143.     _NOP();
  144.     lcdtemp = LCD2MCU_Data;
  145.     LCD_EN_L;
  146.   }
  147.   while(lcdtemp&0x80);
  148.   
  149.   LCD_DataOut;
  150.   LCD_RS_L;
  151.   LCD_RW_L;
  152.   MCU2LCD_Data = cmd;
  153.   
  154.   LCD_EN_H;
  155.   _NOP();
  156.   LCD_EN_L;
  157. }

  158. /**************************************************
  159. 函数名称:Write_Data
  160. 功    能:向液晶中写数据
  161. 参    数:dat--显示数据
  162. 返回值  :无
  163. **************************************************/
  164. void Write_Data(uchar dat)
  165. {
  166.    uchar lcdtemp;
  167.    LCD_RS_L;
  168.    LCD_RW_H;
  169.    LCD_DataIn;
  170.    do
  171.    {
  172.      LCD_EN_H;
  173.      _NOP();
  174.      lcdtemp = LCD2MCU_Data;
  175.      LCD_EN_L;
  176.    }
  177.    while(lcdtemp&0x80);
  178.    
  179.    LCD_RS_H;
  180.    LCD_RW_L;
  181.    LCD_DataOut;
  182.    MCU2LCD_Data = dat;
  183.    LCD_EN_H;
  184.    _NOP();
  185.    LCD_EN_L;
  186. }

  187. /**************************************************
  188. 函数名称:Ini_lcd
  189. 功    能:初始化液晶模块
  190. 参    数:无
  191. 返回值  :无
  192. **************************************************/
  193. void Ini_lcd(void)
  194. {   
  195.    // Delay_Nms(500);
  196.     Write_Cmd(0x30);   //基本指令集
  197.     Delay_1ms();
  198.     Write_Cmd(0x02);   // 地址归位
  199.     Delay_1ms();
  200.     Write_Cmd(0x0c);   //整体显示打开,游标关闭
  201.     Delay_1ms();
  202.     Write_Cmd(0x01);   //清除显示
  203.     Delay_1ms();
  204.     Write_Cmd(0x06);   //游标右移
  205.     Delay_1ms();
  206.     Write_Cmd(0x80);   //设定显示的起始地址
  207. }
  208. /**************************************************
  209. 函数名称:Clear_GDRAM
  210. 功    能:清除液晶GDRAM中的随机数据
  211. 参    数:无
  212. 返回值  :无
  213. **************************************************/

  214. void Clear_GDRAM(void)
  215. {
  216.     uchar i,j,k;
  217.    
  218.         Write_Cmd(0x34);        //打开扩展指令集
  219.         i = 0x80;            
  220.         for(j = 0;j < 32;j++)
  221.         {
  222.         Write_Cmd(i++);
  223.         Write_Cmd(0x80);
  224.                   for(k = 0;k < 16;k++)
  225.                   {
  226.                       Write_Data(0x00);
  227.                   }
  228.         }
  229.         i = 0x80;
  230.          for(j = 0;j < 32;j++)
  231.         {
  232.              Write_Cmd(i++);
  233.         Write_Cmd(0x88);           
  234.                   for(k = 0;k < 16;k++)
  235.                   {
  236.                        Write_Data(0x00);
  237.                    }
  238.         }   
  239.         Write_Cmd(0x30);        //回到基本指令集
  240. }
  241. /**************************************************
  242. 函数名称:Lcd_ReadData
  243. 功    能:读取12864中一个字节的数据
  244. 参    数:无
  245. 返回值  :显示的数据
  246. **************************************************/
  247. uchar Lcd_ReadData()
  248. {
  249.   unsigned char Data_Temp;
  250.    uchar lcdtemp;
  251.    LCD_RS_L;
  252.    LCD_RW_H;
  253.    LCD_DataIn;
  254.    do
  255.    {
  256.      LCD_EN_H;
  257.      _NOP();
  258.      lcdtemp = LCD2MCU_Data;
  259.      LCD_EN_L;
  260.    }
  261.    while(lcdtemp&0x80);
  262.    
  263.   LCD_RS_H;
  264.   LCD_RW_H;
  265.   LCD_DataIn;
  266.   
  267.   LCD_EN_H;
  268.   _NOP();
  269.   Data_Temp = LCD2MCU_Data;
  270.   LCD_EN_L;
  271.   
  272.   return Data_Temp;
  273. }
  274. /**************************************************
  275. 函数名称:LCD_Set_Dot
  276. 功    能:任意位置画点,但覆盖原来的数据
  277. 参    数:X--横坐标
  278.           Y--纵坐标
  279. 返回值  :无
  280. **************************************************/

  281. void LCD_Set_Dot(uchar X,uchar Y)
  282. {
  283.   uchar X_byte,X_bit;
  284.   uchar Y_byte,Y_bit;
  285.   X_byte = X<<4;
  286.   X_bit  = X%16;
  287.   
  288.   Y_byte = Y<<5;  
  289.   Y_bit  = Y%32;
  290.   
  291.   Write_Cmd(0x34);
  292.   Write_Cmd(0x80 + Y_bit);
  293.   Write_Cmd(0x80 + X_byte + (Y_byte<<3));
  294.   
  295.   if(X_bit < 8)
  296.   {
  297.     Write_Data(0x01<<(7 - X_bit)) ;
  298.     Write_Data(0x00);
  299.   }
  300.   else
  301.   {
  302.     Write_Data(0x00);
  303.     Write_Data(0x01<<(15 - X_bit));
  304.   }
  305.   Write_Cmd(0x36);
  306. //  Write_Cmd(0x30);
  307. }
  308. /**************************************************
  309. 函数名称:LCD_Set_Dot1
  310. 功    能:任意位置画点,不覆盖原来的数据
  311. 参    数:X--横坐标
  312.           Y--纵坐标
  313. 返回值  :无
  314. **************************************************/
  315. void LCD_Set_Dot1(uchar X,uchar Y)
  316. {
  317.   uchar X_byte,X_bit,Oldbyte_H;
  318.   uchar Y_byte,Y_bit,Oldbyte_L;
  319.   X_byte = X/16;
  320.   X_bit  = X%16;
  321.   
  322.   Y_byte = Y/32;  
  323.   Y_bit  = Y%32;
  324.   
  325. // Write_Cmd(0x34);
  326.   Write_Cmd(0x80 + Y_bit);
  327.   Write_Cmd(0x80 + X_byte + (Y_byte<<3));
  328.   
  329.   Lcd_ReadData();                 //没有这一条指令出错~!
  330.   Oldbyte_H = Lcd_ReadData();
  331.   Oldbyte_L = Lcd_ReadData();
  332.   
  333. //  Write_Cmd(0x34);
  334.   Write_Cmd(0x80 + Y_bit);
  335.   Write_Cmd(0x80 + X_byte + (Y_byte<<3));
  336.   if(X_bit < 8)
  337.   {
  338.     Write_Data((Oldbyte_H) | (0x01<<(7 - X_bit))) ;
  339.     Write_Data(Oldbyte_L);
  340.   }
  341.   else
  342.   {
  343.     Write_Data(Oldbyte_H);
  344.     Write_Data((Oldbyte_L) | (0x01<<(15 - X_bit)));
  345.   }
  346.   Write_Cmd(0x36);
  347. //  Write_Cmd(0x30);
  348. }
  349. /**************************************************
  350. 函数名称:LCD_ReSet_Dot1
  351. 功    能:任意位置点复位,不覆盖原来的数据
  352. 参    数:X--横坐标
  353.           Y--纵坐标
  354. 返回值  :无
  355. **************************************************/


  356. void LCD_ReSet_Dot1(uchar X,uchar Y)
  357. {
  358.   uchar X_byte,X_bit,Oldbyte_H;
  359.   uchar Y_byte,Y_bit,Oldbyte_L;
  360.   X_byte = X/16;
  361.   X_bit  = X%16;
  362.   
  363.   Y_byte = Y/32;  
  364.   Y_bit  = Y%32;
  365.   
  366. //  Write_Cmd(0x34);
  367.   Write_Cmd(0x80 + Y_bit);
  368.   Write_Cmd(0x80 + X_byte + (Y_byte<<3));
  369.   Lcd_ReadData();                 //没有这一条指令出错~!
  370.   Oldbyte_H = Lcd_ReadData();
  371.   Oldbyte_L = Lcd_ReadData();
  372.   
  373. //  Write_Cmd(0x34);
  374.   Write_Cmd(0x80 + Y_bit);
  375.   Write_Cmd(0x80 + X_byte + (Y_byte<<3));
  376.   if(X_bit < 8)
  377.   {
  378.     Write_Data((Oldbyte_H) & (~(0x01<<(7 - X_bit)))) ;
  379.     Write_Data(Oldbyte_L);
  380.   }
  381.   else
  382.   {
  383.     Write_Data(Oldbyte_H);
  384.     Write_Data((Oldbyte_L) & (~(0x01<<(15 - X_bit))));
  385.   }
  386.   Write_Cmd(0x36);
  387. //  Write_Cmd(0x30);
  388. }
  389. /**************************************************
  390. 函数名称:Lcd_DrawLine_X
  391. 功    能:画横线
  392. 参    数:x_start--起始地址x坐标
  393.           x_end  --结束地址x坐标
  394.           y      --y坐标
  395. 返回值  :无
  396. **************************************************/
  397. void Lcd_DrawLine_X(uchar x_start,uchar x_end,uchar y)
  398. {
  399.   uchar temp;
  400.   if(x_start > x_end)
  401.   {
  402.     temp = x_start;
  403.     x_start = x_end;
  404.     x_end = temp;
  405.   }
  406.   do
  407.   {
  408.     LCD_Set_Dot1(x_start,y);
  409.     x_start++;
  410.   }
  411.   while(x_start <= x_end);
  412. }

  413. /**************************************************
  414. 函数名称:Lcd_DrawLine_Y
  415. 功    能:画竖线
  416. 参    数:y_start--起始地址y坐标
  417.           y_end  --结束地址y坐标
  418.           x      --x坐标
  419. 返回值  :无
  420. **************************************************/
  421. void Lcd_DrawLine_Y(uchar y_start,uchar y_end,uchar x)
  422. {
  423.   uchar temp;
  424.   if(y_start > y_end)
  425.   {
  426.     temp = y_start;
  427.     y_start = y_end;
  428.     y_end = temp;
  429.   }
  430.   do
  431.   {
  432.     LCD_Set_Dot1(x,y_start);
  433.     y_start++;
  434.   }
  435.   while(y_start <= y_end);
  436. }

  437. /**************************************************
  438. 函数名称:Lcd_ReDrawLine_Y
  439. 功    能:取消画的竖线
  440. 参    数:y_start--起始地址y坐标
  441.           y_end  --结束地址y坐标
  442.           x      --x坐标
  443. 返回值  :无
  444. **************************************************/
  445. void Lcd_ReDrawLine_Y(uchar y_start,uchar y_end,uchar x)
  446. {
  447.   uchar temp;
  448.   if(y_start > y_end)
  449.   {
  450.     temp = y_start;
  451.     y_start = y_end;
  452.     y_end = temp;
  453.   }
  454.   do
  455.   {
  456.     LCD_ReSet_Dot1(x,y_start);
  457.     y_start++;
  458.   }
  459.   while(y_start <= y_end);
  460. }
  461. /**************************************************
  462. 函数名称:Lcd_DrawLine
  463. 功    能:画任意直线
  464. 参    数:x_start--起始地址x坐标
  465.           x_end  --结束地址x坐标
  466.           y_start--起始地址y坐标
  467.           y_end  --结束地址y坐标
  468. 返回值  :无
  469. **************************************************/
  470. void DrawLine(uchar x_start,uchar y_start,uchar x_end,uchar y_end)
  471. {
  472.   int range_x,range_y;
  473.   int flagx,flagy;
  474.   uchar distance,t;
  475.   uchar xt,yt;
  476.   range_x = x_end - x_start;
  477.   range_y = y_end - y_start;
  478.   
  479.   if(range_x > 0) flagx = 1;
  480.   else
  481.     if(range_x == 0)
  482.     {
  483.       Lcd_DrawLine_Y(y_start,y_end,x_start);
  484.       return;
  485.     }
  486.   else
  487.   {
  488.     flagx  = -1;
  489.   }
  490. /************Y轴****************/
  491.   if(range_y > 0) flagy = 1;
  492.   else
  493.     if(range_y == 0)
  494.     {
  495.       Lcd_DrawLine_X(x_start,x_end,y_start);
  496.       return;
  497.     }
  498.   else
  499.   {
  500.     flagy  = -1;
  501.   }
  502.   
  503.   range_x = abs(range_x);
  504.   range_y = abs(range_y);
  505.   if(range_x > range_y) distance = range_x;
  506.   else
  507.     distance = range_y;
  508.   
  509.   
  510. /************Bresenham 画线算法****************/
  511.   for(t=0;t<distance+1;t++)
  512.   {
  513.      LCD_Set_Dot1(x_start,y_start);
  514.      xt = xt + range_x;
  515.      yt = yt + range_y;
  516.      if(xt>distance)
  517.      {
  518.         xt = xt - distance;
  519.         x_start = x_start + flagx;
  520.      }
  521.      if(yt > distance)
  522.      {
  523.         yt = yt - distance;
  524.         y_start = y_start + flagy;
  525.      }
  526.   }
  527. }
  528. /*************************************************
  529. 函数名称:Draw_TX
  530. 功    能:画圆
  531. 参    数:Yaddr--Y地址
  532.           Xaddr--Y地址
  533.           dp   --指向图形数据存放地址的指针
  534. 返回值  :无
  535. **************************************************/
  536. void Lcd_DrawCircle(uchar x,uchar y,uchar r)
  537. {
  538.   uchar a,b;
  539.   float c;
  540.   a = 0;
  541.   b = r;
  542. //  c = 1.25 - r;
  543.   c = 3 - 2*r;
  544.   while(a < b)
  545.   {
  546.     LCD_Set_Dot1(x+a,y+b);
  547.     LCD_Set_Dot1(x-a,y+b);
  548.     LCD_Set_Dot1(x+a,y-b);
  549.     LCD_Set_Dot1(x-a,y-b);
  550.    
  551.     LCD_Set_Dot1(x+b,y+a);
  552.     LCD_Set_Dot1(x-b,y+a);
  553.     LCD_Set_Dot1(x+b,y-a);
  554.     LCD_Set_Dot1(x-b,y-a);
  555.    
  556.     if(c < 0)
  557.     {
  558.       c = c+4*a + 6;
  559.     }
  560.     else
  561.     {
  562.       c= c + 4*(a - b) + 10;
  563.       b-=1;
  564.     }
  565.     a = a + 1;
  566.   }
  567.   if(a == b)
  568.   {
  569.     LCD_Set_Dot1(x+a,y+b);
  570.     LCD_Set_Dot1(x-a,y+b);
  571.     LCD_Set_Dot1(x+a,y-b);
  572.     LCD_Set_Dot1(x-a,y+b);
  573.    
  574.     LCD_Set_Dot1(x+b,y+a);
  575.     LCD_Set_Dot1(x-b,y+a);
  576.     LCD_Set_Dot1(x+b,y-a);
  577.     LCD_Set_Dot1(x-b,y-a);   
  578.   }
  579. }
  580. /**************************************************
  581. 函数名称:Disp_HZ
  582. 功    能:显示汉字
  583. 参    数:addr--指定行
  584.           *pt--指向数据的指针
  585.           num--汉字的个数
  586. 返回值  :无
  587. **************************************************/
  588. void DisplayCgrom(uchar addr,uchar *hz)
  589. {
  590.   Write_Cmd(addr);
  591.    Delay_Nms(2);
  592.   while(*hz != '\0')  
  593.   {
  594.     Write_Data(*hz);
  595.     hz++;
  596.      Delay_Nms(2);
  597.   }

  598. }
  599. /*******************************************
  600. 函数名称:Draw_PM
  601. 功    能:在整个液晶屏幕上画图
  602. 参    数:无
  603. 返回值  :无
  604. ********************************************/
  605. void Draw_PM(const uchar *ptr)
  606. {
  607.     uchar i,j,k;
  608.    
  609.         Write_Cmd(0x34);        //打开扩展指令集
  610.         i = 0x80;            
  611.         for(j = 0;j < 32;j++)
  612.         {
  613.         Write_Cmd(i++);
  614.         Write_Cmd(0x80);
  615.                   for(k = 0;k < 16;k++)
  616.                   {
  617.                       Write_Data(*ptr++);
  618.                   }
  619.         }
  620.         i = 0x80;
  621.          for(j = 0;j < 32;j++)
  622.         {
  623.              Write_Cmd(i++);
  624.         Write_Cmd(0x88);           
  625.                   for(k = 0;k < 16;k++)
  626.                   {
  627.                        Write_Data(*ptr++);
  628.                    }
  629.         }  
  630.     Write_Cmd(0x36);        //打开绘图显示
  631.         Write_Cmd(0x30);        //回到基本指令集
  632. }
  633. /**************************************************
  634. 函数名称:Draw_TX
  635. 功    能:显示一个16X16的图形
  636. 参    数:Yaddr--Y地址
  637.           Xaddr--Y地址
  638.           dp   --指向图形数据存放地址的指针
  639. 返回值  :无
  640. **************************************************/
  641. void Draw_TX(uchar Yaddr,uchar Xaddr,const uchar *dp)
  642. {
  643.   uchar j;
  644.   uchar k=0;
  645.   Write_Cmd(0x34);
  646.   for(j=0;j<16;j++)
  647.   {
  648.     Write_Cmd(Yaddr++);
  649.     Write_Cmd(Xaddr);
  650.     Write_Data(dp[k++]);
  651.     Write_Data(dp[k++]);
  652.   }
  653.   Write_Cmd(0x36);
  654. }

  655. /**************************************************
  656. 函数名称:Draw_Font
  657. 功    能:显示一个3X8的图形
  658. 参    数:Yaddr--Y地址
  659.           Xaddr--Y地址
  660.           dp   --指向图形数据存放地址的指针
  661. 返回值  :无
  662. **************************************************/
  663. void Draw_Font(uchar Yaddr,uchar Xaddr,uchar *dp)
  664. {
  665.   uchar j,i;
  666. //  uchar k=0;
  667. //  Write_Cmd(0x34);
  668.   for(i=0;i<3;i++)
  669.   {
  670.     for(j=0;j<8;j++)
  671.     {
  672.       if( (0x80>>j) & (*dp) )
  673.         LCD_Set_Dot1(Xaddr+i,Yaddr+j);
  674.       else
  675.         LCD_ReSet_Dot1(Xaddr+i,Yaddr+j);
  676.     }
  677.     dp++;
  678.   }
  679. /*  for(j=0;j<16;j++)
  680.   {
  681.     Write_Cmd(Yaddr++);
  682.     Write_Cmd(Xaddr);
  683.     Write_Data(dp[k++]);
  684.     Write_Data(dp[k++]);
  685.   }*/
  686. //  Write_Cmd(0x36);
  687. }
  688. /**************************************************
  689. 函数名称:ADC_Init()
  690. 功    能:初始化ADC12
  691. 参    数:无
  692. 返回值  :无
  693. **************************************************/
  694. void ADC_Init(void)
  695. {
  696.   P6SEL |=0X01;
  697.   ADC12CTL0 |=ADC12ON + SHT0_1 + MSC;
  698.   ADC12CTL1 |=SHP + CONSEQ_2;
  699.   ADC12MCTL0 |=INCH_0;
  700.   ADC12IE |=0X0F;
  701.   ADC12CTL0 |=ENC+ADC12SC;//使能转换;开始转换
  702.    
  703.    _EINT();
  704. }
  705. /**************************************************
  706. 函数名称:ADC12ISR
  707. 功    能:ADC中断服务函数
  708. 参    数:无
  709. 返回值  :无
  710. **************************************************/
  711. #pragma vector = ADC12_VECTOR
  712. __interrupt void ADC12ISR (void)
  713. {

  714.   results[index] = ADC12MEM0;               // Move results
  715.   index++;
  716.   if(index == 100)
  717.   ADC12IE = 0X00;
  718. }
  719. /*******************测量电压峰峰值********************/
  720. void Disp_Vpp(unsigned char hang,unsigned char lie,unsigned int VPP)
  721. {
  722.   float real_vpp;
  723.   unsigned int Uintvpp;
  724.   unsigned char ge,shi,bai;   //,i;
  725.   real_vpp = (VPP*3.3)/1024;
  726.   Uintvpp = (int)real_vpp*100;
  727.   bai = Uintvpp/100%10;
  728.   shi = Uintvpp/10%10;
  729.   ge = Uintvpp%10;
  730.   
  731.   Draw_Font(hang,lie+0,font3x6+9+bai*3);
  732.   Draw_Font(hang,lie+3,font3x6+3);
  733.   Draw_Font(hang,lie+6,font3x6+9+shi*3);
  734.   Draw_Font(hang,lie+10,font3x6+9+ge*3);
  735.   
  736. }
  737. void Measure_Vol(void)
  738. {
  739.    unsigned int Max_voltage = 0,Min_voltage = 1024;
  740.    unsigned int c;
  741. //   float Real_Voltage;
  742.    uchar i,e=0;
  743.    
  744.    for(i=0;i<100;i++)
  745.    {
  746.       if(results[i] > Max_voltage)
  747.         Max_voltage = results[i];
  748.       else
  749.         if(results[i] < Min_voltage)
  750.           Min_voltage = results[i];
  751.    }
  752.    
  753.    c = Max_voltage - Min_voltage;
  754.    e++;
  755.    if(e > 5)
  756.    {
  757.      Disp_Vpp(10,102,Max_voltage);
  758.      Disp_Vpp(17,102,Min_voltage);
  759.      Disp_Vpp(24,102,c);
  760.      e = 0;
  761.    }
  762. }
  763. void Clock_Init()
  764. {
  765.    uchar i;
  766.    BCSCTL1&=~XT2OFF;                 //打开XT振荡器
  767.   BCSCTL2|=SELM1+SELS;              //MCLK为8MHZ,SMCLK为8MHZ
  768.    do{
  769.      IFG1&=~OFIFG;                   //清除震荡标志
  770.     for(i=0;i<100;i++)
  771.         _NOP();                      //延时等待
  772.   }
  773.    while((IFG1&OFIFG)!=0);           //如果标志为1,则继续循环等待
  774.   IFG1&=~OFIFG;
  775. }
  776. main()
  777. {
  778.   uchar y1=0,y2=0,i=0,j;          //y坐标
  779.   uchar TimeDiv = 3;
  780.   WDTCTL = WDTPW + WDTHOLD;
  781.   P1DIR = 0X00;                //P1.7--P1.0设为输入,检测按键
  782.   Clock_Init();
  783.   Ini_lcd();
  784.   Clear_GDRAM();
  785.   Draw_PM(tab);
  786.   ADC_Init();
  787.   _EINT();
  788.    DisplayCgrom(0x80,"电子欢迎你");
  789.   DisplayCgrom(0x88,"xxxxxx");
  790.   DisplayCgrom(0x90,"xxxxxx  ");
  791.   DisplayCgrom(0x98,"xxxxxxxx");
  792.   while(1)
  793.   {
  794.    
  795.       if(!(P1IN&(0x01<<S3)))
  796.    {
  797.     while(!(P1IN&(0x01<<S3)));
  798.     Write_Cmd(0x01);
  799.    }
  800. /**********************检测按键**************************/
  801.     if(!(P1IN&(0x01<<S2)))
  802.     {
  803.       while(!(P1IN&(0x01<<S2)));
  804.       TimeDiv--;
  805.       ADC12CTL0 &=~ ENC;
  806.       ADC12CTL0 = SHT0_1;
  807.       ADC12MCTL0 |=INCH_0;//通道选择
  808.       ADC12CTL1 = CONSEQ_2;                     //Repeat single channel
  809.       ADC12CTL0 = TimeDiv*0x800 + MSC + ADC12ON ;   //ADC10ON , Interrupt enale  
  810.       ADC12IE |=0X01;
  811.       ADC12CTL0 |= ENC + ADC12SC;             // Sampling and conversion start
  812.       if(TimeDiv == 0) TimeDiv = 4;
  813.     }
  814.    
  815.     if(index == 100)                           //测完99次再显示
  816.   {
  817.     index = 0;
  818.     for(i=0;i<=99;i++)                      //显示时把99次测量的值画点在12864上   
  819.     {
  820.       y1 = y2;
  821.       y2 = 61 - (results[i]/17);            //17表示 满量程1024除以60个点
  822.       LCD_Set_Dot1(i,y2);
  823.       
  824.       if((abs(y2 - y1)>=3)&&(i!=0))        //如果下一点的纵坐标比前一个点的纵坐标大3,为了看起来连续,两个点画线
  825.       Lcd_DrawLine_Y(y1,y2,i);
  826.     }
  827.     Measure_Vol();                         //测量最大值最小值     
  828.     Delay_Nms(200);                       //等待1秒左右时间

  829. /**********************检测按键S1**************************/
  830.       while(!(P1IN&(0x01<<S1)));                // FREEZE,锁定屏幕
  831. /**********************检测按键S1**************************/

  832.    
  833.     for(i=0;i<=99;i++)                    //显示过后删除,不能清屏,因为清屏的话看起来屏幕会闪烁
  834.     {
  835.       y1 = y2;
  836.       y2 = 61 - (results[i]/17);
  837.       if((! ((i%10 == 9)&&(y2%10 == 1) ||(i==0) )))
  838.       LCD_ReSet_Dot1(i,y2);
  839.       
  840.       if((abs(y2 - y1)>=3)&&(i!=0))
  841.       Lcd_ReDrawLine_Y(y1,y2,i);
  842.       
  843.     }
  844.    
  845.       for(j=1;j<100;j=j+2)                                  //画中心X,Y轴
  846.       {
  847.         LCD_Set_Dot1(49,j);
  848.         LCD_Set_Dot1(j,31);
  849.         for(i=1;i<62;i=i+10)                                
  850.         {
  851.           if( ( ((j%10 == 9)||j==1))&&(i%10 == 1) )           //保存背景的点
  852.           LCD_Set_Dot1(j,i);
  853.         }
  854.       }
  855.    ADC12IE |=0X01;   
  856.   }
  857.   
  858.   }
  859. }
复制代码


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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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