How to change the Samples Averaged of the SAR Seq by code ?

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
MaPe_1936286
Level 3
Level 3
First like received First like given Welcome!

Hi,

I need to change runtime the number of the Samples Averaged of the Sequencing SAR ADC.

Is it possible to do this ?

Thank you

Maurizio Pesce

0 Likes
1 Solution
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

I studied some more since then.

CYREG_PERI_DIV_16_CTLx register seems to control the frequency of peripherals.

I saw ADC conideration(s) in the Ch 10 of AN88619.

https://www.cypress.com/file/141176/download

It says the acquisition time must be longer than 9 * (Rsrc + Rsw) * Chold

but I don't think the number of times changes each acquisition time.

And when we change AVG_CNT 0 ~ 7 the number of average change 2 ~ 256

I made following function and a sample project for CY8CKIT-044

=======================

void set_avg_cnt(uint16_t avg_cnt)

{

    uint32_t ctrl_reg ;

    if ((0 <= avg_cnt) && (avg_cnt < 8)) { /* it must be 3bits */

        ADC_Stop() ;

        ctrl_reg = ADC_SAR_SAMPLE_CTRL_REG ;

        ctrl_reg &= ~(0x70) ;

        ctrl_reg |= (avg_cnt << 4) ;

        ADC_SAR_SAMPLE_CTRL_REG = ctrl_reg ;

        ADC_Start() ;

     

        snprintf(str, STR_BUF_LEN, "AVG_CNT is set to %d : ", avg_cnt) ;

        print(str) ;

        snprintf(str, STR_BUF_LEN, "Avg number is now %d\n", 1 << (avg_cnt + 1)) ;

        print(str) ;

    } else {

        print("Avg_cnt must be 0 ~ 7\n") ;

    }

}

=======================

The Tera Term log looked like

002-teraterm-log.JPG

moto

P.S. Last but not least, as usual I may be wrong...

View solution in original post

0 Likes
8 Replies
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Dear Maurizio-san,

I think that you can change it with the configuration dialog which will be shown when you double-click the component in the schematic.

000-ADC_Configuration.JPG

Best Regards,

6-Dec-2019

Motoo Tanaka

0 Likes

Dear Motoo,

I know that I can change the Samples averaged with the configuration dialog, but I need to change it runtime from my application (by code), but there is no API function to change it.

Best Regards

Maurizio Pesce

0 Likes
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Dear Maurizio-san,

> I know that I can change the Samples averaged with the configuration dialog,

> but I need to change it runtime from my application (by code), but there is no API function to change it.

Oh, I'm sorry for miss-reading your question.

Although I tried to find an API to do that I could not find it.

So I read ADC.h and ADC.c

In ADC_Init(void) in ADC.c there are lines like

==========

    ADC_SAR_SAMPLE_CTRL_REG = ADC_DEFAULT_SAMPLE_CTRL_REG_CFG;

    ADC_SAR_RANGE_THRES_REG = ADC_DEFAULT_RANGE_THRES_REG_CFG;

    ADC_SAR_RANGE_COND_REG  = ADC_COMPARE_MODE;

    ADC_SAR_SAMPLE_TIME01_REG = ADC_DEFAULT_SAMPLE_TIME01_REG_CFG;

    ADC_SAR_SAMPLE_TIME23_REG = ADC_DEFAULT_SAMPLE_TIME23_REG_CFG;

==========

and ADC_DEFAULT_SAMPLE_CTRL_REG_CFG is defined in ADC.h as

==========

#define ADC_DEFAULT_SAMPLE_CTRL_REG_CFG (ADC_DEFAULT_DIFF_RESULT_FORMAT \

                                                    | ADC_DEFAULT_SE_RESULT_FORMAT \

                                                    | ADC_DEFAULT_JUSTIFICATION \

                                                    | ADC_DEFAULT_ALT_RESOLUTION \

                                           | (uint8)(ADC_DEFAULT_AVG_SAMPLES_NUM \

                                                   << ADC_AVG_CNT_OFFSET) \

                                                    | ADC_AVG_SHIFT_MODE \

                                                    | ADC_DSI_TRIGGER \

                                                    | ADC_EOS_DSI_OUT_EN)

