本帖最后由 panfuqiang215 于 2011-7-9 15:25 编辑
if(((b2-b1)-(b2-b3))>5){
TACCR1= TACCR1+16;
delay(60000);
TACCR1=49;
}
if(((b2-b3)-(b2-b1))>5){
TACCR1= TACCR1-16;
delay(60000);
TACCR1=49;
}
else TACCR1=49;
b1 b2 b3 是ad采集的数据量 通过比较数据量的,从而决定舵机的转向 为什么我这里的舵机总是不停的来回转 停不了 是什么原因 周期是655 用的是定时器A
#include <msp430x14x.h>
#define uchar unsigned char
#define uint unsigned int
#define RS_HIGH P3OUT|=BIT5
#define RS_LOW P3OUT&=~BIT5
#define RW_HIGH P3OUT|=BIT6
#define RW_LOW P3OUT&=~BIT6
#define E_HIGH P3OUT|=BIT7
#define E_LOW P3OUT&=~BIT7
static unsigned int results[4]; // Needs to be global in this example
unsigned int sum=0,sum1=0,sum2=0; // Otherwise, the compiler removes it
unsigned int a=0,b=0,c=0,d=0,e=0,f=0;
unsigned int b1,b2,b3,b4,b5;
unsigned char table[12];
unsigned int live[12];
unsigned int live1[12];
unsigned int live2[12];
// because it is not used for anything.
void delay(unsigned int t)
{
while(t>0)t--;
}
void DelayMS(unsigned char ms)
{
unsigned int i;
while(ms--){
for(i=0; i<800;i++);
}
}
void WriteCommand(uchar Command)
{
RS_LOW;
RW_LOW;
E_HIGH;
_NOP();
_NOP();
// DelayMS(5);
P4OUT=Command;
_NOP();
_NOP();
// DelayMS(5);
E_LOW;
}
//
void WriteData(uchar Data)
{
RS_HIGH;
RW_LOW;
E_HIGH;
_NOP();
_NOP();
//DelayMS(5);
P4OUT=Data;
_NOP();
_NOP();
// DelayMS(5);
E_LOW;
}
//
void LcdInit(void)
{
WriteCommand(0x38);//
// DelayMS(5);
WriteCommand(0x0c);//
// DelayMS(5);
WriteCommand(0x06);//
// DelayMS(5);
WriteCommand(0x01);//
// DelayMS(5);
}
void Display()
{
if(a>=8)
{
for(d=0;d<8;d++)
{
sum=sum+live[d];
}
sum=sum/8;
b1=sum;
a=0;
table[0]=sum/1000;
table[1]=sum%1000/100;
table[2]=sum%1000%100/10;
table[3]=sum%1000%100%10;
sum=0;
}
if(b>=8)
{
for(e=0;e<8;e++)
{
sum1=sum1+live1[e];
}
sum1=sum1/8;
b2=sum1;
b=0;
table[4]=sum1/1000;
table[5]=sum1%1000/100;
table[6]=sum1%1000%100/10;
table[7]=sum1%1000%100%10;
sum1=0;
}
if(c>=8)
{
for(f=0;f<8;f++)
{
sum2=sum2+live2[f];
}
sum2=sum2/12;
b3=sum2;
c=0;
table[8]=sum2/1000;
table[9]=sum2%1000/100;
table[10]=sum2%1000%100/10;
table[11]=sum2%1000%100%10;
sum2=0;
}
uchar i;
WriteCommand(0x80);
for(i=0;i<4;i++)
{
WriteData(table+0x30);
}
WriteData(' ');
for(i=4;i<8;i++)
{
WriteData(table+0x30);
}
WriteData(' ');
for(i=8;i<12;i++)
{
WriteData(table+0x30);
}
}
//////////////////////////PWM///////////////////////////
void PWM_TA()
{
P1DIR |=BIT5+BIT6+BIT7; // P1.5,P1.6 and P1.7 output
P1SEL |=BIT5+BIT6+BIT7; // P1.5,P1.6 and P1.7 TB1/2 otions
TACCR0 = 655; // PWM Period/2
TACCTL1 = OUTMOD_7; // CCR1 toggle t
TACCR1 = 1500; // CCR1 PWM duty cycle
TBCCTL2 = OUTMOD_7; // CCR2 toggle t
TBCCR2 = 1500; // CCR2 PWM duty cycle*/
TACTL = TASSEL_1 + MC_1; // ACLK, up mode
}
/*************************************************************
*启用XT2振荡器
**************************************************************/
void InitClock()
{
unsigned int iq0;
//使用XT2振荡器
BCSCTL1&=~XT2OFF; //打开XT2振荡器
do
{
IFG1 &= ~OFIFG; // 清除振荡器失效标志
for (iq0 = 0xFF; iq0 > 0; iq0--); // 延时,等待XT2起振
}
while ((IFG1 & OFIFG) != 0); // 判断XT2是否起振
BCSCTL2 =SELM_2+SELS; //选择MCLK、SMCLK为XT2
}
void main(void)
{
WDTCTL = WDTPW+WDTHOLD; // Stop watchdog timer
// InitClock();
PWM_TA(); //PWM初始化
P4DIR=0XFF;
P4OUT=0X00;
P3DIR=0XFF;
P3OUT=0X00;
P6SEL = 0x0F; // Enable A/D channel inputs
ADC12CTL0 &= ~ENC;
ADC12CTL0 = ADC12ON+MSC+SHT0_8; // Turn on ADC12, set sampling time
ADC12CTL1 = SHP+CONSEQ_1; // Use sampling timer, single sequence
ADC12MCTL0=SREF_2;
ADC12MCTL0 = INCH_0; // ref+=AVcc, channel = A0
ADC12MCTL1 = INCH_1; // ref+=AVcc, channel = A1
ADC12MCTL2 = INCH_2; // ref+=AVcc, channel = A2
ADC12MCTL3 = INCH_3+EOS; // ref+=AVcc, channel = A3, end seq.
ADC12IE = 0x08; // Enable ADC12IFG.3
ADC12CTL0 |= ENC; // Enable conversions
LcdInit();
while(1)
{
ADC12CTL0 |= ADC12SC; // Start conversion
_BIS_SR(LPM0_bits + GIE); // Enter LPM0, Enable interrupts
Display();
if(((b2-b1)-(b2-b3))>5){
TACCR1= TACCR1+16;
delay(60000);
TACCR1=49;
}
if(((b2-b3)-(b2-b1))>5){
TACCR1= TACCR1-16;
delay(60000);
TACCR1=49;
}
else TACCR1=49;
}
}
#pragma vector=ADC12_VECTOR
__interrupt void ADC12ISR (void)
{
results[0] = ADC12MEM0; // Move results, IFG is cleared
live[a++]=results[0];
results[1] = ADC12MEM1; // Move results, IFG is cleared
live1[b++] = results[1];
results[2] = ADC12MEM2; // Move results, IFG is cleared
live2[c++]=results[2];
results[3] = ADC12MEM3; // Move results, IFG is cleared
_BIC_SR_IRQ(LPM0_bits); // Clear LPM0, SET BREAKPOINT HERE
} |