本文主要解决两个问题
1 STM32的IO口要反转,怎么实现?
2 嘀嗒定时器systick的配置
解答1:
单片机的口,反转非常easy。sbit led = P1 ^6; led = ~led;而STM32的口要让它反转。如何实现呢?
非常easy,对想要反转的IO口取异或: GPIOx->ODR ^= GPIO_Pin;
解答2:
SysTick是1个24bit递减计数器
SysTick_Config(SystemFrequency / 10) 函数的形參就是systick重装定时器的值。
systck计数频率为每秒48000000次,所以4800000次就是1/10秒。也就是100ms。
SysTick的重装寄存器决定了定时器频率。
以下介绍STM32中的systick。Systick 部分内容属于NVIC控制部分。一共同拥有4个寄存器,名称和地址各自是:
STK_CSR, 0xE000E010 -- 控制寄存器
STK_LOAD, 0xE000E014 -- 重载寄存器
STK_VAL, 0xE000E018 -- 当前值寄存器
STK_CALRB, 0xE000E01C -- 校准值寄存器
首先看STK_CSR控制寄存器:寄存器内有4个位t具有意义
第0位:ENABLE。Systick 使能位 (0:关闭Systick功能;1:开启Systick功能)
第1位:TICKINT。Systick 中断使能位 (0:关闭Systick中断;1:开启Systick中断)
第2位:CLKSOURCE,Systick时钟源选择 (0:使用HCLK/8 作为Systick时钟;1:使用HCLK作为Systick时钟)
第3位:COUNTFLAG。Systick计数比較标志,假设在上次读取本寄存器后,SysTick 已经数到了0。则该位为1。假设读取该位,该位将自己主动清零
STK_LOAD 重载寄存器:
Systick是一个递减的定时器。当定时器递减至0时,重载寄存器中的值就会被重装载,继续開始递减。STK_LOAD 重载寄存器是个24位的寄存器最大计数0xFFFFFF。
STK_VAL当前值寄存器:
也是个24位的寄存器。读取时返回当前倒计数的值,写它则使之清零,同一时候还会清除在SysTick 控制及状态寄存器中的COUNTFLAG 标志。
STK_CALRB 校准值寄存器:
这个寄存器好像眼下的水平我还用不到,大体意思明确点,把英文说明放这吧:
位31 NOREF :1=没有外部參考时钟(STCLK 不可用)0=外部參考时钟可用
位30 SKEW:1=校准值不是准确的1ms 0=校准值是准确的1ms
位[23:0] :Calibration value
Indicates the calibration value when the SysTick counter runs on HCLK max/8 as external clock. The value is product dependent, please refer to the Product Reference Manual, SysTick Calibration Value section. When HCLK is programmed at the maximum frequency, the SysTick period is 1ms. If calibration information is not known, calculate the calibration value required from the frequency of the processor clock or external clock.
cortex_m0.c文件里定义
<span style="font-size:14px;">/** \brief System Tick Configuration The function initializes the System Timer and its interrupt, and starts the System Tick Timer.
Counter is in free running mode to generate periodic interrupts. \param [in] ticks Number of ticks between two interrupts. \return 0 Function succeeded.
\return 1 Function failed. \note When the variable <b>__Vendor_SysTickConfig</b> is set to 1, then the
function <b>SysTick_Config</b> is not included. In this case, the file <b><i>device</i>.h</b>
must contain a vendor-specific implementation of this function. */
__STATIC_INLINE uint32_t SysTick_Config(uint32_t ticks)
{
if ((ticks - 1) > SysTick_LOAD_RELOAD_Msk) return (1); /* Reload value impossible */ SysTick->LOAD = ticks - 1; /* set reload register */
NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1); /* set Priority for Systick Interrupt */
SysTick->VAL = 0; /* Load the SysTick Counter Value */
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk |
SysTick_CTRL_TICKINT_Msk |
SysTick_CTRL_ENABLE_Msk; /* Enable SysTick IRQ and SysTick Timer */
return (0); /* Function successful */
}</span>
<span style="font-size:14px;">#include "SysTick.h" __IO uint32_t TimingDelay;
uint32_t time_count = 0;
uint16_t idx_led = 0;
//SystemCoreClock/100000) 10 us 进入一次中断
void SysTick_Config_Init(void)
{
/* SystemFrequency / 1000 1ms中断一次
* SystemFrequency / 100000 10us中断一次
* SystemFrequency / 1000000 1us中断一次
*/
if(SysTick_Config(SystemCoreClock/1000))
{
while(1);
}
// 关闭滴答定时器
//SysTick->CTRL &= ~ SysTick_CTRL_ENABLE_Msk;
} /**
* @brief Inserts a delay time.
* @param nTime: specifies the delay time length, in milliseconds.
* @retval None
*/
void Delay_ms(__IO uint32_t nTime)
{
TimingDelay = nTime;
// 使能滴答定时器
// SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; while(TimingDelay != 0);
} /**
* @brief Decrements the TimingDelay variable.
* @param None
* @retval None
*/
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
} void SysTick_Handler(void)
{ TimingDelay_Decrement(); idx_led++;
if(idx_led >= 1000){
idx_led = 0;
GPIOA->ODR^=GPIO_Pin_4;
}
}</span>