打印

请教我的代码为什么无法在Usart1输出数据?

[复制链接]
3478|9
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
javenreal|  楼主 | 2011-7-20 18:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
MCU是STM32F207 (不是107哦)
main函数中的内容:

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);  
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  
    /* Connect PXx to USARTx_Tx*/  //这2句要不要都试过了
    //  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
   /* Connect PXx to USARTx_Rx*/
   //  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
  
  GPIO_InitTypeDef    GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);

// Configure USART1_Rx  
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//GPIO_Mode_IN_FLOATING;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_Init(GPIOA, &GPIO_InitStructure);  

USART_InitTypeDef   USART_InitStructure;
  
   //初始化串口
USART_InitStructure.USART_BaudRate = 9600;  //波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8位数据
USART_InitStructure.USART_StopBits = USART_StopBits_1;  //1个停止位
USART_InitStructure.USART_Parity = USART_Parity_No;     //无校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //不用RTS、CTS等
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;   //发送、接收使能

USART_Init(USART1, &USART_InitStructure);
   USART_Cmd(USART1, ENABLE);
   USART_SendData(USART1, 0xF0);
   while(1)
   {
    USART_SendData(USART1, 0xF0);
   }

下载后为何串口接收不到任何数据?(硬件是好的,因为程序可以下载进去,用的是和103相同的下载工具。)
沙发
相信哥咯| | 2011-7-21 10:20 | 只看该作者
AFIO时钟没有使能

使用特权

评论回复
板凳
javenreal|  楼主 | 2011-7-21 11:36 | 只看该作者
AFIO时钟没有使能
相信哥咯 发表于 2011-7-21 10:20


你好,在STM32F2xx的相关头文件中,没有RCC_APB2Periph_AFIO 及其相关的定义。(搜不到包含AFIO或者AF的时钟相关常量)

官方的USART历程里面,也没有和AFIO时钟相关的代码。(官方历程用的是USART3)

使用特权

评论回复
地板
香水城| | 2011-7-21 11:53 | 只看该作者
有没有运行过固件库中的例程?

使用特权

评论回复
5
javenreal|  楼主 | 2011-7-21 12:11 | 只看该作者
有没有运行过固件库中的例程?
香水城 发表于 2011-7-21 11:53

我就是在例程里面加进去USART1相关代码的,因为我的板子上USART3没有引出。

使用特权

评论回复
6
相信哥咯| | 2011-7-21 14:34 | 只看该作者
103的哈哈

void USART_Configuration(void)  
{  
    GPIO_InitTypeDef GPIO_InitStructure;  
    USART_InitTypeDef USART_InitStructure;  
    USART_ClockInitTypeDef USART_ClockInitStructure;  
      
    //使能串口1,PA,AFIO总线  
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |   
            RCC_APB2Periph_AFIO |  
            RCC_APB2Periph_USART1 ,   
            ENABLE);  
    /* A9 USART1_Tx */  
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;  
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;     //推挽输出-TX  
    GPIO_Init(GPIOA, &GPIO_InitStructure);  
    /* A10 USART1_Rx  */  
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;  
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入-RX  
    GPIO_Init(GPIOA, &GPIO_InitStructure);  
  
    USART_InitStructure.USART_BaudRate = 19200;  
    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_ClockInitStructure.USART_Clock = USART_Clock_Disable;  
    USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;  
    USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;  
    USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;  
    USART_ClockInit(USART1, &USART_ClockInitStructure);  
    USART_Init(USART1, &USART_InitStructure);
       
    /* Enable the USARTx */  
    USART_Cmd(USART1, ENABLE);  
}  
void USART1_Putc(unsigned char c)  
{  
    USART_SendData(USART1, c);  
    while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET );  
}

使用特权

评论回复
7
javenreal|  楼主 | 2011-7-21 20:42 | 只看该作者
103的哈哈

