void USART_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //ÉùÃ÷GPIO³õʼ»¯½á¹¹Ìå
USART_InitTypeDef USART_InitStructure; //ÉùÃ÷USART³õʼ»¯½á¹¹Ìå
NVIC_InitTypeDef NVIC_InitStructure;
/*GPIOA Periph clock enable*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/*USART3 Periph clock enable*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
USART_InitStructure.USART_BaudRate = Baud; //²¨ÌØÂÊÉèÖÃ
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;
/* Configure USART3 */
USART_Init(USART3, &USART_InitStructure);
/* Enable USART3 Receive and Transmit interrupts */
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //ʹÄܽÓÊÕÖжÏ
USART_ITConfig(USART3, USART_IT_TC, ENABLE); //·¢ËÍÖжϣ¬·¢ËÍÍê³ÉÖ®ºó½øÈëÖжÏ
/* Configure USART3 Tx (PB.10) as alternate function open-drain */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //ÅäÖÃTXËùÔÚµÄPB10¹Ü½Å
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //ÕâÀï²»ÄÜ´í
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure USART3 Rx as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;//ʹÄÜGPIOʱÖÓ£¬RS485¿ØÖÆCTRÏßÒ²ÔÚÕâÀïʹÄÜ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//Usart3 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//ÇÀÕ¼ÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×ÓÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀʹÄÜ
NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷
/* Enable the USART3 */
USART_Cmd(USART3, ENABLE);//ʹÄÜUSART3
}
void USART_SendDataString(u8 *dataSource)
{
isUsartBusy = 1;//±íʾÕýÔÚ·¢ËÍ
PtrBuf = dataSource;
USART_ClearFlag(USART3, USART_FLAG_TC);//Çå³ý´«ÊäÍê³É±ê־룬·ñÔò¿ÉÄܶªÊ§µÚÒ»¸ö×Ö½Ú
USART_SendData(USART3, PtrBuf[BufferDataNum++]);//·¢ËÍÒ»¸ö×Ö½Ú
}
|