您的位置:控制工程论坛网论坛 » 软件与程序 » STM32 串口终端的其它部分源码

xilinxue

xilinxue   |   当前状态:在线

总积分:16186  2024年可用积分:0

注册时间: 2008-06-26

最后登录时间: 2020-03-22

空间 发短消息加为好友

STM32 串口终端的其它部分源码

xilinxue  发表于 2009/1/13 13:08:29      1602 查看 0 回复  [上一主题]  [下一主题]

手机阅读

用STM32的库直接配置串口:  
USART_Cmd(USARTx, DISABLE);
USART_InitStructure.USART_BaudRate = 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_InitStructure.USART_Clock = USART_Clock_Disable;
USART_InitStructure.USART_CPOL = USART_CPOL_Low;
USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_InitStructure.USART_LastBit = USART_LastBit_Disable;
USART_Init(USARTx, &USART_InitStructure);
  //采用等待的方式发送
  //USART_ITConfig(USARTx, USART_IT_TXE, ENABLE); ---walter 20080422
  //中断接收方式
  USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE);
  /* Enable the USARTx */
  USART_Cmd(USARTx, ENABLE);

串口发送测试代码:
void UartxPutCh(char data)
{
   if(data=='\n')
 {
  while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);   
  Delay(10); //由于超级终端反应较慢,有一个微小延迟
  USART_SendData(USARTx, (u8) '\r');
 }
  while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET); 
 Delay(10);
 USART_SendData(USARTx, (u8) data);
}

串口接收中断的代码:
  if(USART_GetITStatus(USARTx, USART_IT_RXNE) != RESET)
  {
    /* Read one byte from the receive data register */
 uartx_rxBuffer[uartx_rxIndexIn++] = (USART_ReceiveData(USARTx) & 0xFF);

    /* Clear the USART1 Receive interrupt */
    USART_ClearITPendingBit(USARTx, USART_IT_RXNE);
//这里在接收的地方建了一个缓冲区,用来在控制台处理命令时仍可接收命令,并缓冲稍后处理
 if (uartx_rxIndexIn>=_MAX_UART_BUFFER_LEN) uartx_rxIndexIn=0;
   
  }

AD部分的配置代码:
   /* ADC1 configuration ------------------------------------------------------*/
  ADC_InitTypeDef ADC_InitStructure;
  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfChannel = 1;
  ADC_Init(ADC1, &ADC_InitStructure);
  /* ADC1 regular channel14 configuration */
  ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1,   ADC_SampleTime_55Cycles5);
  /* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);
  /* Enable ADC1 reset calibaration register */  
  ADC_ResetCalibration(ADC1);
  /* Check the end of ADC1 reset calibration register */
  while(ADC_GetResetCalibrationStatus(ADC1));
  /* Start ADC1 calibaration */
  ADC_StartCalibration(ADC1);
  /* Check the end of ADC1 calibration */
  while(ADC_GetCalibrationStatus(ADC1));

AD采样的测试代码:
  /* Start ADC1 Software Conversion */
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);   
  if(IS_ADC_GET_FLAG(ADC_FLAG_EOC))
  {
      printf("\r\nCurrentValue: 0x%04X\r\n",ADC_GetConversionValue(ADC1));
  }

1楼 0 0 回复