PIC16F886 basic programming

Thread Starter

abhisheksaini

Joined Aug 26, 2009
2
Hi,

I am learning PIC programming in assembly.

I am trying to light a led using PIC16F886.

I am able to light the led only on PIN 6 (RA4).
I have written the program so it lights all the pins of Port A.
Please have a look at the code below and let me know what I am doing wrong.
I am also attaching PIC16F886 diagram for reference.

Rich (BB code):
#include <p16F886.inc>
	__config _CONFIG1, _DEBUG_OFF & _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_ON & _PWRTE_ON & _WDT_OFF & _INTOSCIO
	__config _CONFIG2, _WRT_OFF & _BOR40V

org 0

Start:
	bsf 03h,5  ;to bank 1 

	movlw 00000000b  ;put 00000000 into W register	

	movwf 85h ;Move 00000000 onto TRISA (all pins set to output)
	
	bcf 03h,5 ;back to bank 0

	movlw 11111111b      ;write 11111111 into w register

	movwf 05h      ;move the contents of register w to Port A


end
 

Thread Starter

abhisheksaini

Joined Aug 26, 2009
2
Hi,

Thanks for your reply.

I tried to fix the code.

But when I enter

Rich (BB code):
movlw    00000000b
movwf    CMCON
to my code....I get the following error message...

Rich (BB code):
Error[113]   C:\USERS\DIRECTORY\FILE.asm 11 : Symbol not previously defined (CMCON)
Here is the code I worked on....


Rich (BB code):
#include <p16F886.inc>

    __config _CONFIG1, _DEBUG_OFF & _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_ON & _PWRTE_ON & _WDT_OFF & _INTOSCIO

    __config _CONFIG2, _WRT_OFF & _BOR40V
     
org 0

Start:

    bsf 03h,5  ;Go to Bank 1 

movlw    00000000b
movwf    CMCON            

movlw 11111111b
movwf ADCON1


    movlw 00000000b                  ;Put 00000 into W



movwf 85h                  ;Move 00000 onto TRISA – all pins set to output


bcf                   03h,5 ;Come back to Bank 0




movlw 11111111b     
movwf 05h    

end
 

Dragonblight

Joined Aug 19, 2009
35
in the data sheet, it looks like the CMCON registers are in the third bank of registers at 0x87 to 0x89- you'll need to use the STATUS register to swap over to the third bank- there's plenty of tutorials out there that address using the STATUS register- good luck!
 

elal

Joined Dec 19, 2008
6
I dont think the CMCON register exists in the PIC16F886, thats why is giving you an error, to set PORTA to digital mode you need to use ANSEL register.
You just need to use the ADCON registers if you working with the ADC.

Have look at the datasheet in the PORTA section its everything there.
 

kammenos

Joined Aug 3, 2008
127
I use the following code as part of an inialising sub routine for the F88. It may work for the 886 as well.


Rich (BB code):
                bank1  
                movlw b'11111000'        ; OPTION_REG setup 
                movwf option_reg              
                clrf pie1                ; 
                clrf pie2                ; No peripherial interrupt 
                clrf ansel                ; No analog to digital conversion 
                clrf txsta                ;Disable AUSART 
                Bank0 
                clrf intcon                ;No Interrupt control 
                clrf ccp1con            ;Capture/Compare/PWM Disabled 
                clrf t1con                ;No TIMER 1 
 
                ;Watchdog Timer 
                bank2  
                movlw b'00010111' 
                movwf WDTCON 
                bcf    WDTCON,SWDTEN        ;Disable watchdog timer 
                bank1 
                ;PSA must be to WDA (bsf OPTION_REG,psa) 
                bcf OPTION_REG,PS2 
                bcf OPTION_REG,PS1 
                bsf OPTION_REG,PS0 
                bank0 
                ;General 
                clrf rcsta                ;Disable AUSART receive 
                clrf adcon0                ;No analog to digital conversion
 

poniwil

Joined Nov 15, 2013
1
ist because you have to configure the ANSEL and ANSELH registers, they are in the bank 3

try this,

bsf status,rp0
crf trisa
bsf status,rp1
clrf ansel
clrf anselh
bcf status,rp0
bcf status,rp1
movlwf 0xff
movwf porta

loop
goto lopp
end
 

ErnieM

Joined Apr 24, 2011
8,377
ist because you have to configure the ANSEL and ANSELH registers, they are in the bank 3

try this,

bsf status,rp0
crf trisa
bsf status,rp1
clrf ansel
clrf anselh
bcf status,rp0
bcf status,rp1
movlwf 0xff
movwf porta

loop
goto lopp
end
Simon sez do not do that.

Simon also sez use the banksel macro:
Rich (BB code):
banksel trisa
crf trisa
banksel ansel
clrf ansel 
banksel anselh
clrf anselh
banksel porta
movlwf 0xff
movwf porta
Generally you want the assembler (or compiler) to do as much grunt work as you can hand it; it's a computer and very good at those tasks. Way better then you or I am.

Once you are sure the routine works you can look to remove redundant banksel invocations.
 
Top