常用嵌入式编程技巧

根据频率设置定时器
#define SYSCLK_FREQ_96MHz_HSE  96000000
uint32_t SystemCoreClock         = SYSCLK_FREQ_96MHz_HSE;        /* System Clock Frequency (Core Clock) */
#define CLOCK_CYCLES_PER_SECOND  SystemCoreClock
#define MAX_RELOAD               0xFFFF

uint32_t period_cycles = CLOCK_CYCLES_PER_SECOND / freq;
uint16_t prescaler = (uint16_t)(period_cycles / MAX_RELOAD + 1);
uint16_t overflow = (uint16_t)((period_cycles + (prescaler / 2)) / prescaler);
printf("pulse:%d cycles,psc=%d,overflow=%d\r\n", period_cycles, prescaler, overflow);

TIM_PrescalerConfig(TIM4, prescaler-1, TIM_PSCReloadMode_Immediate);
TIM_SetAutoreload(TIM4, overflow-1);
置位清零切换字节中的某个位
//Through Macro we can do set resset Bit
#define set(a,n)   a|=(1<
设置16位寄存器的低8位
void dac_set_pulse(unsigned char amp)
{
    unsigned char data = 255 * amp / 100;
    unsigned short reg = GPIOB->OUTDR; // 获取当前值
    reg = reg & 0xFF00; // 高8位保持不变,清零低8位
    reg |= data;        // 高8位保持不变,赋值低8位
    GPIOB->OUTDR = reg;
}
切换 GPIO 引脚
GPIO_WriteBit(GPIOC, GPIO_Pin_13, GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_13) ^ 1);

加作者薇信:jiyuyun18, 留言:CSDN 嵌入式,加入嵌入式技术交流群

你可能感兴趣的:(STM32,risc-v)