21ic问答首页 - GD32为什么用PA2代替key按键功能不成功
GD32为什么用PA2代替key按键功能不成功
kk932024-07-25
配置PA4 ADC引脚,将PA2代替key功能,一脚接地一脚接PA2,想实现在另一key按键控制oled屏同时(使用了PA1),如果杜邦线连接了地与PA2,可以在oled屏幕上显示读取的ADC值程序如下 运行无误但是PA2连接时无法实现预期功能,PA1正常运行
请大佬解答~谢谢
#include "gd32f30x.h"
#include "systick.h"
#include <stdio.h>
#include "oled.h"
extern uint8_t IntFlag;//进入中断服务程序标志,在EXTI1_IRQHandler()函数置位
// 定义一个变量来跟踪当前显示的行
static uint8_t currentLine = 0;
// 定义一个数组来存储要显示的信息的索引或字符串指针
static const uint8_t chineseMessages[][2] = {
{12, 0}, //
{30, 1}, //
{48, 2}, //
{66, 3}, //
{84, 4}, //
{102, 5} //
};
static uint8_t *englishMessages[] = {
"HUZHOU",
"TEL:12345678"
};
uint16_t adc_value;
uint16_t Vol_Value;
float Vol_Value1;
void adc_config(void)
{
/* reset ADC */
adc_deinit(ADC1);
// /* ADC mode config */
adc_mode_config(ADC_MODE_FREE);
/* ADC continous function enable */
adc_special_function_config(ADC1, ADC_CONTINUOUS_MODE, ENABLE);
/* ADC data alignment config */
adc_data_alignment_config(ADC1, ADC_DATAALIGN_RIGHT);
/* ADC resolusion 6B */
// adc_resolution_config(ADC1, ADC_RESOLUTION_6B);
/* ADC channel length config */
adc_channel_length_config(ADC1, ADC_REGULAR_CHANNEL, 1);
/* ADC regular channel config */
adc_regular_channel_config(ADC1, 0, ADC_CHANNEL_4, ADC_SAMPLETIME_55POINT5);
/* ADC trigger config */
adc_external_trigger_source_config(ADC1, ADC_REGULAR_CHANNEL, ADC0_1_2_EXTTRIG_REGULAR_NONE);
adc_external_trigger_config(ADC1, ADC_REGULAR_CHANNEL, ENABLE);
/* enable ADC interface */
adc_enable(ADC1);
delay_1ms(1);
/* ADC calibration and reset calibration */
adc_calibration_enable(ADC1);
/* enable ADC interface */
// adc_software_trigger_enable(ADC0,ADC_REGULAR_CHANNEL);
}
int main(void)
{
rcu_ahb_clock_config(RCU_AHB_CKSYS_DIV1);//设置主频120M(#define __SYSTEM_CLOCK_120M_PLL_HXTAL (uint32_t)(120000000),8M外部晶振 (#define HXTAL_VALUE ((uint32_t)8000000))
systick_config();//1ms systick
rcu_periph_clock_enable(RCU_AF); //AF时钟使能
delay_1ms(1000);//等待1000ms
gpio_pin_remap_config(GPIO_SWJ_NONJTRST_REMAP, ENABLE);//下载口NJTRST引脚当做普通I/O口
rcu_periph_clock_enable(RCU_GPIOB);//PB时钟使能
gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_4);//PB4配置成输出
//配置PA4 ADC引脚
rcu_periph_clock_enable(RCU_GPIOA);
gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_4);
/* enable ADC1 clock */
rcu_periph_clock_enable(RCU_ADC1);
/* config ADC clock */
rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV8);
//配置ADC
adc_config();
adc_software_trigger_enable(ADC1, ADC_REGULAR_CHANNEL);
OLED_Init();//OLED初始化
OLED_Clear();//OLED清屏
gpio_init(GPIOA, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_1);//PA1配置成上拉输入
nvic_irq_enable(EXTI1_IRQn, 2U, 0U);//enable and set key EXTI interrupt to the lowest priority
gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOA, GPIO_PIN_SOURCE_1);//connect key EXTI line to key GPIO pin
exti_init(EXTI_1, EXTI_INTERRUPT, EXTI_TRIG_FALLING);//下降沿中断
exti_interrupt_flag_clear(EXTI_1);//清中断标志
//PA2给ADC
gpio_init(GPIOA, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_2);//PA2配置成上拉输入
nvic_irq_enable(EXTI2_IRQn, 2U, 0U);//enable and set key EXTI interrupt to the lowest priority
gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOA, GPIO_PIN_SOURCE_2);//connect key EXTI line to key GPIO pin
exti_init(EXTI_2, EXTI_INTERRUPT, EXTI_TRIG_FALLING);//下降沿中断
exti_interrupt_flag_clear(EXTI_2);//清中断标志
while (1)
{
if (gpio_input_bit_get(GPIOA, GPIO_PIN_2) == RESET && IntFlag == 0x55) //ADC显示电压 需要换个接口
{
adc_flag_clear(ADC1,ADC_FLAG_EOC);//清ADC结束标志
while(SET != adc_flag_get(ADC1,ADC_FLAG_EOC))
{//判断是否结束
adc_value = ADC_RDATA(ADC1);//读取ADC寄存器值
Vol_Value=adc_value*3300/4095/1000;//寄存器值转换成电压值
//Vol_Value1=330;
OLED_Clear();//OLED清屏
OLED_ShowNum(5,2,Vol_Value1,4,32);
delay_1ms(1000);
}//等待5秒
}
if (gpio_input_bit_get(GPIOA, GPIO_PIN_1) == RESET && IntFlag == 0x55) //信息显示
{
// 清除中断标志(重要,防止重复触发)
IntFlag = 0; // 假设这是一个合适的清零操作
OLED_Clear();
gpio_bit_set(GPIOB, GPIO_PIN_4);
// 根据currentLine的值显示相应的信息
if (currentLine < sizeof(chineseMessages) / sizeof(chineseMessages[0]))
{
OLED_ShowCHinese(chineseMessages[currentLine][0], 0, chineseMessages[currentLine][1]);
}
else if (currentLine >= sizeof(chineseMessages) / sizeof(chineseMessages[0]) &&
currentLine < sizeof(chineseMessages) / sizeof(chineseMessages[0]) + sizeof(englishMessages) / sizeof(englishMessages[0]))
{
if((currentLine - sizeof(chineseMessages) / sizeof(chineseMessages[0]))==0)
{
OLED_ShowCHinese(12,0,0);//显示中文(浙)
OLED_ShowCHinese(30,0,1);//显示中文(江)
OLED_ShowCHinese(48,0,2);//显示中文(力)
OLED_ShowCHinese(66,0,3);//显示中文(控)
OLED_ShowCHinese(84,0,4);//显示中文(科)
OLED_ShowCHinese(102,0,5);//显示中文(技)
OLED_ShowString(21, 3, englishMessages[currentLine - sizeof(chineseMessages) / sizeof(chineseMessages[0])]);
}
if((currentLine - sizeof(chineseMessages) / sizeof(chineseMessages[0]))==1)
{
OLED_ShowCHinese(12,0,0);//显示中文(浙)
OLED_ShowCHinese(30,0,1);//显示中文(江)
OLED_ShowCHinese(48,0,2);//显示中文(力)
OLED_ShowCHinese(66,0,3);//显示中文(控)
OLED_ShowCHinese(84,0,4);//显示中文(科)
OLED_ShowCHinese(102,0,5);//显示中文(技)
OLED_ShowString(21,3,(u8 *)"HUZHOU*ANJI");//显示字符 这里都是先列后行
OLED_ShowString(16, 6, englishMessages[currentLine - sizeof(chineseMessages) / sizeof(chineseMessages[0])]);
}
}
delay_1ms(100); // 假设这里延迟1秒,以便观察显示结果
gpio_bit_reset(GPIOB, GPIO_PIN_4);
// 更新currentLine以显示下一行(如果有的话)
currentLine = (currentLine + 1) % (sizeof(chineseMessages) / sizeof(chineseMessages[0]) + sizeof(englishMessages) / sizeof(englishMessages[0]));
}
}
}
请大佬解答~谢谢
#include "gd32f30x.h"
#include "systick.h"
#include <stdio.h>
#include "oled.h"
extern uint8_t IntFlag;//进入中断服务程序标志,在EXTI1_IRQHandler()函数置位
// 定义一个变量来跟踪当前显示的行
static uint8_t currentLine = 0;
// 定义一个数组来存储要显示的信息的索引或字符串指针
static const uint8_t chineseMessages[][2] = {
{12, 0}, //
{30, 1}, //
{48, 2}, //
{66, 3}, //
{84, 4}, //
{102, 5} //
};
static uint8_t *englishMessages[] = {
"HUZHOU",
"TEL:12345678"
};
uint16_t adc_value;
uint16_t Vol_Value;
float Vol_Value1;
void adc_config(void)
{
/* reset ADC */
adc_deinit(ADC1);
// /* ADC mode config */
adc_mode_config(ADC_MODE_FREE);
/* ADC continous function enable */
adc_special_function_config(ADC1, ADC_CONTINUOUS_MODE, ENABLE);
/* ADC data alignment config */
adc_data_alignment_config(ADC1, ADC_DATAALIGN_RIGHT);
/* ADC resolusion 6B */
// adc_resolution_config(ADC1, ADC_RESOLUTION_6B);
/* ADC channel length config */
adc_channel_length_config(ADC1, ADC_REGULAR_CHANNEL, 1);
/* ADC regular channel config */
adc_regular_channel_config(ADC1, 0, ADC_CHANNEL_4, ADC_SAMPLETIME_55POINT5);
/* ADC trigger config */
adc_external_trigger_source_config(ADC1, ADC_REGULAR_CHANNEL, ADC0_1_2_EXTTRIG_REGULAR_NONE);
adc_external_trigger_config(ADC1, ADC_REGULAR_CHANNEL, ENABLE);
/* enable ADC interface */
adc_enable(ADC1);
delay_1ms(1);
/* ADC calibration and reset calibration */
adc_calibration_enable(ADC1);
/* enable ADC interface */
// adc_software_trigger_enable(ADC0,ADC_REGULAR_CHANNEL);
}
int main(void)
{
rcu_ahb_clock_config(RCU_AHB_CKSYS_DIV1);//设置主频120M(#define __SYSTEM_CLOCK_120M_PLL_HXTAL (uint32_t)(120000000),8M外部晶振 (#define HXTAL_VALUE ((uint32_t)8000000))
systick_config();//1ms systick
rcu_periph_clock_enable(RCU_AF); //AF时钟使能
delay_1ms(1000);//等待1000ms
gpio_pin_remap_config(GPIO_SWJ_NONJTRST_REMAP, ENABLE);//下载口NJTRST引脚当做普通I/O口
rcu_periph_clock_enable(RCU_GPIOB);//PB时钟使能
gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_4);//PB4配置成输出
//配置PA4 ADC引脚
rcu_periph_clock_enable(RCU_GPIOA);
gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_4);
/* enable ADC1 clock */
rcu_periph_clock_enable(RCU_ADC1);
/* config ADC clock */
rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV8);
//配置ADC
adc_config();
adc_software_trigger_enable(ADC1, ADC_REGULAR_CHANNEL);
OLED_Init();//OLED初始化
OLED_Clear();//OLED清屏
gpio_init(GPIOA, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_1);//PA1配置成上拉输入
nvic_irq_enable(EXTI1_IRQn, 2U, 0U);//enable and set key EXTI interrupt to the lowest priority
gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOA, GPIO_PIN_SOURCE_1);//connect key EXTI line to key GPIO pin
exti_init(EXTI_1, EXTI_INTERRUPT, EXTI_TRIG_FALLING);//下降沿中断
exti_interrupt_flag_clear(EXTI_1);//清中断标志
//PA2给ADC
gpio_init(GPIOA, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_2);//PA2配置成上拉输入
nvic_irq_enable(EXTI2_IRQn, 2U, 0U);//enable and set key EXTI interrupt to the lowest priority
gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOA, GPIO_PIN_SOURCE_2);//connect key EXTI line to key GPIO pin
exti_init(EXTI_2, EXTI_INTERRUPT, EXTI_TRIG_FALLING);//下降沿中断
exti_interrupt_flag_clear(EXTI_2);//清中断标志
while (1)
{
if (gpio_input_bit_get(GPIOA, GPIO_PIN_2) == RESET && IntFlag == 0x55) //ADC显示电压 需要换个接口
{
adc_flag_clear(ADC1,ADC_FLAG_EOC);//清ADC结束标志
while(SET != adc_flag_get(ADC1,ADC_FLAG_EOC))
{//判断是否结束
adc_value = ADC_RDATA(ADC1);//读取ADC寄存器值
Vol_Value=adc_value*3300/4095/1000;//寄存器值转换成电压值
//Vol_Value1=330;
OLED_Clear();//OLED清屏
OLED_ShowNum(5,2,Vol_Value1,4,32);
delay_1ms(1000);
}//等待5秒
}
if (gpio_input_bit_get(GPIOA, GPIO_PIN_1) == RESET && IntFlag == 0x55) //信息显示
{
// 清除中断标志(重要,防止重复触发)
IntFlag = 0; // 假设这是一个合适的清零操作
OLED_Clear();
gpio_bit_set(GPIOB, GPIO_PIN_4);
// 根据currentLine的值显示相应的信息
if (currentLine < sizeof(chineseMessages) / sizeof(chineseMessages[0]))
{
OLED_ShowCHinese(chineseMessages[currentLine][0], 0, chineseMessages[currentLine][1]);
}
else if (currentLine >= sizeof(chineseMessages) / sizeof(chineseMessages[0]) &&
currentLine < sizeof(chineseMessages) / sizeof(chineseMessages[0]) + sizeof(englishMessages) / sizeof(englishMessages[0]))
{
if((currentLine - sizeof(chineseMessages) / sizeof(chineseMessages[0]))==0)
{
OLED_ShowCHinese(12,0,0);//显示中文(浙)
OLED_ShowCHinese(30,0,1);//显示中文(江)
OLED_ShowCHinese(48,0,2);//显示中文(力)
OLED_ShowCHinese(66,0,3);//显示中文(控)
OLED_ShowCHinese(84,0,4);//显示中文(科)
OLED_ShowCHinese(102,0,5);//显示中文(技)
OLED_ShowString(21, 3, englishMessages[currentLine - sizeof(chineseMessages) / sizeof(chineseMessages[0])]);
}
if((currentLine - sizeof(chineseMessages) / sizeof(chineseMessages[0]))==1)
{
OLED_ShowCHinese(12,0,0);//显示中文(浙)
OLED_ShowCHinese(30,0,1);//显示中文(江)
OLED_ShowCHinese(48,0,2);//显示中文(力)
OLED_ShowCHinese(66,0,3);//显示中文(控)
OLED_ShowCHinese(84,0,4);//显示中文(科)
OLED_ShowCHinese(102,0,5);//显示中文(技)
OLED_ShowString(21,3,(u8 *)"HUZHOU*ANJI");//显示字符 这里都是先列后行
OLED_ShowString(16, 6, englishMessages[currentLine - sizeof(chineseMessages) / sizeof(chineseMessages[0])]);
}
}
delay_1ms(100); // 假设这里延迟1秒,以便观察显示结果
gpio_bit_reset(GPIOB, GPIO_PIN_4);
// 更新currentLine以显示下一行(如果有的话)
currentLine = (currentLine + 1) % (sizeof(chineseMessages) / sizeof(chineseMessages[0]) + sizeof(englishMessages) / sizeof(englishMessages[0]));
}
}
}
赞0
评论
2024-08-31
赞0
评论
2024-08-31
赞0
评论
2024-08-31
赞0
评论
2024-08-31
赞0
评论
2024-08-31
赞0
评论
2024-08-31
赞0
评论
2024-08-31
赞0
评论
2024-08-31
赞0
评论
2024-08-31
您需要登录后才可以回复 登录 | 注册