测试程序如下:
#include "stm8s.h"
void CLK_Init(void) { /* Configure clock prescaler */ CLK->CKDIVR = 0x01; /* To be programmed with different value */ /* Fmaster, Fmaster/1, ... */ /* To select external source by automtic switch */ CLK->SWCR |= 0x02; /* Enable switch */ CLK->SWR = 0xB4; /* Select HSE */ while (!(CLK->SWCR & 0x08)); /* Wait for switch done */ /* verify the external clock is selected (optional) */ if (CLK->CMSR != 0xB4) while(1); CLK->CSSR |= 0x01; } /* Private defines -----------------------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/ /* Private functions ---------------------------------------------------------*/ #define LEDS_PORT (GPIOD) #define LED1_PIN (GPIO_PIN_0) #define LED2_PIN (GPIO_PIN_2) #define LED3_PIN (GPIO_PIN_3)
void Delay(u16 nCount) { /* Decrement nCount value */ while (nCount != 0) { nCount--; } }
int main(void) { u8 Leds = LED1_PIN|LED2_PIN|LED3_PIN; /* Contains which LEDs to operate */
u8 t = 10; /* Initialize I/Os in Output Mode */ GPIO_Init(LEDS_PORT, (LED1_PIN | LED2_PIN | LED3_PIN ), GPIO_MODE_OUT_PP_LOW_FAST); // 这里是用启动后默认的16MHz(HSI)/8=2MHz时钟工作 while (t--) { /* LEDs ON */ GPIO_WriteHigh(LEDS_PORT, Leds); Delay((u16)60000); Delay((u16)60000); /* LEDs OFF */ GPIO_WriteLow(LEDS_PORT, Leds); Delay((u16)60000); Delay((u16)60000); }
CLK_Init(); t=10; // 切换到HSE 24MHz晶振,FCPU=FMASTER/2,正常工作,LED闪烁变快了 while (t--) { /* LEDs ON */ GPIO_WriteHigh(LEDS_PORT, Leds); Delay((u16)60000); Delay((u16)60000); /* LEDs OFF */ GPIO_WriteLow(LEDS_PORT, Leds); Delay((u16)60000); Delay((u16)60000); }
CLK->CKDIVR = 0X00; // 切换到FCPU=FMASTER/1 24MHz,不工作,看上去是很快系统复位了。 while (1) { /* LEDs ON */ GPIO_WriteHigh(LEDS_PORT, Leds); Delay((u16)60000); Delay((u16)60000); /* LEDs OFF */ GPIO_WriteLow(LEDS_PORT, Leds); Delay((u16)60000); Delay((u16)60000); }
}
实验板是STM8 MINI KIT |