==========

So if you change "ADC_DEFAULT_AVG_SAMPLES_NUM" here you may be able to change it.

Although I have not tested it, I hope that doing follow may do what you want.

Note: Please test it before using it...

assign required average number to my_new_avg_number below

==========

ADC_Stop() ;

ADC_SAR_SAMPLE_CTRL_REG = ADC_DEFAULT_DIFF_RESULT_FORMAT \

                                                    | ADC_DEFAULT_SE_RESULT_FORMAT \

                                                    | ADC_DEFAULT_JUSTIFICATION \

                                                    | ADC_DEFAULT_ALT_RESOLUTION \

                                           | (uint8)(my_new_avg_number \

                                                   << ADC_AVG_CNT_OFFSET) \

                                                    | ADC_AVG_SHIFT_MODE \

                                                    | ADC_DSI_TRIGGER \

                                                    | ADC_EOS_DSI_OUT_EN) ;

ADC_Init() ;

ADC_Start() ;

==========

Best Regards,

6-Dec-2019

Motoo Tanaka

0 Likes

Dear Motoo,

Thank you for your support.

I also think the only way is to change the control registers.

But I have two considerations:

1) About your suggested code, If I call ADC_Init() after changing ADC_SAR_SAMPLE_CTRL_REG it will restore the ADC_DEFAULT_AVG_SAMPLES_NUM.

2) If I compare the Generated Code of two projects with different Samples Averaged settings, I see there are changes also in CYREG_PERI_DIV_16_CTLx register, so I think there is a cross impact with the ADC clock settings.

As you write, I could do some tests with the registers, but without specific indications I am afraid that there may be unpredictable side effects.

Best regards

Maurizio Pesce

0 Likes
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Dear Maurizio-san,

(1) You are right!

Probably we should not call ADC_Init() after changing ADC_SAR_SAMPLE_CTRL_REG

(2) I was not thinking about it, but I think you are right about this point, too.

Unless Cypress provides API for changing average number, the safest approach must be

as you did, configure each set in separate projects and  copy the result register sets

to assign for each mode switching.

(If you are familiar with "diff" command or something similar,

I hope that you can detect all differences,

not quite elegant though.)

Best Regards,

6-Dec-2019

Motoo Tanaka

0 Likes

I hope someone can give me an answer.

0 Likes
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

I studied some more since then.

CYREG_PERI_DIV_16_CTLx register seems to control the frequency of peripherals.

I saw ADC conideration(s) in the Ch 10 of AN88619.

https://www.cypress.com/file/141176/download

It says the acquisition time must be longer than 9 * (Rsrc + Rsw) * Chold

but I don't think the number of times changes each acquisition time.

And when we change AVG_CNT 0 ~ 7 the number of average change 2 ~ 256

I made following function and a sample project for CY8CKIT-044

=======================

void set_avg_cnt(uint16_t avg_cnt)

{

    uint32_t ctrl_reg ;

    if ((0 <= avg_cnt) && (avg_cnt < 8)) { /* it must be 3bits */

        ADC_Stop() ;

        ctrl_reg = ADC_SAR_SAMPLE_CTRL_REG ;

        ctrl_reg &= ~(0x70) ;

        ctrl_reg |= (avg_cnt << 4) ;

        ADC_SAR_SAMPLE_CTRL_REG = ctrl_reg ;

        ADC_Start() ;

     

        snprintf(str, STR_BUF_LEN, "AVG_CNT is set to %d : ", avg_cnt) ;

        print(str) ;

        snprintf(str, STR_BUF_LEN, "Avg number is now %d\n", 1 << (avg_cnt + 1)) ;

        print(str) ;

    } else {

        print("Avg_cnt must be 0 ~ 7\n") ;

    }

}

=======================

The Tera Term log looked like

002-teraterm-log.JPG

moto

P.S. Last but not least, as usual I may be wrong...

0 Likes

Thank you Moto.

I hope to have no unpredictable side effects