📄 main1.c
字号:
lenth=3;
else if(dis>9)
lenth=2;
sentopcbuf[8]='D';
for(j=0;j<5-lenth;j++)
{
sentopcbuf[j+9]='0';
}
for(i=0;i<lenth;i++)
{
sentopcbuf[14-lenth+i]=distance[i];
}
sentopcbuf[14]='E';
BridgeBuf[0]='C';
BridgeBuf[8]='C';
}
if((sentopcbuf[0]=='A')&&(sentopcbuf[6]=='P')&&(sentopcbuf[8]=='D')&&(sentopcbuf[14]=='E'))
{
USART0SendData(sentopcbuf, 15);
for(i=0; i<0x2fff; i++)
{
_NOP();
}
sentopcbuf[0]=='C';
}
} /* End of sentopc() */
/* 无线传输模块函数 */
void
SetTxMode(void)
{
P4OUT=CS+PWR+TXEN;
Delay(1); /* delay for mode change(>=650us) */
} /* End of SetTxMode() */
void
SetRxMode(void)
{
P4OUT=CS+PWR;
Delay(1); // delay for mode change(>=650us)
} /* End of SetRxMode() */
/* 步进电机控制函数 */
void
stepzheng(unsigned int step_n)
{
unsigned int i;
unsigned int flag;
flag=1;
step_num=0;
CCTL0=CCIE;
for(i=1;i<=step_n;i++)
{
do
{
if(step_num>16)
{
CCTL0=0;
flag=0;
}
////////////////////////////////////////////////////////////////
/*
if((BridgeBuf[0]=='A')&&(BridgeBuf[10]=='E'))
{
USART0SendData(BridgeBuf, 11);
// USART0SendData(suc, 14);
//for(i=0; i<0xff; i++) _NOP();
Delay(1);
BridgeBuf[0]='C';
}
*/
if(rcfrpcbuf[3]=='0')
{
CCTL0=0;
flag=0;
}
}while(flag);
if((rcfrpcbuf[3]=='0')||(rcfrpcbuf[1]=='1'))
{
break;
}
}
} /* End of stepzheng() */
void
stepfan(unsigned int step_n)
{
unsigned int i;
unsigned int flag,lenth,j,stepchange;
flag=1;
step_num=0;
CCTL0=CCIE;
stepchange=step_n;
for(i=1;i<=step_n;)
{
CCTL0=CCIE;
do
{
if(step_num>15)
{
i++;
CCTL0=0;
flag=0;
step_num=0;
stepchange--;
/*
CCTL0=0;
if(((BridgeBuf[0]=='A')&&(BridgeBuf[8]=='E'))&&(BridgeBuf[6]=='P'))
{
sentopcbuf[0]='A';
for(i=1;i<6;i++)
sentopcbuf[i]='9';
sentopcbuf[6]='P';
sentopcbuf[7]='1';
Distance(0,stepchange);
itoa(dis,distance,10);
lenth=1;
if(dis>9999)
lenth=5;
else if(dis>999)
lenth=4;
else if(dis>99)
lenth=3;
else if(dis>9)
lenth=2;
sentopcbuf[8]='D';
for(j=0;j<5-lenth;j++)
{sentopcbuf[j+9]='0';}
for(i=0;i<lenth;i++)
{sentopcbuf[14-lenth+i]=distance[i];}
sentopcbuf[14]='E';
BridgeBuf[0]='C';
BridgeBuf[8]='C';
}
if((sentopcbuf[0]=='A') && (sentopcbuf[6]=='P')
&&(sentopcbuf[8]=='D') && (sentopcbuf[14]=='E'))
{
USART0SendData(sentopcbuf, 15);
for(i=0; i<0xfff; i++) _NOP();
sentopcbuf[0]=='C';
}
CCTL0=CCIE;
*/
}
}while(flag);
}
CCTL0=0;
//Delay(5);
USART0SendData(resettopcbuf, 15);
// USART0SendData(huiche, 5);
// USART0SendData(sentopcbuf, 15);
for(i=0; i<0xff; i++)
{
_NOP();
}
} /* End of stepfan() */
void
OneCircle(void)
{
unsigned int flag;
unsigned int i,j,lenth;
flag=1;
step_num=0;
CCTL0=CCIE;
stepall=0;
stepcount=0;
do
{
if(step_num>3200)
{
CCTL0=0;
flag=0;
}
////////////////////////////////////////////////////////////////
//flag1=1;
/*
if((step_num%4)==0)
{
if((BridgeBuf[0]=='A')&&(BridgeBuf[10]=='E'))
{
Distance(circleall,stepall);
itoa(dis,distance,10);
USART0SendData(huiche,2);
for(i=0; i<0xf; i++) _NOP();
USART0SendData(distance, 4);
//for(i=0; i<0xff; i++) _NOP();
BridgeBuf[0]='C';
BridgeBuf[10]='C';
}
}
*/
if(((step_num%16)==0)&&(flag1==1))
{
stepall++;
flag1=0;
CCTL0=0;
if(((BridgeBuf[0]=='A')&&(BridgeBuf[8]=='E'))&&(BridgeBuf[6]=='P'))
{
for(i=0;i<8;i++)
{
sentopcbuf[i]=BridgeBuf[i];
}
Distance(circleall,stepall);
itoa(dis,distance,10);
lenth=1;
if(dis>9999)
{
lenth=5;
}
else if(dis>999)
{
lenth=4;
}
else if(dis>99)
{
lenth=3;
}
else if(dis>9)
{
lenth=2;
}
sentopcbuf[8]='D';
for(j=0;j<5-lenth;j++)
{
sentopcbuf[j+9]='0';
}
for(i=0;i<lenth;i++)
{
sentopcbuf[14-lenth+i]=distance[i];
}
sentopcbuf[14]='E';
BridgeBuf[0]='C';
BridgeBuf[8]='C';
}
if((sentopcbuf[0]=='A') && (sentopcbuf[6]=='P')
&& (sentopcbuf[8]=='D') && (sentopcbuf[14]=='E'))
{
USART0SendData(sentopcbuf, 15);
for(i=0; i<0xfff; i++) _NOP();
sentopcbuf[0]=='C';
}
CCTL0=CCIE;
}
if((rcfrpcbuf[3]=='0')||(rcfrpcbuf[1]=='1'))
{
CCTL0=0;
flag=0;
}
flag1=0;
}while(flag);
} /* End of OneCircle() */
void
OneCirclefan(void)
{
unsigned int flag;
unsigned int i,lenth,j,cha;
flag=1;
flag1=0;
stepf=200;
step_num=0;
CCTL0=CCIE;
do
{
if(step_num>3200)
{
CCTL0=0;
flag=0;
}
if(((step_num%128)==0)&&(flag1==1))
{
flag1=0;
CCTL0=0;
if(((BridgeBuf[0]=='A')&&(BridgeBuf[8]=='E'))&&(BridgeBuf[6]=='P'))
{
sentopcbuf[0]='A';
for(i=1;i<6;i++)
sentopcbuf[i]='9';
sentopcbuf[6]='P';
sentopcbuf[7]='1';
//cha=Distance(1,0);
Distance(circleall-2,(stepf+stepcountever));
//dis=dis-cha;
itoa(dis,distance,10);
lenth=1;
if(dis>9999)
lenth=5;
else if(dis>999)
lenth=4;
else if(dis>99)
lenth=3;
else if(dis>9)
lenth=2;
sentopcbuf[8]='D';
for(j=0;j<5-lenth;j++)
{sentopcbuf[j+9]='0';}
for(i=0;i<lenth;i++)
{sentopcbuf[14-lenth+i]=distance[i];}
sentopcbuf[14]='E';
BridgeBuf[0]='C';
BridgeBuf[8]='C';
}
if((sentopcbuf[0]=='A') && (sentopcbuf[6]=='P') &&
(sentopcbuf[8]=='D') && (sentopcbuf[14]=='E'))
{
USART0SendData(sentopcbuf, 15);
for(i=0; i<0xfff; i++) _NOP();
sentopcbuf[0]=='C';
}
CCTL0=CCIE;
}
if(rcfrpcbuf[3]=='0')
{
CCTL0=0;
flag=0;
}
flag1=0;
//
///////////////////////////////////////////////////////////////////
}while(flag);
} /* End of OneCirclefan() */
#if 0
void MotorTurn(unsigned int circle_num,unsigned char direction)
{
unsigned int i;
unsigned int l;
if(direction=='0')
{P2OUT|=0x02;}
else P2OUT&=0xfd;
if(rcfrpcbuf[3]=='1')
for(i=0;i<circle_num;i++)
{if(rcfrpcbuf[3]=='1')
OneCircle();
else if(rcfrpcbuf[3]=='0')
break;
}
}
#endif
#if 0
void
MotorTurn(unsigned int circle_n,unsigned int step_n,unsigned char direction)
{
unsigned int i;
circleall=0;
if(direction=='0')
{P2OUT|=0x02;}
else P2OUT&=0xfd;
if(rcfrpcbuf[3]=='1')
for(i=0;i<circle_n;i++)
{if(rcfrpcbuf[3]=='1')&&(rcfrpcbuf[1]=='0')
{ OneCircle();
circleall++;}
else if((rcfrpcbuf[3]=='0')||(rcfrpcbuf[1]=='0'))
break;
}
//step(step_n);
}
#endif
void
Motorzheng(unsigned int circle_n,unsigned int step_n)
{
unsigned int i;
circleall=0;
P2OUT|=0x02;
// else P2OUT&=0xfd;
if(rcfrpcbuf[3]=='1')
{
for(i=0;i<circle_n;i++)
{
if((rcfrpcbuf[3]=='1')&&(rcfrpcbuf[1]=='0'))
{
OneCircle();
circleall++;}
else if((rcfrpcbuf[3]=='0')||(rcfrpcbuf[1]=='1'))
{
break;
}
}
}
// stepzheng(step_n);
} /* End of Motorzheng() */
void
Motorfan(unsigned int circle_n,unsigned int step_n)
{
unsigned int i;
// circleall=0;
P2OUT&=0xfd;
// if(rcfrpcbuf[3]=='1')
for(i=0;i<circle_n;i++)
// {if((rcfrpcbuf[3]=='1')&&(rcfrpcbuf[1]=='1'))
{
OneCirclefan();
circleall--;
// }
// else if((rcfrpcbuf[3]=='0')||(rcfrpcbuf[1]=='0'))
// break;
}
stepfan(step_n);
} /* Emd of Motorfan() */
unsigned int
Distance(INT8U circle,INT8U step)
{
unsigned int i,n,D,DC,DS,DT;
n=circle;
D=0;
DC=0;
DS=0;
DT=0;
for(i=0;i<n;i++)
{
DT=3.1416*(169-i*1);
DC=DC+DT;
}
DS=3.1416*(169-n*1)*step/200;
dis=DC+DS;
return dis;
} /* End of Distance() */
unsigned int
CheckSum(unsigned char* buf, unsigned char len)
{
unsigned char i;
unsigned int checkdata;
checkdata=0;
for(i=0;i<len;i++)
{
checkdata+=buf[i];
}
return checkdata;
} /* End of CheckSum() */
INT8U suc[]="A12345D1011P1E",can[]="S";
INT8U head=0,tail=0,inter,k;
void
main(void)
{
unsigned int uiCounterm = 0,uiFlg = 0,check=0;
INT32 i,j,lenth;
WDTCTL=WDTPW+WDTHOLD;
BCSCTL1=0x0; // ACLK = LFXT1 = HF XTAL
BCSCTL2 |= SELM_2+SELS;
/********************等待时钟源转换结束**************************/
do
{
IFG1 &= ~OFIFG; // Clear OSCFault flag
for (i = 0xFF; i > 0; i--); // Time for flag to set
}
while ((IFG1 & OFIFG) != 0); // OSCFault flag still set?
/**************************I/O口初始化**************************/
P5DIR |= GAS + WATER + OIL + POWER;//
P5OUT |= GAS;
P5OUT &= ~POWER;
P4SEL |= BIT7;//TB COUNT INIT
P4DIR &= ~BIT7;
//比较器 由CA1输入,接于正端,参考源接于负端,开比较器,下降沿中断,中断允许
P2SEL |= BIT4;
P2DIR &= ~BIT4;
CACTL2 = P2CA1 ;
CACTL1 = 0;
//CACTL1= CAREF_2 + CAON + CAIE;// + CAIES;Comp. A Int. Ref. Select 2 : 0.5*Vcc */
CACTL1= CAREF_2 + CAON;
// CACTL1 |= /*CAIE +*/ ;
// 外部引脚连接到比较器,比较信号经过RC滤波
CCR0 = 700;//10ms
CCR2 = 1000;//15MS
//TBCCR0 = 0X0FFFF;
// CCTL2 |= OUTMOD_7 + CCIE;//INTERRUPT ENABLE
TACTL |= TASSEL_2 + ID_3 + MC_1 + TACLR ;// smclk,60ms //can 1/8分频增计数模式,计数到ccro,再清零计数;
CCTL0=0;
TBCTL |= TBSSEL_0 + TBCLR + MC_1;//STAR TB选择外部引脚信号作为输入源,增计数模式
P2DIR =0xff;
P2OUT =0x02;
P1DIR =0xff;
P1OUT =0x00;
P5OUT &= ~(WATER + OIL);
P4DIR=0xff;
P4OUT=0x0;
Uart0Init();
Uart1Init();
_EINT();
SetRxMode();
Delay(2);
dis=0;
sentopcbuf[8]='D';
for(j=0;j<5;j++)
{sentopcbuf[j+9]='0';}
stepf=200;
flagall=1;
while(1)
{
if(flagall==1)
{ if(((BridgeBuf[0]=='A')&&(BridgeBuf[8]=='E'))&&(BridgeBuf[6]=='P'))
{
for(i=0;i<8;i++)
sentopcbuf[i]=BridgeBuf[i];
if(circleall==0)circleall=1;
Distance(circleall-1,stepall);
itoa(dis,distance,10);
lenth=1;
if(dis>9999)
lenth=5;
else if(dis>999)
lenth=4;
else if(dis>99)
lenth=3;
else if(dis>9)
lenth=2;
sentopcbuf[8]='D';
for(j=0;j<5-lenth;j++)
{sentopcbuf[j+9]='0';}
for(i=0;i<lenth;i++)
{sentopcbuf[14-lenth+i]=distance[i];}
sentopcbuf[14]='E';
BridgeBuf[0]='C';
BridgeBuf[8]='C';
}
if((sentopcbuf[0]=='A')&&(sentopcbuf[6]=='P')&&(sentopcbuf[8]=='D')&&(sentopcbuf[14]=='E'))
{
USART0SendData(sentopcbuf, 15);
for(i=0; i<0xfff; i++) _NOP();
sentopcbuf[0]=='C';
}
}
_NOP();
if(flagall==0)
{ USART0SendData(resettopcbuf, 15);
for(i=0; i<0xfff; i++) _NOP();}
Delay(6000);
USART0RecvData(rcfrpcbuf, 6);
for(i=0; i<0x2fff; i++) _NOP();
if(((rcfrpcbuf[2]=='K')&&(rcfrpcbuf[3]=='1'))&&(rcfrpcbuf[1]=='0'))
{
CCTL0=CCIE;
flagall=1;
Motorzheng(25,0);
rcfrpcbuf[2]='0';
}
if((rcfrpcbuf[0]=='R')&&(rcfrpcbuf[1]=='1')&&(rcfrpcbuf[3]=='1'))
{
stepcountever=stepcount;
flagall=0;
Motorfan(circleall-1,stepcount);
rcfrpcbuf[0]='r';
}
}
} /* End of main() */
#pragma vector=TIMERA0_VECTOR
__interrupt void
Timer_CCR0(void)
{
P2OUT^=0x01;
P5OUT^=0x04;
step_num++;
flag1=1;
if((step_num%16)==0)
{
stepcount++;
stepf--;
}
//CCTL2 &= ~CCIFG;//软件清除中断标志
} /* End of Timer_CCR0() */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -