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);
}
|
|