Author Topic: STM8S103F3 ,ADC single scan issue! AIN3 on PD2 not reading  (Read 575 times)

0 Members and 1 Guest are viewing this topic.

Offline SajeevTopic starter

  • Contributor
  • Posts: 24
  • Country: in
STM8S103F3 ,ADC single scan issue! AIN3 on PD2 not reading
« on: September 06, 2024, 06:55:08 pm »
Dear Forum members,
I tried to implement  ADC1 single scan  on AIN2,AIN3,AIN4 (STM8S103F3 pins PC4,PD2,PD3). The ADC scans and readings are stored in the ADC buffers 2,3,and4. The readings display  AIN2 and AIN4.properly with expected value but AIN3 always displays 0. I have connected AIN2,AIN3 and AIN4 to 3.3V . AIN2 and AIN4 displays 1023 but AIN3 displays0. Please find attached the STVD project if simulation is needed. The configuration is as below. Please advice where i have gone wrong?
Thanks
Sajeev

         mov CLK_CKDIVR,#$00   ; cpu clock no divisor = 16mhz
ADC_INIT:
   mov ADC_CR1,#$21              ; ADC prscaler iis clk/4 = 010 ; ADON ,0b01000001
   mov ADC_CSR,#$04           ; select channel-2 , channel-3 and chaneel 4, PC4,PD2,PD3
   mov ADC_CR2,#$0A             ; enable scan mode -bit1,result right align bit 3
   bset ADC_CR3,#7                  ; enable buffer for result storage
   mov ADC_TDRL,#$1C          ; disable schmitt trigger #2,disable schmitt trigger #3,disable schmitt trigger #4
   bres ADC_CR1,#1                  ; single conversion mode
ADC_READ:
   bset ADC_CR1,#0    ; ADON
   
con_not_finish:
   ld a,ADC_CSR      ; copy ADC flag register to A
   and a,#$80         ; AND with 0x80 contents of flag register
   jreq con_not_finish            ; loop label con_finish if anding results0, bit7 set if conversion finished
   ld a,ADC_DB2RL      ; copy ADC lower result register to A ,(buffer2)
   ld result1,a               ; store in result1
   ld a,ADC_DB2RH      ; copy ADC higher result register to A( ,buffer2)
   ld result2,a              ; store MSB in result2
   ld a,ADC_DB3RL      ; copy ADC lower result register to A ,(buffer3)
   ld result3,a               ; store in result3
   ld a,ADC_DB3RH      ; copy ADC higher result register to A ,(buffer3)
   ld result4,a              ; store MSB in result4
   ld a,ADC_DB4RL      ; copy ADC lower result register to A, (buffer4)
   ld result5,a               ; store in result5
   ld a,ADC_DB4RH      ; copy ADC higher result register to A, (buffer4)
   ld result6,a              ; store MSB in result6
   bres ADC_CR3,#6      ; clear overrun flag
   mov ADC_CSR,#$04   ; clear ADC conversion finish flag, and restore max chanel



ADC2:   
   ldw X,#$0000      ; load 0 in X
   ldw buffer14,x      ; store 0 in buffer14,buffer15
   ldw X,result2      ; load X with contents of result2,result1 MSB-LSB
   ldw buffer16,x      ; store word in buffer16,buffer17
   call bin_to_ascii   ; procedure to convert binary value to ASCII , to be converted values in buffer15,16,17 msb to lsb , result in buffers to buffers+11
   pointerX #buffers    ; point X to buffers register , start point of ascii value storage, ascii values in buffers
   mov temp1,#11      ; temp1 as counter, 10 values to be printed
   call write_from_buffers1

ADC3:   
   ldw X,#$0000      ; load 0 in X
   ldw buffer14,x      ; store 0 in buffer14,buffer15
   ldw X,result4      ; load X with contents of result2,result1 MSB-LSB
   ldw buffer16,x      ; store word in buffer16,buffer17
   call bin_to_ascii   ; procedure to convert binary value to ASCII , to be converted values in buffer15,16,17 msb to lsb , result in buffers to buffers+11
   pointerX #buffers     ; point X to buffers register , start point of ascii value storage, ascii values in buffers
   mov temp1,#11      ; temp1 as counter, 10 values to be printed
   call write_from_buffers1

