马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
#include <MSP430x14x.h>#define ADCMEM ((int *) 0x0140)
//ADC12MEMx定义void InitSYS(void);
void InitUST0(void);
//初始化系统及ADC寄存器void InitUST1(void);void InitADC(void);
void SetZero(void);int
GetBusData(void);void SetBusData(int busvdata);void SendRomCode(void);void Averege(void);void Filter(void);void Analysis(void);void Delay(int delaydata);interrupt[ADC_VECTOR] void ADC12(void);interrupt[USART1RX_VECTOR] void USART1(void);unsigned char savedata[1500];unsigned char saveromdata[7];unsigned char enddata[3];unsigned int adtime;unsigned int recromok;unsigned int ave;
unsigned int datai;unsigned int vdata;
unsigned int time;
void main(void)
//使用中断方式
{
unsigned int i,busvdata,low,hi,ldata;
InitSYS();
InitUST0();
InitUST1();
InitADC();
P1DIR |= 0x20;
P1DIR |= 0x1c;
//p1.4 p1.3 p1.2
P1OUT |= BIT4;
SetZero();
//设置x9015到零
for(i=0;i<30000;i++)
{
_NOP(); }
//wait to sample
busvdata=GetBusData();
//
while(1)
{
SetBusData(busvdata);
busvdata=GetBusData();
if(busvdata<150)
{
break; }
}
IE2 |=URXIE1;
while(1)
{
// for(time=0;time<256;time++)
// {
//IE2 |=URXIE1;
//low=time;
recromok=0;
while(recromok!=1);
SendRomCode();
for(i=0;i<6500;i++)
{
_NOP(); }
//wait to sample
ADC12CTL0 |= 0x02;
ADC12CTL0 |= 0x01; //start AD
adtime=0;
while(1)
{
if(adtime>1500)
{
ADC12CTL0 &= ~0x02;
Filter();
Averege();
Analysis();
for(i=0;i<3;i++)
{
U1TXBUF = enddata;
while((U1TCTL&0x01)==0);
}
break;
}
//}
}
}}void SetZero(void){
int i;
P1OUT &= ~BIT4;
//cs
P1OUT &= ~BIT3;
//u/d
for(i=0;i<64;i++)
{
P1OUT ^= BIT2;
//inc
}
P1OUT |= BIT4;
//cs}
int GetBusData(void){
unsigned char vdata1,vdata2;//,vdata;
adtime=0;
ADC12CTL0 |= 0x02;
ADC12CTL0 |= 0x01; //start AD
while(1)
{
if(adtime>20)
{
ADC12CTL0 &= ~0x02;
break;
}
}
//计算
vdata1=(savedata[0]+savedata[1]+savedata[2]+savedata[3]+savedata[4]+savedata[5]+savedata[6]+savedata[7]+savedata[8]+savedata[9])/10;
vdata2=(savedata[10]+savedata[11]+savedata[12]+savedata[13]+savedata[14]+savedata[15]+savedata[16]+savedata[17]+savedata[18]+savedata[19])/10;
vdata=(vdata1+vdata2)/2;
return(vdata);}
void SetBusData(int busvdata){
int i;
unsigned int temp;
unsigned int movetime;
if(busvdata>150)
{
temp=busvdata-150;
movetime=(temp*20*3)/256/3;
P1OUT &= ~BIT4;
//cs
P1OUT |= BIT3;
//u/d
for(i=0;i<movetime+1;i++)
{
P1OUT ^= BIT2;
//inc
}
P1OUT |= BIT4;
//cs
}
}
void SendRomCode(void){
U0TXBUF = saveromdata[6];
while((U0TCTL&0x01)==0);
U0TXBUF = saveromdata[5];
while((U0TCTL&0x01)==0);
U0TXBUF = saveromdata[4];
while((U0TCTL&0x01)==0);
U0TXBUF = saveromdata[3];
while((U0TCTL&0x01)==0);
U0TXBUF = saveromdata[2];
while((U0TCTL&0x01)==0);
U0TXBUF = saveromdata[1];
while((U0TCTL&0x01)==0);}void Filter(void){
unsigned int i,j,m,a[3],ldata;
for(i=0;i<1500;i++)
{
a[0]=savedata;
a[1]=savedata[i+1];
a[2]=savedata[i+2];
for(j=0;j<2;j++)
{
for(m=1;m<3;m++)
{
if(a[m]>a[j])
{
ldata=a[j];
a[j]=a[m];
a[m]=ldata;
}
}
}
savedata=a[1];
}
}void Averege(void)//取5-20共15个得到总线电压值{
unsigned int i,he;
he=0;
for(i=20;i<30;i++)
{
he=he+savedata;
}
ave=he/10;
} |