|
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "rs485.h"
int main(void)
{
u8 buf[3]={3,4,5};
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(115200);
LED_Init();
RS485_Init(9600);
GPIO_ResetBits(GPIOF,GPIO_Pin_1);
GPIO_ResetBits(GPIOF,GPIO_Pin_2);
printf("\r\n開始\r\n");
RS485_Send_Data(buf,3);
printf("\r\n接收\r\n");
while(1);
}
- #include "led.h"
- #include "delay.h"
- #include "sys.h"
- #include "usart.h"
- #include "rs485.h"
-
- #ifdef EN_USART4_RX //如果使能了接收
- //接收緩存區(qū)
- u8 RS485_RX_BUF[64]; //接收緩沖,最大64個字節(jié).
- //接收到的數(shù)據(jù)長度
- u8 RS485_RX_CNT=0;
- #endif
-
- void RS485_Init(u32 bound)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);//使能GPIOA,D時鐘
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART4,ENABLE);//使能USART2時鐘
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復用推挽
- GPIO_Init(GPIOC, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空輸入
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART4,ENABLE);//復位串口2
- RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART4,DISABLE);//停止復位
-
-
- #ifdef EN_USART4_RX //如果使能了接收
- USART_InitStructure.USART_BaudRate = bound;//波特率設置
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;//8位數(shù)據(jù)長度
- USART_InitStructure.USART_StopBits = USART_StopBits_1;//一個停止位
- USART_InitStructure.USART_Parity = USART_Parity_No;///奇偶校驗位
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//無硬件數(shù)據(jù)流控制
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//收發(fā)模式
-
- NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn; //使能串口4中斷
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //先占優(yōu)先級3級
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //從優(yōu)先級3級
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中斷通道
- NVIC_Init(&NVIC_InitStructure); //根據(jù)NVIC_InitStruct中指定的參數(shù)初始化外設NVIC寄存器
- USART_Init(USART4, &USART_InitStructure); ; //初始化串口
- USART_ITConfig(USART4, USART_IT_RXNE, ENABLE);//開啟中斷
-
- USART_Cmd(USART4, ENABLE); //使能串口
- #endif
- RS485_TX_EN=0; //默認為接收模式
-
- }
- void RS485_Send_Data(u8 *buf,u8 len)
- {
- u8 t;
- RS485_TX_EN=1; //設置為發(fā)送模式
- if(RS485_TX_EN==1)
- {
- printf("\r\n發(fā)送\r\n");
- for(t=0;t<len;t++) //循環(huán)發(fā)送數(shù)據(jù)
- {
-
- printf("%d\n",buf[0]);
- printf("%d\n",buf[1]);
- printf("%d\n",buf[2]);
- USART_SendData(USART4, buf[t]);
- while(USART_GetFlagStatus(USART4,USART_FLAG_TC)!=SET);
- }
- printf("\r\n已發(fā)送\r\n\r\n");
- USART_ClearFlag(USART4,USART_FLAG_TC);
- }
- RS485_TX_EN=0; //設置為接收模式
- }
- void UART4_IRQHandler(void)
- {
- u16 res=0;
- if(USART_GetITStatus(USART4, USART_IT_RXNE) != RESET) //接收到數(shù)據(jù)
- {
- res =USART4->DR;
- }
- printf("reserve=%x\n",res);
- USART_ClearFlag(USART4,USART_IT_RXNE);
- }
復制代碼
|
-
-
Keil代碼.7z
2023-3-16 17:02 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
212.42 KB, 下載次數(shù): 48, 下載積分: 黑幣 -5
485
評分
-
查看全部評分
|