|
- #include "stm32f4xx.h"
- #include "misc.h"
- #include "stm32f4xx_gpio.h"
- #include "stm32f4xx_rcc.h"
- #include "stm32f4xx_usart.h"
- #include "stm32f4xx_crc.h"
- #define uart6_EN_1 GPIO_SetBits(GPIOB, GPIO_Pin_15); //485發(fā)送使能
- #define uart6_EN_0 GPIO_ResetBits(GPIOB, GPIO_Pin_15); //485接收使能
- #define uart3_EN_1 GPIO_SetBits(GPIOD, GPIO_Pin_10); //485發(fā)送使能
- #define uart3_EN_0 GPIO_ResetBits(GPIOD, GPIO_Pin_10); //485接收使能
- void NVIC_Config(void) {
- NVIC_InitTypeDef NVIC_InitStructure;
- /* Enable the USARTx Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;
- NVIC_Init(&NVIC_InitStructure);
- }
- void USART_Config(void) {
- USART_InitTypeDef USART_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); //使能GPIOD時(shí)鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);//使能USART3時(shí)鐘
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);//使能USART6時(shí)鐘
-
-
-
- /* ---------------串口3 IO口----------------- */
- GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3); //PD8復(fù)用為串口3
- GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3); //PD9復(fù)用為串口3
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;//GPIO8與GPIO9
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //復(fù)用功能
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽復(fù)用輸出
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
- GPIO_Init(GPIOD,&GPIO_InitStructure); //初始化PD8、PD9
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PD10 控制485_2的輸入
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //輸出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽輸出
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- /* --------------------------------------------- */
-
-
-
- /* ---------------串口6 IO口----------------- */
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);//PD6復(fù)用為串口6
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);//PD7復(fù)用為串口6
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; //GPIO6與GPIO7
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //復(fù)用功能
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽復(fù)用輸出
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
-
- GPIO_Init(GPIOD,&GPIO_InitStructure); //初始化PD6、PD7
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; //PB15控制485_1的輸入
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //輸出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽輸出
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- /* --------------------------------------------- */
- /* USART configured as follow: //串口配置如下:
-
- - BaudRate = 115200 baud //波特率:115200
- - Word Length = 8 Bits //字節(jié)長度=8比特
- - One Stop Bit //一個(gè)停止位
- - No parity //無校驗(yàn)碼位
- - Hardware flow control disabled (RTS and CTS signals)//禁止硬件流控制
- - Receive and transmit enabled //接受發(fā)送已啟動
- */
- USART_InitStructure.USART_BaudRate = 9600;//串口的波特率設(shè)置為9600bps
- 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;//設(shè)置串口既可以發(fā)送也可以接收數(shù)據(jù)
- USART_Init(USART3, &USART_InitStructure);//設(shè)置串口3
- USART_Init(USART6, &USART_InitStructure);//設(shè)置串口6
- USART_ITConfig(USART6, USART_IT_TC, ENABLE);
- USART_ITConfig(USART6, USART_IT_IDLE, ENABLE);
- USART_ITConfig(USART6, USART_IT_RXNE, ENABLE);
- USART_ITConfig(USART3, USART_IT_TC, ENABLE);
- USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);
- USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
- USART_Cmd(USART6, ENABLE);
- USART_Cmd(USART3, ENABLE);
- uart6_EN_0;
- uart3_EN_0;
- }
- int main(void) {
- NVIC_Config();
- USART_Config();
- while (1) {
- ; //中斷里實(shí)現(xiàn)串口收發(fā),數(shù)據(jù)處理
- }
- }
- #if 0 /*串口6調(diào)通,收什么發(fā)什么*/
- uint8_t uart6_RxBuffer[500];//設(shè)置數(shù)據(jù)緩沖
- uint8_t uart6_TxCounter = 0;
- uint16_t uart6_RxCounter = 0;
- void USART3_IRQHandler(void) {
- if (USART_GetITStatus(USART3, USART_IT_RXNE) != RESET){
- uart6_RxBuffer[uart6_RxCounter++] = USART_ReceiveData(USART3);
- }
- if (USART_GetITStatus(USART3, USART_IT_IDLE) != RESET) {
- USART_ReceiveData(USART3); //清中斷
- USART_ITConfig(USART3, USART_IT_RXNE, DISABLE); //禁止接收
- USART_ITConfig(USART3, USART_IT_IDLE, DISABLE);
- //數(shù)據(jù)處理
- uart6_EN_1;
- USART_ITConfig(USART3, USART_IT_TC, ENABLE);
- uart6_TxCounter = 0;
- USART_SendData(USART3, uart6_RxBuffer[uart6_TxCounter++]);
- }
- if (USART_GetITStatus(USART3, USART_IT_TC) != RESET){
- USART_SendData(USART3, uart6_RxBuffer[uart6_TxCounter++]);
- if (uart6_TxCounter > uart6_RxCounter){
- //開啟接收,禁止發(fā)送
- uart6_EN_0;
- uart6_RxCounter = 0;
- USART_ITConfig(USART3, USART_IT_TC, DISABLE);
- USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
- USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);
- }
- }
- }
- uint8_t uart3_RxBuffer[500];
- uint8_t uart3_TxCounter = 0;
- uint16_t uart3_RxCounter = 0;
- void USART3_IRQHandler(void) {
- if (USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) {
- uart3_RxBuffer[uart3_RxCounter++] = USART_ReceiveData(USART3);
- }
- if (USART_GetITStatus(USART3, USART_IT_IDLE) != RESET) {
- USART_ReceiveData(USART3); //清中斷
- USART_ITConfig(USART3, USART_IT_RXNE, DISABLE); //禁止接收
- USART_ITConfig(USART3, USART_IT_IDLE, DISABLE);
- //數(shù)據(jù)處理
- uart3_EN_1;
- USART_ITConfig(USART3, USART_IT_TC, ENABLE);
- uart3_TxCounter = 0;
- USART_SendData(USART3, uart3_RxBuffer[uart3_TxCounter++]);
- }
- if (USART_GetITStatus(USART3, USART_IT_TC) != RESET) {
- USART_SendData(USART3, uart3_RxBuffer[uart3_TxCounter++]);
- if (uart3_TxCounter > uart3_RxCounter) {
- //開啟接收,禁止發(fā)送
- uart3_EN_0;
- uart3_RxCounter = 0;
- USART_ITConfig(USART3, USART_IT_TC, DISABLE);
- USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
- USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);
- }
- }
- }
- #endif
- /* 串口3收到數(shù)據(jù)從串口6發(fā)送,串口6收到數(shù)據(jù)從串口3發(fā)送 */
- #if 1
- uint8_t uart6_RxBuffer[500];
- uint8_t uart6_TxBuffer[500];
- uint8_t uart6_TxSize = 0;
- uint8_t uart6_TxCounter = 0;
- uint8_t uart6_RxCounter = 0;
- uint8_t uart3_RxBuffer[500];
- uint8_t uart3_TxBuffer[500];
- uint8_t uart3_TxSize = 0;
- uint8_t uart3_TxCounter = 0;
- uint8_t uart3_RxCounter = 0;
- void src_cpy2_des(uint8_t* src, uint8_t* des, uint8_t len) {
- while (len--) *des++ = *src++;
- }
- void USART6_IRQHandler(void) {
- if (USART_GetITStatus(USART6, USART_IT_RXNE) != RESET) {
- uart6_RxBuffer[uart6_RxCounter++] = USART_ReceiveData(USART6);
- }
- if (USART_GetITStatus(USART6, USART_IT_IDLE) != RESET) {
- USART_ReceiveData(USART6); //清中斷
- uart3_EN_1;
- uart3_TxCounter = 0;
- uart3_TxSize = uart6_RxCounter;
- src_cpy2_des(uart6_RxBuffer, uart3_TxBuffer, uart3_TxSize);
- do { /* 用戶數(shù)據(jù)處理寫在此處 */
- } while (0);
- USART_SendData(USART3, uart3_TxBuffer[uart3_TxCounter++]);
- uart6_RxCounter = 0;
- }
- if (USART_GetITStatus(USART6, USART_IT_TC) != RESET) {
- if (uart6_TxCounter > uart6_TxSize) {
- uart6_EN_0; //開啟接收,禁止發(fā)送
- }
- else{
- USART_SendData(USART6, uart6_TxBuffer[uart6_TxCounter++]);
- }
- USART_ClearFlag(USART6, USART_FLAG_TC);
- }
- }
- void USART3_IRQHandler(void) {
- if (USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) {
- uart3_RxBuffer[uart3_RxCounter++] = USART_ReceiveData(USART3);
- }
- if (USART_GetITStatus(USART3, USART_IT_IDLE) != RESET) {
- USART_ReceiveData(USART3); //清中斷
- uart6_EN_1;
- uart6_TxCounter = 0;
- uart6_TxSize = uart3_RxCounter;
- src_cpy2_des(uart3_RxBuffer, uart6_TxBuffer, uart6_TxSize);
- do { /* 用戶數(shù)據(jù)處理寫在此處 */
- } while (0);
- USART_SendData(USART6, uart6_TxBuffer[uart6_TxCounter++]);
- uart3_RxCounter = 0;
- }
- if (USART_GetITStatus(USART3, USART_IT_TC) != RESET) {
- if (uart3_TxCounter > uart3_TxSize) {
- uart3_EN_0; //開啟接收,禁止發(fā)送
- }
- else{
- USART_SendData(USART3, uart3_TxBuffer[uart3_TxCounter++]);
- }
- USART_ClearFlag(USART3, USART_FLAG_TC);
- }
- }
- #endif
復(fù)制代碼

|
|