中级会员
- 积分
- 329
- 金钱
- 329
- 注册时间
- 2014-2-25
- 在线时间
- 44 小时
|
楼主 |
发表于 2014-6-13 14:54:39
|
显示全部楼层
回复【15楼】RJJ3872288:
---------------------------------
//GPIO初始化
void RS485_GPIOConfig(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE); //GPIOD clock
GPIO_PinAFConfig(GPIOG, GPIO_PinSource14, GPIO_AF_USART6); //uart6 TX
GPIO_PinAFConfig(GPIOG, GPIO_PinSource9 , GPIO_AF_USART6); //uart6 RX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_Init(GPIOG, &GPIO_InitStructure);
}
//优先级
void RS485_NVICConfig(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x05;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void RS485_Config(u8 port,u32 BaudRate)
{
USART_DeInit(USART6);
RS485_GPIOConfig();
RS485_NVICConfig();
USART_StructInit(&USART_InitStructure);
USART_InitStructure.USART_BaudRate = BaudRate;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART6, &USART_InitStructure);
USART_ITConfig (USART6, USART_IT_RXNE, ENABLE);
USART_ClearFlag(USART6, USART_FLAG_TC);
USART_Cmd(USART6, ENABLE);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
//Usart6 handler
void USART6_IRQHandler(void)
{
if(USART_GetITStatus(USART6,USART_IT_RXNE) != RESET) // ???????????§
{
// delay_us(1);
USARTx_RecvBuff[UART6_PORT][RS485_RecvCnt[UART6_PORT]]=USART_ReceiveData(USART6);
RS485_RecvCnt[UART6_PORT]++;
if(USARTx_RecvBuff[UART6_PORT][RS485_RecvCnt[UART6_PORT]-1]==0xff)
{
RS485_RecvFlag[UART6_PORT]=1;
RS485_RecvLen[UART6_PORT] =RS485_RecvCnt[UART6_PORT];
RS485_RecvCnt[UART6_PORT]=0x00;
}
USART_ClearITPendingBit(USART6,USART_IT_RXNE); // ????USART1??????????
}
}
/*************************************************************/
void Car_controal(void)
{
u8 i;
for(i=0;i<5;i++)
{
Car_order=USARTx_RecvBuff[6];
}
switch(Car_order[1])
{
case 0xAA: DCMotor_Stop(5,0); break;
case 0x11: DCMotor4_MoveForward(5,85); break;
case 0x22: DCMotor4_MoveBackward(5,85); break;
case 0x33: DCMotor4_RotateLeft(5,85); break;
case 0x44: DCMotor4_RotateRight(5,85); break;
}
} |
|