找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5830|回复: 0
打印 上一主题 下一主题
收起左侧

求救!! stm32f103c8t6 TIM4 定时器问题

[复制链接]
跳转到指定楼层
楼主
ID:348627 发表于 2018-11-9 20:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
每一秒 是数码管显示加一   可是实际操作大约10s才加一  。想了很长时间 没搞懂  希望大佬帮帮忙谢谢谢  !!跪谢! 下面是代码


extern  unsigned int Count;
int main(void)
{
NVIC_Config();
SMG_Config();
TIM4_Config();

GPIO_ResetBits(GPIOC,GPIO_Pin_13);

while(1)
{
if(Count>=60)
Count=0;

SMG_Display(Count);
}
}
void TIM4_IRQHandler(void)
{

TIM_ClearITPendingBit(TIM4,TIM_IT_Update);
GPIO_SetBits(GPIOC,GPIO_Pin_13);
Count++;

}

#include "stm32f10x.h"
#include "tim4.h"
u8 table[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,
0x5e,0x79,0x71};
#define Set_Dula   GPIO_SetBits(GPIOB,GPIO_Pin_0)
#define Set_Wela   GPIO_SetBits(GPIOB,GPIO_Pin_1)
#define CLR_Dula   GPIO_ResetBits(GPIOB,GPIO_Pin_0)
#define CLR_Wela   GPIO_ResetBits(GPIOB,GPIO_Pin_1)
unsigned int Count=0,shi=0,ge=0;
void Delay_ms(__IO u32 nCount)
{
  for(; nCount != 0; nCount--);
}
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM4_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
TIM_DeInit(TIM4);
TIM_InternalClockConfig(TIM4);
TIM_TimeBaseStructure.TIM_Prescaler = 35999;
TIM_TimeBaseStructure.TIM_Period = 1999;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode =TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);
TIM_Cmd(TIM4,ENABLE);
//TIM_PrescalerConfig(TIM4,2000,TIM_PSCReloadMode_Immediate);
TIM_ClearFlag(TIM4,TIM_FLAG_Update);
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);//计数溢出时候发生中断

}
void SMG_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
   
}
void SMG_Display(unsigned int Count)
{
shi=Count/10;
ge=Count%10;
GPIO_Write(GPIOA,0XFe);
Set_Wela;
CLR_Wela;

GPIO_Write(GPIOA,table[shi]);
Set_Dula;
CLR_Dula;

Delay_ms(0X7FB);

  GPIO_Write(GPIOA,0XFD);
Set_Wela;
CLR_Wela;

GPIO_Write(GPIOA,table[ge]);
Set_Dula;
CLR_Dula;
  Delay_ms(0X7FB);

}



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表