新手上路
- 积分
- 47
- 金钱
- 47
- 注册时间
- 2016-2-14
- 在线时间
- 37 小时
|
10金钱
这是我根据查阅资料编写的485程序,大神帮忙看下:
主程序
#include "stm32f10x.h"
#include "delay.h"
#include "sys.h"
#include "rs485.h"
void RS485_Init(void);
int main(void)
{
RS485_Init();
delay_init();
while(1)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_1);
delay_ms(300);
if(USART_GetFlagStatus(USART2,USART_IT_RXNE)==SET)
{
uint16_t i;
i=USART_ReceiveData(USART2);
delay_ms(300);
GPIO_SetBits(GPIOA,GPIO_Pin_1);
USART_SendData(USART2,i);
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET)
delay_ms(300);
}
}
}
485初始化:
#include "rs485.h"
void RS485_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOD, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//PA3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //¸¡¿Õêäèë
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//8λêy¾Y3¤¶è
USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò»¸öí£Ö1λ
USART_InitStructure.USART_Parity = USART_Parity_No;///ÆæÅ¼D£Ñéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎTó2¼têy¾Yá÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//êÕ·¢Ä£ê½
USART_Init(USART2, &USART_InitStructure); ; //3õê¼»ˉ′®¿ú
USART_Cmd(USART2, ENABLE); //ê1Äü′®¿ú
}
我使用的是USART2 PA1是485控制端 PA2是DI PA3是RO
这样写程序 是想实现 ::
我在串口发送一个数据,那么就能在串口再显示出这个程序
哪里不对吗???谢谢!!!!!
|
-
-
|