STM32F103 的RS485串口问题

2019-08-14 06:23发布

本帖最后由 xx3360765 于 2017-8-22 16:25 编辑

以下是我的代码程序烧录到板上后一点反应都没有,串口通讯助手也一点反应都没有。求解救。
/////////////usart.c#include "stm32f10x.h"#include "stm32f10x_gpio.h"#include "stm32f10x_rcc.h"#include "stm32f10x_usart.h"#include "misc.h"#include "usart.h"#define RS485_DIR_TX GPIO_WriteBit(GPIOA,GPIO_Pin_9,1);#define RS485_DIR_RX GPIO_WriteBit(GPIOA,GPIO_Pin_9,0);void USART1_Initial(void){        GPIO_InitTypeDef GPIO_InitStructure2;        USART_InitTypeDef USART_InitStructure;        NVIC_InitTypeDef NVIC_InitStructure;        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);      RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);  //????PA,USART1?????±??        GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_9 ;    GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_50MHz; //50M?±??????  GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_Out_PP; //???ì????   GPIO_Init(GPIOA, &GPIO_InitStructure2);                GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_10;          //485_TXD   GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_50MHz;     GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_AF_PP;  //???????ì????    GPIO_Init(GPIOA, &GPIO_InitStructure2);      GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_11;           //485_RXD    GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_IN_FLOATING; //????????   GPIO_Init(GPIOA, &GPIO_InitStructure2);                   NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//??????????3        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;                //×???????3        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                        //IRQ?¨??????        NVIC_Init(&NVIC_InitStructure);        //?ù?????¨????????????VIC?????÷                        USART_InitStructure.USART_BaudRate = 9600;             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_Tx | USART_Mode_Rx;    USART_Init(USART1, &USART_InitStructure);   USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//????????????????                USART_Cmd(USART1, ENABLE);                RS485_DIR_RX;}/////////////main.c#include "gpio.h"#include "usart.h"#include "stm32f10x.h"#include "stm32f10x_gpio.h"#include "stm32f10x_rcc.h"#include "stm32f10x_usart.h"int main(void){        SystemInit();    GPIO_Initial();        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);    USART1_Initial();                RS485_DIR_TX;   while(1)        {                                USART_SendData(USART1, 55);                        }}


友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
13条回答
jiutianshenjian
1楼-- · 2019-08-14 11:02
你确定usart1不是9和10,而是10和11?
xx3360765
2楼-- · 2019-08-14 15:27
/////////////usart.c
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_usart.h"
#include "misc.h"
#include "usart.h"
#define RS485_DIR_TX GPIO_WriteBit(GPIOA,GPIO_Pin_9,1);
#define RS485_DIR_RX GPIO_WriteBit(GPIOA,GPIO_Pin_9,0);

void USART1_Initial(void)
{
        GPIO_InitTypeDef GPIO_InitStructure2;
        USART_InitTypeDef USART_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);   
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);  //????PA,USART1?????±??


        GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_9 ;  
  GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_50MHz; //50M?±??????
  GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_Out_PP; //???ì????
  GPIO_Init(GPIOA, &GPIO_InitStructure2);  
  
         
  GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_10;          //485_TXD
  GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_50MHz;   
  GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_AF_PP;  //???????ì????  
  GPIO_Init(GPIOA, &GPIO_InitStructure2);  
  
  GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_11;           //485_RXD  
  GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_IN_FLOATING; //????????
  GPIO_Init(GPIOA, &GPIO_InitStructure2);
       
       
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//??????????3
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;                //×???????3
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                        //IRQ?¨??????
        NVIC_Init(&NVIC_InitStructure);        //?ù?????¨????????????VIC?????÷
       
       
        USART_InitStructure.USART_BaudRate = 9600;           
  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_Tx | USART_Mode_Rx;  
  USART_Init(USART1, &USART_InitStructure);
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//????????????????       
        USART_Cmd(USART1, ENABLE);
       
        RS485_DIR_RX;
}


/////////////main.c
#include "gpio.h"
#include "usart.h"
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_usart.h"

int main(void)
{
   
    SystemInit();
    GPIO_Initial();
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    USART1_Initial();
                RS485_DIR_TX;
   while(1)
        {
               
                USART_SendData(USART1, 55);
               
        }
}
ADS1993
3楼-- · 2019-08-14 19:08
xx3360765
4楼-- · 2019-08-14 21:22
 精彩回答 2  元偷偷看……
jiutianshenjian
5楼-- · 2019-08-14 21:49
没看到你中断函数在哪里
xx3360765
6楼-- · 2019-08-14 23:54
jiutianshenjian 发表于 2017-8-22 16:33
你确定usart1不是9和10,而是10和11?

我也很好奇,原理图不是我画的。

一周热门 更多>