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.

PIC16F877 ADC programming with PicBasic Compiler

Status
Not open for further replies.

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!
 
the problem is no one knows what pic basic is sending to the pic...
lemmy ask , does pic basic work like a compiled basic ? or interpreted basic?
 
also since you wrote most of the code in assembler.,
why not write it all in assembly??
 
I really would want to do that but I am just a newbie to microcontroller programming and I am having a hard time coding a multiplication subroutine in pic asm. Maybe you can suggest a a reading material or a website wherein I can learn how to multiply 8 and 16 bit variables..

Thank you so much!!!
 
Erwin_Macaraig said:
I really would want to do that but I am just a newbie to microcontroller programming and I am having a hard time coding a multiplication subroutine in pic asm. Maybe you can suggest a a reading material or a website wherein I can learn how to multiply 8 and 16 bit variables..

You might also try my PIC tutorials, although I don't have any maths routines in them - the ones I usually use are 16 bit, from the PIC Source Book, available free online at https://www.dontronics.com/see.html.
 
variables

Thank you guys!! But I still have a question, here goes, PIC16F877 has 4 banks. And if i happened to be in Bank 0 and declared a variable temp there at 20h and I switch banks say I switch to Bank1, From Bank1 do i have to switch to Bank0 for me to access the variable temp?? I believe that PIC16F877 has four banks each of which has its own general purpose register for variable storage puposes.
In general, do I really have to switch banks to where I declared the variables for me to access them???

Thanks again!!
 
Re: variables

Erwin_Macaraig said:
Thank you guys!! But I still have a question, here goes, PIC16F877 has 4 banks. And if i happened to be in Bank 0 and declared a variable temp there at 20h and I switch banks say I switch to Bank1, From Bank1 do i have to switch to Bank0 for me to access the variable temp?? I believe that PIC16F877 has four banks each of which has its own general purpose register for variable storage puposes.
In general, do I really have to switch banks to where I declared the variables for me to access them???

Yes you do, but it's rarely required, there's usually more than enough GPR's in bank 0 - so I just use those. If you're needing more GPR's you're probably doing things the hard way, and you should rethink it - unless you're trying to use the GPR's as a buffer?.
 
My main concern is that ADRESH and ADRESL resides between two different Banks. Maybe my algorithm will just be to store the value of ADRESL or ADRESH to the accumulator and then switch to Bank0 to store the value to the designated variable. Is it possible that way??

Thank you again!
 
Erwin_Macaraig said:
My main concern is that ADRESH and ADRESL resides between two different Banks. Maybe my algorithm will just be to store the value of ADRESL or ADRESH to the accumulator and then switch to Bank0 to store the value to the designated variable. Is it possible that way??

Check my analogue tutorial, it shows you exactly how to do it!. Notice that the BANKSEL assembler directive is ONLY around the read from ADRESL, it reverts back to bank 0 to write to the variable.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top