将UART与STM32F0发现和指纹扫描仪一起使用时出现问题

将UART与STM32F0发现和指纹扫描仪一起使用时出现问题,stm32,uart,stm32f4discovery,Stm32,Uart,Stm32f4discovery,我目前正在从事一个涉及STM32F0发现板和指纹扫描仪的项目,该项目使用UART通信。要初始化UART,请执行以下操作: void init_uart(void){ GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_WordL

我目前正在从事一个涉及STM32F0发现板和指纹扫描仪的项目,该项目使用UART通信。要初始化UART,请执行以下操作:

void init_uart(void){
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;

    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_Rx | USART_Mode_Tx;

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

    GPIO_Init(GPIOA, &GPIO_InitStructure);
    USART_Init(USART1, &USART_InitStructure);

    USART_Cmd(USART1, ENABLE);
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
}
void writechar(char c) {
    while((USART1 -> ISR & USART_ISR_TC) != USART_ISR_TC);
    USART1 -> TDR = c;
}

char readchar(void) {
    return USART_ReceiveData(USART1);
}
然后,要发送/接收字节,我执行以下操作:

void init_uart(void){
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;

    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_Rx | USART_Mode_Tx;

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

    GPIO_Init(GPIOA, &GPIO_InitStructure);
    USART_Init(USART1, &USART_InitStructure);

    USART_Cmd(USART1, ENABLE);
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
}
void writechar(char c) {
    while((USART1 -> ISR & USART_ISR_TC) != USART_ISR_TC);
    USART1 -> TDR = c;
}

char readchar(void) {
    return USART_ReceiveData(USART1);
}
为了测试这一点,我通过for循环逐字节发送OPEN命令,然后使用相同的方法发送LEN-ON命令,但没有结果。在OPEN命令之后读取ACK时,读取的结果只是一个空的字符字节数组


所以,我的问题是,我的初始化是错误吗?还是读/写错误?或者可能是其他原因吗?

您收到确认响应了吗?如果没有,则可能存在配置或接线问题。我建议您尝试将串行引脚连接到串行usb转换器 (FTDI如此:单击) 并使用Putty或YAT等应用程序向stm发送命令(ACK)。尝试使用与扫描仪相同的配置,如果不起作用,这是您的配置的问题,如果起作用,这是扫描仪方面的问题