|
#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 //如果使能了接收
- //接收缓存区
- u8 RS485_RX_BUF[64]; //接收缓冲,最大64个字节.
- //接收到的数据长度
- 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位数据长度
- 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;//收发模式
-
- NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn; //使能串口4中断
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //先占优先级3级
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
- NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设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; //设置为发送模式
- if(RS485_TX_EN==1)
- {
- printf("\r\n发送\r\n");
- for(t=0;t<len;t++) //循环发送数据
- {
-
- 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已发送\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) //接收到数据
- {
- res =USART4->DR;
- }
- printf("reserve=%x\n",res);
- USART_ClearFlag(USART4,USART_IT_RXNE);
- }
复制代码
|
-
-
Keil代码.7z
212.42 KB, 下载次数: 42, 下载积分: 黑币 -5
485
评分
-
查看全部评分
|