用了
tiM3定时器中断每一秒钟显示数值加1利用串口发送数据到7CHC164驱动一位数码管显示
现在数字跳的特别快而且感觉每个数字也只是部分段亮
#include "
STM32f10x.h"
u8 disp_sm[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
u8 dis,mcnt;
void RCC_Configuration(void);
void GPIO_Configuration(void);
void USART1_Configuration(void);
void TIM3_Configuration(void);
void NVIC_Configuration(void);
void TIM3_IRQHandler(void);
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
USART1_Configuration();//串口初始化
TIM3_Configuration();
NVIC_Configuration();
while(1)
{
if(mcnt>=200)//1s到
{
mcnt=0;
dis++;
if(dis>=10)
{
dis=0;
}
}
}
}
void RCC_Configuration(void)//系统时钟初始化函数
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//串口时钟使能
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //TIM3时钟使能
}
void GPIO_Configuration(void)//端口初始化函数
{
GPIO_InitTypeDef GPIO_Initstructure;
GPIO_Initstructure.GPIO_Pin=GPIO_Pin_9;//TX
GPIO_Initstructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Initstructure.GPIO_Mode=GPIO_Mode_AF_PP;//复用推挽输出
GPIO_Init(GPIOA,&GPIO_Initstructure);//初始化GPIOA.9
GPIO_Initstructure.GPIO_Pin=GPIO_Pin_10;//RX
GPIO_Initstructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Initstructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA,&GPIO_Initstructure);//初始化GPIOA.10
}
void TIM3_Configuration(void)//配置定时器中断
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);//中断标志位清空
TIM_TimeBaseStructure.TIM_Period = 9;//5ms中断一次
TIM_TimeBaseStructure.TIM_Prescaler = 36000-1;//预分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式
TIM_TimeBaseInit(TIM3, & TIM_TimeBaseStructure);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE );//允许中断产生
TIM_Cmd(TIM3, ENABLE);//定时器外设打开
}
void NVIC_Configuration(void)//分配中断优先级
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//分组
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//抢占优先级为0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;//相应优先级为1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM3_IRQHandler(void)//中断处理函数
{
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);//清空中断标志位
mcnt++;//1s计数
USART_SendData(USART1,disp_sm[dis]);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
}
void USART1_Configuration(void)//串口初始化
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600;//波特率
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_Tx | USART_Mode_Rx;//发送接收使能
USART_Init(USART1, &USART_InitStructure);//初始化串口
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//开启中断,接收到数据中断
USART_Cmd(USART1,ENABLE);//使能串口
USART_ClearFlag(USART1,USART_FLAG_TC);//清除标志位
}
一周热门 更多>