给STM32工程加入类似Arduino编程里的micros()、millis()函数

想给STM32工搞一个已经开机时长,和执行一段函数所需要的时间,用一个定时器去计时又浪费。在其他平台有类似的函数可以实现,STM32也搞一个。找了一圈没发现前人的脚步(有人做出来了没公开发表而已),倒是发现一个标题党。最终在crazepony飞控代码中发现了蛛丝马迹。我要开始栽树了。
下面开始移植
平台 :stm32f407
函数库:STM32F4xx_DSP_StdPeriph_Lib_V1.8.0
IDE:IAR
先在stm32f4xx_it.c中添加要用的变量

// cycles per microsecond
static volatile uint32_t usTicks = 0;
// current uptime for 1kHz systick timer. will rollover after 49 days. hopefully we won't care.
volatile uint32_t sysTickUptime = 0;

在stm32f4xx_it.c中添加下面函数,利用嘀嗒定时器中断计数实现

void SysTick_Handler(void)
{
  sysTickUptime++;
}

///循环计数器初始化
void cycleCounterInit(void)
{
    RCC_ClocksTypeDef clocks;
    RCC_GetClocksFreq(&clocks);
    usTicks = clocks.SYSCLK_Frequency / 1000000;
    SysTick_Config(clocks.HCLK_Frequency / 1000);  //SysTick开启系统tick定时器并初始化其中断,1ms
}

// Return system uptime in microseconds (rollover in
  • 12
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
以下是使用STM32F103库函数控制HC-SR04超声波模块的一些示例代码: 1. 初始化GPIO口 ```c void HC_SR04_GPIO_Init(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能GPIOA时钟 GPIO_InitTypeDef GPIO_InitStructure; //PA0作为Trig输出,PA1作为Echo输入 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); } ``` 2. 计算距离 ```c #define SOUND_SPEED 340.0 //音速,单位m/s float HC_SR04_GetDistance(void) { uint32_t start_time, end_time; float distance; //发送10us脉冲 GPIO_SetBits(GPIOA, GPIO_Pin_0); delay_us(10); GPIO_ResetBits(GPIOA, GPIO_Pin_0); //等待Echo输出高电平 while (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1) == RESET); //计时,直到Echo输出低电平 start_time = micros(); while (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1) == SET); end_time = micros(); //计算距离 distance = (end_time - start_time) / 2.0 * SOUND_SPEED / 1000000.0; return distance; } ``` 其中,`delay_us()`和`micros()`是自定义的延时函数,用于产生10us脉冲和计时。`SOUND_SPEED`是音速,根据环境温度和湿度不同而有所变化,一般取340m/s。 3. 完整代码 ```c #include "stm32f10x.h" void HC_SR04_GPIO_Init(void); float HC_SR04_GetDistance(void); void delay_us(uint32_t us); uint32_t micros(void); int main(void) { HC_SR04_GPIO_Init(); while (1) { float distance = HC_SR04_GetDistance(); printf("Distance: %0.2f cm\r\n", distance * 100); delay_us(500000); } } void HC_SR04_GPIO_Init(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能GPIOA时钟 GPIO_InitTypeDef GPIO_InitStructure; //PA0作为Trig输出,PA1作为Echo输入 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); } float HC_SR04_GetDistance(void) { uint32_t start_time, end_time; float distance; //发送10us脉冲 GPIO_SetBits(GPIOA, GPIO_Pin_0); delay_us(10); GPIO_ResetBits(GPIOA, GPIO_Pin_0); //等待Echo输出高电平 while (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1) == RESET); //计时,直到Echo输出低电平 start_time = micros(); while (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1) == SET); end_time = micros(); //计算距离 distance = (end_time - start_time) / 2.0 * SOUND_SPEED / 1000000.0; return distance; } void delay_us(uint32_t us) { us *= (SystemCoreClock / 1000000) / 5; while (us--) { asm("nop"); } } uint32_t micros(void) { static uint32_t us_ticks = 0; us_ticks += (SysTick->LOAD + 1 - SysTick->VAL) / (SystemCoreClock / 1000000); return us_ticks; } ```
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值