Author Topic: ch32v003 adc PA1 PA2 not working  (Read 482 times)

0 Members and 1 Guest are viewing this topic.

Offline nt_ptrTopic starter

  • Newbie
  • Posts: 3
  • Country: th
ch32v003 adc PA1 PA2 not working
« on: February 29, 2024, 02:04:14 am »
I read ADC1 6 channels A0 A1 A2 A3 A4 A7. I noticed that channels ADC1 ADC2 didn't work. But that's more confusing. The knob that doesn't work is the slot connected to pins PA1 and PA2, which should be A0 A1. What do I need to do to fix this?

Code: [Select]
ADC_: 1  520   507  1  1   1
Code: [Select]
   ADC_B[0] = Get_ADC_Val(ADC_Channel_0);           Delay_Ms(1);
        ADC_B[1] = Get_ADC_Val(ADC_Channel_1);           Delay_Ms(1);
        ADC_B[2] = Get_ADC_Val(ADC_Channel_2);           Delay_Ms(1);
        ADC_B[3] = Get_ADC_Val(ADC_Channel_3);           Delay_Ms(1);
        ADC_B[4] = Get_ADC_Val(ADC_Channel_4);           Delay_Ms(1);
        ADC_B[5] = Get_ADC_Val(ADC_Channel_7);           Delay_Ms(1);

        printf("ADC_: %d  %d   %d  %d  %d   %d\r\n",ADC_B[0],ADC_B[1],ADC_B[2],ADC_B[3],ADC_B[4],ADC_B[5]);

 
Code: [Select]

/********************************** (C) COPYRIGHT *******************************
 * File Name          : main.c
 * Author             : WCH
 * Version            : V1.0.0
 * Date               : 2023/12/25
 * Description        : Main program body.
 *********************************************************************************
 * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
 * Attention: This software (modified or not) and binary are used for
 * microcontroller manufactured by Nanjing Qinheng Microelectronics.
 *******************************************************************************/

/*
 *@Note
 *Timer synchronization mode:
 *TIM1_CH1(PD2)
 *This example demonstrates 4 timer synchronization modes.
 *
 */

#include "debug.h"

#define buffWAV 2


/* CH1CVR register Definition */
#define TIM1_CH1CVR_ADDRESS    0x40012C34

/* Private variables */
u16 pbuf[buffWAV];

int ADC_B[6]={};

void MAIN_GPIO_Init( void );
u16 Get_ADC_Val( u8 ch ) ;


void TIM_TimSynchroMode1_Init( u16 arr, u16 psc , u16 sync,u16 ccp);
void TIM1_DMA_Init(DMA_Channel_TypeDef *DMA_CHx, u32 ppadr, u32 memadr, u16 bufsize);

void ADC_Channel0_Init( void );
u16 Get_ADC_Val( u8 ch );


void USARTx_CFG(void);
/*********************************************************************
 * @fn      main
 *
 * @brief   Main program.
 *
 * @return  none
 */
int main(void)
{
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
    SystemCoreClockUpdate();
    Delay_Init();
    USARTx_CFG();
    USART_Printf_Init(115200);

    MAIN_GPIO_Init();
    ADC_Channel0_Init();



    printf("SystemClk:%d\r\n", SystemCoreClock);
    printf( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );


    /* Timer synchronization Mode Selection */



    TIM_TimSynchroMode1_Init(250,6-1,buffWAV-1, pbuf[0]);
    TIM1_DMA_Init(DMA1_Channel5, (u32)TIM1_CH1CVR_ADDRESS, (u32)pbuf, buffWAV);

    TIM_DMACmd(TIM1, TIM_DMA_Update, ENABLE);
    TIM_Cmd(TIM1, ENABLE);
    TIM_CtrlPWMOutputs(TIM1, ENABLE);


    while(1)
    {



        ADC_B[0] = Get_ADC_Val(ADC_Channel_0);           Delay_Ms(1);
        ADC_B[1] = Get_ADC_Val(ADC_Channel_1);           Delay_Ms(1);
        ADC_B[2] = Get_ADC_Val(ADC_Channel_2);           Delay_Ms(1);
        ADC_B[3] = Get_ADC_Val(ADC_Channel_3);           Delay_Ms(1);
        ADC_B[4] = Get_ADC_Val(ADC_Channel_4);           Delay_Ms(1);
        ADC_B[5] = Get_ADC_Val(ADC_Channel_7);           Delay_Ms(1);

        printf("ADC_: %d  %d   %d  %d  %d   %d\r\n",ADC_B[0],ADC_B[1],ADC_B[2],ADC_B[3],ADC_B[4],ADC_B[5]);


    }
}



