PIC16F886 basic programming

Discussion in 'Embedded Systems and Microcontrollers' started by abhisheksaini, Aug 26, 2009.

  1. abhisheksaini

    Thread Starter New Member

    Aug 26, 2009
    2
    0
    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.

    Code ( (Unknown Language)):
    1. #include <p16F886.inc>
    2.     __config _CONFIG1, _DEBUG_OFF & _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_ON & _PWRTE_ON & _WDT_OFF & _INTOSCIO
    3.     __config _CONFIG2, _WRT_OFF & _BOR40V
    4.  
    5. org 0
    6.  
    7. Start:
    8.     bsf 03h,5  ;to bank 1
    9.  
    10.     movlw 00000000b  ;put 00000000 into W register 
    11.  
    12.     movwf 85h ;Move 00000000 onto TRISA (all pins set to output)
    13.    
    14.     bcf 03h,5 ;back to bank 0
    15.  
    16.     movlw 11111111b      ;write 11111111 into w register
    17.  
    18.     movwf 05h      ;move the contents of register w to Port A
    19.  
    20.  
    21. end
    [​IMG]
     
  2. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    You should turn of the comparators and set the port to digital.
    See the A/D section of the datasheet.
    ADCON1 & CMCON will be of interest.
     
  3. abhisheksaini

    Thread Starter New Member

    Aug 26, 2009
    2
    0
    Hi,

    Thanks for your reply.

    I tried to fix the code.

    But when I enter

    Code ( (Unknown Language)):
    1. movlw    00000000b
    2. movwf    CMCON    
    to my code....I get the following error message...

    Code ( (Unknown Language)):
    1. Error[113]   C:\USERS\DIRECTORY\FILE.asm 11 : Symbol not previously defined (CMCON)
    2.  
    Here is the code I worked on....


    Code ( (Unknown Language)):
    1.  
    2. #include <p16F886.inc>
    3.  
    4.     __config _CONFIG1, _DEBUG_OFF & _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_ON & _PWRTE_ON & _WDT_OFF & _INTOSCIO
    5.  
    6.     __config _CONFIG2, _WRT_OFF & _BOR40V
    7.      
    8. org 0
    9.  
    10. Start:
    11.  
    12.     bsf 03h,5  ;Go to Bank 1
    13.  
    14. movlw    00000000b
    15. movwf    CMCON            
    16.  
    17. movlw 11111111b
    18. movwf ADCON1
    19.  
    20.  
    21.     movlw 00000000b                  ;Put 00000 into W
    22.  
    23.  
    24.  
    25. movwf 85h                  ;Move 00000 onto TRISA – all pins set to output
    26.  
    27.  
    28. bcf                   03h,5 ;Come back to Bank 0
    29.  
    30.  
    31.  
    32.  
    33. movlw 11111111b    
    34. movwf 05h    
    35.  
    36. end
    37.  
    38.  
    39.  
     
  4. Dragonblight

    Active Member

    Aug 19, 2009
    35
    0
    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!
     
  5. elal

    New Member

    Dec 19, 2008
    6
    0
    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.
     
  6. kammenos

    Active Member

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


    Code ( (Unknown Language)):
    1.  
    2.                 bank1  
    3.                 movlw b'11111000'        ; OPTION_REG setup
    4.                 movwf option_reg              
    5.                 clrf pie1                ;
    6.                 clrf pie2                ; No peripherial interrupt
    7.                 clrf ansel                ; No analog to digital conversion
    8.                 clrf txsta                ;Disable AUSART
    9.                 Bank0
    10.                 clrf intcon                ;No Interrupt control
    11.                 clrf ccp1con            ;Capture/Compare/PWM Disabled
    12.                 clrf t1con                ;No TIMER 1
    13.  
    14.                 ;Watchdog Timer
    15.                 bank2  
    16.                 movlw b'00010111'
    17.                 movwf WDTCON
    18.                 bcf    WDTCON,SWDTEN        ;Disable watchdog timer
    19.                 bank1
    20.                 ;PSA must be to WDA (bsf OPTION_REG,psa)
    21.                 bcf OPTION_REG,PS2
    22.                 bcf OPTION_REG,PS1
    23.                 bsf OPTION_REG,PS0
    24.                 bank0
    25.                 ;General
    26.                 clrf rcsta                ;Disable AUSART receive
    27.                 clrf adcon0                ;No analog to digital conversion
    28.  
     
  7. kammenos

    Active Member

    Aug 3, 2008
    127
    0
  8. poniwil

    New Member

    Nov 15, 2013
    1
    0
    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
     
  9. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,515
    2,369
    Hopefully he has solved it after 3 years!;)
    Max.
     
    tshuck likes this.
  10. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Simon sez do not do that.

    Simon also sez use the banksel macro:
    Code ( (Unknown Language)):
    1.  
    2. banksel trisa
    3. crf trisa
    4. banksel ansel
    5. clrf ansel
    6. banksel anselh
    7. clrf anselh
    8. banksel porta
    9. movlwf 0xff
    10. movwf porta
    11.  
    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.
     
Loading...