标题:
基于STM32f103C8T6的485通讯参考源码有问题 求帮助
[打印本页]
作者:
liude
时间:
2018-9-17 10:17
标题:
基于STM32f103C8T6的485通讯参考源码有问题 求帮助
1、该例程为RS485例程。
2、使用说明
(1)工程文件路径:例程目录\RS485\MDK-ARM\Project.uvproj。
(2)请使用MDK 4.0以上版本打开,MDK版本过低会导致无法识别工程。
(3)下载调试工具为ULINK。
(4)请将RS485A、RS485B连接RS485转RS232,并打开超级终端或串口助手,配置波特率115200,8位,一个停止位,无校验位。
(5)HEX文件下载到板子后,使用超级终端或串口调试助手可以看到调试信息,表明例程运行正确。
3、注意事项
请务必在下载、调试、运行过程中,保持板子上电、ULINK连接并插在电脑上。
单片机源程序如下:
/****************************************Copyright (c)****************************************************
**--------------File Info---------------------------------------------------------------------------------
** File name: main.c
** Descriptions: The RS485 application function
**
**--------------------------------------------------------------------------------------------------------
** Created by: AVRman
** Created date: 2010-10-30
** Version: v1.0
** Descriptions: The original version
**
**--------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
*********************************************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include <stdio.h>
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
#define DIR485_Receive() GPIO_ResetBits(GPIOB,GPIO_Pin_2)
#define DIR485_Send() GPIO_SetBits(GPIOB,GPIO_Pin_2)
/* Private function prototypes -----------------------------------------------*/
void USART_Configuration(void);
/*******************************************************************************
* Function Name : Delay
* Description : Delay Time
* Input : - nCount: Delay Time
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void Delay (uint32_t nCount)
{
for(; nCount != 0; nCount--);
}
/*******************************************************************************
* Function Name : main
* Description : Main program
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
int main(void)
{
uint8_t ch;
USART_Configuration();
printf("*****************************************************************\r\n");
printf("* *\r\n");
printf("* Thank you for using HY-RedBull V3.0 Development Board ! ^_^ *\r\n");
printf("* *\r\n");
printf("*****************************************************************\r\n");
printf("\r\nPlease input any word :\r\n");
/* Infinite loop */
while (1){
/* Loop until RXNE = 1 */
while (USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET);
ch = USART_ReceiveData(USART3);
Delay(10000);
printf("%c",ch);
}
}
/*******************************************************************************
* Function Name : USART_Configuration
* Description : Configure USART3
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void USART_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB , ENABLE);
RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART3 , ENABLE);
/*
* USART3_TX -> PB10 , USART3_RX -> PB11 , 485_DIR -> PB2
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
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;
USART_Init(USART3, &USART_InitStructure);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
USART_ClearFlag(USART3,USART_FLAG_TC);
USART_Cmd(USART3, ENABLE);
}
/**
* @brief Retargets the C library printf function to the USART.
* @param None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
DIR485_Send();
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART3, (uint8_t) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET)
{}
DIR485_Receive();
return ch;
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
……………………
…………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
RS485.rar
(336.45 KB, 下载次数: 141)
2018-9-17 10:16 上传
点击文件名下载附件
stm32 485
作者:
1203162053
时间:
2019-7-5 13:54
程序不能用
作者:
D-伟
时间:
2019-10-19 17:53
有原理图 吗?
作者:
linmiaomiaoa
时间:
2019-12-19 13:56
不知道行不行 先看看
作者:
ws0564
时间:
2021-6-6 17:10
不能用,已验证!
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1