这是我的程序
/* MAIN.C file
*
* stm32-project
*/
/***************************************************************
/ 深圳EU电子出品-版权所有-翻版必究
/ EU-热爱嵌入式开发
/ http://euse.taobao.com
***************************************************************/
//类型转换
typedef unsigned char bool;
typedef unsigned char u8;
typedef unsigned short u16;
#define True 1
#define False 0
//SET BIT. Example: a |= SETBIT0
enum
{
SETBIT0 = 0x0001, SETBIT1 = 0x0002, SETBIT2 = 0x0004, SETBIT3 = 0x0008,
SETBIT4 = 0x0010, SETBIT5 = 0x0020, SETBIT6 = 0x0040, SETBIT7 = 0x0080,
SETBIT8 = 0x0100, SETBIT9 = 0x0200, SETBIT10 = 0x0400, SETBIT11 = 0x0800,
SETBIT12 = 0x1000, SETBIT13 = 0x2000, SETBIT14 = 0x4000, SETBIT15 = 0x8000
};
//CLR BIT. Example: a &= CLRBIT0
enum
{
CLRBIT0 = 0xFFFE, CLRBIT1 = 0xFFFD, CLRBIT2 = 0xFFFB, CLRBIT3 = 0xFFF7,
CLRBIT4 = 0xFFEF, CLRBIT5 = 0xFFDF, CLRBIT6 = 0xFFBF, CLRBIT7 = 0xFF7F,
CLRBIT8 = 0xFEFF, CLRBIT9 = 0xFDFF, CLRBIT10 = 0xFBFF, CLRBIT11 = 0xF7FF,
CLRBIT12 = 0xEFFF, CLRBIT13 = 0xDFFF, CLRBIT14 = 0xBFFF, CLRBIT15 = 0x7FFF
};
//CHOSE BIT. Example: a = b&CHSBIT0
enum
{
CHSBIT0 = 0x0001, CHSBIT1 = 0x0002, CHSBIT2 = 0x0004, CHSBIT3 = 0x0008,
CHSBIT4 = 0x0010, CHSBIT5 = 0x0020, CHSBIT6 = 0x0040, CHSBIT7 = 0x0080,
CHSBIT8 = 0x0100, CHSBIT9 = 0x0200, CHSBIT10 = 0x0400, CHSBIT11 = 0x0800,
CHSBIT12 = 0x1000, CHSBIT13 = 0x2000, CHSBIT14 = 0x4000, CHSBIT15 = 0x8000
};
/* INCLUDES */
//MCU
#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_usart.h"
//串口驱动应用标志
volatile static bool Derive_UART1SendFlag, Derive_UART1TxIntState;
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void Time_Init(void);
void UARTON(void);
u8 Return_KeyScan(void);
void UARTSendByByter(u8 Data);
void USART_Configuration(void);
/*-------------------------------------------------------------------------------------------------------
* 程序从这里执行
* 本程序通过定时器 闪烁STM32-MINI开发板LED 请保持LS1跳线帽处于连接状态
-------------------------------------------------------------------------------------------------------*/
int main(void)
{
RCC_Configuration();
GPIO_Configuration(); //端口初始化
NVIC_Configuration(); //中断源配置
USART_Configuration(); //串口1初始化
Time_Init(); //定时器初始化
//开总中断
__enable_irq();
UARTSendByByter(0xfa);
while(1){
}
}
void NVIC_Configuration()
{
NVIC_InitTypeDef NVIC_InitStructure;
//中断配置 2-level interrupt
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
//定时器中断配置
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢占优先级1 低优先级别中断
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级0 高级别的响应中断
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//串口1中断设置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //低优先级别的中断
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应中断等级为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void GPIO_Configuration(void) //端口初始化
{
GPIO_InitTypeDef GPIO_InitStructure;
//led
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//key
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* UART1 TXIO */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* UART1 RXIO */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void RCC_Configuration(void)
{
//--------------------------- CLK INIT, HSE PLL ----------------------------
ErrorStatus HSEStartUpStatus;
//RCC reset
RCC_DeInit();
//开启外部时钟 并执行初始化
RCC_HSEConfig(RCC_HSE_ON);
//等待外部时钟准备好
HSEStartUpStatus = RCC_WaitForHSEStartUp();
//启动失败 在这里等待
while(HSEStartUpStatus == ERROR);
//设置内部总线时钟
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
//外部时钟为8M 这里倍频到72M
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);
//----------------------------- CLOSE HSI ---------------------------
//关闭内部时钟HSI
RCC_HSICmd(DISABLE);
//开启APB2时钟
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA //GPIOA端口时钟
| RCC_APB2Periph_GPIOB //GPIOB端口时钟
| RCC_APB2Periph_USART1 //开启串口1时钟
| RCC_APB2Periph_AFIO //重映射时钟
, ENABLE);
//开启定时器外设时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
}
//定时器配置并开启 使用定时器3
void Time_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//配置定时器参数
TIM_DeInit(TIM3);
TIM_TimeBaseStructure.TIM_Period = 10000; //10ms定时
TIM_TimeBaseStructure.TIM_Prescaler = (72000000/1000000 - 1);
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
//开中断
TIM_ClearFlag(TIM3, TIM_FLAG_Update);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
//开启定时器
TIM_Cmd(TIM3, ENABLE);
}
//定时器中断处理 从stm32f10x_it.c添加
u8 Return_KeyScan()
{
u8 return_key=0;
return_key = GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_2);
return return_key;
}
//从串口发送一个字节
void UARTSendByByter(u8 Data)
{
//发送数据
USART_SendData(USART1, Data);
if(!Derive_UART1TxIntState)
{
Derive_UART1TxIntState = True;
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
}
Derive_UART1SendFlag = True;
}
//开串口 并执行初始化
//8位数据位 无校验 1位起始位/1位停止位 允许收发中断 宏定义BAUDRATE设定波特率 低优先级中断
void USART_Configuration(void)
{
//波特率设置
#define UART_BAUDDEF 9600
USART_InitTypeDef USART_InitStructure; //串口初始化结构体声明
USART_ClockInitTypeDef USART_ClockInitStruct;
USART_InitStructure.USART_BaudRate = UART_BAUDDEF; //设置波特率为115200bps
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据位8位
USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位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_ClockInitStruct.USART_Clock=USART_Clock_Disable; //串口时钟禁止
USART_ClockInitStruct.USART_CPOL=USART_CPOL_Low; //数据低电平有效
USART_ClockInitStruct.USART_CPHA=USART_CPHA_2Edge; //配置CPHA使数据在第2个边沿的时候被捕获
USART_ClockInitStruct.USART_LastBit=USART_LastBit_Disable; // 禁用最后一位,使对应的时钟脉冲不会再输出到SCLK引脚
USART_ClockInit(USART1, &USART_ClockInitStruct); //配置USART与时钟相关的设置
USART_Init(USART1, &USART_InitStructure); //配置串口参数函数
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //使能接收中断
//USART_ITConfig(USART1, USART_IT_TXE, ENABLE); //使能发送缓冲空中断
//USART_ITConfig(USART1, USART_IT_TC, ENABLE); //使能发送完成中断
USART_ClearFlag(USART1,USART_FLAG_TC); //清除发送完成标志位
USART_Cmd(USART1, ENABLE); //使能串口1
}
----------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------
u8 GuanJi = 0;
u8 Num = 0;
bool Tim3_jishu=False;
void TIM3_IRQHandler(void)
{
//清中断标识
TIM_ClearFlag(TIM3, TIM_FLAG_Update);
//---------------- 中断处理 ---------------------
if(!Return_KeyScan())
GuanJi++;
else
GuanJi=0;
if(GuanJi==200)
Tim3_jishu = ! Tim3_jishu ;
// UARTSendByByter(0x01);
if(Tim3_jishu)
GPIO_SetBits(GPIOB, GPIO_Pin_10);
else
GPIO_ResetBits(GPIOB, GPIO_Pin_10);
}
void USART1_IRQHandler(void)
{
u8 c;
if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
{
c = USART1->DR;
if(c==1)
GPIO_SetBits(GPIOB, GPIO_Pin_10);
else
GPIO_ResetBits(GPIOB, GPIO_Pin_10);
}
}
------解决思路----------------------
没有清理中断接收标志,一直进串行接收中断吧,断点调试试试看呢。