STM8S103F3 ,ADC single scan issue!  AIN3 on PD2 not reading - Page 1
Forum section: electronics / general

author: Sajeev
author_rank: Contributor
timestamp: September 06, 2024, 06:55:08 pm
content: 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?ThanksSajeevmov CLK_CKDIVR,#$00 ; cpu clock no divisor = 16mhzADC_INIT:mov ADC_CR1,#$21 ; ADC prscaler iis clk/4 = 010 ; ADON ,0b01000001mov ADC_CSR,#$04 ; select channel-2 , channel-3 and chaneel 4, PC4,PD2,PD3mov ADC_CR2,#$0A ; enable scan mode -bit1,result right align bit 3bset ADC_CR3,#7 ; enable buffer for result storagemov ADC_TDRL,#$1C ; disable schmitt trigger #2,disable schmitt trigger #3,disable schmitt trigger #4bres ADC_CR1,#1 ; single conversion modeADC_READ:bset ADC_CR1,#0 ; ADONcon_not_finish:ld a,ADC_CSR ; copy ADC flag register to Aand a,#$80 ; AND with 0x80 contents of flag registerjreq con_not_finish ; loop label con_finish if anding results0, bit7 set if conversion finishedld a,ADC_DB2RL ; copy ADC lower result register to A ,(buffer2)ld result1,a ; store in result1ld a,ADC_DB2RH ; copy ADC higher result register to A( ,buffer2)ld result2,a ; store MSB in result2ld a,ADC_DB3RL ; copy ADC lower result register to A ,(buffer3)ld result3,a ; store in result3ld a,ADC_DB3RH ; copy ADC higher result register to A ,(buffer3)ld result4,a ; store MSB in result4ld a,ADC_DB4RL ; copy ADC lower result register to A, (buffer4)ld result5,a ; store in result5ld a,ADC_DB4RH ; copy ADC higher result register to A, (buffer4)ld result6,a ; store MSB in result6bres ADC_CR3,#6 ; clear overrun flagmov ADC_CSR,#$04 ; clear ADC conversion finish flag, and restore max chanelADC2:ldw X,#$0000 ; load 0 in Xldw buffer14,x ; store 0 in buffer14,buffer15ldw X,result2 ; load X with contents of result2,result1 MSB-LSBldw buffer16,x ; store word in buffer16,buffer17call 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+11pointerX #buffers ; point X to buffers register , start point of ascii value storage, ascii values in buffersmov temp1,#11 ; temp1 as counter, 10 values to be printedcall write_from_buffers1ADC3:ldw X,#$0000 ; load 0 in Xldw buffer14,x ; store 0 in buffer14,buffer15ldw X,result4 ; load X with contents of result2,result1 MSB-LSBldw buffer16,x ; store word in buffer16,buffer17call 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+11pointerX #buffers ; point X to buffers register , start point of ascii value storage, ascii values in buffersmov temp1,#11 ; temp1 as counter, 10 values to be printedcall write_from_buffers1ADC4:ldw X,#$0000 ; load 0 in Xldw buffer14,x ; store 0 in buffer14,buffer15ldw X,result6 ; load X with contents of result2,result1 MSB-LSBldw buffer16,x ; store word in buffer16,buffer17call 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+11pointerX #buffers ; point X to buffers register , start point of ascii value storage, ascii values in buffersmov temp1,#11 ; temp1 as counter, 10 values to be printedcall write_from_buffers1delay: ; apprx 3 second delaymov buffer3,#255L2:ldw X,#65535L1:decw Xjrne L1dec buffer3jrne L2jp ADC_READ ; jump to ADC_READ label to restart scan ADC

author: Sajeev
author_rank: Contributor
timestamp: September 07, 2024, 04:01:29 am
content: 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?RegardsSajeevADC_INIT:mov ADC_CR1,#$21 ; ADC prscaler iis clk/4 = 010 ; ADON ,0b01000001bset ADC_CR2,#3 ; result right align bit 3mov ADC_TDRL,#$1C ; disable schmitt trigger #2,disable schmitt trigger #3,disable schmitt trigger #4bres ADC_CR1,#1 ; single conversion modeADC_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 result2mov ADC_CSR,#$03 ; channel-3bset ADC_CR1,#0 ; ADONcall wait_convertbres ADC_CSR,#7 ; clear ADC conversion finish flagld a,ADC_DRL ; copy ADC lower result register to Ald result3,a ; store in result1ld a,ADC_DRH ; copy ADC higher result register to Ald 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 result2ADC2:;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_buffers1ADC3:ldw X,#$0000 ; load 0 in Xldw buffer14,x ; store 0 in buffer14,buffer15ldw X,result4 ; load X with contents of result2,result1 MSB-LSBldw buffer16,x ; store word in buffer16,buffer17call 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+11pointerX #buffers ; point X to buffers register , start point of ascii value storage, ascii values in buffersmov temp1,#11 ; temp1 as counter, 10 values to be printedcall write_from_buffers1ADC4:;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

author: Sajeev
author_rank: Contributor
timestamp: September 07, 2024, 06:44:50 pm
content: Dear,I tried the same code on a different chip but result same.RegardsSajeev

author: ozcar
author_rank: Frequent Contributor
timestamp: September 07, 2024, 11:21:15 pm
content: So, this is where you stash the ADC channel 3 values:Code:...ld a,ADC_DB3RL ; copy ADC lower result register to A ,(buffer3)ld result3,a ; store in result3ld 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 thisCode:...clr result4; clear sram registers for bin_to_ascii calculationsclr 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?

author: Sajeev
author_rank: Contributor
timestamp: September 08, 2024, 10:06:04 am
content: 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 adviceRegardsSajeev