ADC4:   
   ldw X,#$0000      ; load 0 in X
   ldw buffer14,x      ; store 0 in buffer14,buffer15
   ldw X,result6      ; load X with contents of result2,result1 MSB-LSB
   ldw buffer16,x      ; store word in buffer16,buffer17
   call bin_to_ascii   ; procedure to convert binary value to ASCII , to be converted values in buffer15,16,17 msb to lsb , result in buffers to buffers+11
   pointerX #buffers     ; point X to buffers register , start point of ascii value storage, ascii values in buffers
   mov temp1,#11      ; temp1 as counter, 10 values to be printed
   call write_from_buffers1


delay:               ; apprx 3 second delay
   mov buffer3,#255
L2:   
   ldw X,#65535
L1:
   decw X
   jrne L1
   dec buffer3
   jrne L2

   jp ADC_READ         ; jump to ADC_READ label to restart scan ADC
 

Offline SajeevTopic starter

  • Contributor
  • Posts: 24
  • Country: in
Re: STM8S103F3 ,ADC single scan issue! AIN3 on PD2 not reading
« Reply #1 on: September 07, 2024, 04:01:29 am »
I tried using AIN3 individually and it works in single shot mode. The AIN3 doesnt work if I do a individual single shot conversion on (scan  mode disabled)  AIN2,AIN3,AIN4 one after another. Is it that the chip is faulty. Has anyone experienced this? Below code for ADC single shot blocking code. IF commented section is used AIN3 doesn't work . If AIN2 and AIN4 is commented AIN2 works. I tried by only selecting AIN3 and AIN4 and it works fine . If I select AIN2,AIN3,AIN4 in sequence AIN3 result is null other 2 shows?
Regards
Sajeev


ADC_INIT:
   mov ADC_CR1,#$21    ; ADC prscaler iis clk/4 = 010 ; ADON ,0b01000001
   bset ADC_CR2,#3      ; result right align bit 3
   mov ADC_TDRL,#$1C    ; disable schmitt trigger #2,disable schmitt trigger #3,disable schmitt trigger #4
   bres ADC_CR1,#1    ; single conversion mode
ADC_READ:
   ;mov ADC_CSR,#$02    ; select channel-2
   ;bset ADC_CR1,#0    ; ADON
   ;call wait_convert
   ;bres ADC_CSR,#7      ; clear ADC conversion finish flag
   ;ld a,ADC_DRL      ; copy ADC lower result register to A
   ;ld result1,a      ; store in result1
   ;ld a,ADC_DRH      ; copy ADC higher result register to A
   ;ld result2,a      ; store MSB in result2

   mov ADC_CSR,#$03    ; channel-3
   bset ADC_CR1,#0    ; ADON
   call wait_convert
   bres ADC_CSR,#7      ; clear ADC conversion finish flag
   ld a,ADC_DRL      ; copy ADC lower result register to A
   ld result3,a      ; store in result1
   ld a,ADC_DRH      ; copy ADC higher result register to A
   ld result4,a      ; store MSB in result2
   
   ;mov ADC_CSR,#$04    ; chaneel 4
   ;bset ADC_CR1,#0    ; ADON
   ;call wait_convert   
   ;bres ADC_CSR,#7      ; clear ADC conversion finish flag
   ;ld a,ADC_DRL      ; copy ADC lower result register to A
   ;ld result5,a      ; store in result1
   ;ld a,ADC_DRH      ; copy ADC higher result register to A
   ;ld result6,a      ; store MSB in result2

