新手上路
- 积分
- 44
- 金钱
- 44
- 注册时间
- 2019-8-12
- 在线时间
- 9 小时
|
10金钱
本帖最后由 说人不说见 于 2022-4-2 09:28 编辑
STM32F103的485通信,可以接收不能发送,仿真时可以查看到PC发过来的数据,却返回不了。
主函数
485头文件
#define UART_BUFF_SIZE 1024
#define RS485_USART USART1
#define RS485_USART_CLK RCC_APB2Periph_USART1
#define RS485_USART_BAUDRATE 9600
#define RS485_USART_RX_GPIO_PORT GPIOA
#define RS485_USART_RX_GPIO_CLK RCC_APB2Periph_GPIOA
#define RS485_USART_RX_PIN GPIO_Pin_10
#define RS485_USART_TX_GPIO_PORT GPIOA
#define RS485_USART_TX_GPIO_CLK RCC_APB2Periph_GPIOA
#define RS485_USART_TX_PIN GPIO_Pin_9
#define RS485_RE_GPIO_PORT GPIOA
#define RS485_RE_GPIO_CLK RCC_APB2Periph_GPIOA
#define RS485_RE_PIN GPIO_Pin_11
#define RS485_INT_IRQ USART1_IRQn
#define RS485_IRQHandler USART1_IRQHandler
485函数
static void Delay(__IO u32 nCount);
static void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* Enable the USARTy Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = RS485_INT_IRQ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void RS485_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* config USART clock */
RCC_APB2PeriphClockCmd(RS485_USART_RX_GPIO_CLK, ENABLE);
RCC_APB2PeriphClockCmd(RS485_USART_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = RS485_USART_TX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(RS485_USART_TX_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = RS485_USART_RX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(RS485_USART_RX_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = RS485_RE_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(RS485_RE_GPIO_PORT, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = RS485_USART_BAUDRATE;
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(RS485_USART, &USART_InitStructure);
USART_Cmd(RS485_USART, ENABLE);
NVIC_Configuration();
USART_ITConfig(RS485_USART, USART_IT_RXNE, ENABLE);
//USART_ITConfig(RS485_USART, USART_IT_TXE, ENABLE);
GPIO_ResetBits(RS485_RE_GPIO_PORT,RS485_RE_PIN);
}
void RS485_SendByte( uint8_t ch )
{
USART_SendData(RS485_USART,ch);
while (USART_GetFlagStatus(RS485_USART, USART_FLAG_TC) == RESET);
void RS485_SendStr_length( uint8_t *str,uint32_t strlen )
{
unsigned int k=0;
RS485_TX_EN() ;
do
{
RS485_SendByte( *(str + k) );
k++;
} while(k < strlen);
Delay(0xFFF);
RS485_RX_EN() ;
}
void RS485_SendString( uint8_t *str)
{
unsigned int k=0;
RS485_TX_EN() ;
do
{
RS485_SendByte( *(str + k) );
k++;
} while(*(str + k)!='\0');
Delay(0xFFF);
RS485_RX_EN() ;
}
|
最佳答案
查看完整内容[请看2#楼]
上示波器 控制485一直是发送的状态下 先看看串口输出波形 再看看485AB的波形是不是都在
另外你这是共性问题吗? 有换过485芯片吗
|