使用外部精准时钟
#include “rcc_driver.h”
//1.硬件初始化:72MHz
void rcc_init(void)
{
//0.将RCC设置为缺省状态
RCC_DeInit();
//1.选择合适的时钟源:HSE
RCC_HSEConfig(RCC_HSE_ON);
//2.检测HSE是否真的启动
if(RCC_WaitForHSEStartUp() == SUCCESS)
{
//AHB APB1 APB2
RCC_HCLKConfig(RCC_SYSCLK_Div1); //AHB
RCC_PCLK1Config(RCC_HCLK_Div2); //APB1
RCC_PCLK2Config(RCC_SYSCLK_Div1); //APB2 //RCC_HCLK_Div1
//配置锁相环PLL
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
//检测(等待PLL启动)
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) != SET);
//将锁相环时钟设置为系统时钟
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
//等待PLL时钟已经设置为系统时钟
while(RCC_GetSYSCLKSource() != 0x08);
}
}
滴答计时器,实现精准延时
#include “systick_driver.h”
//1.硬件初始化:滴答定时器
void systick_init(void)
{
SysTick-》CTRL &= ~(0x01 《《 2); //选择AHB/8= 9MHz
// SysTick-》CTRL &= 0xFFFFFFFB; //选择AHB/8= 9MHz
}
//2.使用硬件:延时
//毫秒延时
void delay_ms(int nms)
{
int temp;
SysTick-》LOAD = nms * 9000; //计数值
SysTick-》VAL = 0x00; //清除当前值寄存器
SysTick-》CTRL |= 0x01; //使能计数器
do
{
temp = SysTick-》CTRL;
}while(((temp & 0x01) == 0x01) && !(temp & (0x01 《《 16))); //判断计数完成(使能位,16bit(计数到0的标志位))
SysTick-》CTRL &= ~0x01; //关闭计数器
//SysTick-》CTRL &= 0xFFFFFFFE;
SysTick-》LOAD = 0x00; //清除计数值
SysTick-》VAL = 0x00; //清除当前值寄存器
}
//毫秒延时
void delay_us(int nus)
{
int temp;
SysTick-》LOAD = nus * 9; //计数值
SysTick-》VAL = 0x00; //清除当前值寄存器
SysTick-》CTRL |= 0x01; //使能计数器
do
{
temp = SysTick-》CTRL;
}while(((temp & 0x01) == 0x01) && !(temp & (0x01 《《 16))); //判断计数完成(使能位,16bit(计数到0的标志位))
SysTick-》CTRL &= ~0x01; //关闭计数器
//SysTick-》CTRL &= 0xFFFFFFFE;
SysTick-》LOAD = 0x00; //清除计数值
SysTick-》VAL = 0x00; //清除当前值寄存器
}
#include “stm32f10x.h”
#include “led_driver.h”
#include “rcc_driver.h”
#include “systick_driver.h”
void flow_lamp(void)
{
//红灯亮
led_control(LED_ALL,LED_OFF);
led_control(LED_RED,LED_ON);
delay_ms(1000);
//绿灯亮
led_control(LED_ALL,LED_OFF);
led_control(LED_GREEN,LED_ON);
delay_ms(1000);
//蓝灯亮
led_control(LED_ALL,LED_OFF);
led_control(LED_BLUE,LED_ON);
delay_ms(1000);
}
int main(void)
{
rcc_init();
systick_init();
led_init();
while (1)
{
flow_lamp();
}
}
使用外部精准时钟
#include “rcc_driver.h”
//1.硬件初始化:72MHz
void rcc_init(void)
{
//0.将RCC设置为缺省状态
RCC_DeInit();
//1.选择合适的时钟源:HSE
RCC_HSEConfig(RCC_HSE_ON);
//2.检测HSE是否真的启动
if(RCC_WaitForHSEStartUp() == SUCCESS)
{
//AHB APB1 APB2
RCC_HCLKConfig(RCC_SYSCLK_Div1); //AHB
RCC_PCLK1Config(RCC_HCLK_Div2); //APB1
RCC_PCLK2Config(RCC_SYSCLK_Div1); //APB2 //RCC_HCLK_Div1
//配置锁相环PLL
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
//检测(等待PLL启动)
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) != SET);
//将锁相环时钟设置为系统时钟
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
//等待PLL时钟已经设置为系统时钟
while(RCC_GetSYSCLKSource() != 0x08);
}
}
滴答计时器,实现精准延时
#include “systick_driver.h”
//1.硬件初始化:滴答定时器
void systick_init(void)
{
SysTick-》CTRL &= ~(0x01 《《 2); //选择AHB/8= 9MHz
// SysTick-》CTRL &= 0xFFFFFFFB; //选择AHB/8= 9MHz
}
//2.使用硬件:延时
//毫秒延时
void delay_ms(int nms)
{
int temp;
SysTick-》LOAD = nms * 9000; //计数值
SysTick-》VAL = 0x00; //清除当前值寄存器
SysTick-》CTRL |= 0x01; //使能计数器
do
{
temp = SysTick-》CTRL;
}while(((temp & 0x01) == 0x01) && !(temp & (0x01 《《 16))); //判断计数完成(使能位,16bit(计数到0的标志位))
SysTick-》CTRL &= ~0x01; //关闭计数器
//SysTick-》CTRL &= 0xFFFFFFFE;
SysTick-》LOAD = 0x00; //清除计数值
SysTick-》VAL = 0x00; //清除当前值寄存器
}
//毫秒延时
void delay_us(int nus)
{
int temp;
SysTick-》LOAD = nus * 9; //计数值
SysTick-》VAL = 0x00; //清除当前值寄存器
SysTick-》CTRL |= 0x01; //使能计数器
do
{
temp = SysTick-》CTRL;
}while(((temp & 0x01) == 0x01) && !(temp & (0x01 《《 16))); //判断计数完成(使能位,16bit(计数到0的标志位))
SysTick-》CTRL &= ~0x01; //关闭计数器
//SysTick-》CTRL &= 0xFFFFFFFE;
SysTick-》LOAD = 0x00; //清除计数值
SysTick-》VAL = 0x00; //清除当前值寄存器
}
#include “stm32f10x.h”
#include “led_driver.h”
#include “rcc_driver.h”
#include “systick_driver.h”
void flow_lamp(void)
{
//红灯亮
led_control(LED_ALL,LED_OFF);
led_control(LED_RED,LED_ON);
delay_ms(1000);
//绿灯亮
led_control(LED_ALL,LED_OFF);
led_control(LED_GREEN,LED_ON);
delay_ms(1000);
//蓝灯亮
led_control(LED_ALL,LED_OFF);
led_control(LED_BLUE,LED_ON);
delay_ms(1000);
}
int main(void)
{
rcc_init();
systick_init();
led_init();
while (1)
{
flow_lamp();
}
}
举报