【STM8-SO8-DISCO】+ 基本定时器四中点灯
小8芯的STM8S001 只有TIM1,2,4。 TIM4为基本定时器。只有些基本功能。用它最好理解了。
先看一下原理图:
IO口1 PA1/PD6
IO口5 PA3/PB5
IO口6 PB4
IO口7 PC3/PC4/PC5 上面四个IO口可以用
IO口8 SWID不能占用
IO口7用到按键了,IO口5用到LED1了。
可以根据情况使用。
下面开始
1,安装IAR开发环境。
2,申请一个免费的lisensee。
3,建立stm8s001test目录,在它下面建立:project,user,libraries三个目录。
4,拷贝一个STM8的工程模板到project目录。
5,拷贝main.c stm8s_conf.h stm8s_it.h stm8s_it.c到user目录。
6,拷贝库文件到libraries目录。
7,加入文件到工程。(如果以前有删除后加入):
8,配置头文件路径。
9,main.c 加入
- /* Includes ------------------------------------------------------------------*/
- #include "stm8s.h"
- #define LED_GPIO_PORT (GPIOA)
- #define LED_GPIO_PINS (GPIO_PIN_3)
- #define TIM4_PERIOD 124
- void Delay (uint16_t nCount);
- static void TIM4_Config(void);
- void LED1_Toggle();
- void main(void){
- /* Initialize I/Os in Output Mode */
- GPIO_Init(LED_GPIO_PORT, (GPIO_Pin_TypeDef)LED_GPIO_PINS, GPIO_MODE_OUT_PP_LOW_FAST);
- /* TIM4 configuration -----------------------------------------*/
- TIM4_Config();
- while (1)
- {
- }
- }
- void Delay(uint16_t nCount){
- while (nCount != 0){ nCount--; }
- }
- static void TIM4_Config(void)
- {
- /* TIM4 configuration:
- - TIM4CLK is set to 16 MHz, the TIM4 Prescaler is equal to 128 so the TIM1 counter
- clock used is 16 MHz / 128 = 125 000 Hz
- - With 125 000 Hz we can generate time base:
- max time base is 2.048 ms if TIM4_PERIOD = 255 --> (255 + 1) / 125000 = 2.048 ms
- min time base is 0.016 ms if TIM4_PERIOD = 1 --> ( 1 + 1) / 125000 = 0.016 ms
- - In this example we need to generate a time base equal to 1 ms
- so TIM4_PERIOD = (0.001 * 125000 - 1) = 124 */
- /* Time base configuration */
- TIM4_TimeBaseInit(TIM4_PRESCALER_128, TIM4_PERIOD);
- /* Clear TIM4 update flag */
- TIM4_ClearFlag(TIM4_FLAG_UPDATE);
- /* Enable update interrupt */
- TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
- /* enable interrupts */
- enableInterrupts();
- /* Enable TIM4 */
- TIM4_Cmd(ENABLE);
- }
- void LED1_Toggle(){
- GPIO_WriteReverse(LED_GPIO_PORT, (GPIO_Pin_TypeDef)LED_GPIO_PINS);
- }
[color=rgb(51, 102, 153) !important]复制代码
stm8s_it.c加入
- extern void LED1_Toggle();
- ...
- ...
- INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23)
- {
- /* In order to detect unexpected events during development,
- it is recommended to set a breakpoint on the following instruction.
- */
- static uint16_t tims=0;
- tims++;
- if(tims >1000)
- {
- tims =0;
- LED1_Toggle();
- }
- }
[color=rgb(51, 102, 153) !important]复制代码
10,编译调试:
本文转载于【STM8-SO8-DISCO】+ (1)基本定时器四中点灯
http://www.stmcu.org.cn/module/forum/thread-619231-1-1.html
|