Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

AD result transfer from ADRESL and ADRESH

Status
Not open for further replies.

lipschutz

Member
Hi

the following code segment is used to transfer ad result from ADRESL and ADRESH. i think there might be something wrong with the code. instead of showing different value the program writes one fixed number on the lcd screen.

banksel ADRESL ;read and transfer
movf ADRESL,W ;AD resul to location
banksel ADRESH ;adres_low and adres_high
movwf adres_low ;respectively
movf ADRESH,W ;
movwf adres_high ;
bcf ADCON0,ADON ;AD module turned off

thanks
lipschutz
 
hi,
How are you trying to display the contents of a WORD address to the LCD.??
 
if i understand your question, i think you probably asked whether there is another program module to convert ad result into bcd and then display on the lcd conforming display requirements. then yes there is code to do this. and these segment of codes are tested independently on MPLAB simulator and lcd module respectivle.

my question is whether the posted code segment successfully transfer it's(ad result) contents to 'adres_low' and 'adres_high' register which are in the bank 0?
 
hi
Please post the initialisation section/header of your program listing.
 
I have used the ADC in a 16F887 with the code shown below.

When you start your program, you must first initialise the ADC.

When reading the ADC, you have to initiate a conversion, and unless you are using interupts, wait for the conversion to complete befor reading the results in ADRESH and ADRESL.

