Adc polling

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Adc polling

812 Views
duong
Contributor II

I read adc value to control a led, but it doesm't work. this is my code.
in file drive.c

typedef struct
{
    uint8_t clockDivide;
    uint8_t sampleTime;
    uint8_t resolution;
    uint8_t inputClock;
    uint8_t triggerType;
    uint8_t voltageRef;
    bool    dmaEnable;
} adc_config_t;
 
typedef struct
{
    uint8_t controlChannel;
    uint8_t inputChannel;
    bool    enableInterrupt;
} adc_channel_t;
void ADC_strriger(){}
uint8_t ADC_Init(adc_config_t * adcConfig)
{
    if (adcConfig == NULL)
    {
        return 1;
    }
 
    ADC0->CFG1 &= ~ADC_CFG1_ADIV_MASK;
    ADC0->CFG1 |= ADC_CFG1_ADIV(adcConfig->clockDivide);
 
    ADC0->CFG1 &= ~ADC_CFG1_MODE_MASK;
    ADC0->CFG1 |= ADC_CFG1_MODE(adcConfig->resolution);
 
    ADC0->CFG1 &= ~ADC_CFG1_ADICLK_MASK;
    ADC0->CFG1 |= ADC_CFG1_ADICLK(adcConfig->inputClock);
 
 
    ADC0->CFG2 &= ~ADC_CFG2_SMPLTS_MASK;
    ADC0->CFG2 |= ADC_CFG2_SMPLTS(adcConfig->sampleTime);
ADC0->SC2 &= ~ADC_SC2_ADTRG_MASK;
    ADC0->SC2 |= ADC_SC2_ADTRG(adcConfig->triggerType);
 
ADC0->SC2 &= ~ADC_SC2_REFSEL_MASK;
    ADC0->SC2 |= ADC_SC2_REFSEL(adcConfig->voltageRef);
 
ADC0->SC2 &= ~ADC_SC2_DMAEN_MASK;
if (adcConfig->dmaEnable == true)
{
ADC0->SC2 |= ADC_SC2_DMAEN(1);
}
else ADC0->SC2 |= ADC_SC2_DMAEN(0);
    return 0xFFu;
}
uint8_t ADC_ConfigChannel(adc_channel_t * adcChannel)
{
    uint32_t tempVal;
 
    if(adcChannel==NULL) return 1;
    tempVal  = ADC0->SC1[adcChannel->controlChannel];
    tempVal &= ~ADC_SC1_ADCH_MASK;
    tempVal |= ADC_SC1_ADCH(adcChannel->inputChannel);
    tempVal &= ~ADC_SC1_AIEN_MASK;
    if(adcChannel->enableInterrupt==true)
    tempVal |= ADC_SC1_AIEN(1);
    else tempVal |= ADC_SC1_AIEN(0);
    ADC0->SC1[adcChannel->controlChannel] = tempVal;
 
    return 0xFFu; // success
}
 
void ADC_WaitConversionComplete(void)
{
    while(((ADC0->SC1[0] & ADC_SC1_COCO_MASK)>>ADC_SC1_COCO_SHIFT) == 0);
 
}
 uint8_t Clear_GPIO(uint8_t pinNumber)
{
    
    PTD->PCOR = (uint32_t)(1 << pinNumber);
 
    return 0;
}
void ADC_StartConversion(uint8_t inputChannel)
{
 
    adc_config_t adcConfig =
    {
        1,255,1,0,0,0,false
    };
 
 
 
 
    (void)ADC_Init(&adcConfig);
 
 
    adc_channel_t adcChannel = {
        0,inputChannel,0
    };
    (void)ADC_ConfigChannel(&adcChannel);
 
    ADC_WaitConversionComplete();
}
 
uint8_t Set_GPIO(uint8_t pinNumber)
{
 
    PTD->PSOR = (uint32_t)(1 << pinNumber);
 
    return 0;
}
uint32_t ADC_GetConversionResult(uint8_t controlChannel)
{
    return (uint32_t)(ADC0->R[controlChannel] & ADC_R_D_MASK);
}
void Init_GPIO(uint8_t pinNumber)
{
 
 
    PCC-> PCCn[PCC_PORTD_INDEX] =  PCC_PCCn_CGC_MASK;
 
 
    PORTD->PCR[pinNumber] = PORT_PCR_MUX(1);
 
    PTD->PDDR = (uint32_t)(1 << pinNumber);
    PORTC->PCR[14]=PORT_PCR_MUX(0);
}
void ADC_ControlLight(uint8_t inputChannel)
{
 
   Init_GPIO(15);
    ADC_StartConversion(inputChannel);
    uint32_t a=ADC_GetConversionResult(12);
 
    if(a>=1024)Set_GPIO(15);
    else Clear_GPIO(15);
 
}

/**********************************/
in file main.c

int appMain(void)
{
CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
                 g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
    CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);
    PCC->PCCn[PCC_ADC0_INDEX] &=~ PCC_PCCn_CGC_MASK; /* Disable clock to change PCS */
    PCC->PCCn[PCC_ADC0_INDEX] |= PCC_PCCn_PCS(1); /* PCS=1: Select SOSCDIV2 */
    PCC->PCCn[PCC_ADC0_INDEX] |= PCC_PCCn_CGC_MASK; /* Enable bus clock in ADC */
     
Init_GPIO();
 
 
    while(1) {
    ADC_ControlLight();
 
    }
 
}
Tags (1)
0 Kudos
Reply
3 Replies

738 Views
duong
Contributor II

how can I find other demo code in s32

0 Kudos
Reply

719 Views
Senlent
NXP TechSupport
NXP TechSupport

Hi@duong

bare metal demo code .

Senlent_0-1694741431391.png

 

0 Kudos
Reply

770 Views
Senlent
NXP TechSupport
NXP TechSupport

Hi@duong

If you have S32 Design for Arm V2.2 installed, you can find a similar demo.

For your convenience, I have upload the demo in the attachment for your reference.

 

0 Kudos
Reply