查看: 2388|回复: 1

msp430g2553AD10采样正弦波峰值

[复制链接]
  • TA的每日心情
    奋斗
    2016-7-20 11:37
  • 签到天数: 8 天

    连续签到: 1 天

    [LV.3]偶尔看看II

    发表于 2016-6-29 17:29:00 | 显示全部楼层 |阅读模式
    分享到:
    1与非币
    msp430g2553AD10采样正弦波峰值,显示的不是峰值,是一个特别小的数,单通道多次采样,谢谢大家
    #include <msp430g2553.h>


    #define uint unsigned int
    #define uchar unsigned char


    #define CS_H P2OUT|=BIT2
    #define CS_L P2OUT&=~BIT2


    #define SID_H P2OUT|=BIT1
    #define SID_L P2OUT&=~BIT1


    #define SCLK_H P2OUT|=BIT0
    #define SCLK_L P2OUT&=~BIT0


    uint aver[64]={0};
    uchar flag=0;
    uchar zifu[10]={"0123456789"};
    uint maxaver=0;
    uchar dis=0;
    double max=0,maxr=0;


    void delay(uchar z)
    {
      uint x,y;
      for(x = 110;x>0;x--)
        for(y = z;y>0;y--);
    }




    void PORT_init()
    {
      WDTCTL = WDTPW + WDTHOLD;
      P2DIR = 0XFF;
      P2OUT = 0XFF;
    }


    void Sys_init()
    {
      uchar i;
      WDTCTL = WDTPW + WDTHOLD;
      BCSCTL1 = CALBC1_8MHZ;
      DCOCTL = CALDCO_8MHZ;//设置内部时钟8M
      do
      {
        IFG1 &= ~OFIFG;
        for(i = 0xff;i>0;i--);
      }
      while((IFG1 & OFIFG) != 0);
    //  BCSCTL2 |= SELM_0;
    //  BCSCTL2 |= DIVS_0;
    }


    void send_byte(uchar bbyte)
    {
      uchar i,temp;
      for(i=0;i<8;i++)
      {
    //    P2DIR = 0xff;
        temp = bbyte & 0x80;
        if(temp)
        {
          SID_H;
        }
        else
        {
          SID_L;
        }
        SCLK_L;
        SCLK_H;
        bbyte = bbyte<<1;
      }
    }


    void LCD12864_write_cmd(uchar ddata)
    {
      uchar hdata,ldata;
      hdata = ddata & 0xf0;
      ldata = (ddata<<4) & 0xf0;
      CS_H;
      send_byte(0xf8);
      delay(5);
      send_byte(hdata);
      delay(1);
      send_byte(ldata);
      delay(1);
      CS_L;
    }


    void LCD12864_write_data(uchar ddata)
    {
      uchar hdata,ldata;
      hdata = ddata & 0xf0;
      ldata = (ddata<<4) & 0xf0;
      CS_H;
      send_byte(0xfa);
      delay(5);
      send_byte(hdata);
      delay(1);
      send_byte(ldata);
      delay(1);
      CS_L;
    }


    void LCD12864_init()
    {
      P2DIR |= BIT0 + BIT1 + BIT2;
      delay(10);
      CS_H;
      LCD12864_write_cmd(0x34);
      delay(1);
      LCD12864_write_cmd(0x30);
      delay(1);
      LCD12864_write_cmd(0x0c);//
      delay(1);
      LCD12864_write_cmd(0x01);
      delay(1);
      LCD12864_write_cmd(0x06);
      delay(1);
    }


    void LCD12864_pos(uchar x,uchar y)
    {
      uchar pos;
      switch(x)
      {
      case 1:pos = 0x80;break;
      case 2:pos = 0x90;break;
      case 3:pos = 0x88;break;
      case 4:pos = 0x98;break;
      default:pos = 0x80;
      }
      pos += y;
      LCD12864_write_cmd(pos);
    }






    void shuju()
    {
      uchar j;
      maxr = 0;
      maxaver = 0;
      for(j = 0;j < 64;j+=2)
      {
        if(aver[j]>maxaver && aver[j]<1024)
        {
          maxaver = aver[j];
        }
        maxr = (maxaver*3.56)/1023;
        max = maxr*1000;
        dis = max;
       
        LCD12864_pos(2,2);
        LCD12864_write_data(zifu[dis/10000]);
        LCD12864_write_data('.');
        LCD12864_write_data(zifu[dis/1000%10]);
        LCD12864_write_data(zifu[dis/100%10]);
        LCD12864_write_data(zifu[dis/10%10]);
        LCD12864_write_data(zifu[dis%10]);


      }
    }


    void ADC_init(void)
    {
      ADC10CTL0 = ADC10ON + ADC10SHT_3 + MSC + ADC10IE;
      ADC10CTL1 |= CONSEQ_2 + INCH_1;
      ADC10AE0 |= BIT0 + BIT1;
      ADC10DTC1 = 0x20;
    }


    void main(void)
    {
      WDTCTL = WDTPW + WDTHOLD;
    // IO_init();
      LCD12864_init();
      PORT_init();
      Sys_init();
      ADC_init();
      _EINT();
      while(1)
      {
        ADC10CTL0 &=~ENC;
        while(ADC10CTL1 & ADC10BUSY);
        ADC10SA = (unsigned int)aver;
        ADC10CTL0 |= ENC + ADC10SC;
    //   ADC10CTL0&=~ADC10IFG;


        _BIS_SR(CPUOFF + GIE);
      }
      
    }


    #pragma vector=ADC10_VECTOR
    __interrupt void ADC10_ISR(void)
    {
      shuju();
    //  dis =ADC10MEM;
      _BIC_SR_IRQ(CPUOFF + GIE);
    }






    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    2016-7-20 11:37
  • 签到天数: 8 天

    连续签到: 1 天

    [LV.3]偶尔看看II

     楼主| 发表于 2016-7-1 05:24:38 | 显示全部楼层
    我以自己解决~嘿嘿,是输入没有弄好,下面是我的程序,供大家参考,少走弯路~
    //输出接P1.0,不接则显示引脚悬空电压
    #include <msp430g2553.h>

    #define uint unsigned int
    #define uchar unsigned char

    #define CS_H P2OUT|=BIT2
    #define CS_L P2OUT&=~BIT2

    #define SID_H P2OUT|=BIT1
    #define SID_L P2OUT&=~BIT1

    #define SCLK_H P2OUT|=BIT0
    #define SCLK_L P2OUT&=~BIT0

    #define Number 128

    static uint index = 0;
    static uint result[Number];
    uchar flag=0;
    uchar zifu[10]={"0123456789"};
    uint maxaver=0;
    long dis=0;
    double max=0,maxr=0;

    void delay(uchar z)
    {
      uint x,y;
      for(x = 800;x>0;x--)
        for(y = z;y>0;y--);
    }



    void Sys_init()
    {
    //  uchar i;
      WDTCTL = WDTPW + WDTHOLD;
      if(CALBC1_8MHZ==0XFF || CALDCO_8MHZ==0XFF)
      {
        while(1);
      }
      BCSCTL1 = CALBC1_16MHZ;
      DCOCTL = CALDCO_16MHZ;//设置内部时钟8M
    // BCSCTL2 |= SELM_0;
      BCSCTL2 |= SELM_1 + DIVM_3;
    }

    void send_byte(uchar bbyte)
    {
      uchar i,temp;
      for(i=0;i<8;i++)
      {
    //    P2DIR = 0xff;
        temp = bbyte & 0x80;
        if(temp)
        {
          SID_H;
        }
        else
        {
          SID_L;
        }
        SCLK_L;
        SCLK_H;
        bbyte = bbyte<<1;
      }
    }

    void LCD12864_write_cmd(uchar ddata)
    {
      uchar hdata,ldata;
      hdata = ddata & 0xf0;
      ldata = (ddata<<4) & 0xf0;
      CS_H;
      send_byte(0xf8);
      delay(5);
      send_byte(hdata);
      delay(1);
      send_byte(ldata);
      delay(1);
      CS_L;
    }

    void LCD12864_write_data(uchar ddata)
    {
      uchar hdata,ldata;
      hdata = ddata & 0xf0;
      ldata = (ddata<<4) & 0xf0;
      CS_H;
      send_byte(0xfa);
      delay(5);
      send_byte(hdata);
      delay(1);
      send_byte(ldata);
      delay(1);
      CS_L;
    }

    void LCD12864_init()
    {
      P2DIR |= BIT0 + BIT1 + BIT2;
      delay(10);
      CS_H;
      LCD12864_write_cmd(0x34);
      delay(1);
      LCD12864_write_cmd(0x30);
      delay(1);
      LCD12864_write_cmd(0x0c);//
      delay(1);
      LCD12864_write_cmd(0x01);
      delay(1);
      LCD12864_write_cmd(0x02);
      delay(1);
    }

    void LCD12864_pos(uchar x,uchar y)
    {
      uchar pos;
      switch(x)
      {
      case 1:pos = 0x80;break;
      case 2:pos = 0x90;break;
      case 3:pos = 0x88;break;
      case 4:pos = 0x98;break;
      default:pos = 0x80;
      }
      pos += y;
      LCD12864_write_cmd(pos);
    }

    void LCD12864_write(uchar add,uchar *str)
    {
      uchar k;
      LCD12864_write_cmd(add);
      for(k=0;k<*str;k++)
      {
        LCD12864_write_data(*str);
        str++;
      }
    }

    void IO_init()
    {
      P1DIR &=~BIT0;
      P1REN |= BIT0;
      P2DIR = 0XFF;
      P2OUT = 0XFF;
    }

    void shuju()
    {
      uchar j;
      maxr = 0;
      maxaver = 0;
      for(j = 0;j < 128;j++)
      {
        if((result[j]>maxaver) && (result[j]<1024))
        {
          maxaver = result[j];
        }
      }
        maxr = (maxaver*2.00)/1023;
        max = maxr*10000;
        dis = max;
       
        LCD12864_pos(2,2);
        LCD12864_write_data(zifu[dis/10000]);
        LCD12864_write_data('.');
        LCD12864_write_data(zifu[dis/1000%10]);
        LCD12864_write_data(zifu[dis/100%10]);
        LCD12864_write_data(zifu[dis/10%10]);
        LCD12864_write_data(zifu[dis%10]);

    }

    void ADC_init(void)
    {
      ADC10CTL0 = ADC10ON + ADC10SHT_3 + MSC + ADC10IE + REF2_5V + REFON + SREF_1;
      ADC10CTL1 |= CONSEQ_2 + INCH_0 + ADC10DIV_3;
    //  ADC10AE0 |= 0XFF;
    // ADC10CTL0 &=~ADC10IFG;
    // ADC10DTC1 = 0x40;
    }

    void main(void)
    {
      WDTCTL = WDTPW + WDTHOLD;

      LCD12864_init();
      Sys_init();
      ADC_init();
      _EINT();
      
       LCD12864_pos(1,1);
       LCD12864_write(0x81,"采样结果:");
       LCD12864_write(0x99,"By:Watermelon");
      
      while(1)
      {
        ADC10CTL0 &=~ENC;
        while(ADC10CTL1 & ADC10BUSY);
        ADC10SA = result[index];
        ADC10CTL0 |= ENC + ADC10SC;//启动
        _NOP();
    //   ADC10CTL0&=~ADC10IFG;

        _BIS_SR(LPM0_bits + GIE);
      }
      
    }

    #pragma vector=ADC10_VECTOR
    __interrupt void ADC10_ISR(void)
    {
      result[index++] = ADC10MEM;
    // index += 1;
      if(index == 128)
      {
        shuju();
        index = 0;
    }
      _NOP();

      ADC10CTL0 &=~ADC10IFG;
    //  _BIC_SR_IRQ(CPUOFF + GIE);
    }



    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /4 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-11-27 13:58 , Processed in 0.129360 second(s), 17 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.