我要做一个通过串口控制LED灯状态的程序,用stm32的串口,通过串口调试助手发送数据使得一个led灯分别以该数据为时间间隔闪烁(如发送1000,则LED灯以delay(1000)为间隔闪烁),我把我的程序放上来,原子哥和各位大神帮我看看是哪里的问题。
#include "stm32f10x.h"
static int time;
/***********************************************************************
外设时钟使能
************************************************************************/
void RCC_Configuration(void)
{
/* 使能外设时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 |
RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC |
RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE|RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
}
/*******************************************************************************
全部用到的引脚将在在配置
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//发送接收端口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //TXD
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //RXD
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);//初始化外设GPIO
}
/*******************************************************************************
全部中断在此配置
*******************************************************************************/
void NVIC_Configuration(void)//中断设置
{
NVIC_InitTypeDef NVIC_InitStructure;//定义结构体
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);//串口一分到1组
/* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //通道设置为串口1中断
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //中断响应优先级0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开中断
NVIC_Init(&NVIC_InitStructure); //初始化
}
void USART1_Init(unsigned int baud)//////USART1 初始化 baud 波特率
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = baud; //该成员设置了USART传输的波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//提示了在一个帧中传输或者接收到的数据位数(8位)
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;//指定了使能或者失能发送和接收模式(TX发送RX接收)
USART_Init(USART1, &USART_InitStructure); //初始化外设 USARTx 寄存器
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//使能或者失能指定的USART中断
//(参数选择 USART外设,参数USART_IT 使能或者失能USART的中断 )若接收数据寄存器满,则产生中断
USART_Cmd(USART1, ENABLE); //使能或者失能USART外设,使能 USART1, 配置完毕
USART_ClearFlag(USART1, USART_FLAG_TC); // 清标志,解决第1个字节无法正确发送出去的问题
}
/*******************************************************************************
初始化时钟晶振 72MHZ
*******************************************************************************/
void SysClock_Init(void)
{
ErrorStatus HSEStartUpStatus;
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS){
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
FLASH_SetLatency(FLASH_Latency_2);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET){
;
}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource() != 0x08){
;
}
}
}
void USART1_SendByte(unsigned char temp)//从 USART1 发送一个字节???????????
{
USART_SendData(USART1, temp); //通过外设USARTx 发送单个数据(USARTx :x 可以是1 ,2 或者3 ,来选择 USART外设,Data: 待发送的数据 )
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);//检查指定的USART标志位设置与否
}
void USART1_Printf(char *pch)//从 USART1 发送字符串????????????????????????
{
while(*pch != '\0')
{
USART1_SendByte(*(unsigned char *)pch);
pch++;
}
}
/******************************************
*
* 延时程序 ms
*
****************************************/
void delay(unsigned int xml)
{
int i,j;
for (i=xml;i>0;i--)
for (j=110;j>0;j--);
}
/*******************************************************
MAIN 函数
*******************************************************/
int main(void)
{
SysClock_Init(); // 初始化系统时钟 72MHZ
RCC_Configuration(); // 使能外设
GPIO_Configuration(); // 配置引脚
NVIC_Configuration(); // 配置中断
USART1_Init(9600); // 配置串口1,波特率9600
while(1)
{
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
delay(time);
GPIO_SetBits(GPIOD,GPIO_Pin_2);
delay(time);
}
}
中断服务程序
extern void USART1_SendByte(unsigned char temp); // 声明外部函数
void USART1_IRQHandler(void)
{
static int time;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)// 接收中断
{
USART1_SendByte(USART_ReceiveData(USART1));
time=USART_ReceiveData(USART1);
}
}
|