MSP430F5529的AD电压采集

2019-03-24 09:07发布

我做了一个基于MSP430F5529的AD电压采集的东西,输入口是P6.6,无意间直接把稳压电源接到了接口上,现在采样显示的值非常小,实际3.5V,显示只有0.01V,求助高手,是哪里的问题,还是我单片机坏了?换了一个输入引脚还是这样,AD模块好像还可以用,具体是哪里的问题呢?
  1. //***************************************************************************//
  2. //                                                                           //
  3. //                         MSP-EXP430F5529 LAB CODE                          //
  4. //                                                                           //
  5. //                               lab1 - CLOCK                                //
  6. //                                                                           //
  7. //***************************************************************************//
  8. /*******************************************************************************
  9. *
  10. *  Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
  11. *
  12. *  Redistribution and use in source and binary forms, with or without
  13. *  modification, are permitted provided that the following conditions
  14. *  are met:
  15. *
  16. *    Redistributions of source code must retain the above copyright
  17. *    notice, this list of conditions and the following disclaimer.
  18. *
  19. *    Redistributions in binary form must reproduce the above copyright
  20. *    notice, this list of conditions and the following disclaimer in the
  21. *    documentation and/or other materials provided with the
  22. *    distribution.
  23. *
  24. *    Neither the name of Texas Instruments Incorporated nor the names of
  25. *    its contributors may be used to endorse or promote products derived
  26. *    from this software without specific prior written permission.
  27. *
  28. *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  29. *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  30. *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  31. *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  32. *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  33. *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  34. *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  35. *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  36. *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  37. *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  38. *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. ******************************************************************************/
  41. #include <msp430f5529.h>
  42. #include <stdio.h>
  43. #include "HAL_PMM.h"
  44. #include "HAL_UCS.h"
  45. #include "HAL_Board.h"
  46. #include "HAL_Buttons.h"
  47. #include "HAL_Cma3000.h"
  48. #include "HAL_Dogs102x6.h"
  49. #include "stdlib.h"
  50. #include "lab1.h"

  51. /* *所有的全局变量
  52. *电压采样值和测频是采样到得个数
  53. */
  54. uint16_t timeoutCounter;
  55. uint32_t results[10];
  56. uint32_t sum=0,com,acom,flag=0;
  57. uint32_t cap,newdata,olddata,data;
  58. uint32_t num;

  59. /*设置时钟源*/
  60. void int_clk()
  61. {
  62.            P2DIR |= BIT2;                            // SMCLK set out to pins
  63.            P2SEL |= BIT2;
  64.            P7DIR |= BIT7;                            // MCLK set out to pins
  65.            P7SEL |= BIT7;

  66.            P5SEL |= BIT2+BIT3;                       // Port select XT2
  67. P5SEL |= BIT2+BIT3;                       // Port select XT2

  68.            UCSCTL6 &= ~XT2OFF;                       // Enable XT2
  69.            UCSCTL3 |= SELREF_2;                      // FLLref = REFO
  70.                                                      // Since LFXT1 is not used,
  71.                                                      // sourcing FLL with LFXT1 can cause
  72.                                                      // XT1OFFG flag to set
  73.            UCSCTL4 |= SELA_2+SELS_5 + SELM_5;
  74.            // Loop until XT1,XT2 & DCO stabilizes - in this case loop until XT2 settles
  75.            do
  76.            {
  77.              UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + DCOFFG);
  78.                                                      // Clear XT2,XT1,DCO fault flags
  79.              SFRIFG1 &= ~OFIFG;                      // Clear fault flags
  80.            }while (SFRIFG1&OFIFG);                   // Test oscillator fault flag

  81.            UCSCTL6 &= ~XT2DRIVE0;                    // Decrease XT2 Drive according to
  82. }

  83. /*
  84. *液晶初始化
  85. *开发板初始化
  86. */
  87. void chushi(void)
  88. {
  89.    uint8_t contrast = *((unsigned char *)contrastSetpointAddress);   //读取FLASH中对比度值
  90.    uint8_t brightness = *((unsigned char *)brightnessSetpointAddress); //读取FLASH中背光值
  91.    // Initialize accelerometer offset from flash
  92.    /*Cma3000_setAccel_offset(*((unsigned char *)accelXcalibrationAddress), //初始化加速度偏移量
  93.                            *((unsigned char *)accelYcalibrationAddress),
  94.                            *((unsigned char *)accelZcalibrationAddress));*/
  95.    // Stop WDT
  96.    WDTCTL = WDTPW + WDTHOLD;                     //关闭看门狗
  97. // Basic GPIO initialization
  98.    Board_init();                                 //初始化GPIO

  99.    // Set Vcore to accomodate for max. allowed system speed
  100.    SetVCore(3);                                  //设VCore为最大
  101.     __enable_interrupt();                        //使能全局中断
  102.    // Set up LCD
  103.    Dogs102x6_init();                            //初始化LCD
  104.    Dogs102x6_backlightInit();                   //背光初始化

  105.    // Contrast not programed in Flash Yet
  106.    if (contrast == 0xFF)                        //若当前FLASH中无对比度值,则将对比度值设为11(默认)
  107.        // Set Default Contrast
  108.        contrast = 11;

  109.    // Brightness not programed in Flash Yet
  110.    if (brightness == 0xFF)                      //若当前FLASH中无背光值,则将背光值设为11(默认)
  111.        // Set Default Brightness
  112.        brightness = 11;

  113.    Dogs102x6_setBacklight(brightness);          //设置初始背光值
  114.    Dogs102x6_setContrast(contrast);             //设置初始对比度值
  115.    Dogs102x6_clearScreen();                     //清屏

  116.    // Set up wheel
  117.    Buttons_init(BUTTON_ALL);                    //初始化按键
  118.    Buttons_interruptEnable(BUTTON_ALL);         //使能所有按键中断
  119.    buttonsPressed = 0;                          //键值清零

  120.    Dogs102x6_stringDraw(1, 0, "**== Message ==** ", DOGS102x6_DRAW_NORMAL);
  121.    Dogs102x6_stringDraw(3, 0, "   Welcome to     ", DOGS102x6_DRAW_NORMAL);
  122.    Dogs102x6_stringDraw(5, 0, " SH University", DOGS102x6_DRAW_NORMAL);
  123.    Dogs102x6_stringDraw(7, 0, " MSP-EXP430F5529 ", DOGS102x6_DRAW_NORMAL);
  124.    while (!buttonsPressed)                    //等待按键被按下,或者超时退出等待
  125.       {
  126.           for (timeoutCounter = 0; timeoutCounter < 0x0FFF; timeoutCounter++)
  127. {
  128.               if (buttonsPressed)
  129.                   break;
  130.               __delay_cycles(2000);
  131.           }

  132.           break;
  133.       }
  134.    Dogs102x6_clearScreen();
  135.    Dogs102x6_stringDraw(1, 0, "  Battery power", DOGS102x6_DRAW_NORMAL);
  136.    Dogs102x6_stringDraw(3, 0, "   measurement", DOGS102x6_DRAW_NORMAL);
  137.    Dogs102x6_stringDraw(6, 0, "*S1=Enter S2=Esc*", DOGS102x6_DRAW_NORMAL);
  138.   // Board_ledOn(LED_ALL);

  139.    // Wait for button press
  140.    while (!buttonsPressed)                    //等待按键被按下,或者超时退出等待
  141.    {
  142.        for (timeoutCounter = 0; timeoutCounter < 0x0FFF; timeoutCounter++)
  143.        {
  144.            if (buttonsPressed)
  145.                break;
  146.            __delay_cycles(2000);
  147.        }
  148.        break;
  149.    }
  150.    Board_ledOff(LED_ALL);
  151. }

  152. /*ADC12的初始化*/
  153. void adc1()
  154. {
  155.                  P6SEL |= BIT6;
  156.                  ADC12CTL0 = ADC12ON+ADC12SHT0_8+ADC12MSC;
  157.                  ADC12CTL1 = ADC12SHP+ADC12CONSEQ_2;
  158.                  ADC12MCTL0=ADC12SREF_0+ADC12INCH_6;

  159.                  ADC12CTL0 |= ADC12ENC;
  160. }

  161. /*直流电压采样数据转换显示*/
  162. void display1()
  163. {
  164.          if(flag==1)
  165.             {
  166.                        char string1[20];
  167.                        string1[0]=com/1000+48;
  168.                        string1[1]=46;
  169.                        string1[2]=com/100%10+48;
  170.                        string1[3]=com/10%10+48;
  171.                        string1[4]=com%10+48;
  172.                        string1[5]='';
  173.                        char string2[20];
  174.                        string2[0]=com/1000+48;
  175.                        string2[1]=46;
  176.                        string2[2]=com/100%10+48;
  177.                        string2[3]=com/10%10+48;
  178.                        string2[4]=com%10+48;
  179.                        string2[5]='';

  180. Dogs102x6_stringDraw(1, 0, "*==DC voltage==*", DOGS102x6_DRAW_NORMAL);
  181. Dogs102x6_stringDraw(3, 0, "value1=", DOGS102x6_DRAW_NORMAL);
  182. Dogs102x6_stringDraw(3, 40, string1, DOGS102x6_DRAW_NORMAL);
  183. Dogs102x6_stringDraw(3, 70, "(V)", DOGS102x6_DRAW_NORMAL);
  184. Dogs102x6_stringDraw(5, 0, "value2=", DOGS102x6_DRAW_NORMAL);
  185. Dogs102x6_stringDraw(5, 40, string2, DOGS102x6_DRAW_NORMAL);
  186. Dogs102x6_stringDraw(5, 70, "(V)", DOGS102x6_DRAW_NORMAL);
  187. Dogs102x6_stringDraw(7, 0, " *== good job==*", DOGS102x6_DRAW_NORMAL);

  188.                         for (timeoutCounter = 0; timeoutCounter < 0x02FF; timeoutCounter++)
  189.                                   {
  190.                                       __delay_cycles(1000);
  191. }
  192.                              flag=0;
  193.            }
  194. }


  195. /*交流电压采样数据转化显示
  196. *频率采样显示
  197. */
  198. /*void display2()
  199. {
  200.                        char str1[20],str3[20];

  201.                        cap=40000000/num;

  202.                        str1[0]=acom/1000+48;
  203.                        str1[1]=46;
  204.                        str1[2]=acom/100%10+48;
  205.                        str1[3]=acom/10%10+48;
  206.                        str1[4]=acom%10+48;
  207.                        str1[5]='';


  208.                               str3[0]=cap/10000000+'0';
  209.                               str3[1]=cap/1000000%10+'0';
  210.                               str3[2]=cap/100000%10+'0';
  211.                               str3[3]=cap/10000%10+'0';
  212.                               str3[4]=cap/1000%10+'0';
  213.                               str3[5]=cap/100%10+'0';
  214.                               str3[6]=cap/10%10+'0';
  215.                               str3[7]=46;
  216.                               str3[8]=cap%10+'0';
  217.                               str3[9]='';

  218.         Dogs102x6_stringDraw(1, 0, "*==AC voltage==*", DOGS102x6_DRAW_NORMAL);
  219.         Dogs102x6_stringDraw(3, 0, "value=", DOGS102x6_DRAW_NORMAL);
  220.             Dogs102x6_stringDraw(3, 40, str1, DOGS102x6_DRAW_NORMAL);
  221.             Dogs102x6_stringDraw(3, 75, "(V)", DOGS102x6_DRAW_NORMAL);
  222.             Dogs102x6_stringDraw(5, 1, "F =", DOGS102x6_DRAW_NORMAL);
  223.             Dogs102x6_stringDraw(5, 30, str3, DOGS102x6_DRAW_NORMAL);
  224.             Dogs102x6_stringDraw(5, 90, "HZ", DOGS102x6_DRAW_NORMAL);
  225.         Dogs102x6_stringDraw(7, 0, " *== good job==*", DOGS102x6_DRAW_NORMAL);

  226. }*/
  227. /*主函数设置*/
  228. void main()
  229. {
  230.         int_clk();
  231.         chushi();
  232.         adc1();

  233.         Dogs102x6_clearScreen();
  234.         Board_ledOff(LED_ALL);
  235.         ADC12CTL0 |= ADC12SC;
  236.         _EINT();

  237.         cap=0;
  238.         acom=0;
  239.         com=0;
  240. P1DIR |= BIT3;
  241.         P2SEL |= BIT0;           //TA1.1
  242.     while (1)
  243.      {
  244.             buttonsPressed = 0;
  245.             Dogs102x6_clearScreen();
  246.             /* while (!(buttonsPressed & BUTTON_S1))
  247.             {
  248.                     //ADC12IE = 0x01;
  249.                  Board_ledOn(LED4);

  250.                  TA1CCTL1=CM_1+SCS+CCIS_0+CAP+CCIE;
  251.                  TA1CTL=TASSEL_2+MC_2+TACLR+TAIE;

  252.                  display2();
  253.             }
  254.              buttonsPressed = 0;
  255.              Dogs102x6_clearScreen();
  256.              __delay_cycles(2000);
  257.              Board_ledOff(LED_ALL);*/


  258.              while (!(buttonsPressed & BUTTON_S2))
  259.              {
  260.                 ADC12IE = 0x01;
  261.                 Board_ledOn(LED6);
  262.                 display1();
  263.              }
  264.              Dogs102x6_clearScreen();
  265.              __delay_cycles(2000);
  266.              Board_ledOff(LED_ALL);
  267.              ADC12IE = 0x00;
  268.              ADC12CTL0 &= ~ADC12SC;
  269.    }
  270. }

  271. /*ADC12中断函数*/
  272. #pragma vector=ADC12_VECTOR
  273. __interrupt void ADC12ISR (void)
  274. {
  275.   static unsigned int m,count = 0;
  276.   float factor=0.80566;
  277.   results[count]=ADC12MEM0;
  278.   count++;
  279.   for(m=0;m<10;m++)
  280.   {
  281.           sum+=results[m];
  282.   }
  283.   sum/=10;
  284.   sum=sum*factor;
  285.   com=sum;
  286.   if (count == 10)
  287.         {
  288.           count = 0;
  289.           flag=1;
  290.           ADC12IE = 0x00;
  291.         }
  292. }

  293. /*TA1的中断函数*/
  294. #pragma vector=TIMER1_A1_VECTOR
  295. __interrupt void TIMER1_A1_ISR(void)
  296. {
  297.   switch(__even_in_range(TA1IV,14))
  298.   {
  299. case  0: break;                          // No interrupt
  300.   case  2:
  301.              if(TA1CCTL1&CM0)
  302.          {
  303.                newdata=TA1R;
  304.                if(newdata<olddata)
  305.                {
  306.                        data=65536+newdata-olddata;
  307.                }
  308.                else
  309.                {
  310.                        data=newdata-olddata;
  311.                }
  312.                olddata=newdata;
  313.                num=data;
  314.          }
  315.                P1OUT ^= BIT3;
  316.                break;                                      // CCR1 not used
  317.     case  4: break;                                    // CCR2 not used
  318.     case  6: break;           // reserved
  319.     case  8: break;           // reserved
  320.     case 10: break;           // reserved
  321.     case 12: break;           // reserved
  322.     case 14: break;           // overflow
  323.     default: break;
  324.   }
  325. }
复制代码
此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
12条回答
dj狂人
2019-03-25 03:25
huixianfxt 发表于 2014-4-29 21:38
建议你把AD采集模块和显示模块拿出来,重新编写一个程序来检测电压,这样更容易判断端口是否损坏呢!一个一 ...

如果用3.3V的单片机自带的AD测10V或12V能么,还是要考虑相应单片机I/O的灌电流/拉电流?

一周热门 更多>

相关问题

    相关文章