msp430g2553+adc采样正弦波峰值+12864显示,采样数值不变化

2019-07-15 16:09发布

下面是我的程序,谢谢!
#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 32

  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 = 110;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_8MHZ;
  DCOCTL = CALDCO_8MHZ;//设置内部时钟8M
// 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(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 shuju()
{
  uchar j;
  maxr = 0;
  maxaver = 0;
  for(j = 0;j < 64;j++)
  {
    if((result[j]>maxaver) && (result[j]<1024))
    {
      maxaver = result[j];
    }
  }
    maxr = (maxaver*3.56)/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 IO_init(void)
{
  P1REN = BIT0 + BIT1;
  P1OUT = 0x00;
  P1DIR |= BIT0 + BIT1 + BIT2 + BIT3;
}
void ADC_init(void)
{
  ADC10CTL0 = ADC10ON + ADC10SHT_3 + MSC + ADC10IE + SREF_1 + REFON;
  ADC10CTL1 |= CONSEQ_2 + INCH_10;
  ADC10AE0 |= BIT0 + BIT1;
  ADC10CTL0 &=~ADC10IFG;
// ADC10DTC1 = 0x40;
}

void main(void)
{
  WDTCTL = WDTPW + WDTHOLD;
//  IO_init();
//  P2DIR = 0XFF;
  P1DIR |= BIT3;
  P1SEL |= BIT3;

  LCD12864_init();
  Sys_init();
  ADC_init();
  _EINT();
  while(1)
  {
    ADC10CTL0 &=~ENC;
    while(ADC10CTL1 & ADC10BUSY);

    ADC10CTL0 |= ENC + ADC10SC;//启动
    _NOP();
  //      ADC10SA = (unsigned int)aver;
//   ADC10CTL0&=~ADC10IFG;

    _BIS_SR(LPM0_bits + GIE);
  }

}

#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void)
{
// uint i;
//
// P2SEL = 0X00;

  result[index++] = ADC10MEM;
//  if(index == 32)
//  {
    shuju();
// }
  _NOP();

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




友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。