#include "MSP430X16X.h"
#define uint unsigned int
#define uchar unsigned char
#define Num_Of_Results 16
uint A0result[Num_Of_Results];
uint A1result[Num_Of_Results];
uint A2result[Num_Of_Results];
uint A3result[Num_Of_Results];
uchar key1=0;
uint average;
uint x,y,x0,x1,x2,x3;
int long sum;
int long IntDegC;
sta
tic uchar index=0;
/**********工作频率设置**********/
void InitClock(void)
{
uint i;
BCSCTL1&=~XT2OFF;
do
{
IFG1&=~OFIFG;
for(i=0xff;i>0;i--);
}
while((IFG1&OFIFG));
BCSCTL2=0X00;
BCSCTL2=SELS+SELM_3;
}
/**********舵机初始化**********/
void init_pwm(void)
{
P1SEL |= BIT2 + BIT3 ; //选择 p1.2-TA1 作为PWM输出 第二功能
P1DIR |= BIT2 + BIT3;
CCR0 = 20000; //PWM信号频率1KHZ
CCTL1 = OUTMOD_7;
CCTL2 = OUTMOD_7;
TACTL=TASSEL_2 + MC_1 + ID_3 ;//
}
/********************ADC函数********************/
void initADC12(void)
{
P6DIR|=0X00;
P6SEL|=0X0F;
ADC12CTL0=ADC12ON+MSC+SHT0_8+REFON+REF2_5V;
ADC12CTL1=SHP+CONSEQ_3;
ADC12MCTL0=SREF_1+INCH_0;
ADC12MCTL0=SREF_1+INCH_1;
ADC12MCTL0=SREF_1+INCH_2;
ADC12MCTL0=SREF_1+INCH_3+EOS;
ADC12IE=0X08;
ADC12CTL0|=ENC;
_EINT();
ADC12CTL0|=ADC12SC;//开始转换
}
# pragma vector=ADC_VECTOR
__interrupt void ADC12ISR(void)
{
A0result[index]=ADC12MEM0;
A1result[index]=ADC12MEM1;
A2result[index]=ADC12MEM2;
A3result[index]=ADC12MEM3;
index=(index+1)%Num_Of_Results;
}
/********************延时函数********************/
void Delay(uint num)
{
uint kk,i;
for(i=0; i<num; i++)
for(kk=0; kk<8000; kk++);
}
void main( void )
{
WDTCTL = WDTPW+WDTHOLD;
//IE1 |= WDTIE;
InitClock();//推荐选择
init_pwm();
initADC12();
x = CCR1;
y = CCR2;
for(;;)
{
x0=A0result[index];
x1=A1result[index];
x2=A2result[index];
x3=A3result[index];
if(x0>x2)x=x+180;
else if(x0<x2)x=x-180;
else
x=0;
if(x1>x3)y=y+180;
else if(x1<x3)y=y-180;
else
y=0;
}
}
王彪 15818572651
QQ 3103278211
地址:深圳市南山区高新技术产业园北区紫光信息港A栋10层
公司专注于LED点光源驱动IC的生产设计和研发,原厂供应
一周热门 更多>