HCS08 ADC (Analog to digital)

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

HCS08 ADC (Analog to digital)

2,764 Views
admin
Specialist II

Hello Everyone!

 

I have three buttons, each on different voltage dividers. They are connected to an ADC pin on my microcontroller. I am having trouble getting them to respond though.

 

I setup ADC1SC2 for software triggering, and set ADC1SC1_ADCH to the correct input channel. The problem is that it doesn't seem they are initiating a conversion. I send data to ADC1SC1 to trigger the conversion, but nothing gets written to ADC1RH and ADC1RL.

 

Below is a code snippet I have been working off of.

Thanks in advance for any help!

 

 

int result_H2bit;
int result_L8bit;
ADC1SC1 = 0x01;//input channel 0001 = PTB1/AD1P1 - Pin Control ADPC1
ADC1SC2 = 0x00;//no conv. in progress, Software trigger, compare function disabled
PTED &= ~0x01; //LED indicator on
while ((ADC1SC1 & 0x80) == 0x01)  //While 7th bit is set, won't proceed
while ((ADC1SC2 & 0x80) == 0x01)  //while 7th bit is set, won't proceed
result_H2bit = ADC1RH; //the 2 high bits of the 10-bit conversion
result_L8bit = ADC1RL; //lower 8 bits of the 10-bit conversion
Labels (1)
Tags (3)
3 Replies

1,457 Views
bigmac
Specialist III

Hello,

In addition to Mike's comments about your wait-for-conversion loop, there is another problem.

  ADC1SC1 = 0x01; // Start single conversion for channel 1, no interrupt

  ADC1SC2 = 0x00; // Aborts conversion just started

The register ADC1SC2 should be initialised at the same time as the other ADC registers, prior to the start of any conversion.

Additionally, I believe the following simplification will work correctly -

unsigned int ADCresult;

...

ADCresult = ADC1R;  // 16 bits simultaneously read

Regards,

Mac

0 Kudos
Reply

1,457 Views
admin
Specialist II

Hi

I think there is something wrong in:

while ((ADC1SC1 & 0x80) == 0x01)  //While 7th bit is set, won't proceed

the last bit(COCO, Conversion Complete Flag) means: Conversion completed=1;Conversion not completed=0;

wait until COCO is 1 (wait until Conversion is completed) works with:

while (!(ADC1SC1 & 0x80));

I also wrote an ADC-routine and I have this:

while(!ADCSC1_COCO);//wait until ADC-meaurement is finished

your routine "while ((ADC1SC1 & 0x80) == 0x01)" does not work, because

ADC1SC1 & 0x80 -> this can be 0x80 or 0x00 (& is logic AND)

and if you compare this result with 0x01, like (0x80 or 0x00) == 0x01 always result False (== is arith. AND)

I hope this helps

Mike

0 Kudos
Reply

1,457 Views
Encoder1
Contributor III

Hi Chris

I cannot completely understand your C code: I am an assembler lover. In any case I cannot see ADC configuration: this is usually done at the program initialization. You have to assign the ADC clock source and prescaler if any in ADCCFG and the physical pin I/O you intend to use for ADC input(s).

With standard device declaration.inc, in assembler this would be something like this:

; Initial declarations

; ADC

    ADICLK1m equ %00000000  ; Bus clock

    ADIV4m equ %01000000    ; Input Clock /4

    ADMODE08m equ %00000000 ; 8-bit mode ADC

; I/O ADC used ports

    Pt_Ch_L    EQU APCTL2        ; AD11 pin control reg.

    Ch_L_in_m  EQU %00001011     ; channel AD11 (Ch Left)

    Ch_L_in    EQU APCTL2_ADPC11 ; pin control

    Pt_Ch_R    EQU APCTL1        ; AD7 pin control reg.

    Ch_R_in_m  EQU %00000111     ; channel AD7 (Ch Right)

    Ch_R_in    EQU APCTL1_ADPC7  ; pin control

; ADC initialization, 20MHz BusClock

; ADCclk= BusClock/4 (5MHz), 8bit ADC, short sample

    lda #(ADICLK1m+ADIV4m+ADMODE08m)

    sta ADCCFG

    bset Ch_R_in,Pt_Ch_R      ; assign Ch_R_in to ADC

    bset Ch_L_in,Pt_Ch_L       ; assign Ch_L_in to ADC

.

.

; ADC conversion on CH_L_in and Ch_R_in

    mov #Ch_L_in_m,ADCSC1      ; start sample L

    brclr ADCSC1_COCO,ADCSC1,* ; loops here untill conv. end

    lda ADCRL                  ; 8bit result in A

    mov #Ch_R_in_m,ADCSC1      ; start sample R

    brclr ADCSC1_COCO,ADCSC1,* ; conv. end?

    ldx ADCRL                  ; 8bit result in X

My code snippet does a sequential 8bit conversion on two different channels. I think you too may do an 8bit conversion to read 3 buttons: you do not need 10bit resolution for this, but this is a minor problem, the example would be the same with the addition of reading ADCRH as you correctly do.

You may see there is an ADC and I/O port initial configuration you completely miss. Obviously you do not need the first literal assignments if you use the correct numerical constants in the sequential instructions. Please note you, as in my case, do not need AD1SC2 writing: it is always =0 from CPU reset. You need only to write on ADC1SC1 to start the conversion.

I think this example is so simple you can easily translate in C in the same manner I read your code. Good luck

Salvatore