This is how I did it.
(I don't claim that this code as all my original work).

JimB

Code:
;*****************************************************************************
;This code block configures the ADC for polling,
;Uses Vdd and Vss as reference, Frc clock and AN2 input.
;

Initialise_ADC_left_AN2
		BANKSEL TRISA 				;
		bsf 	TRISA,2 			;Set RA2 to input
		BANKSEL ANSEL 				;
		bsf 	ANSEL,2				;Set RA2 to analog

		BANKSEL	ADCON1 				;
		movlw  	B'00000000'			;left justify  	ADFM = 0
		movwf 	ADCON1 				;Vdd and Vss as Vref 	VCFG0 and VCFG1 = 0

		BANKSEL ADCON0 				;
		movlw 	B'11001001'			;ADC Frc clock,
		movwf 	ADCON0 				;AN2, On

ADC_delay		
		movlw	D'5'				;10us with an 8Mhz clock	
		movwf	DELAY_ADC
ADC_delay_loop
		decfsz	DELAY_ADC, f
		goto	ADC_delay_loop
		return

	
;*****************************************************************************
Read_ADC_AN2
		bsf 	ADCON0,GO 			;Start conversion
		btfsc 	ADCON0,GO 			;Is conversion done?
		goto 	$-1 				;No, test again

		BANKSEL ADRESH 				
		movf 	ADRESH,W 			;Read upper 8 bits
		movwf 	ADC_RESULT_HI 			;store in GPR space
		BANKSEL ADRESL 				
		movf 	ADRESL,W 			;Read lower 2 bits 
		BANKSEL	ADRESH
		movwf 	ADC_RESULT_LO 			;Store in GPR space
		return
;*****************************************************************************
 
it seems that the AD result transfer segment is okay. may be something else is wrong. here is my code. porta is configured before this module. RA4 is the analog input.

Code:
;***********config A/D module**************
;
;
;configure AD module
;
                 banksel     ANSEL         ;  
                 ;bcf        STATUS,6h     ;
                 ;bsf        STATUS,5h     ;
;bank1 selected
;
                  clrf       ANSEL         ;
                  bsf        ANSEL,an_ch   ;
;RA4 is selected as analog channel
;                                
                  clrf       ADCON1        ;
                  bsf        ADCON1,ADFM   ;
;AD result is right justified
;
                  bcf        ADCON1,ADCS2  ;
;AD clock divide by 2 enabled
;
                  banksel    ADCON0        ;
;back to bank0
;
                  clrf       ADCON0        ;
                  bsf        ADCON0,CHS_2  ;
;RA4 selected as analog input:configuring ADCON0
;
                  bSf        ADCON0,ADCS1  ;## 
                  bSf        ADCON0,ADCS0  ;##
;INTRC is set as AD conversion clock 
;
                  bsf        ADCON0,ADON   ;
;AD module turned on
;
;wait 15us
                  movlw      05h           ;
                  movwf      temp          ;
delay_15us        decfsz     temp,F        ;
                  goto       delay_40us    ;
;acquisition time elapsed              
;
                  bsf      ADCON0,go_done ;                  
;AD conversion starts now
;
check             btfsc    ADCON0,go_done ;
                  goto     check          ;
;
;above segment implement polling of go_done bit in
;every 100us
;read ad result register pair
                  banksel  ADRESL         ;read and transfer
                  movf     ADRESL,W       ;AD resul to location
                  banksel  ADRESH         ;adres_low and adres_high
                  movwf    adres_low      ;respectively
                  movf     ADRESH,W       ;
                  movwf    adres_high     ;
;
 
hi,
I think would be more helpful if you posted your full code.
 
ok, here it is..

Code:
                  processor 16F88
                 include <p16f88.inc>
                 __CONFIG    _CONFIG1,  _CP_OFF & _CCP1_RB0 & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_ON & _PWRTE_ON & _WDT_OFF & _INTRC_IO
                 __CONFIG    _CONFIG2,  _IESO_OFF & _FCMEN_OFF
;
;SPECIFY REGISTER_LCD
;
counter_l EQU     28H
counter_s EQU     29H
porta     EQU     05h
portb     EQU     06h
;
;bit_definition for lcd module
RS        EQU     0h
RW        EQU     1h
E         EQU     2h
;---------------------------
;
;Specify register_AD_MODULE
;
ANSEL       EQU     9bh
ADCON1      EQU     9fh
ADCON0      EQU     1fh
ADRESL      EQU     9eh
ADRESH      EQU     1eh
TRISA       equ     85h
;
;bit_definition
;
an_ch       equ     4h
RP_1        EQU     06H
RP_0        EQU     05H
ADFM        EQU     07h
ADCS2       EQU     06h
ADCS1       EQU     07H
ADCS0       EQU     06H
go_done     equ     02h
ADON        equ     00h
CHS_0       EQU     03H    
CHS_1       EQU     04H
CHS_2       EQU     05H
;------------------------------
;*****VARIABLES DEFINITION*******
;(binary to bcd conversion)
adres_low  equ      27h
adres_high equ      26h
residue    equ      25h
bcd_0      equ      20h
bcd_1      equ      21h
bcd_2      equ      22h
bcd_3      equ      23h
temp       equ      24h
;---------CONSTANT DEFINITION---------
ascii_digi   equ      30H
ascii_char1  equ      60H
ascii_char2  equ      70H
;PROGRAM
                   org 00h
;
;OSCILLATOR FREQUENCY MODE SELECTION
                banksel   OSCCON           ;goto bank 1
                bsf       OSCCON,IRCF2     ; 
                bcf       OSCCON,IRCF1     ;
                bcf       OSCCON,IRCF0     ;
                bcf       OSCCON,SCS1      ;
                bcf       OSCCON,SCS0      ;
;*********clock frequency set to 1MHz****************
                banksel   TRISA            ;##back to bank0
;**************CONFIGURING PORTS*****************************
SETPORT         movlw     b'00010000'  ;
                movwf     TRISA        ;##
                ;tris      porta        ;
;RA0,1,2 are RS, RW and E respectively of lcd control 
;lines, RA4 is the analog input for AD converter
;
                movlw     b'00000000'  ;
                movwf      TRISB       ;##
;all of the 8 pins of portb are configured as outputs.
;Only the higher nibble will be used to send data 
;and command to the lcd module
; 
;****************CONFIGURING PORT ENDS***********************
                banksel   porta        ;
                bcf       porta,E      ;
;clearing E
;          
;************START OF INITIALISATION LCD***************
initio          movlw     14h          ;20
                movwf     temp         ;times
loop            call      delay_1ms    ;1ms
                decfsz    temp,F       ;
                goto      loop         ;
;A 20ms delay will give the lcd module enough time to 
;stabilize itself on power up 
;
                bcf       porta,RS     ;
                bcf       porta,RW     ;
                movlw     30h          ;
                movwf     portb        ;
                call      enab_lcd     ;
;the above segment sends the command
;RS RW DB7 DB6 DB5 DB4=000011 as required by the module
;ref:HD44780 datasheet 8-bit interface
                movlw     05h          ;
                movwf     temp         ;
five_ms         call      delay_1ms    ;
                decfsz    temp,F       ;
                goto      five_ms      ;
;5ms delay ref:HD44780 datasheet 8-bit interface
;
               ; bcf       porta,RS     ;
               ; bcf       porta,RW     ;
                movlw     30h          ;
                movwf     portb        ;
                call      enab_lcd     ;
;RS RW DB7 DB6 DB5 DB4=000011 as required by the module
;ref:HD44780 datasheet 8-bit interface
;                
                movlw     46h          ;
                movwf     temp         ;
delay_200us     decfsz    temp,F       ;
                goto      delay_200us  ;
;200us delay ref:HD44780 datasheet 8-bit interface
;
               ; bcf       porta,RS     ;
               ; bcf       porta,RW     ;
                movlw     30h          ;
                movwf     portb        ;
                call      enab_lcd     ;
;again the same bit sequence is sent as before
;i.e 000011; 8-bit interface
;
fucntion_set    bcf       porta,RS     ;               
                bcf       porta,RW     ;
                movlw     20h          ;
                movwf     portb        ;
                call      enab_lcd     ; 
                movlw     46h          ;200US delay as per HD44780 datasheet
                movwf     temp         ;
ALPHA           decfsz    temp,F       ;
                goto      ALPHA        ;
;DB4 is cleared to select 4-bit mode
;from now on every byte is sent in two nibbles and the 
;the higher nibble is sent first
;
               ; bcf       porta,RS     ;
               ; bcf       porta,RW     ;
                movlw     20h          ;
                movwf     portb        ;
                call      enab_lcd     ;
                movlw     00h          ;
                movwf     portb        ;
                call      enab_lcd     ;
                movlw     46h          ;incorporates a delay of 200us
                movwf     temp         ;ref: HD44780 datasheet
BETA            decfsz    temp,F       ;
                goto      BETA         ;
;the 'function set' command is sent again. the upper nibble 
;being the same as before and the lower nibble specifying 
;1-line and 5*7 pixel format i.e N=0 F=0 
;ref:HD44780 datasheet 
;
                movlw     20h          ;
                movwf     portb        ;
                call      enab_lcd     ;
                movlw     80h          ;
                movwf     portb        ;
                call      enab_lcd     ;
                movlw     46h          ;
                movwf     temp         ;delay 200us ref: HD44780 datasheet
GAMMA           decfsz    temp,F       ;
                goto      GAMMA        ;  
;'display off' command 00001000 ref:HD44780 datasheet
                movlw     00h          ;
                movwf     portb        ;
                call      enab_lcd     ;  
                movlw     60h          ;
                movwf     portb        ;
                call      enab_lcd     ;
                movlw     46h          ;delay 200us ref: HD44780 datasheet
                movwf     temp         ;
DELTA           decfsz    temp,F       ;
                goto      DELTA        ;
;'display on' command 00000001 ref:HD44780 datasheet                              
                movlw     00h          ;
                movwf     portb        ;
                call      enab_lcd     ;
                movlw     0C0h         ;
                movwf     portb        ;                
                call      enab_lcd     ;
                movlw     5h           ;delay 5ms ref: HD44780 datasheet
                movwf     temp         ;
MIKE            call      delay_1ms    ;
                decfsz    temp,F       ;
                goto      MIKE         ;
;'entry mode' set
;
;*****************END OF INITIALIZATION*********************
;
;
;***********config A/D module**************
;
;
;configure AD module
;
                 banksel     ANSEL         ;  
                 ;bcf        STATUS,6h     ;
                 ;bsf        STATUS,5h     ;
;bank1 selected
;
                  clrf       ANSEL         ;
                  bsf        ANSEL,an_ch   ;
;RA4 is selected as analog channel
;                                
                  clrf       ADCON1        ;
                  bsf        ADCON1,ADFM   ;
;AD result is right justified
;
                  bcf        ADCON1,ADCS2  ;
;AD clock divide by 2 enabled
;
                  banksel    ADCON0        ;
;back to bank0
;
                  clrf       ADCON0        ;
                  bsf        ADCON0,CHS_2  ;
;RA4 selected as analog input:configuring ADCON0
;
                  bSf        ADCON0,ADCS1  ;## 
                  bSf        ADCON0,ADCS0  ;##
;INTRC is set as AD conversion clock 
;
                  bsf        ADCON0,ADON   ;
;AD module turned on
;
;wait 15us
                  movlw      05h           ;
                  movwf      temp          ;
delay_15us        decfsz     temp,F        ;
                  goto       delay_40us    ;
;acquisition time elapsed              
;
                  bsf      ADCON0,go_done ;                  
;AD conversion starts now
;
check             btfsc    ADCON0,go_done ;
                  goto     check          ;
;
;above segment implement polling of go_done bit in
;every 100us
;read ad result register pair
                  banksel  ADRESL         ;read and transfer
                  movf     ADRESL,W       ;AD resul to location
                  banksel  ADRESH         ;adres_low and adres_high
                  movwf    adres_low      ;respectively
                  movf     ADRESH,W       ;
                  movwf    adres_high     ;
;
;**********BINARY TO BCD CONVERSION CODE************
                  clrf     bcd_0       ;
                  clrf     bcd_1       ;
                  clrf     bcd_2       ;
                  clrf     bcd_3       ;
                  clrf     residue     ;
                  clrf     temp        ; clearing the registers finishes                   
ONE               movf    adres_high,F ;
                  btfss   STATUS,Z     ;
                  goto    TWO          ;
                  goto    SIX          ;
;
;
TWO               movf    adres_high,0 ;
                  sublw   01h          ;
                  btfss   STATUS,Z     ;
                  goto    THREE        ;
                  movlw   02h          ;
                  movwf   bcd_2        ;
                  movlw   38h          ;
                  movwf   residue      ;
                  goto    SIX          ;
;
;
THREE             movf    adres_high,0 ;
                  sublw   02h          ;
                  btfss   STATUS,Z     ;
                  goto    FOUR         ;
                  movlw   05h          ;
                  movwf   bcd_2        ;
                  movlw   0ch          ;
                  movwf   residue      ;
                  goto    SIX          ;
;
;
FOUR              movlw   07h          ;
                  movwf   bcd_2        ;
                  movlw   44h          ;
                  movwf   residue      ;
;
;
SIX               movlw   64h          ;
                  subwf   adres_low,W  ;
                  movwf   temp         ;
                  btfsc   STATUS,C     ;
                  goto    SIX_A        ;
                  movf    residue,W    ;
                  addwf   adres_low,F  ;
                  goto    SEVEN        ;
;
SIX_A             incf    bcd_2,F      ;
                  movf    residue,W    ;
                  addwf   temp,F       ;
                  movf    temp,W       ;
                  movwf   adres_low    ;
;
;
SEVEN             movlw   64h          ;
                  subwf   adres_low,W  ;
                  movwf   temp         ;
                  btfsc   STATUS,C     ;
                  goto    SEVEN_A      ;
                  goto    EIGHT        ;
;
SEVEN_A           incf    bcd_2,F      ;
                  movf    temp,W       ;
                  movwf   adres_low    ;
                  goto    SEVEN        ;
                  
                  
;
;
EIGHT             movlw   0ah          ;
                  subwf   bcd_2,W      ;
                  movwf   temp         ;
                  btfsc   STATUS,C     ;
                  goto    EIGHT_A      ;                 
                  goto    NINE         ;
;
EIGHT_A           incf    bcd_3,F      ;
                  movf    temp,W       ;
                  movwf   bcd_2        ; 
                  
;
;
NINE              movlw  0ah          ;
                  subwf  adres_low,W  ;
                  movwf  temp         ;
                  btfsc  STATUS,C     ;
                  goto   NINE_A       ;
                  goto   TEN          ;                 
;
NINE_A            incf    bcd_1,F      ;
                  movf    temp,F       ;
                  movwf   adres_low    ;
                  goto    NINE         ;
;
;
TEN               movf   adres_low,W  ;
                  movwf  bcd_0        ;
;FIN               GOTO   FIN          ;
;
;**************DISPLAY BCD ON LCD*******************
;                  
                  bsf    porta, RS    ;
                  bcf    porta, RW    ;
show_bcd_3        movlw  ascii_digi   ;
                  movwf  portb        ;
                  call   enab_lcd     ;
                  swapf  bcd_3,W      ;
                  movwf  portb        ;
                  call   enab_lcd     ; 
;
show_bcd_2        movlw  ascii_digi   ;
                  movwf  portb        ;
                  call   enab_lcd     ;
                  swapf  bcd_2,W      ;
                  movwf  portb        ;
                  call   enab_lcd     ;
;
show_bcd_1        movlw  ascii_digi   ;
                  movwf  portb        ;
                  call   enab_lcd     ;
                  swapf  bcd_1,W      ;
                  movwf  portb        ;
                  call   enab_lcd     ;
;
show_bcd_0        movlw  ascii_digi   ;
                  movwf  portb        ;
                  call   enab_lcd     ;
                  swapf  bcd_0,W      ;
                  movwf  portb        ;
                  call   enab_lcd     ;
;
show_p            movlw  ascii_char2  ;
                  movwf  portb        ;
                  call   enab_lcd     ;
                  movlw  0h           ;
                  movwf  portb        ;
                  call   enab_lcd     ;
;
show_p2           movlw  ascii_char2  ;
                  movwf  portb        ;
                  call   enab_lcd     ;
                  movlw  0h           ;
                  movwf  portb        ;
                  call   enab_lcd     ;
;
show_b            movlw  ascii_char1  ;
                  movwf  portb        ;
                  call   enab_lcd     ;
                  movlw  20h          ;
                  movwf  portb        ;
                  call   enab_lcd     ;
fin               goto   fin          ;
;************************************************                  
;
;SUBROUTINES
;
enab_lcd        bsf       porta,E      ;
                nop                    ;
                bcf       porta,E      ;
                nop                    ;
               return                  ;     
;data is transferred when E goes from high to low
;
delay_1ms       movlw     0ffh         ;
                movwf     counter_s    ;
point           nop                    ;
                decfsz    counter_s,F  ;
                goto      point        ;
                return                 ;
                      end              ;
;
 
A bit of general advice.

You are making work for yourself by redefining many things which are already defined in the file p16f88.inc
That is why there is a line include<p16f88.inc>, the nice fellows at Microchip have done all the work for you already.

See my edits to part of your program here:

Code:
;SPECIFY REGISTER_LCD
;
counter_l EQU     28H
counter_s EQU     29H
porta     EQU     05h			;Specified in p16f88.inc as PORTA
portb     EQU     06h			;Specified in p16f88.inc as PORTB
;
;bit_definition for lcd module
RS        EQU     0h
RW        EQU     1h
E         EQU     2h
;---------------------------
;
;Specify register_AD_MODULE
;
ANSEL       EQU     9bh			;Specified in p16f88.inc
ADCON1      EQU     9fh			;Specified in p16f88.inc
ADCON0      EQU     1fh			;Specified in p16f88.inc
ADRESL      EQU     9eh			;Specified in p16f88.inc
ADRESH      EQU     1eh			;Specified in p16f88.inc
TRISA       equ     85h			;Specified in p16f88.inc
;
;bit_definition
;
an_ch       equ     4h
RP_1        EQU     06H			;Specified in p16f88.inc
RP_0        EQU     05H			;Specified in p16f88.inc
ADFM        EQU     07h			;Specified in p16f88.inc
ADCS2       EQU     06h			;Specified in p16f88.inc
ADCS1       EQU     07H			;Specified in p16f88.inc
ADCS0       EQU     06H			;Specified in p16f88.inc
go_done     equ     02h			;Specified in p16f88.inc
ADON        equ     00h			;Specified in p16f88.inc
CHS_0       EQU     03H   		;Specified in p16f88.inc 
CHS_1       EQU     04H			;Specified in p16f88.inc
CHS_2       EQU     05H			;Specified in p16f88.inc   as CHS2

Where I have written "Specified in p16f88.inc" you can delete this line, it is already defined in p16f88.inc


Note that port B should be written in the program as PORTB not portb otherwise the MPLAB assembler will not recognise it and throw up an error.

JimB
 
Last edited:
hi,
I cannot get your code to run using Oshonsoft.??
It also reports a missing delay_400us routine.

You could use this short program for testing your ADC and LCD it is written using the same pin out for the 16F88 as your program.

You can either program your 16F88 with the hex file or use MPLAB to assemble the ASM file.

I have included the Oshonsoft Basic program for reference.
 

Attachments

  • adc2lcd3.asm
    7.7 KB · Views: 141
  • adc2lcd3.hex
    2.2 KB · Views: 145
  • adc2lcd3.bas
    1.1 KB · Views: 136
Last edited:
hi

this piece of code will be very helpful. i am gonna try this and check where my code gone wrong.

I cannot get your code to run using Oshonsoft.??
It also reports a missing delay_400us routine.

by the way MPLAB simulator generate one message when i run this code which tells there is no stimulant file for ADRESL and ADRESH.

do you think i should try some other software tools for debugging? i will gladly accept suggestions and advices.

thanks
lipschutz
 
It also reports a missing delay_400us routine.

If you have not sorted this already, it is a simple mismatch of labels.

Code:
wait 15us
                  movlw      05h           ;
                  movwf      temp          ;
delay_15us        decfsz     temp,F        ;
                  goto       delay_40us    ;

JimB
 
hi

the problem is solved i.e the program is behaving as desired i.e some analog voltage is input through pin RA4, the uC read this voltage and show its corresponding BCD vlaue on LCD. but this performance is obtained only after employing sufficient delays. i guess i may not understand the datasheet properly. here is two point to ponder.

the INTRC is set to 4MHz.

1. according to my understanding the ad clock frequencey Fosc/8 should be sufficient ref: page 118, datasheet 16F88. but doing so generate an error of Tad less than 1.6us. until i reduce it to Fosc/64 the message keep on showing.

2. after each nibble transfer an 1ms delay is employed. without this delay characters become garbled on the lcd screen. but according to the data sheet HD44780 it should handle each character within 1us.

there might be two reasons, first i mistakenly set the clock frequency to much higher value. second something mistakenly interpreted from data sheet. i enclose my code. please advice if you find some reason for this findings.



Code:
                  processor 16F88
                 include <p16f88.inc>
                 __CONFIG    _CONFIG1,  _CP_OFF & _CCP1_RB0 & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_ON & _PWRTE_ON & _WDT_OFF & _INTRC_IO
                 __CONFIG    _CONFIG2,  _IESO_OFF & _FCMEN_OFF
;
;SPECIFY REGISTER_LCD
;
counter_l EQU     28H
counter_s EQU     29H
porta     EQU     05h
portb     EQU     06h
;
;bit_definition for lcd module
RS        EQU     0h
RW        EQU     1h
E         EQU     2h
;---------------------------
;
;Specify register_AD_MODULE
;
;
;bit_definition
;
an_ch       equ     4h
RP_1        EQU     06H
RP_0        EQU     05H
ADFM        EQU     07h
ADCS2       EQU     06h
ADCS1       EQU     07H
ADCS0       EQU     06H
go_done     equ     02h
ADON        equ     00h
CHS_0       EQU     03H    
CHS_1       EQU     04H
CHS_2       EQU     05H
;------------------------------
;*****VARIABLES DEFINITION*******
;(binary to bcd conversion)
adres_low  equ      27h
adres_high equ      26h
residue    equ      25h
bcd_0      equ      20h
bcd_1      equ      21h
bcd_2      equ      22h
bcd_3      equ      23h
temp       equ      24h
;---------CONSTANT DEFINITION---------
ascii_digi   equ      30H
ascii_char1  equ      60H
ascii_char2  equ      70H
;PROGRAM
                   org 00h
;
;OSCILLATOR FREQUENCY MODE SELECTION
                banksel   OSCCON           ;goto bank 1
                clrf      OSCTUNE          ;center frequency
                clrf      OSCCON           ;
                movlw     b'11101010'      ;
                movwf     OSCCON           ;
;*********clock frequency set to 4MHz****************
                banksel   TRISA            ;##back to bank0
;**************CONFIGURING PORTS*****************************
SETPORT         movlw     b'00010000'  ;
                movwf     TRISA        ;##
                ;tris      porta        ;
;RA0,1,2 are RS, RW and E respectively of lcd control 
;lines, RA4 is the analog input for AD converter
;
                movlw     b'00000000'  ;
                movwf      TRISB       ;##
;all of the 8 pins of portb are configured as outputs.
;Only the higher nibble will be used to send data 
;and command to the lcd module
; 
;****************CONFIGURING PORT ENDS***********************
                banksel   porta        ;
                bcf       porta,E      ;
;clearing E
;          
;************START OF INITIALISATION LCD***************
initio          movlw     14h          ;20
                movwf     temp         ;times
loop            call      delay_1ms    ;1ms
                decfsz    temp,F       ;
                goto      loop         ;
;A 20ms delay will give the lcd module enough time to 
;stabilize itself on power up 
;
                bcf       porta,RS     ;
                bcf       porta,RW     ;
                movlw     30h          ;
                movwf     portb        ;
                call      enab_lcd     ;
;the above segment sends the command
;RS RW DB7 DB6 DB5 DB4=000011 as required by the module
;ref:HD44780 datasheet 8-bit interface
                movlw     05h          ;
                movwf     temp         ;
five_ms         call      delay_1ms    ;
                decfsz    temp,F       ;
                goto      five_ms      ;
;5ms delay ref:HD44780 datasheet 8-bit interface
;
               ; bcf       porta,RS     ;
               ; bcf       porta,RW     ;
                movlw     30h          ;
                movwf     portb        ;
                call      enab_lcd     ;
;RS RW DB7 DB6 DB5 DB4=000011 as required by the module
;ref:HD44780 datasheet 8-bit interface
;                
                movlw     46h          ;
                movwf     temp         ;
delay_200us     decfsz    temp,F       ;
                goto      delay_200us  ;
;200us delay ref:HD44780 datasheet 8-bit interface
;
               ; bcf       porta,RS     ;
               ; bcf       porta,RW     ;
                movlw     30h          ;
                movwf     portb        ;
                call      enab_lcd     ;
;again the same bit sequence is sent as before
;i.e 000011; 8-bit interface
;
fucntion_set    bcf       porta,RS     ;               
                bcf       porta,RW     ;
                movlw     20h          ;
                movwf     portb        ;
                call      enab_lcd     ; 
                movlw     46h          ;200US delay as per HD44780 datasheet
                movwf     temp         ;
ALPHA           decfsz    temp,F       ;
                goto      ALPHA        ;
;DB4 is cleared to select 4-bit mode
;from now on every byte is sent in two nibbles and the 
;the higher nibble is sent first
;
               ; bcf       porta,RS     ;
               ; bcf       porta,RW     ;
                movlw     20h          ;
                movwf     portb        ;
                call      enab_lcd     ;
                movlw     00h          ;
                movwf     portb        ;
                call      enab_lcd     ;
                movlw     46h          ;incorporates a delay of 200us
                movwf     temp         ;ref: HD44780 datasheet
BETA            decfsz    temp,F       ;
                goto      BETA         ;
;the 'function set' command is sent again. the upper nibble 
;being the same as before and the lower nibble specifying 
;1-line and 5*7 pixel format i.e N=0 F=0 
;ref:HD44780 datasheet 
;
                movlw     20h          ;
                movwf     portb        ;
                call      enab_lcd     ;
                movlw     80h          ;
                movwf     portb        ;
                call      enab_lcd     ;
                movlw     46h          ;
                movwf     temp         ;delay 200us ref: HD44780 datasheet
GAMMA           decfsz    temp,F       ;
                goto      GAMMA        ;  
;'display off' command 00001000 ref:HD44780 datasheet
                movlw     00h          ;
                movwf     portb        ;
                call      enab_lcd     ;  
                movlw     60h          ;
                movwf     portb        ;
                call      enab_lcd     ;
                movlw     46h          ;delay 200us ref: HD44780 datasheet
                movwf     temp         ;
DELTA           decfsz    temp,F       ;
                goto      DELTA        ;
;'display on' command 00000001 ref:HD44780 datasheet                              
                movlw     00h          ;
                movwf     portb        ;
                call      enab_lcd     ;
                movlw     0C0h         ;
                movwf     portb        ;                
                call      enab_lcd     ;
                movlw     5h           ;delay 5ms ref: HD44780 datasheet
                movwf     temp         ;
MIKE            call      delay_1ms    ;
                decfsz    temp,F       ;
                goto      MIKE         ;
;'entry mode' set
;
;*****************END OF INITIALIZATION*********************
;
;
;***********config A/D module**************
;
;
;configure AD module                 
;
                 banksel  ANSEL         ;
                 clrf     ADRESL        ;##  
;bank1 selected
;
                  clrf     ANSEL         ;
                  bsf      ANSEL,04H     ;
;RA4 is selected as analog channel
;                                
;RA4 set as analog input pin
;
                  clrf     ADCON1        ;
                  movlw    b'11000000'   ;
                  movwf    ADCON1        ;
;AD result is right justified
;AD clock divide by 2 enabled?
;setting reference voltage as Vdd and Vss
;
                  banksel  ADCON0        ;
                  movlw    b'10100000'   ;
                  movwf    ADCON0        ;
                  clrf     ADRESH        ;##
;back to bank0
;RA4 selected as analog input:configuring ADCON0
;Fosc/8 is set as AD conversion clock 
;
                  bsf      ADCON0,ADON   ;
;AD module turned on
;
;wait 25us acquisition time
                  movlw    08h           ;
                  movwf    temp          ;
delay_25us        decfsz   temp,F        ;
                  goto     delay_25us    ;
;acquisition time elapsed              
;
                  bsf      ADCON0,go_done ;                  
;AD conversion starts now
;
CHECK             btfsc    ADCON0,go_done ;
                  goto     CHECK          ;
;polling of go_done bit in
;read ad result register pair
                  banksel  ADRESL         ;read and transfer
                  movf     ADRESL,W       ;AD resul to location
                  banksel  ADRESH         ;adres_low and adres_high
                  movwf    adres_low      ;respectively
                  movf     ADRESH,W       ;
                  movwf    adres_high     ;
                  bcf      ADCON0,ADON    ;AD module turned off
;**********BINARY TO BCD CONVERSION CODE************
                  clrf     bcd_0       ;
                  clrf     bcd_1       ;
                  clrf     bcd_2       ;
                  clrf     bcd_3       ;
                  clrf     residue     ;
                  clrf     temp        ; clearing the registers finishes                   
ONE               movf    adres_high,F ;
                  btfss   STATUS,Z     ;
                  goto    TWO          ;
                  goto    SIX          ;
;
;
TWO               movf    adres_high,0 ;
                  sublw   01h          ;
                  btfss   STATUS,Z     ;
                  goto    THREE        ;
                  movlw   02h          ;
                  movwf   bcd_2        ;
                  movlw   38h          ;
                  movwf   residue      ;
                  goto    SIX          ;
;
;
THREE             movf    adres_high,0 ;
                  sublw   02h          ;
                  btfss   STATUS,Z     ;
                  goto    FOUR         ;
                  movlw   05h          ;
                  movwf   bcd_2        ;
                  movlw   0ch          ;
                  movwf   residue      ;
                  goto    SIX          ;
;
;
FOUR              movlw   07h          ;
                  movwf   bcd_2        ;
                  movlw   44h          ;
                  movwf   residue      ;
;
;
SIX               movlw   64h          ;
                  subwf   adres_low,W  ;
                  movwf   temp         ;
                  btfsc   STATUS,C     ;
                  goto    SIX_A        ;
                  movf    residue,W    ;
                  addwf   adres_low,F  ;
                  goto    SEVEN        ;
;
SIX_A             incf    bcd_2,F      ;
                  movf    residue,W    ;
                  addwf   temp,F       ;
                  movf    temp,W       ;
                  movwf   adres_low    ;
;
;
SEVEN             movlw   64h          ;
                  subwf   adres_low,W  ;
                  movwf   temp         ;
                  btfsc   STATUS,C     ;
                  goto    SEVEN_A      ;
                  goto    EIGHT        ;
;
SEVEN_A           incf    bcd_2,F      ;
                  movf    temp,W       ;
                  movwf   adres_low    ;
                  goto    SEVEN        ;
                  
                  
;
;
EIGHT             movlw   0ah          ;
                  subwf   bcd_2,W      ;
                  movwf   temp         ;
                  btfsc   STATUS,C     ;
                  goto    EIGHT_A      ;                 
                  goto    NINE         ;
;
EIGHT_A           incf    bcd_3,F      ;
                  movf    temp,W       ;
                  movwf   bcd_2        ; 
                  
;
;
NINE              movlw  0ah          ;
                  subwf  adres_low,W  ;
                  movwf  temp         ;
                  btfsc  STATUS,C     ;
                  goto   NINE_A       ;
                  goto   TEN          ;                 
;
NINE_A            incf    bcd_1,F      ;
                  movf    temp,F       ;
                  movwf   adres_low    ;
                  goto    NINE         ;
;
;
TEN               movf   adres_low,W  ;
                  movwf  bcd_0        ;
;FIN               GOTO   FIN          ;
;
;**************DISPLAY BCD ON LCD*******************
;                  
                  bsf    porta, RS    ;
                  bcf    porta, RW    ;
show_bcd_3        movlw  ascii_digi   ;
                  movwf  portb        ;
                  call   enab_lcd     ;
                  call   delay_1ms    ;
                  swapf  bcd_3,W      ;
                  movwf  portb        ;
                  call   enab_lcd     ;
                  call   delay_1ms    ; 
;
show_bcd_2        movlw  ascii_digi   ;
                  movwf  portb        ;
                  call   enab_lcd     ;
                  call   delay_1ms    ;
                  swapf  bcd_2,W      ;
                  movwf  portb        ;
                  call   enab_lcd     ;
                  call   delay_1ms    ;
;
show_bcd_1        movlw  ascii_digi   ;
                  movwf  portb        ;
                  call   enab_lcd     ;
                  call   delay_1ms    ;
                  swapf  bcd_1,W      ;
                  movwf  portb        ;
                  call   enab_lcd     ;
                  call   delay_1ms    ;
;
show_bcd_0        movlw  ascii_digi   ;
                  movwf  portb        ;
                  call   enab_lcd     ;
                  call   delay_1ms    ;
                  swapf  bcd_0,W      ;
                  movwf  portb        ;
                  call   enab_lcd     ;
                  call   delay_1ms    ;
;
show_p            movlw  ascii_char2  ;
                  movwf  portb        ;
                  call   enab_lcd     ;
                  call   delay_1ms    ;
                  movlw  0h           ;
                  movwf  portb        ;
                  call   enab_lcd     ;
                  call   delay_1ms    ;
;
show_p2           movlw  ascii_char2  ;
                  movwf  portb        ;
                  call   enab_lcd     ;
                  call   delay_1ms    ;
                  movlw  0h           ;
                  movwf  portb        ;
                  call   enab_lcd     ;
                  call   delay_1ms    ;
;
show_b            movlw  ascii_char1  ;
                  movwf  portb        ;
                  call   enab_lcd     ;
                  call   delay_1ms    ;
                  movlw  20H          ;
                  movwf  portb        ;
                  call   enab_lcd     ;
                  call   delay_1ms    ;
fin               goto   fin          ;
;************************************************                  
;
;SUBROUTINES
;
enab_lcd        bsf       porta,E      ;
                nop                    ;
                bcf       porta,E      ;
                nop                    ;
               return                  ;     
;data is transferred when E goes from high to low
;
delay_1ms       movlw     0ffh         ;
                movwf     counter_s    ;
point           nop                    ;
                decfsz    counter_s,F  ;
                goto      point        ;
                return                 ;
                      end              ;
;


thanks
lipschutz
 
hi,
Your delays are too long also why is the 'fin' trap used, the program locks up!

look a this image.

I will run it again later today, let you know.

E.
 

Attachments

  • AAesp01.gif
    AAesp01.gif
    67.8 KB · Views: 176
hi

yes my delays are far too long, two reasons: first my problem is not time critical and it measures one sample at a time, basically it measures opacity of a paper strip.and this is why the program is locked to a point. i think the same thing could have been done without locking it to that line. a reset MCLR push switch will be implemented to get as many run as one wants. but do you suggest something that would be better?

Second i was desperate to see my program into work so used the subroutine of 1ms knowing that it would be sufficient anyway.

1. according to my understanding the ad clock frequencey Fosc/8 should be sufficient ref: page 118, datasheet 16F88. but doing so generate an error of Tad less than 1.6us. until i reduce it to Fosc/64 the message keep on showing.

i could not figure out this point so far. also are there any way to know the internal clock frequency using oscilloscope i.e manually? i mean how could i be sure it is running on 4MHz? i am a bit skeptic about the oscillator frequency settings code resides at the begnning of the code.

thanks for your help
lipschutz
 
hi,
I have looked thru and tried your code, it will not work correctly for me.

The BCD conversion routine is slow, there a many assembly subroutines available that are far more efficient.

For the 4MHz check, write a simple test loop that toggles a PORT pin every 1 sec. If you have a scope you could measure it or use a 390R resistor and a LED on that toggled pin.
 
hi

BCD conversion code is entirely written by me, that's why inexperience showed up.

For the 4MHz check, write a simple test loop that toggles a PORT pin every 1 sec. If you have a scope you could measure it or use a 390R resistor and a LED on that toggled pin.

and i will use this technique to test clock frequency right away.

thanks
lipschutz
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top