ADC2:   
   ;ldw X,#$0000      ; load 0 in X
   ;ldw buffer14,x      ; store 0 in buffer14,buffer15
   ;ldw X,result2      ; load X with contents of result2,result1 MSB-LSB
   ;ldw buffer16,x      ; store word in buffer16,buffer17
   ;call bin_to_ascii   ; procedure to convert binary value to ASCII , to be converted values in buffer15,16,17 msb to lsb , result in buffers to buffers+11
   ;pointerX #buffers   ; point X to buffers register , start point of ascii value storage, ascii values in buffers
   ;mov temp1,#11      ; temp1 as counter, 10 values to be printed
   ;call write_from_buffers1

ADC3:   
   ldw X,#$0000      ; load 0 in X
   ldw buffer14,x      ; store 0 in buffer14,buffer15
   ldw X,result4      ; load X with contents of result2,result1 MSB-LSB
   ldw buffer16,x      ; store word in buffer16,buffer17
   call bin_to_ascii   ; procedure to convert binary value to ASCII , to be converted values in buffer15,16,17 msb to lsb , result in buffers to buffers+11
   pointerX #buffers   ; point X to buffers register , start point of ascii value storage, ascii values in buffers
   mov temp1,#11      ; temp1 as counter, 10 values to be printed
   call write_from_buffers1

ADC4:   
   ;ldw X,#$0000      ; load 0 in X
   ;ldw buffer14,x      ; store 0 in buffer14,buffer15
   ;ldw X,result6      ; load X with contents of result2,result1 MSB-LSB
   ;ldw buffer16,x      ; store word in buffer16,buffer17
   ;call bin_to_ascii   ; procedure to convert binary value to ASCII , to be converted values in buffer15,16,17 msb to lsb , result in buffers to buffers+11
   ;pointerX #buffers   ; point X to buffers register , start point of ascii value storage, ascii values in buffers
   ;mov temp1,#11      ; temp1 as counter, 10 values to be printed
   ;call write_from_buffers1
 

Offline SajeevTopic starter

  • Contributor
  • Posts: 24
  • Country: in
Re: STM8S103F3 ,ADC single scan issue! AIN3 on PD2 not reading
« Reply #2 on: September 07, 2024, 06:44:50 pm »
Dear,
I tried the same code on a different chip but result same.
Regards
Sajeev
 

Offline ozcar

  • Frequent Contributor
  • **
  • Posts: 338
  • Country: au
Re: STM8S103F3 ,ADC single scan issue! AIN3 on PD2 not reading
« Reply #3 on: September 07, 2024, 11:21:15 pm »
So, this is where you stash the ADC channel 3 values:

Code: [Select]
...
ld a,ADC_DB3RL                  ; copy ADC lower result register to A ,(buffer3)
ld result3,a                    ; store in result3
ld a,ADC_DB3RH                  ; copy ADC higher result register to A ,(buffer3)
ld result4,a                    ; store MSB in result4
...

But, in bin_to_ascii you do this

Code: [Select]
...
clr result4 ; clear sram registers for bin_to_ascii calculations
clr result3 ; clear sram registers for bin_to_ascii calculations
...

You also destroy the stored values for ADC channel 2, in result1 & result2, in bin_to_ascii, but by then you have already picked those up to convert them, so you don't notice that.

Maybe you should give them more meaningful names?
 
The following users thanked this post: Sajeev

Offline SajeevTopic starter

  • Contributor
  • Posts: 24
  • Country: in
Re: STM8S103F3 ,ADC single scan issue! AIN3 on PD2 not reading
« Reply #4 on: September 08, 2024, 10:06:04 am »
Dear SIR!!!,
Thankyou very much!!!!!!!!!!!!!!!!!!
I was really upset for last 3 days. You are absolutely right. I was destroying the ADC data in result3/result4 every time i use the bin_to_ascii function. I was doing copy/paste all the variables from my old programs (lazy). I have now changed the names of the variables to store result to ADCresultx...... Kept result1 to result4 for exclusively for bin_to_ascii operations. The code works fine with the corrections you have pointed out. The corrected code is attached here and the screen shot of the terminal.
Thanks again for the valuable advice
Regards
Sajeev
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf