吹水
写了快半年go语言吧,毕业后还是打算从事嵌入式,重新捡起来,调这个我犯了个很睿智的问题,调了两天才发现!就是配置串口相关的参数!一定要配置对!开始我通过串口和ld3320连接,有数据传输,32的串口也能接收到电脑发的数据,但是两个连起来就是拿不到数据,最后洗澡的时候才想起来ld3320给的波特率不是115200而是9600。
ld3320那边的程序如何修改就不贴了,网上蛮多的。
硬件平台
工程代码
ld3320连接USART1,通过透传的方式将获取到的数据通过USART2打印出来
main.c
#include "stm32f10x.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "led.h"
#include "usart.h"
#include "delay.h"
int main()
{
Led_init();
usart1_init();
usart2_init();
initSysTick();
printf("main start!");
GPIO_ResetBits(GPIOC, GPIO_Pin_13);
delay_ms(300);
GPIO_SetBits(GPIOC, GPIO_Pin_13);
}
void USART1_IRQHandler(void)
{
int temp;
while(USART_GetITStatus(USART1,USART_IT_RXNE) != RESET){
temp = USART_ReceiveData(USART1);
printf("%c",temp);
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
}
}
delay.c
#include "stm32f10x.h"
#include "delay.h"
void initSysTick(void)
{
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
SysTick->LOAD = 9;
}
void delay_us(u32 xus)
{
SysTick->LOAD = 9 * xus;
SysTick->VAL = 0;
while (!(SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk));
}
void delay_ms(u32 xms)
{
SysTick->LOAD = 9000;
SysTick->VAL = 0;
while (xms--)
{
while (!(SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk));
}
}
delay.h
#ifndef _DELAY_H_
#define _DELAY_H_
#include "stm32f10x.h"
void initSysTick(void);
void delay_us(u32 xus);
void delay_ms(u32 xms);
#endif
usart.c
#include "usart.h"
#include "stm32f10x.h"
void usart1_init(void)
{
GPIO_InitTypeDef gpio_init;
USART_InitTypeDef usartStruct;
NVIC_InitTypeDef nvic_initStruct;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
gpio_init.GPIO_Mode = GPIO_Mode_AF_PP;
gpio_init.GPIO_Pin = GPIO_Pin_9;
gpio_init.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&gpio_init);
gpio_init.GPIO_Mode = GPIO_Mode_IN_FLOATING;
gpio_init.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA,&gpio_init);
usartStruct.USART_BaudRate = 9600;
usartStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
usartStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
usartStruct.USART_Parity = USART_Parity_No;
usartStruct.USART_StopBits = USART_StopBits_1;
usartStruct.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1,&usartStruct);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_Cmd(USART1, ENABLE );
nvic_initStruct.NVIC_IRQChannel = USART1_IRQn;
nvic_initStruct.NVIC_IRQChannelPreemptionPriority = 1;
nvic_initStruct.NVIC_IRQChannelSubPriority = 1;
nvic_initStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvic_initStruct);
}
void usart2_init(void)
{
GPIO_InitTypeDef gpio_init;
USART_InitTypeDef usartStruct;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
gpio_init.GPIO_Mode = GPIO_Mode_AF_PP;
gpio_init.GPIO_Pin = GPIO_Pin_2;
gpio_init.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&gpio_init);
gpio_init.GPIO_Mode = GPIO_Mode_IN_FLOATING;
gpio_init.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOA,&gpio_init);
usartStruct.USART_BaudRate = 115200;
usartStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
usartStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
usartStruct.USART_Parity = USART_Parity_No;
usartStruct.USART_StopBits = USART_StopBits_1;
usartStruct.USART_WordLength = USART_WordLength_8b;
USART_Init(USART2,&usartStruct);
USART_Cmd(USART2, ENABLE );
}
void usartSendByte(USART_TypeDef* USARTx, uint16_t Data)
{
USART_SendData(USARTx,Data);
while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
}
void usartSendStr(USART_TypeDef* USARTx,char *str)
{
uint16_t i = 0;
do{
usartSendByte(USARTx,*(str+i));
i++;
}while(*(str+i) != '\0');
while(USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET);
}
int fputc(int ch,FILE *f)
{
USART_SendData(USART2,(uint8_t)ch);
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
return (ch);
}
int fgetc(FILE *f)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET);
return (int) USART_ReceiveData(USART2);
}
usart.h
#include "stm32f10x.h"
#include <stdio.h>
void usart1_init(void);
void usart2_init(void);
void usartSendByte(USART_TypeDef* USARTx, uint16_t Data);
void usartSendStr(USART_TypeDef* USARTx,char *str);
led.c
#include "stm32f10x.h"
#include "led.h"
void Led_init(void)
{
GPIO_InitTypeDef Led_initStructer;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
Led_initStructer.GPIO_Mode = GPIO_Mode_Out_PP;
Led_initStructer.GPIO_Pin = GPIO_Pin_13;
Led_initStructer.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOC, &Led_initStructer);
}
led.h
#include "stm32f10x.h"
void Led_init(void);
再次注意!ld3320波特率为9600
|