点光源追踪代码,是要在一个太阳能电池板上四个角安装四个光敏三极管然后根据光敏三极管产生的电压在单片机进行比较,从而控制电机偏转来跟踪点光源。但是现在烧录程序之后点光源不论怎么移动电机都不会偏转,求高手帮忙解决问题,过两天要用到了,急。。。多谢了!
#include "msp430g2553.h"
#define uint unsigned int
#define uchar unsigned char
unsigned int a[32],b[4];
unsigned int i=50,j=50;
void delay(uint z);
void pwm();
void circle();
void main( void )
{
P1OUT=0x00;
WDTCTL = WDTPW + WDTHOLD;
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;
pwm();
while(1)
{
ADC10CTL0 = ADC10SHT_2 + ADC10ON + ADC10IE; // ADC10ON, interruptenabled
ADC10CTL0 |= 0x0020;
ADC10CTL1 = INCH_4; // input A1
ADC10AE0 |= 0x10; // PA.1 ADC option select
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
__bis_SR_register(CPUOFF + GIE); // LPM0, ADC10_ISR will force exit
b[1]=ADC10MEM;
delay(2);
ADC10CTL0 = ADC10SHT_2 + ADC10ON + ADC10IE; //ADC10ON, interrupt enabled
ADC10CTL0 |= 0x0020;
ADC10CTL1 = INCH_3; // input A3
ADC10AE0 |= 0x08; // PA.3 ADC option select
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
__bis_SR_register(CPUOFF + GIE); // LPM0, ADC10_ISR will force exit
b[3]=ADC10MEM;
delay(2);
ADC10CTL0 = ADC10SHT_2 + ADC10ON + ADC10IE; // ADC10ON, interruptenabled
ADC10CTL0 |= 0x0020;
ADC10CTL1 = INCH_0; // input A0
ADC10AE0 |= 0x01; // PA.0 ADC option select
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
__bis_SR_register(CPUOFF + GIE); // LPM0, ADC10_ISR will force exit
b[0]=ADC10MEM;
delay(2);
ADC10CTL0 = ADC10SHT_2 + ADC10ON + ADC10IE; // ADC10ON, interruptenabled
ADC10CTL0 |= 0x0020;
ADC10CTL1 = INCH_5; // input A2
ADC10AE0 |= 0x20; // PA.2 ADC option select
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
__bis_SR_register(CPUOFF + GIE); // LPM0, ADC10_ISR will force exit
b[2]=ADC10MEM;
delay(2);
if(b[1]>b[3])
{
if(b[1]-b[3]>150)
{
CCR1=CCR1+i;
delay(300);
i=i+3;
}
}
if(b[3]>b[1])
{
if(b[3]-b[1]>150)
{
CCR1=CCR1-i;
delay(300);
i=i+3;
}
}
if(b[0]>b[2])
{
if(b[0]-b[2]>150)
{
CCR2=CCR2+j;
delay(300);
j=j+3;
}
}
if(b[2]>b[0])
{
if(b[2]-b[0]>150)
{
CCR2=CCR2-j;
delay(300);
j=j+3;
}
}
}
}
void pwm()
{
P1DIR|=BIT6;
P1SEL|=BIT6;
P2DIR|=BIT4;
P2SEL|=BIT4;
TACTL|= MC_1+TASSEL_2;
TACCTL1= OUTMOD_7;
TACCR0= 20000;
TACCR1= 1000 ;
TACCR2= 1000;
}
void delay(uint z)
{
uint i,j;
for(i=210;i>0;i--)
for(j=z;j>0;j--);
}
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void)
{
__bic_SR_register_on_exit(CPUOFF); // Clear CPUOFF bit from 0(SR)
} |