freeRTOS操作系统机器人实现
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

36 lines
1.0 KiB

#include "main.h"
#include "bsp_pin.h"
void bsp_pin_init(GPIO_TypeDef *port, uint16_t pin, uint8_t mode)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = pin;
GPIO_InitStruct.Mode = (mode == 0) ? GPIO_MODE_OUTPUT_PP : GPIO_MODE_INPUT;
if (mode == 1) GPIO_InitStruct.Pull = GPIO_PULLUP;
else if (mode == 2) GPIO_InitStruct.Pull = GPIO_PULLDOWN;
else GPIO_InitStruct.Pull = GPIO_NOPULL;
if (mode == 0) GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(port, &GPIO_InitStruct);
}
void delay_us(int nus)
{
uint32_t ticks;
uint32_t told,tnow,tcnt=0;
uint32_t reload=SysTick->LOAD; //LOAD的值
ticks=nus*72; //需要的节拍数
told=SysTick->VAL; //刚进入时的计数器值
while(1)
{
tnow=SysTick->VAL;
if(tnow!=told)
{
if(tnow<told)tcnt+=told-tnow; //这里注意一下SYSTICK是一个递减的计数器就可以了.
else tcnt+=reload-tnow+told;
told=tnow;
if(tcnt>=ticks)break; //时间超过/等于要延迟的时间,则退出.
}
};
}