void USART_Configuration(void)  
{  
    GPIO_InitTypeDef GPIO_InitStructure;  
    USART_InitTypeDef USART_InitStructure;  
    USART_ClockInitTypeDef USART_ClockInitStructure;  
      
   ...
相信哥咯 发表于 2011-7-21 14:34


你好,103和20x很多寄存器都不一样了,无法照搬。。。

使用特权

评论回复
8
yujie870705| | 2011-7-28 10:35 | 只看该作者
static void ComConfig(COM_PORT port)
{
      GPIO_InitTypeDef GPIO_InitStructure;
      uint8_t com=port;
//      for(com=0;com<COMn;com++){
            /* Enable GPIO clock */
            RCC_AHB1PeriphClockCmd(COM_TX_PORT_CLK[com] | COM_RX_PORT_CLK[com], ENABLE);
           
            /* Enable UART clock */
            RCC_APB1PeriphClockCmd(COM_USART_CLK[com], ENABLE);
         
            /* Connect PXx to USARTx_Tx*/
            GPIO_PinAFConfig(COM_TX_PORT[com], COM_TX_PIN_SOURCE[com], COM_TX_AF[com]);
         
            /* Connect PXx to USARTx_Rx*/
            GPIO_PinAFConfig(COM_RX_PORT[com], COM_RX_PIN_SOURCE[com], COM_RX_AF[com]);
         
            /* Configure USART Tx as alternate function  */
            GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
            GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
            GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
         
            GPIO_InitStructure.GPIO_Pin = COM_TX_PIN[com];
            GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
            GPIO_Init(COM_TX_PORT[com], &GPIO_InitStructure);
         
            /* Configure USART Rx as alternate function  */
            GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
            GPIO_InitStructure.GPIO_Pin = COM_RX_PIN[com];
            GPIO_Init(COM_RX_PORT[com], &GPIO_InitStructure);
//      }
}

char ComOpen( COM_PORT port,COM_PORTMODE *Portmode )
{
  USART_InitTypeDef USART_InitStructure;


  g_iCOM=port;  
  //初始化IO配置
  ComConfig(port);
  //参数配置
  USART_InitStructure.USART_BaudRate = Portmode->BaudRate;
  USART_InitStructure.USART_WordLength = Portmode->WordLength;;
  USART_InitStructure.USART_StopBits = Portmode->StopBits;
  USART_InitStructure.USART_Parity = Portmode->Parity;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  /* USART configuration */
  USART_Init(COM_USART[port], &USART_InitStructure);
  /* Enable USART */
  USART_Cmd(COM_USART[port], ENABLE);
  return true;
}
照这个初始化看看!

使用特权

评论回复
9
yujie870705| | 2011-7-28 10:37 | 只看该作者
#define COMn 2
#define COMIntn 2
pFnSCON COM_Int[COMn][COMIntn]={0};
USART_TypeDef* COM_USART[COMn] = {USART2,USART3};
GPIO_TypeDef* COM_TX_PORT[COMn] = {GPIOA,GPIOB};
GPIO_TypeDef* COM_RX_PORT[COMn] = {GPIOA,GPIOB};
const uint32_t COM_USART_CLK[COMn] = {RCC_APB1Periph_USART2,RCC_APB1Periph_USART3};
const uint32_t COM_TX_PORT_CLK[COMn] = {RCC_AHB1Periph_GPIOA,RCC_AHB1Periph_GPIOB};
const uint32_t COM_RX_PORT_CLK[COMn] = {RCC_AHB1Periph_GPIOA,RCC_AHB1Periph_GPIOB};
const uint16_t COM_TX_PIN[COMn] = {GPIO_Pin_2,GPIO_Pin_10};
const uint16_t COM_RX_PIN[COMn] = {GPIO_Pin_3,GPIO_Pin_11};
const uint8_t COM_TX_PIN_SOURCE[COMn] = {GPIO_PinSource2,GPIO_PinSource10};
const uint8_t COM_RX_PIN_SOURCE[COMn] = {GPIO_PinSource3,GPIO_PinSource11};
const uint8_t COM_TX_AF[COMn] = {GPIO_AF_USART2,GPIO_AF_USART3};
const uint8_t COM_RX_AF[COMn] = {GPIO_AF_USART2,GPIO_AF_USART3};
__IO  uint8_t COM_RxFlag[COMn][2] ={0};//串口接收中断标识
__IO  uint8_t COM_TxFlag[COMn]={0};    //串口发送中断标识

使用特权

评论回复
10
网络转接| | 2012-1-17 10:35 | 只看该作者
不行的 串口以是不是还要改别的什么啊

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

15

主题

48

帖子

1

粉丝