直接上代码,
带中断点灯调试程序
bsp_usart.h
cpp
#ifndef __BSP_UART_H__
#define __BSP_UART_H__
#include "stm32f10x.h"
/* 串口缓冲区的数据长度 */
#define USART2_RECEIVE_LENGTH 1024
extern uint8_t u2_recv_buff[USART2_RECEIVE_LENGTH]; // ?????
extern uint16_t u2_recv_length; // ??????
extern uint8_t u2_recv_flag; // ???????
//外部可调用函数的声明
void uart2_init(uint32_t __Baud);
void usart_send_data(uint8_t ucch);
void usart_send_String(uint8_t *ucstr);
void USART2_IRQHandler(void);
void uart2_receive_clear(void);
uint8_t *uart2_get_data(void);
void USART2_on_recv(uint8_t* data, uint32_t len);
#endif
// #ifndef __BSP_UART_H__
// #define __BSP_UART_H__
//
// #include "stm32f10x.h"
//
//
// /* 串口缓冲区的数据长度 */
//#define USART2_RECEIVE_LENGTH 1024
// //外部可调用函数的声明
//void uart2_init(uint32_t __Baud);
//void usart_send_data(uint8_t ucch);
//void usart_send_String(uint8_t *ucstr);
//
//
//
//#endif
bsp_usart.c
cpp
#include "bsp_uart.h"
#include "stdio.h"
uint8_t u2_recv_buff[USART2_RECEIVE_LENGTH];
uint16_t u2_recv_length = 0;
uint8_t u2_recv_flag = 0;
void uart2_init(uint32_t __Baud)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitTypeDef USART_InitStructure;
USART_DeInit(USART2);
USART_StructInit(&USART_InitStructure);
USART_InitStructure.USART_BaudRate = __Baud;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART2, &USART_InitStructure);
USART_ClearFlag(USART2, USART_FLAG_RXNE);
USART_ClearFlag(USART2, USART_FLAG_IDLE);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART2, USART_IT_IDLE, ENABLE);
USART_Cmd(USART2, ENABLE);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void usart_send_data(uint8_t ucch)
{
USART_SendData(USART2, (uint8_t)ucch);
while( RESET == USART_GetFlagStatus(USART2, USART_FLAG_TXE) ){}
}
void usart_send_String(uint8_t *ucstr)
{
while(ucstr && *ucstr)
{
usart_send_data(*ucstr++);
}
}
#if !defined(__MICROLIB)
#if (__ARMCLIB_VERSION <= 6000000)
{
int handle;
};
#endif
FILE __stdout;
void _sys_exit(int x)
{
x = x;
}
#endif
/* retarget the C library printf function to the USART */
int fputc(int ch, FILE *f)
{
USART_SendData(USART2, (uint8_t)ch);
while( RESET == USART_GetFlagStatus(USART2, USART_FLAG_TXE) ){}
return ch;
}
void USART2_IRQHandler(void)
{
static uint16_t recv_len = 0; // 中断内部使用的临时变量
if(USART_GetITStatus(USART2, USART_IT_RXNE) == SET)
{
if(recv_len < USART2_RECEIVE_LENGTH - 1)
{
u2_recv_buff[recv_len ++] = USART_ReceiveData(USART2);
}
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
}
if(USART_GetITStatus(USART2, USART_IT_IDLE) == SET)
{
volatile uint32_t temp;
temp = USART2->SR;
temp = USART2->DR;
// 保存接收到的数据长度
u2_recv_length = recv_len;
// 添加字符串结束符
u2_recv_buff[recv_len] = '\0';
u2_recv_flag = 1;
// 调用回调函数
USART2_on_recv(u2_recv_buff, u2_recv_length);
recv_len = 0;
//USART_ClearITPendingBit(USART2, USART_IT_IDLE);
}
}
void USART2_on_recv(uint8_t* data, uint32_t len)
{
printf("recv[%d]: %s \n", len, data);
}
void uart2_receive_clear(void)
{
u2_recv_length = 0;
u2_recv_flag = 0;
}
uint8_t *uart2_get_data(void)
{
if( u2_recv_flag == 1 )
{
uart2_receive_clear();
return u2_recv_buff;
}
return NULL;
}
/***************************************/
//#include "bsp_uart.h"
//#include "stdio.h"
//void uart2_init(uint32_t __Baud)
//{
// GPIO_InitTypeDef GPIO_InitStructure;
// // 开启GPIOA和USART1的时钟,以及AFIO(复用功能)时钟
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOA,ENABLE);
// // 初始化GPIO结构体,并配置TX引脚
// GPIO_StructInit(&GPIO_InitStructure);
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //TX引脚
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz
// GPIO_Init(GPIOA, &GPIO_InitStructure);
// // 重新初始化GPIO结构体,并配置RX引脚
// GPIO_StructInit(&GPIO_InitStructure);
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //RX引脚
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
// GPIO_Init(GPIOA, &GPIO_InitStructure);
//
// USART_InitTypeDef USART_InitStructure; //定义配置串口的结构体变量
// // 开启USART1的时钟
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
// // 将USART1寄存器重置为默认值
// USART_DeInit(USART2);
// // 初始化USART结构体
// USART_StructInit(&USART_InitStructure);
// USART_InitStructure.USART_BaudRate = __Baud; //设置波特率
// USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字节长度为8bit
// USART_InitStructure.USART_StopBits = USART_StopBits_1; //1个停止位
// USART_InitStructure.USART_Parity = USART_Parity_No ; //没有校验位
// USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
// USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无流控
// USART_Init(USART2, &USART_InitStructure); //初始化USART1
//
// USART_Cmd(USART2,ENABLE);//开启串口1
//
//}
//void usart_send_data(uint8_t ucch)
//{
// USART_SendData(USART2, (uint8_t)ucch);
//
// // 等待发送数据缓冲区标志置位
// while( RESET == USART_GetFlagStatus(USART2, USART_FLAG_TXE) ){}
//}
//void usart_send_String(uint8_t *ucstr)
//{
// while(ucstr && *ucstr) // 地址为空或者值为空跳出
// {
// usart_send_data(*ucstr++);
// }
//}
//#if !defined(__MICROLIB)
////不使用微库的话就需要添加下面的函数
//#if (__ARMCLIB_VERSION <= 6000000)
////如果编译器是AC5 就定义下面这个结构体
//struct __FILE
//{
// int handle;
//};
//#endif
//FILE __stdout;
////定义_sys_exit()以避免使用半主机模式
//void _sys_exit(int x)
//{
// x = x;
//}
//#endif
///* retarget the C library printf function to the USART */
//int fputc(int ch, FILE *f)
//{
// USART_SendData(USART2, (uint8_t)ch);
//
// while( RESET == USART_GetFlagStatus(USART2, USART_FLAG_TXE) ){}
//
// return ch;
//}
main.c
cpp
#include "stm32f10x.h"
#include "bsp_uart.h"
#include <stdio.h>
#include "systick.h"
int main(void)
{
systick_init();
uart2_init(115200U);
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_ResetBits(GPIOC,GPIO_Pin_13);
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_Init(GPIOC, &GPIO_InitStructure);
// GPIO_ResetBits(GPIOC,GPIO_Pin_14);
//
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_Init(GPIOC, &GPIO_InitStructure);
// GPIO_ResetBits(GPIOC,GPIO_Pin_15);
//char * t = "qwer";
while(1)
{
GPIO_SetBits(GPIOC, GPIO_Pin_13);
//GPIO_SetBits(GPIOC, GPIO_Pin_14);
//GPIO_SetBits(GPIOC, GPIO_Pin_15);
printf("LED OFF!\r\n");
delay_ms(500);
GPIO_ResetBits(GPIOC, GPIO_Pin_13);
//GPIO_ResetBits(GPIOC, GPIO_Pin_14);
//GPIO_ResetBits(GPIOC, GPIO_Pin_15);
printf("LED ON!\r\n");
delay_ms(500);
//usart_send_data('s');
// uint8_t *t = uart2_get_data();
// if(t != NULL)
// {
// printf("data = %s\r\n",t);
// }
//usart_send_String((uint8_t *)"hello\r\n");
//printf("count = \r\n");
}
}