可以提供一個CH32V307RCT6 的多個觸摸按鍵的ADC 和 別的 ADC采樣一起使用采集的例程嗎?我這里不知道為啥總會有點問題,
不用這個
//? ? while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC))
//? ? ;
用和這個的話??
?if(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == 0)
? ? {
? ? }
? ? else
? ? {
? ? ? ? TK2_Data = (uint16_t)TKey1->RDATAR;
? ? }
總有個ADC的值是錯的
/*
?* Touch_SW.c
?*
?*? Created on: Nov 24, 2023
?*? ? ? Author: Administrator
?*/
#include? "Touch_SW.h"
#include "UI_Display.h"
#include "OLED_Drv.h"
extern uint8_t Touch_ADC_Time_Flag;
uint8_t Touch_SW_Flag[2];
/*********************************************************************
?* @fn? ? ? Touch_Key_Init
?*
?* @brief? ?Initializes Touch Key collection.
?*
?* @return? none
?*/
void Touch_Key_Init(void)
{
? ? GPIO_InitTypeDef GPIO_InitStructure = {0};
? ? ADC_InitTypeDef? ADC_InitStructure = {0};
? ? RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
? ? RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
? ? RCC_ADCCLKConfig(RCC_PCLK2_Div8);
? ? GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;//通道2
? ? GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
? ? GPIO_Init(GPIOA, &GPIO_InitStructure);
? ? GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;//通道1
? ? GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
? ? GPIO_Init(GPIOA, &GPIO_InitStructure);
? ? ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
? ? ADC_InitStructure.ADC_ScanConvMode = DISABLE;
? ? ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
? ? ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
? ? ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
? ? ADC_InitStructure.ADC_NbrOfChannel = 1;
? ? ADC_Init(ADC1, &ADC_InitStructure);
? ? ADC_Cmd(ADC1, ENABLE);
? ? TKey1->CTLR1 |= (1 << 26) | (1 << 24); // Enable TouchKey and Buffer\
? ? TKey1->CTLR1 |= 1 << 25;//電流減半
}
/*********************************************************************
?* @fn? ? ? Touch_Key_Adc
?*
?* @brief? ?Returns ADCx conversion result data.
?*
?* @param? ?ch - ADC channel.
?*? ? ? ? ? ? ADC_Channel_0 - ADC Channel0 selected.
?*? ? ? ? ? ? ADC_Channel_1 - ADC Channel1 selected.
?*? ? ? ? ? ? ADC_Channel_2 - ADC Channel2 selected.
?*? ? ? ? ? ? ADC_Channel_3 - ADC Channel3 selected.
?*? ? ? ? ? ? ADC_Channel_4 - ADC Channel4 selected.
?*? ? ? ? ? ? ADC_Channel_5 - ADC Channel5 selected.
?*? ? ? ? ? ? ADC_Channel_6 - ADC Channel6 selected.
?*? ? ? ? ? ? ADC_Channel_7 - ADC Channel7 selected.
?*? ? ? ? ? ? ADC_Channel_8 - ADC Channel8 selected.
?*? ? ? ? ? ? ADC_Channel_9 - ADC Channel9 selected.
?*? ? ? ? ? ? ADC_Channel_10 - ADC Channel10 selected.
?*? ? ? ? ? ? ADC_Channel_11 - ADC Channel11 selected.
?*? ? ? ? ? ? ADC_Channel_12 - ADC Channel12 selected.
?*? ? ? ? ? ? ADC_Channel_13 - ADC Channel13 selected.
?*? ? ? ? ? ? ADC_Channel_14 - ADC Channel14 selected.
?*? ? ? ? ? ? ADC_Channel_15 - ADC Channel15 selected.
?*? ? ? ? ? ? ADC_Channel_16 - ADC Channel16 selected.
?*? ? ? ? ? ? ADC_Channel_17 - ADC Channel17 selected.
?*
?* @return? val - The Data conversion value.
?*/
u16 Touch_Key1_Adc(u8 KEY1ch)
{
? ? static? ?uint16_t TK_Data = 0;
? ? ADC_RegularChannelConfig(ADC1, KEY1ch, 1, ADC_SampleTime_7Cycles5);
? ? TKey1->IDATAR1 = 0x08; //Charging Time
? ? TKey1->RDATAR = 0x8;? ?//Discharging Time
//? ? while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC))
//? ? ;
? ? if(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == 0)
? ?{
? ?}
? ?else
? ?{
? ? ? ?TK_Data =? (uint16_t)TKey1->RDATAR;
? ?}
? ? return TK_Data;
}
u16 Touch_Key2_Adc(u8 KEY2ch)
{
? ? static? ? uint16_t TK2_Data = 0;
? ? ADC_RegularChannelConfig(ADC1, KEY2ch, 1, ADC_SampleTime_7Cycles5);
? ? TKey1->IDATAR1 = 0x08; //Charging Time
? ? TKey1->RDATAR = 0x8;? ?//Discharging Time
//? ? while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC))
//? ? ;
? ? if(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == 0)
? ? {
? ? }
? ? else
? ? {
? ? ? ? TK2_Data = (uint16_t)TKey1->RDATAR;
? ? }
? ? return TK2_Data;
}
//void Touch_Key_dataProcess(void)
//{
//? ? static uint16_t ADCData[2] = {0};
//
//
//? ? ADCData[0] = Touch_Key1_Adc(ADC_Channel_1);
//? ? ADCData[1] = Touch_Key2_Adc(ADC_Channel_2);
//
//
//
//}
void Touch_Key_Task(void)
{
? ? static uint16_t ADCData[2] = {0};
//? ? static uint16_t Last_Sta[2] = {0};
? ? ?ADCData[0] = Touch_Key1_Adc(ADC_Channel_1);
? ? ?ADCData[1] = Touch_Key2_Adc(ADC_Channel_2);
//? ? ?ADCData[1]=10000;
? ? if(Touch_ADC_Time_Flag != 0)
? ? {
? ? ? ? Touch_ADC_Time_Flag = 0;
? ? ? ? TFTLCD_Show5x7num(5,22,ADCData[0],GREED_COLOR,BACK_MAIN_COLOR);
? ? ? ? TFTLCD_Show5x7num(40,80,ADCData[1],GREED_COLOR,BACK_MAIN_COLOR);
? ? }
}
別的地方還用著一個通道10在采集著別的東西