RM-M3 时钟控制与System Tick操作。
- void System_Init (void)
{
RCC->GCCR = RCC_GCCR_HSIEN; // 启用内部8MHz RC oscillator。
while (!(RCC->GCCR & RCC_GCCR_HSISTB)); // 等待直到内部RC oscillator稳定。
/* [31] rw PLLDV = 0, CK_OUT的输入分频系数CK_PLL divide by 2.
[30:28] rw CKOUTDIV = 0, CK_OUT的输出分频系数。
[26:24] rw CKOUTSRC = 0, CKOUT时钟输出PIN的输出频率(No clock selected).
[23:22] rw USBPS = 0, USB时钟等于CK_PLL / 1.5
[27,21:18]rw PLLMF = 0x12, PLL倍频系数是18x.
[17] rw PLLPREDV = 0, PLL时钟源选用HSE时钟(PLLSEL=1时有效).
[16] rw PLLSEL = 0, PLL时钟源等于HSI时钟除2(8MHz / 2)
[15:14] rw ADCPS = 0, ADC时钟等于CK_APB2 / 2.
[13:11] rw APB2PS = 0, APB2时钟等于CK_AHB.
[10:8] rw APB1PS = 0, APB1时钟等于CK_AHB.
[7:4] rw AHBPS = 0, AHB时钟等于CK_SYS.
[3:2] r SCSS System clock switch status(Read only).
[1:0] rw SCS = 0, CK_SYS时钟来自CK_HSI
*/
RCC->GCFGR = 0x808000C;
/* [31:26] Reserved.
[25] r PLLSTB PLL Clock Stabilization Flag, 0: PLL is not stable
[24] rw PLLEN = 1, PLL enable.
[23:20] Reserved.
[19] rw CKMEN = 0, Disable the External 4 ~ 32 MHz crystal oscillator (HSE) clock monitor.
[18] rw HSEBPS = 0, Disable the HSE Bypass mode.
[17] r HSESTB 0: HSE oscillator is not stable
[16] rw HSEEN = 0, 0: External 4 ~ 32 MHz crystal oscillator disabled.
[15:8] r HSICALIB High Speed Internal Oscillator calibration value register
[7:3] rw HSIADJ High Speed Internal Oscillator clock trim adjust value
[2] Reserved.
[1] r HSISTB 0: HSI oscillator is not stable
[0] rw HSIEN 1: Internal 8 MHz RC oscillator enabled
*/
RCC->GCCR |= RCC_GCCR_PLLEN;
while(!(RCC->GCCR & RCC_GCCR_PLLSTB)); // Wait untill PLL is ready
RCC->GCFGR |= (uint32_t)RCC_GCFGR_SCS_PLL; // 切换CK_SYS时钟为CK_PLL
/* Wait untill PLL is chosen as system clock source */
while ((RCC->GCFGR & (uint32_t)RCC_GCFGR_SCSS) != (uint32_t)RCC_GCFGR_SCSS_PLL);
}
/**
* @brief This function handles SysTick Handler.
* @param None
* @retval None
*/
void SysTick_Handler(void)
{
if (!(++flag % 1000)) {
if (GPIO_ReadOutputBit(GPIOB, GPIO_PIN_10))
GPIO_ResetBits(GPIOB, GPIO_PIN_10);
else
GPIO_SetBits(GPIOB, GPIO_PIN_10);
flag = 0;
}
}
void delay_us(uint32_t nTime)
{
SysTick->VAL=0X00; // 初值
SysTick->LOAD=72*nTime; // 重装值;48个时钟周期=1us
SysTick->CTRL=0x00000005; // 第0位:使能时钟 第3位:使用内部时钟
while(!(SysTick->CTRL&0x00010000)); //计时
SysTick->CTRL|=0x00000004; // 第1位:产生异常请求
}
int main(void)
{
GPIO_InitPara GPIO_InitStructure;
RCC_AHBPeriphClock_Enable(RCC_AHBPERIPH_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_10;
GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
SysTick_Config(72000); /// 72000 / 72000000(72MHz) = 0.001秒。
while(1) {
//GPIO_SetBits(GPIOB, GPIO_PIN_11);
//delay_us(1000000);
//GPIO_ResetBits(GPIOB, GPIO_PIN_11);
//delay_us(1000000);
}
return 0;
}
|