GCC __attribute__((mode(XX))

用gcc -E可以得到下面的预编译结果:
typedef unsigned int u_int8_t __attribute__ ((__mode__ (__QI__)));
typedef unsigned int u_int16_t __attribute__ ((__mode__ (__HI__)));
typedef unsigned int u_int32_t __attribute__ ((__mode__ (__SI__)));
typedef unsigned int u_int64_t __attribute__ ((__mode__ (__DI__)));


QI: An integer that is as wide as the smallest addressable unit, usually 8 bits.

HI: An integer, twice as wide as a QI mode integer, usually 16 bits.

SI: An integer, four times as wide as a QI mode integer, usually 32 bits.

DI: An integer, eight times as wide as a QI mode integer, usually 64 bits.

SF: A floating point value, as wide as a SI mode integer, usually 32 bits.

DF: A floating point value, as wide as a DI mode integer, usually 64 bits.

So DI is essentially sizeof(char) * 8.
#include "stm32f4xx.h" #include "stm32f4xx_adc.h" #include "stm32f4xx_gpio.h" #include "stm32f4xx_rcc.h" // 系统时钟配置 (168MHz HSE) void SystemClock_Config(void) { RCC_DeInit(); RCC_HSEConfig(RCC_HSE_ON); while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET); RCC_PLLConfig(RCC_PLLSource_HSE, 8, 336, 2, 7); // PLLM=8, PLLN=336, PLLP=2, PLLQ=7 RCC_PLLCmd(ENABLE); while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); RCC_HCLKConfig(RCC_SYSCLK_Div1); RCC_PCLK1Config(RCC_HCLK_Div4); RCC_PCLK2Config(RCC_HCLK_Div2); } // ADC初始化函数修正 void ADC1_Init(void) { // 修改函数名避免冲突 ADC_InitTypeDef ADC_InitStruct = {0}; ADC_CommonInitTypeDef ADC_CommonInitStruct = {0}; // 启用时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); // 配置GPIO GPIO_InitTypeDef GPIO_InitStruct = {0}; GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStruct); // ADC公共配置 ADC_CommonInitStruct.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStruct.ADC_Prescaler = ADC_Prescaler_Div4; ADC_CommonInitStruct.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStruct.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; ADC_CommonInit(&ADC_CommonInitStruct); // ADC独立配置 ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b; ADC_InitStruct.ADC_ScanConvMode = DISABLE; ADC_InitStruct.ADC_ContinuousConvMode = ENABLE; ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; // 修正参数名 ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStruct.ADC_NbrOfConversion = 1; ADC_Init(ADC1, &ADC_InitStruct); // 配置规则通道 ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_480Cycles); ADC_Cmd(ADC1, ENABLE); ADC_SoftwareStartConv(ADC1); } // GPIO初始化 (PD15控制继电器) void Relay_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; // 降低速度避免过冲 GPIO_Init(GPIOD, &GPIO_InitStruct); } // ADC均值读取函数(避免变量未使用警告) uint16_t ADC_ReadAverage(uint8_t samples) { uint32_t sum = 0; for(uint8_t i = 0; i < samples; i++) { while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); sum += ADC_GetConversionValue(ADC1); } return (uint16_t)(sum / samples); } // 滞回比较控制 void PumpControl(void) { #define WATER_THRESHOLD 1500 // 12位ADC阈值 #define HYSTERESIS 50 // 滞回区间 static uint8_t pumpState = 0; // 使用 变量避免警告 uint16_t adc_value = ADC_ReadAverage(10); if (adc_value > (WATER_THRESHOLD + HYSTERESIS)) { GPIO_ResetBits(GPIOD, GPIO_Pin_15); // 关水泵 pumpState = 0; // 更新状态 } else if (adc_value < (WATER_THRESHOLD - HYSTERESIS)) { GPIO_SetBits(GPIOD, GPIO_Pin_15); // 开水泵 pumpState = 1; // 更新状态 } } int main(void) { SystemClock_Config(); ADC1_Init(); // 使用修正后的函数名 Relay_GPIO_Init(); while(1) { PumpControl(); for(volatile uint32_t i = 0; i < 500000; i++); // 消除变量未使用警告 } } *** Using Compiler 'V5.06 update 6 (build 750)', folder: 'D:\stm32\软件\ARM\ARMCC\Bin' compiling main.c... main.c(87): warning: #550-D: variable "pumpState" was set but never used static uint8_t pumpState = 0; // 使用 变量避免警告 main.c: 1 warning, 0 errors "main.c" - 0 Error(s), 1 Warning(s).进行修改
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值