MSP430F149,IAR,ADC采样之后对太阳点光源进行跟踪
MSP430F149,IAR,ADC采样之后对太阳点光源进行跟踪.zip
(30.58 KB)
#include "msp430x14x.h"
#define left result[0]
#define right result[1]
#define key_zuo (P4IN & 0x10)
#define key_you (P4IN & 0x20)
#define uchar unsigned char
uchar i_1,i_2;
unsigned char sd=0;
unsigned char ct=0;
unsigned char tf=0;
unsigned char sf=1;
unsigned char zf=0;
unsigned char reset=0;
unsigned char adc_flag=0;
unsigned int tt=0;
unsigned int a,b,c,d;
unsigned int ctx,cty;
int x,y,xt,yt;
unsigned int results[4];
unsigned char F_Rotation[4]={0x03,0x06,0x0c,0x09};//正转表格0x01,0x02,0x04,0x08
unsigned char B_Rotation[4]={0x09,0x0c,0x06,0x03};//反转表格
void delay(unsigned int z)
{
while(z)
{z--;}
}
void zuozhuan()
{
uchar i;
for(i=0;i<4;i++)
{
for(i_1=0;i_1<4;i_1++)
{
P4OUT =B_Rotation[i_1];
delay(16000);
}
}
}
void youzhuan()
{
uchar i;
for(i=0;i<4;i++)
{
for(i_1=0;i_1<4;i_1++)
{
P4OUT =F_Rotation[i_1];
delay(26000);
}
}
}
int abs(int x)//取绝对值函数
{
int y;
if(x>=0)
{
y=x;
}
else
{
y=-x;
}
return y;
}
void delayms(unsigned int z)
{
unsigned int x,y;
for(x=0;x<z;x++)
{
for(y=0;y<1000;y++);
}
}
void init_timerA()
{
TACTL|=TASSEL1+TACLR+ID0+ID1;
TACTL|=MC0+TAIE;//中断使能&停止模式
TACCR0=2999;
}
void init_clk()
{
unsigned char i;
BCSCTL1&=~XT2OFF;
BCSCTL2|=SELM1+SELS;
do
{
IFG1 &= ~OFIFG;
for(i=0;i<100;i++)
delay(1);
}
while((IFG1 & OFIFG) != 0);
IFG1&=~OFIFG;
}
void init_adc()
{
P6SEL |= 0x0f;
ADC12CTL0=ADC12ON+MSC+SHT0_2;
ADC12CTL1=SHP+CONSEQ_1;
ADC12MCTL0=0x00;
ADC12MCTL1=0x01;
ADC12MCTL2=0x02;
ADC12MCTL3=0x83;
ADC12IE=0x08;
ADC12CTL0 |= ENC;
}
void zuobiao() //实时坐标,亮度显示
{
zf=0;
}
void aim() //初始对准程序
{
//unsigned char i,k;
init_timerA();
sf=1;
adc_flag=1;
tf=1;
_EINT();
if(zf)
{
zf=0;
zuobiao();
}
sf=0;
adc_flag=0;
tf=0;
}
void move() //电机运动控制
{
// unsigned char k;
TACTL&=~MC0;
//k='n';
if(sd==1)
{
// k=keyscan();
//switch(k)
while(1)
{
// if() yt=y+1;
//case '8':yt=y-1;break;
if(key_zuo!=0x10) xt=x+1;
if(key_you!=0x20) xt=x-1;
}
}
// if(yt<0)
// {
// yt=0;
// }
if(xt<0)
{
xt=0;
}
//yt=yt%90;
xt=xt%180;
if(sf==0)
{
if(x>xt)
{
zuozhuan();
ctx--;
x=ctx/24;
}
if(x<xt)
{
youzhuan();
ctx++;
x=ctx/24;
}
// if(y>yt)
// {
// P4OUT&=~(diry);
// P4OUT^=clky;
// cty--;
// y=cty/24;
// }
// if(y<yt)
// {
// P4OUT|=diry;
// P4OUT^=clky;
// cty++;
// y=cty/24;
// }
}
TACTL|=MC0;
}
void track() //寻光
{
// int dx,dy;
int dx;
/* dy=a-b;
if(dy>=0)
{
yt=y+1;
if(yt==90)
{yt=y-1;}
yt=yt%90;
}
else
{
yt=y-1;
if(yt<0)
{ yt=0;}
yt=yt%90;
}*/
dx=a-b;
if(dx>=0)
{
xt=x+1;
if(xt==180)
{xt=x-1;}
xt=xt%180;
}
else
{
xt=x-1;
if(xt<0)
{ xt=0;}
xt=xt%180;
}
// if((abs(dx)<100)&&(abs(dy)<100))
if((abs(dx)<100))
{
sf=1;
}
if((abs(dx)>200))
{
sf=0;
}
}
void main()
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
init_clk();
delayms(1000);
P4DIR=0xff;
init_adc();
aim();
while(1)
{if(reset)
reset=0;
if(zf)
zf=0;
}
}
#pragma vector=TIMERA1_VECTOR
__interrupt void Timer_A(void)
{
switch(TAIV)
{
case 2:break;
case 4:break;
case 10:
_EINT();
tt++;
if(tt==200)
{zf=1;tt=0;}
if(tf)
{
TACTL&=~MC0;
while(ct<10)
{
while(adc_flag==1)
{
ADC12CTL0 |= ADC12SC;
adc_flag=0;
}
}
ct=0;
a=results[0]/10;
results[0]=0;
b=results[1]/10;
results[1]=0;
c=results[2]/10;
results[2]=0;
d=results[3]/10;
results[3]=0;
track();
TACTL|=MC0;
}
move();
break;
default:break;
}
}
#pragma vector=ADC_VECTOR
__interrupt void ADC12ISR()
{
results[0]+=ADC12MEM0;
results[1]+=ADC12MEM1;
results[2]+=ADC12MEM2;
results[3]+=ADC12MEM3;
ct++;
adc_flag=1;
}
|