void ADC_Channel0_Init( void ) // initializes analog-to-digital converter (ADC) channel 7 that is connected to the OPA output
{
    GPIO_InitTypeDef GPIO_InitStructure = {0};
    GPIO_InitTypeDef GPIO_InitStructureA = {0};
    GPIO_InitTypeDef GPIO_InitStructureC = {0};
    ADC_InitTypeDef ADC_InitStructure = {0};

    RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC1, ENABLE );
    RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE );
    RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE );
    RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOD, ENABLE );
    RCC_ADCCLKConfig( RCC_PCLK2_Div8 );



    GPIO_InitStructureA.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2;
    GPIO_InitStructureA.GPIO_Mode = GPIO_Mode_AIN;
    GPIO_Init( GPIOA, &GPIO_InitStructureA );


    GPIO_InitStructureC.GPIO_Pin = GPIO_Pin_4;
    GPIO_InitStructureC.GPIO_Mode = GPIO_Mode_AIN;
    GPIO_Init( GPIOC, &GPIO_InitStructureC );


    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
    GPIO_Init( GPIOD, &GPIO_InitStructure );


    ADC_DeInit(ADC1);
    ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
    ADC_InitStructure.ADC_ScanConvMode = DISABLE;
    ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
    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_Calibration_Vol(ADC1, ADC_CALVOL_50PERCENT);
    ADC_Cmd( ADC1, ENABLE );

    ADC_ResetCalibration(ADC1);
    while(ADC_GetResetCalibrationStatus(ADC1));
    ADC_StartCalibration(ADC1);
    while(ADC_GetCalibrationStatus(ADC1));
}

u16 Get_ADC_Val( u8 ch ) // gets the value of the ADC channel
{
    u16 val;
    ADC_RegularChannelConfig( ADC1, ch, 1, ADC_SampleTime_241Cycles );
    ADC_SoftwareStartConvCmd( ADC1, ENABLE );
    while( !ADC_GetFlagStatus( ADC1, ADC_FLAG_EOC ) );
    val = ADC_GetConversionValue( ADC1 );
    return val;
}
void TIM2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void TIM2_IRQHandler(void)
{
        if( TIM_GetITStatus( TIM2, TIM_IT_Update ) != RESET )
        {

        }
        TIM_ClearITPendingBit( TIM2, TIM_IT_Update );
}

void USARTx_CFG(void)
{
    GPIO_InitTypeDef  GPIO_InitStructure = {0};
    USART_InitTypeDef USART_InitStructure = {0};

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_USART1, ENABLE);

    /* USART1 TX-->D.5   RX-->D.6 */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOD, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOD, &GPIO_InitStructure);

    USART_InitStructure.USART_BaudRate = 115200;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;

    USART_Init(USART1, &USART_InitStructure);
    USART_Cmd(USART1, ENABLE);
}

/*********************************************************************
 * @fn      TIM_TimSynchroMode1_Init
 *
 * @brief   Using TIM2 as prescaler for TIM1.
 *
 * @return  none
 */
void TIM_TimSynchroMode1_Init( u16 arr, u16 psc , u16 sync,u16 ccp)
{
    GPIO_InitTypeDef        GPIO_InitStructure = {0};
    NVIC_InitTypeDef NVIC_InitStructure={0};
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure = {0};
    TIM_OCInitTypeDef       TIM_OCInitStructure = {0};


    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_TIM1, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      GPIO_Init(GPIOD, &GPIO_InitStructure);


        TIM_TimeBaseInitStructure.TIM_Period = arr;
        TIM_TimeBaseInitStructure.TIM_Prescaler = psc;
        TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
        TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
        TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
        TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStructure);

        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
        TIM_OCInitStructure.TIM_Pulse = ccp;

        TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
        TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
        TIM_OC1Init(TIM1, &TIM_OCInitStructure);

        TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Disable);
        TIM_ARRPreloadConfig(TIM1, ENABLE);




    TIM_CounterModeConfig(TIM2, TIM_CounterMode_Up);
    TIM_SetAutoreload(TIM2, sync); //


    TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Update);
    TIM_ITRxExternalClockConfig(TIM2, TIM_TS_ITR0);
    TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_External1);

    TIM_ClearITPendingBit( TIM1, TIM_IT_Update );
    TIM_ClearITPendingBit( TIM2, TIM_IT_Update);



    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

     TIM_ITConfig( TIM1, TIM_IT_Update , ENABLE );
     TIM_ITConfig( TIM2, TIM_IT_Update , ENABLE );


     TIM_Cmd(TIM2, ENABLE);

}

/*********************************************************************
 * @fn      TIM1_DMA_Init
 *
 * @brief   Initializes the TIM DMAy Channelx configuration.
 *
 * @param   DMA_CHx -
 *            x can be 1 to 7.
 *          ppadr - Peripheral base address.
 *          memadr - Memory base address.
 *          bufsize - DMA channel buffer size.
 *
 * @return  none
 */
void TIM1_DMA_Init(DMA_Channel_TypeDef *DMA_CHx, u32 ppadr, u32 memadr, u16 bufsize)
{
    DMA_InitTypeDef DMA_InitStructure = {0};

        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

        DMA_DeInit(DMA_CHx);
        DMA_InitStructure.DMA_PeripheralBaseAddr = ppadr;
        DMA_InitStructure.DMA_MemoryBaseAddr = memadr;
        DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
        DMA_InitStructure.DMA_BufferSize = bufsize;
        DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
        DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
        DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
        DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
        DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
        DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
        DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
        DMA_Init(DMA_CHx, &DMA_InitStructure);

        DMA_Cmd(DMA_CHx, ENABLE);
}
void MAIN_GPIO_Init( void ) // initializes analog-to-digital converter (ADC) channel 7 that is connected to the OPA output
{
    GPIO_InitTypeDef GPIO_InitStructure = {0};
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE );
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; // Defines which Pin to configure
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // Defines Output Type
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // Defines speed
    GPIO_Init(GPIOC, &GPIO_InitStructure);



}


 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf