基于STM32F103的定点数测试

没有FPU的单片机就是废柴(

#include "stm32f10x.h"
#include 
#include 
//stdio.hĿǰ»¹²»ÓÃ

typedef int32_t q16_t;
#define Q16_SHIFT 16
#define Q16_MULT (1 << Q16_SHIFT)

q16_t q16_mul(q16_t a, q16_t b) {
    return (q16_t)(((int64_t)a * b) >> 16);
}

// Q16??
q16_t q16_div(q16_t a, q16_t b) {
    return (q16_t)(((int64_t)a << 16) / b);
}

void fixed_point_example(void) {
    q16_t a = 3.14159 * 65536;  // 3.14159 in Q16
    q16_t b = 2.71828 * 65536;  // 2.71828 in Q16
    q16_t result = q16_mul(a, b);
}

void LED_Init(void) {
    GPIO_InitTypeDef GPIO_InitStruct;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
    
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_Init(GPIOC, &GPIO_InitStruct);
    
    GPIO_SetBits(GPIOC, GPIO_Pin_13); // ????
}

int main(void) {
	  q16_t two = 2 * Q16_MULT;    // 2.0 in Q16
    q16_t three = 3 * Q16_MULT;  // 3.0 in Q16
    q16_t result = q16_div(two, three);
    // ?????
    SystemInit();
    LED_Init();
    
    // ??2/3??(Q16??)
    
    // ????:2/3 ˜ 0.66666 ? Q16??0x0000AAAA
    if(result >= 0x0000AAA9 && result <= 0x0000AAAB) {
        GPIO_ResetBits(GPIOC, GPIO_Pin_13); // ????,??LED
    } else {
        GPIO_SetBits(GPIOC, GPIO_Pin_13);   // ????,??LED
    }
    
    while(1);
}

包亮灯的,不亮评论区见

你可能感兴趣的:(stm32,单片机,嵌入式硬件)