EDN的评论中没法跟贴按格式排布,所以另开一贴:
用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));
}
文章评论(0条评论)
登录后参与讨论