历史上的今天
今天是:2024年12月27日(星期五)
2019年12月27日 | STM8S——8位基本定时器(TIM4)
2019-12-27 来源:eefocus
简介:该定时器由一个带可编程预分频器的8位自动重载的向上计数器所组成,它可以用来作为时基发生器,具有溢出中断功能。
主要功能:
(1)8位向上计数的自动重载计数器;
(2)3位可编程的预分配器(可在运行中修改),提供1、2、4、8、16、32、64、128这8种分频比例;
(3)中断产生:更新中断(溢出,计数器初始化)。
代码实现:
1 /* Includes ------------------------------------------------------------------*/
2 #include "stm8s.h"
3
4 /* Private define ------------------------------------------------------------*/
5 #define TIM4_PERIOD 124
6 /* Private variables ---------------------------------------------------------*/
7 __IO uint32_t TimingDelay = 0;
8 /* Private function prototypes -----------------------------------------------*/
9 void Delay(__IO uint32_t nTime);
10 void TimingDelay_Decrement(void);
11 static void TIM4_Config(void);
12
13 /**
14 * @brief Main program.
15 * @param None
16 * @retval None
17 */
18 void main(void)
19 {
20 /* TIM4 configuration -----------------------------------------*/
21 TIM4_Config();
22
23 /* Insert 50 ms delay */
24 Delay(50);
25 }
26 }
27
28 /**
29 * @brief Configure TIM4 to generate an update interrupt each 1ms
30 * @param None
31 * @retval None
32 */
33 static void TIM4_Config(void)
34 {
35 /* TIM4 configuration:
36 - TIM4CLK is set to 16 MHz, the TIM4 Prescaler is equal to 128 so the TIM1 counter
37 clock used is 16 MHz / 128 = 125 000 Hz
38 - With 125 000 Hz we can generate time base:
39 max time base is 2.048 ms if TIM4_PERIOD = 255 --> (255 + 1) / 125000 = 2.048 ms
40 min time base is 0.016 ms if TIM4_PERIOD = 1 --> ( 1 + 1) / 125000 = 0.016 ms
41 - In this example we need to generate a time base equal to 1 ms
42 so TIM4_PERIOD = (0.001 * 125000 - 1) = 124 */
43
44 /* Time base configuration */
45 TIM4_TimeBaseInit(TIM4_PRESCALER_128, TIM4_PERIOD);
46 /* Clear TIM4 update flag */
47 TIM4_ClearFlag(TIM4_FLAG_UPDATE);
48 /* Enable update interrupt */
49 TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
50
51 /* enable interrupts */
52 enableInterrupts();
53
54 /* Enable TIM4 */
55 TIM4_Cmd(ENABLE);
56 }
57
58
59 /**
60 * @brief Inserts a delay time.
61 * @param nTime: specifies the delay time length, in milliseconds.
62 * @retval None
63 */
64 void Delay(__IO uint32_t nTime)
65 {
66 TimingDelay = nTime;
67
68 while (TimingDelay != 0);
69 }
70
71 /**
72 * @brief Decrements the TimingDelay variable.
73 * @param None
74 * @retval None
75 */
76 void TimingDelay_Decrement(void)
77 {
78 if (TimingDelay != 0x00)
79 {
80 TimingDelay--;
81 }
82 }
TIM4
史海拾趣
|
我想实现P1.1端口跟随P1.4端口同步输出,都是驱动LED,P1.4端口使用定时器定时翻转,主循环程序随时判断P1.4状态,同步驱动P1.1。 开始我是这样写的: 看来没问题,在KEIL仿真运行也没问题,可是下载到89S52中,只见OUT闪烁,LED根本不动, ...… 查看全部问答> |
|
【藏书阁】 集成电路(荒井英甫)——21世纪大学新型参考教材系列 目录: 集成电路a 1 集成电路的学习方法 1·1 集成电路的发明 1·2 集成电路发展的动力 1·3 集成电路的未来 1·4 本书的构成 练习题 引用文献 2 集成电路中的半导体器件 2·1 pn结 2·2 双极型晶体管 2·3 mos晶体管 2·4 集成电路 ...… 查看全部问答> |
|
1、REMAP: 提到REMAP。首先应想到什么是MAP,英语不好,开始就断章取义,MAP就是地图嘛,Memory Map就是存储器地图,不过这个地图的参考坐标不是经纬度,而是地址,进而叫做存储器映射。由于要适应不同存储器容量要求的 ...… 查看全部问答> |
|
哪里有windows CE 5.0(platform builder 5.0)?? google了好久,都没找到windows CE 5.0下载。 不知道各位谁有这个东西,或者知道哪里有这个下载。 如果谁有的可以传给我就更好了。 请加我 QQ:46231244 谢谢… 查看全部问答> |




