STM32F103的串口2配置 /******************************************************************************* * Function Name  : RCC_Configuration * Description    : Configures the different system clocks. * Input          : None * Output         : None * Return         : None *******************************************************************************/ void RCC_Configuration(void) {   // RCC system reset(for debug purpose)    RCC_DeInit();
    // Enable HSE    RCC_HSEConfig(RCC_HSE_ON);
    // Wait till HSE is ready    HSEStartUpStatus = RCC_WaitForHSEStartUp();
    if(HSEStartUpStatus == SUCCESS)   {     // HCLK = SYSCLK      RCC_HCLKConfig(RCC_SYSCLK_Div1);         // PCLK2 = HCLK      RCC_PCLK2Config(RCC_HCLK_Div1); 
      // PCLK1 = HCLK/2      RCC_PCLK1Config(RCC_HCLK_Div2);          // ADCCLK = PCLK2/4       RCC_ADCCLKConfig(RCC_PCLK2_Div4);        // Flash 2 wait state      FLASH_SetLatency(FLASH_Latency_2);     // Enable Prefetch Buffer      FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
      // PLLCLK = 8MHz * 9 = 72 MHz      RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
      // Enable PLL       RCC_PLLCmd(ENABLE);
      // Wait till PLL is ready      while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)     {     }
      // Select PLL as system clock source      RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
      // Wait till PLL is used as system clock source      while(RCC_GetSYSCLKSource() != 0x08)     {     }   }
  /* Enable peripheral clocks --------------------------------------------------*/   // I2C1 Periph clock enable    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);   // Enable DMA clock    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA, ENABLE);  
    // TIM3  TIM4  I2C1  I2C2  USART  clock enable    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4 | RCC_APB1Periph_I2C1 | RCC_APB1Periph_I2C2                           | RCC_APB1Periph_USART2, ENABLE);
    // GPIOA  B  C  D  E  AFIO clock enable    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC                          | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE);   //USART1  ADC1  clock enable   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_ADC1, ENABLE);    } /******************************************************************************* * Function Name  : NVIC_Configuration * Description    : Configures Vector Table base location. * Input          : None * Output         : None * Return         : None *******************************************************************************/ void NVIC_Configuration(void) {   NVIC_InitTypeDef NVIC_InitStructure;    #ifdef  VECT_TAB_RAM     // Set the Vector Table base location at 0x20000000    NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);  #else  // VECT_TAB_FLASH     // Set the Vector Table base location at 0x08000000     NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);    #endif      // Configure 1 bits for preemption priority    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);   
    // Enable the USART2 Interrupt   NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel;   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   NVIC_Init(&NVIC_InitStructure);
  } /******************************************************************************* * Function Name  : GPIO_Configuration * Description    : Configure the GPIOD Pins. * Input          : None * Output         : None * Return         : None *******************************************************************************/ void GPIO_Configuration(void) {   GPIO_InitTypeDef GPIO_InitStructure;
    // GPIOA AF Configuration    // PIN_2 for 485-2T   PIN_7 for OutLeft   Pin_9 for 485-1T   Pin_12 for CANT   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_7 | GPIO_Pin_9 | GPIO_Pin_12;   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);      // GPIOB AF Configuration    // Pin_7 for OutRight      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;      GPIO_Init(GPIOB, &GPIO_InitStructure);      // Enable the I2C1 Pins Software Remapping    GPIO_PinRemapConfig(GPIO_Remap_I2C1, ENABLE);
    // GPIOB AFOD Configuration   // Pin_8.9 for I2C1 SCL.SDA    Pin_10.11 for I2C2 SCL.SDA   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;   GPIO_Init(GPIOB, &GPIO_InitStructure);      // GPIOA Out Configuration    // Pin_0 for 5620Load   Pin_1 for 485-2RTS   Pin_5 for 5620DATA   Pin_8 for 485-1TRS   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_5 | GPIO_Pin_8;   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);      // GPIOB Out Configuration    // Pin_0 for 165-2CLK   Pin_12 for 165-1CLK   Pin_14 for 165-1EN   Pin_15 for 5620CLK   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_12 | GPIO_Pin_14 | GPIO_Pin_15;
    GPIO_Init(GPIOB, &GPIO_InitStructure);      // GPIOC OutConfiguration    // Pin_5 for 165-2EN   Pin_6.7.8 for 595-1CLK.SCLK.EN   Pin_10.11.12 for 595-2CLK.SCLK.EN   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
    GPIO_Init(GPIOC, &GPIO_InitStructure);      // GPIOD Out Configuration    // Pin_2 for BELL   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_Init(GPIOD, &GPIO_InitStructure);        // GPIOA INFLOATING Configuration    // Pin_3 for 485-2R   Pin_4 for OCrossing   Pin_6 for LeftOCrossing   Pin_10 for 485-1R   Pin_11 for CANR    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_6 | GPIO_Pin_10 | GPIO_Pin_11;   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);       // GPIOB INFLOATING Configuration    // Pin_1 for SIGNAL   Pin_6 for RightOCrossing   Pin_13 for 165-1Out   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_6 | GPIO_Pin_13;
    GPIO_Init(GPIOB, &GPIO_InitStructure);       // GPIOC INFLOATING Configuration    // Pin_4 for 165-2Out   Pin_9 for Interrupt   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_9;
    GPIO_Init(GPIOC, &GPIO_InitStructure);      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15 | GPIO_Pin_11 | GPIO_Pin_10 | GPIO_Pin_12;
    GPIO_Init(GPIOD, &GPIO_InitStructure);        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
    GPIO_Init(GPIOD, &GPIO_InitStructure);          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;          GPIO_Init(GPIOE, &GPIO_InitStructure);   // GPIOC AIN Configuration   // Pin_0.1.2.3 for ADC0.1.2.3      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;      GPIO_Init(GPIOC, &GPIO_InitStructure); } /******************************************************************************* * Function Name  : USART_Configuration * Description    : Configures USART1 and USART2 * Input          : None * Output         : None * Return         : None *******************************************************************************/
  USART_InitStructure.USART_BaudRate = 9600;// bps.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;   USART_InitStructure.USART_StopBits = USART_StopBits_2;   USART_InitStructure.USART_Parity = USART_Parity_No ;   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS;//None   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;   USART_InitStructure.USART_Clock = USART_Clock_Disable;   USART_InitStructure.USART_CPOL = USART_CPOL_Low;   USART_InitStructure.USART_CPHA = USART_CPHA_1Edge;   USART_InitStructure.USART_LastBit = USART_LastBit_Enable;
    USART_Init(USART2, &USART_InitStructure);     USART_ITConfig(USART2, USART_IT_TXE, ENABLE);     //USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);   // Enable the USART2    USART_Cmd(USART2, ENABLE); /******************************************************************************* * Function Name  : main * Description    : Main program * Input          : None * Output         : None * Return         : None *******************************************************************************/ int main(void) { #ifdef DEBUG   debug(); #endif
    /* System clocks configuration ---------------------------------------------*/   RCC_Configuration();
    /* NVIC configuration ------------------------------------------------------*/   NVIC_Configuration();
    /* GPIO configuration */   GPIO_Configuration();
       // Configure USART   USART_Configuration();
 
      GPIO_WriteBit(GPIOA, GPIO_Pin_1, Bit_RESET);
 
    while(1)   {int i;      for(i=1;i<20;i++)       {     GPIO_WriteBit(GPIOA, GPIO_Pin_1, Bit_RESET);
           Delay74595(500);Delay74595(500);Delay74595(500);     USART_SendData(USART2,1);}   } }
  单片机的串口配置 //=========================================== void Set485asTransmitter() {    P16=0; } void Set485asReceiver() {    P16=1; } void Send(unsigned char com) {          SBUF=com;     while(TI==0){;}     TI=0; } //================================================ void IniCPU110592Mhz(unsigned int baud_rate) {//VB Setting  2400,n,8,1      EX1=1;    //Enable Infra-Red Interruption     IT1=1;    // Falling Edge Triger INT1 (Infra-red Interruption)       PX1=1;    // INT1 (Infra-red) prioritary      EX0=1;    // Zero-crossing Interruption Enabled        IT0=1;    // -_ INT0 (Zero Interruption) //      TMOD=0x21;/*Timer 0(ZERO_CROSSING): MODE 1, Timer 1:MODE 2 (AUTOCHARGING)        // Timer0 for Zero_crossing    Timer1 for BAUD RATE for 4800     Timer2 for Infra_red      PS=1;     switch(baud_rate)       {           case 1200: TH1=TL1=0xe8;PCON=0;TMOD=0x21;break;           case 2400:  TH1=TL1=0xf4;PCON=0;TMOD=0x21;break;           case 4800:  TH1=TL1=0xfa;PCON=0;TMOD=0x21;break;           case 9600:  TH1=TL1=0xfd;PCON=0;TMOD=0x21;break;           case 19200:  TH1=TL1=0xfd;PCON=0x80;TMOD=0x21;break;         }     SCON=0x50;// 11 bit      ES=1;     TR1=1;    // Start TIMER 1 for BAUD RATE generation     T2CON=0x00;    //TF2:0,EXF2:0, RCLK:1, TCLK:1, EXEN2:0, TR2:1,       TR2=1;     TH0=-(1000/256);     TL0=-(1000%256);     ET0=1;     TR0=1;     EA=1;     }  //===========================================     void SSIO(void) interrupt 4 using 1 {    if(RI)        {        RI=0;        SerialRe[SerialReIndicator]=SBUF;        {SerialReIndicator+=1;}        if(SerialReIndicator==30)            SerialReIndicator=0;            }        } } 
  //================================  void main(void)     {uchar i;     IniCPU110592Mhz(9600);     while(1)             {              for(i=1;i<21;i++)             //Set485asTransmitter();Delay(90);                      //Send(0x1);                 Delay(90);
              //Set485asReceiver();                                }      }
 
  通过以上配置后运行程序,STM32发送数据,单片机接收,单片机接收到的数据要么全是“0x01”,要么全是“0x64”,我查过是否是波特率的问题,经过验证,波特率都是9600.在配置上有什么问题吗?
 
 
 
 
  |   
     
  
 |