本想着是通过串口发送字符串来控制方波的占空比,不知道为什么只有第一次发送有效,求高手帮下忙。
unsigned char Re_buf[11],counter=0;
unsigned char sign,sign2,Rightflag,qianjin,houtui,sign3;
static unsigned char Temp[11];
u16 b;
u8 USART_RX_BUF[20];
u16 USART_RX_STA=0;
u16 zkbl ;
u16 zkbr ;
void uart_init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG|RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate=bound;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_InitStructure);
}
void USART1_IRQHandler(void)
{
u8 Res;
if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
{
Res =USART_ReceiveData(USART1); //¶á衽óêÕμ½μÄêy¾Y
USART_SendData(USART1,Res);
if((USART_RX_STA&0x4000)==0)//½óêÕÎ′íê3é
{
if(Res==0xFF)USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
}
}
}
b=(USART_RX_BUF[0]<<8|USART_RX_BUF[1]);
if (b==0XCC1C)
{ IN2R=1;
IN2L=0;
zkbl=10;
zkbr=10;
}
if (b==0XAA1A)
{ qianjin=1;
IN2R=1;
IN2L=1;
zkbl=30;
zkbr=30;
}
if (b==0XBB1B)
{
houtui=1;
IN2L=0;
IN2R=0;
zkbl=50;
zkbr=50;
}
if (b==0XDD1D)
{
Rightflag=1;
IN2R=0;
IN2L=1;
zkbl=200;
zkbr=213;
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>