[其他ST产品] GPIO_InitTypeDef GPIO_Instructure和ADC_InitTypeDef ADC_InitStructure的先后顺序不同程序结果不一样

[复制链接]
1468|19
 楼主| stormwind123 发表于 2023-4-13 10:03 | 显示全部楼层 |阅读模式
AD, ADC, GPIO, IO, ST
#include "adc.h"
void adc_init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);GPIO_InitTypeDef GPIO_Instruction;GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);        GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);/*配置成员*/GPIO_Instruction.GPIO_Pin=GPIO_Pin_9;        GPIO_Instruction.GPIO_Mode=GPIO_Mode_AF;        GPIO_Instruction.GPIO_OType=GPIO_OType_PP;        GPIO_Instruction.GPIO_Speed=GPIO_Fast_Speed;        GPIO_Instruction.GPIO_PuPd=GPIO_PuPd_UP;        /*调用函数写入*/        GPIO_Init(GPIOA,&GPIO_Instruction);                /*配置成员*/GPIO_Instruction.GPIO_Pin=GPIO_Pin_10;        GPIO_Instruction.GPIO_Mode=GPIO_Mode_AF;        GPIO_Instruction.GPIO_OType=GPIO_OType_PP;        GPIO_Instruction.GPIO_Speed=GPIO_Medium_Speed;        GPIO_Instruction.GPIO_PuPd=GPIO_PuPd_DOWN;        /*调用函数写入*/        GPIO_Init(GPIOA,&GPIO_Instruction);
ADC_CommonInitTypeDef ADC_CommonInitStructure;
// 独立ADC模式
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
// 时钟为fpclk x分频
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div8;
// 禁止DMA直接访问模式
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
// 采样时间间隔
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_10Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
ADC_InitTypeDef ADC_InitStruction;
ADC_InitStruction.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStruction.ADC_ScanConvMode = DISABLE;
ADC_InitStruction.ADC_ContinuousConvMode = DISABLE;
// ADC_InitStruction.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStruction.ADC_ExternalTrigConv = ADC_ExternalTrigConvEdge_None;
ADC_InitStruction.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStruction.ADC_NbrOfConversion =1;
ADC_Init(ADC1, &ADC_InitStruction);// 配置 ADC 通道转换顺序为1,第一个转换,采样时间为3个时钟周期
ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_3Cycles);
ADC_Cmd(ADC1, ENABLE);
ADC_SoftwareStartConv(ADC1);
//                RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
}
void usart(void)
{
USART_InitTypeDef USART_InitStructure;        USART_InitStructure.USART_BaudRate=115200;        USART_InitStructure.USART_HardwareFlowControl=USART_Parity_No;        USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;        USART_InitStructure.USART_Parity=USART_Parity_No;        USART_InitStructure.USART_StopBits=USART_StopBits_1;        USART_InitStructure.USART_WordLength=USART_WordLength_8b;                USART_Init(USART1,&USART_InitStructure);                        /* 使能串口接收中断 */
// USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
//
// NVIC_Configuration();
USART_Cmd(USART1, ENABLE);
// USART_SendData(USART1,14);
}
///重定向c库函数printf到串口DEBUG_USART,重定向后可使用printf函数
int fputc(int ch, FILE f)
{
/ 发送一个字节数据到串口DEBUG_USART */
USART_SendData(USART1, (uint8_t) ch);
/* 等待发送完毕 */        while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);                        return (ch);
}
///重定向c库函数scanf到串口DEBUG_USART,重写向后可使用scanf、getchar等函数
int fgetc(FILE f)
{
/ 等待串口输入数据 */
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
return (int)USART_ReceiveData(USART1);
}
这是adc的文件,先把usart也放进去了,发现如果把void usart整体函数写到adc前面串口只会发一个。
int main(void)
{
Init_GPIO();
adc_init();
usart();
while(1)
{
ADC_SoftwareStartConv(ADC1);
while (!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC)){}ADC_Value[0]=ADC_GetConversionValue(ADC1);AD=(float)ADC_Value[0]/4096*(float)3.3;printf("\n \r %f",AD);}}
V853 发表于 2023-4-14 15:14 | 显示全部楼层
你GPIO和ADC有什么联动的地方吗?
朝生 发表于 2023-4-14 15:15 | 显示全部楼层
具体有什么区别?哪个符合你想要的?
芯路例程 发表于 2023-4-14 15:16 | 显示全部楼层
正常来说初始化顺序是不太会影响现象的。
Stahan 发表于 2023-4-19 23:54 | 显示全部楼层
应该是后初始化的部分把前面部分覆盖了吧
zhuww 发表于 2023-4-21 23:33 | 显示全部楼层
GPIO和ADC有联动
spark周 发表于 2023-4-21 23:35 | 显示全部楼层
具体有什么区别?
zhuww 发表于 2023-4-21 23:36 | 显示全部楼层
哪个符合你想要的?
llljh 发表于 2023-4-21 23:37 | 显示全部楼层
正常来说初始化顺序是不太会影响现象的。
wyjie 发表于 2023-4-21 23:39 | 显示全部楼层

应该是后初始化的部分把前面部分覆盖了吧
SantaBunny 发表于 2023-5-4 18:38 | 显示全部楼层
可能是后初始化的部分把前面部分覆盖了吧
SantaBunny 发表于 2023-5-4 18:38 | 显示全部楼层
可能是后初始化的部分把前面部分覆盖了吧
heweibig 发表于 2023-5-18 23:12 | 显示全部楼层
你GPIO和ADC有什么联动的地方吗?
huangchui 发表于 2023-5-18 23:13 | 显示全部楼层
具体有什么区别?哪个符合你想要的?
jiajs 发表于 2023-5-18 23:15 | 显示全部楼层
正常来说初始化顺序是不太会影响现象的。
jiajs 发表于 2023-5-18 23:16 | 显示全部楼层
应该是后初始化的部分把前面部分覆盖了吧
Pulitzer 发表于 2024-6-1 07:22 | 显示全部楼层

通孔(THT)和表面贴装(SMT)
童雨竹 发表于 2024-6-1 09:18 | 显示全部楼层

首先是进行按比例和具体的施胶量进行混合操作
Wordsworth 发表于 2024-6-1 10:21 | 显示全部楼层

产生较强的阻尼效果
公羊子丹 发表于 2024-6-1 12:17 | 显示全部楼层

该电容可以存储高达8KV的电位
您需要登录后才可以回帖 登录 | 注册

本版积分规则

605

主题

3771

帖子

3

粉丝
快速回复 在线客服 返回列表 返回顶部