|
msp430g2553+ADC10采样正弦波峰值+12864显示问题
[复制链接]
1芯积分
液晶上有显示,但是总显示2.1,不知道是怎么回事,谢谢大家,急!!
#include
#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;
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;
BCSCTL1 = CALBC1_16MHZ;
DCOCTL = CALDCO_16MHZ;//设置内部时钟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(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+=2)
{
if((aver[j]>maxaver) && (aver[j]<1024))
{
maxaver = aver[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)
{
P1DIR &= ~BIT3;
P1SEL |= BIT3;
P2DIR |= BIT0 + BIT1 + BIT2;
}
void ADC_init(void)
{
ADC10CTL0 = ADC10ON + ADC10SHT_3 + MSC + ADC10IE + SREF_0;
ADC10CTL1 |= CONSEQ_2 + INCH_1 + ADC10SSEL_0;
ADC10AE0 |= BIT0 + BIT1;
ADC10DTC1 = 0x40;
}
void main(void)
{
WDTCTL = WDTPW + WDTHOLD;
IO_init();
// P2DIR = 0XFF;
LCD12864_init();
// PORT_init();
Sys_init();
ADC_init();
// _EINT();
while(1)
{
ADC10CTL0 &=~ENC;
while(ADC10CTL1 & ADC10BUSY);
ADC10CTL0 |= ENC + ADC10SC;
ADC10SA = (unsigned int)aver;
// ADC10CTL0&=~ADC10IFG;
_BIS_SR(LPM0_bits + GIE);
}
}
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void)
{
shuju();
// dis =ADC10MEM;
_BIC_SR_IRQ(CPUOFF + GIE);
}
|
|