ADC setup and use problem on K20 twr board

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

ADC setup and use problem on K20 twr board

ソリューションへジャンプ
1,691件の閲覧回数
yvesb
Contributor II

Hi all,

I've been writing a little code to try the ADC with the K20 tower dev board.

it is using ADC0 with pins ADC0_SE6b(PTD5 / 98) & ADC0_SE7b(PTD6 / 99).

I need to read the voltage on each pin as a single ADC input.

here is my init code:

void adc0_init(void)

{

    /* enable ADC0 clock */

    SIM_SCGC6 |= SIM_SCGC6_ADC0_MASK;

    /* set pin as adc input */

    PORTD_PCR5 = PORT_PCR_MUX(0); /* ADC0_SE6b */

    PORTD_PCR6 = PORT_PCR_MUX(0); /* ADC0_SE7b */

  

    /* init registers */

    ADC0_CFG1 = ADLPC_NORMAL

            | ADC_CFG1_ADIV(ADIV_1)

            | ADLSMP_LONG

            | ADC_CFG1_MODE(MODE_8)

            | ADC_CFG1_ADICLK(ADICLK_BUS);

  

    ADC0_CFG2 = MUXSEL_ADCB

            | ADACKEN_DISABLED

            | ADHSC_NORMAL

            | ADC_CFG2_ADLSTS(ADLSTS_20);

  

    ADC0_SC2 = ADTRG_SW

            | ACFE_DISABLED

            | ACFGT_LESS

            | ACREN_DISABLED

            | DMAEN_DISABLED

            | ADC_SC2_REFSEL(REFSEL_EXT);

  

    ADC0_SC3 = CAL_OFF

            | ADCO_SINGLE

            | AVGE_ENABLED

            | ADC_SC3_AVGS(AVGS_32);

  

    ADC0_SC1A = AIEN_OFF

            | DIFF_SINGLE

            | ADC_SC1_ADCH(31);

  

    ADC0_SC1B = AIEN_OFF

            | DIFF_SINGLE

            | ADC_SC1_ADCH(31);

}

then my adc read function:

unsigned int adc0_convert(char channel_number)

{

   ADC0_SC1A = AIEN_OFF

            | DIFF_SINGLE

            | ADC_SC1_ADCH(channel_number);

  

    while (!(ADC0_SC1A && ADC_SC1_COCO_MASK));

  

    return ADC0_RA;

}


now I call it from the main:

unsigned int val;

  

PORTC_ISFR=0xFFFFFFFF;  //Clear ISR flags
printf("AD conversion start\n");
val = adc0_convert(6);
printf("ADC0_SE6b=%6d\n",val);

  

val = adc0_convert(7);
printf("ADC0_SE7b=%6d\n",val);

In order to test it, I set a voltage on pins 98 & 99 of the chip, but I have a problem: voltage on PTD5 seems to be detected in channel AD7b instead of channel AD6b and voltage on PTD6 not really well detected on channel AD6b onstead of channel AD7b.

Have I misunderstood the manual? Am I missing something?

thanks for your help :smileyhappy:

Yves

0 件の賞賛
返信
1 解決策
1,048件の閲覧回数
yvesb
Contributor II

oops...

you might have noticed the mistake was here:

while (!(ADC0_SC1A && ADC_SC1_COCO_MASK));


there should be only one &:

while (!(ADC0_SC1A & ADC_SC1_COCO_MASK));


regards :smileyhappy:

元の投稿で解決策を見る

0 件の賞賛
返信
1 返信
1,049件の閲覧回数
yvesb
Contributor II

oops...

you might have noticed the mistake was here:

while (!(ADC0_SC1A && ADC_SC1_COCO_MASK));


there should be only one &:

while (!(ADC0_SC1A & ADC_SC1_COCO_MASK));


regards :smileyhappy:

0 件の賞賛
返信