Erwin_Macaraig
New Member
I am using PicBasic compiler to program PIC16F877 PIC Microcontroller. My program just cant seem to work. I am trying to convert an analog signal to a digital signal using its 10-bit analog-to-digital converter. I am using a 16X2 LCD. I am trying to display to the LCD the converted digital value of my analog input signal but my program doesnt seem to work. In my program, i tried to display the value of ADRESH and ADRESL. Here is my code:
Symbol TRISA = $85
Symbol TRISB = $86
Symbol TRISC = $87
Symbol TRISD = $88
Symbol TRISE = $89
Symbol PORTA = $05
Symbol PORTB = $06
Symbol PORTC = $07
Symbol PORTD = $08
Symbol PORTE = $09
Symbol ADRESH = $1e
Symbol ADRESL = $9e
Symbol ADCON0 = $1f
Symbol ADCON1 = $9f
Symbol ADCVal = W0
Symbol temp = b2
Symbol buff = b3
Symbol hiByte = b4
Symbol loByte = b5
Symbol valHolder = b6
Symbol product = b9
Symbol var = W6
Symbol LCDadd = b14
init: 'initialization of ports
Poke TRISA,%11111111 'sets PORTA as inputs
Poke TRISE,%11111111 'sets PORTE as inputs
Poke TRISB,0 'sets PORTB as outputs
Poke TRISD,0 'sets PORTD as outputs
Poke TRISE,0
'*analog to digital algorithm algorithm
'*1. Configure analog pins/voltage reference and digital i/o(ADCON1)
'*2. Select A/D input channel (ADCON0)
'*3. Select A/D conversion clock(ADCON0)
'*4. Turn on A/D module (ADCON0)
'*5. Wait the required acquisition time
'*6. Start conversion
'*6.1. Set GO/DONE bit (ADCON0)
'*7. Wait for A/D conversion to complete by polling
'* for the GO/DONE bit to be cleared(interrupts disabled)
'* or waiting for the a/d interrupt
'*8. Read a/d result register
Call initLCD
main:
asm
clrf _valHolder
movlw 128 ;right justified, Fosc/8, 8/0
movwf Adcon1 ;128 for right justified, 0 for left justified
bcf Adcon0,3 ;sets analog channel
bcf Adcon0,4 ;channel 0 selected
bcf Adcon0,5
bcf Adcon0,7 ;sets Fosc/8
bsf Adcon0,6 ;sets Fosc/8
bsf Adcon0,0 ;A/D converter is powered up
Call _delay ;acquisition time
bsf Adcon0,2 ;starts A/D conversion
Call _delay ;acquisition time
Call _delay
bcf Adcon0,2 ;conversion complete
clrf _hiByte
clrf _loByte
movf Adresl,0
movwf _hiByte
movf Adresh,0
movwf _loByte
bcf Adcon0,0 ;A/D converter is powered down
endasm
LCDadd = 64
Call gotoLine
valHolder = hiByte/100 + 48
Call putchar 'hundreds
valHolder = hiByte/10
valHolder = valHolder//10
valHolder = valHolder + 48
Call putchar 'tens
valHolder = hiByte//10
valHolder = valHolder + 48 'ones
LCDadd = 69
Call gotoLine
valHolder = loByte/100 + 48
Call putchar 'hundreds
valHolder = loByte/10
valHolder = valHolder//10 + 48
Call putchar 'tens
valHolder = loByte//10 + 48
Call putchar 'ones
Pause 500
GoTo main
asm
_delay
movlw 255 ;
movwf _temp
movlw 2
movwf _buff
loop
decfsz _temp
GoTo loop
decfsz _buff
GoTo loop
Return
_delayLCD
movlw 255
movwf _temp
movlw 6
movwf _buff
loop2
decfsz _temp
GoTo loop2
decfsz _buff
GoTo loop2
Return
_initLCD
bcf PortD,7 ;PortD,7 is LCDrw
bcf PortD,6 ;PortD,6 is LCDrs
movlw 56
movwf PortB ;Set/Reset Interface length
Call _delay
bsf PortD,5 ;LCD clock on
Call _delay
bcf PortD,5 ;LCD clock off
movlw 6 ;set move cursor direction
movwf PortB
Call _delay
bsf PortD,5 ;LCD clock on
Call _delay
bcf PortD,5 ;LCD clock off
movlw 12 ;blink and cursor is set off
movwf PortB
Call _delay
bsf PortD,5 ;LCD clock on
Call _delay
bcf PortD,5 ;LCD clock off
movlw 1
movwf PortB
Call _delay
bsf PortD,5 ;LCD clock on
Call _delay
bcf PortD,5 ;LCD clock off
movlw 2 ;sets cursor to home cell
movwf PortB
Call _delay
bsf PortD,5 ;LCD clock on
Call _delay
bcf PortD,5 ;LCD clock off
Return
_putchar
bcf PortD,7
bsf portD,6
movf _valHolder,0
movwf PortB
Call _delay
bsf PortD,5 ;LCD clock on
Call _delay
bcf PortD,5 ;LCD clock off
Return
_clearLCD
bcf PortD,7
bcf PortD,6
movlw 1
movwf PortB
Call _delay
bsf PortD,5 ;LCD clock on
Call _delay
bcf PortD,5 ;LCD clock off
Return
_gotoLine
bcf PortD,7
bcf PortD,6
clrw
movlw 128
iorwf _LCDadd,0
movwf PortB
Call _delay
bsf PortD,5 ;LCD clock on
Call _delay
bcf PortD,5 ;LCD clock off
Return
endasm
End
------------
Can anyone care to comment on my source code, specially in the ADC part. I used in-line assembly programming together with PICbasic.
Your help will be much appreciated!
Symbol TRISA = $85
Symbol TRISB = $86
Symbol TRISC = $87
Symbol TRISD = $88
Symbol TRISE = $89
Symbol PORTA = $05
Symbol PORTB = $06
Symbol PORTC = $07
Symbol PORTD = $08
Symbol PORTE = $09
Symbol ADRESH = $1e
Symbol ADRESL = $9e
Symbol ADCON0 = $1f
Symbol ADCON1 = $9f
Symbol ADCVal = W0
Symbol temp = b2
Symbol buff = b3
Symbol hiByte = b4
Symbol loByte = b5
Symbol valHolder = b6
Symbol product = b9
Symbol var = W6
Symbol LCDadd = b14
init: 'initialization of ports
Poke TRISA,%11111111 'sets PORTA as inputs
Poke TRISE,%11111111 'sets PORTE as inputs
Poke TRISB,0 'sets PORTB as outputs
Poke TRISD,0 'sets PORTD as outputs
Poke TRISE,0
'*analog to digital algorithm algorithm
'*1. Configure analog pins/voltage reference and digital i/o(ADCON1)
'*2. Select A/D input channel (ADCON0)
'*3. Select A/D conversion clock(ADCON0)
'*4. Turn on A/D module (ADCON0)
'*5. Wait the required acquisition time
'*6. Start conversion
'*6.1. Set GO/DONE bit (ADCON0)
'*7. Wait for A/D conversion to complete by polling
'* for the GO/DONE bit to be cleared(interrupts disabled)
'* or waiting for the a/d interrupt
'*8. Read a/d result register
Call initLCD
main:
asm
clrf _valHolder
movlw 128 ;right justified, Fosc/8, 8/0
movwf Adcon1 ;128 for right justified, 0 for left justified
bcf Adcon0,3 ;sets analog channel
bcf Adcon0,4 ;channel 0 selected
bcf Adcon0,5
bcf Adcon0,7 ;sets Fosc/8
bsf Adcon0,6 ;sets Fosc/8
bsf Adcon0,0 ;A/D converter is powered up
Call _delay ;acquisition time
bsf Adcon0,2 ;starts A/D conversion
Call _delay ;acquisition time
Call _delay
bcf Adcon0,2 ;conversion complete
clrf _hiByte
clrf _loByte
movf Adresl,0
movwf _hiByte
movf Adresh,0
movwf _loByte
bcf Adcon0,0 ;A/D converter is powered down
endasm
LCDadd = 64
Call gotoLine
valHolder = hiByte/100 + 48
Call putchar 'hundreds
valHolder = hiByte/10
valHolder = valHolder//10
valHolder = valHolder + 48
Call putchar 'tens
valHolder = hiByte//10
valHolder = valHolder + 48 'ones
LCDadd = 69
Call gotoLine
valHolder = loByte/100 + 48
Call putchar 'hundreds
valHolder = loByte/10
valHolder = valHolder//10 + 48
Call putchar 'tens
valHolder = loByte//10 + 48
Call putchar 'ones
Pause 500
GoTo main
asm
_delay
movlw 255 ;
movwf _temp
movlw 2
movwf _buff
loop
decfsz _temp
GoTo loop
decfsz _buff
GoTo loop
Return
_delayLCD
movlw 255
movwf _temp
movlw 6
movwf _buff
loop2
decfsz _temp
GoTo loop2
decfsz _buff
GoTo loop2
Return
_initLCD
bcf PortD,7 ;PortD,7 is LCDrw
bcf PortD,6 ;PortD,6 is LCDrs
movlw 56
movwf PortB ;Set/Reset Interface length
Call _delay
bsf PortD,5 ;LCD clock on
Call _delay
bcf PortD,5 ;LCD clock off
movlw 6 ;set move cursor direction
movwf PortB
Call _delay
bsf PortD,5 ;LCD clock on
Call _delay
bcf PortD,5 ;LCD clock off
movlw 12 ;blink and cursor is set off
movwf PortB
Call _delay
bsf PortD,5 ;LCD clock on
Call _delay
bcf PortD,5 ;LCD clock off
movlw 1
movwf PortB
Call _delay
bsf PortD,5 ;LCD clock on
Call _delay
bcf PortD,5 ;LCD clock off
movlw 2 ;sets cursor to home cell
movwf PortB
Call _delay
bsf PortD,5 ;LCD clock on
Call _delay
bcf PortD,5 ;LCD clock off
Return
_putchar
bcf PortD,7
bsf portD,6
movf _valHolder,0
movwf PortB
Call _delay
bsf PortD,5 ;LCD clock on
Call _delay
bcf PortD,5 ;LCD clock off
Return
_clearLCD
bcf PortD,7
bcf PortD,6
movlw 1
movwf PortB
Call _delay
bsf PortD,5 ;LCD clock on
Call _delay
bcf PortD,5 ;LCD clock off
Return
_gotoLine
bcf PortD,7
bcf PortD,6
clrw
movlw 128
iorwf _LCDadd,0
movwf PortB
Call _delay
bsf PortD,5 ;LCD clock on
Call _delay
bcf PortD,5 ;LCD clock off
Return
endasm
End
------------
Can anyone care to comment on my source code, specially in the ADC part. I used in-line assembly programming together with PICbasic.
Your help will be much appreciated!