PORTA still doesn't respond

Discussion in 'Embedded Systems and Microcontrollers' started by theBuddha, Sep 5, 2009.

  1. theBuddha

    Thread Starter New Member

    Sep 5, 2009
    3
    0
    I am an absolute novice to assembly programming on PIC16F877A(or, any μC for that matter!). I am using microchip's ICD2 to debug this first 'hello-pic-world' program:

    include <p16f877.inc>

    BCF STATUS, RP0 ;
    BCF STATUS, RP1 ; Bank0
    CLRF PORTA ; Initialize PORTA by
    ; clearing output
    ; data latches
    BSF STATUS, RP0 ; Select Bank 1
    MOVLW 0x00 ; Configure all pins
    MOVWF ADCON1 ; as digital outputs
    MOVLW 0x00 ; Value used to
    ; initialize data
    ; direction
    MOVWF TRISA ;


    MOVLW 01h
    MOVWF PORTA

    START

    INCF PORTA,1

    GOTO START

    END


    I am trying to actually see how PORTA behaves by stepping over each line of the code(and that's why the attempt to increment PORTA values one-by-one). I have connected the PORTA lines(pins 2-5 of PIC16F877A-I/P) to 4 LEDs. On the debugger, everything looked fine yesterday but today, PORTA seems to be too stubborn to change!

    Can anyone rescue me please?
     
  2. elal

    New Member

    Dec 19, 2008
    6
    0
    Hi, try this .

    LIST p=16F877a ;tell assembler what chip we are using
    include <p16f877a.inc>
    ;Put your configuration bits here
    cblock 0x20 ;start of general purpose registers
    x
    endc
    org 0x0000 ;org sets the origin
    ;this is where the program starts running
    bsf STATUS, RP0 ;select bank 1
    MOVLW 0x06 ; Configure all pins
    MOVWF ADCON1 ; To digital
    movlw 0x00
    movwf TRISA
    bcf STATUS, RP0 ;select bank 0
    movwf x
    START
    incf x
    movfw x
    movwf PORTA

    GOTO START

    END
     
  3. theBuddha

    Thread Starter New Member

    Sep 5, 2009
    3
    0
    Hello Alberto and Elal,

    Thank you very much for your support and time.

    Indeed, I tried configuring the PORT as just digital output(by making ADCON1 = 0; CMCON = %00000011) but it still didn't work. I might now have to perhaps try with ADCON = 7 as well. I try doing that. I also tried using PORTB as this doesn't seem to be shared for common purposes but in vain.

    As a matter of fact, when I use C-code(compiled by Hitech's picc std compiler), things work pretty fine, I mean, the LEDs are dancing to my tunes! But again, I see that the port-values aren't updated in the SFR view of MPLAB though I use it in the ICD2 debug mode! This is quite weird to me but I don't know what's going on! The LEDs driven by the port glow but the SFR for the port shows '0'.

    @Alberto: Thank you very much for such a detailed walk through the basics - my salute to your patience(I see you are a senior member to this forum).

    @Elal: Thank you very much for the modifications and the corresponding comments. I haven't tried your code yet - shall update as soon as I do it...
     
  4. elal

    New Member

    Dec 19, 2008
    6
    0
    Did you check your configuration bits?
    Testing code with a simulator is not alawys the same then the real thing.
    Set OSC to HS if you are using a crystal oscillator with more then 8MHZ
    Clear WDT, PUT, BODEN, LVP, CPD, WRT_ENABLE and CP.
    It will look like this __config 0x3F3A ,insert this code in the third line of your code like this.

    LIST p=16F877 ;tell assembler what chip we are using
    include <p16f877.inc>
    __config 0x3F3A ; configuration bits
    cblock 0x20 ;start of general purpose registers

    NB If you working with MPLAB assembler you need to put two underscore charaters before config. __config



    Here is the complete code


    LIST p=16F877 ;tell assembler what chip we are using
    include <p16f877.inc>
    __config 0x3F3A
    cblock 0x20 ;start of general purpose registers
    x
    endc
    org 0x0000 ;org sets the origin
    ;this is where the program starts running
    bsf STATUS, RP0 ;select bank 1
    MOVLW 0x06 ; Configure all pins
    MOVWF ADCON1 ; To digital
    movlw 0x00
    movwf TRISA
    bcf STATUS, RP0 ;select bank 0
    movwf x
    START
    incf PORTA
    GOTO START
    END


    With this code and depending on the speed of your oscillator you gone see
    the LEDs allways on, you need to slow down the process, if you want to see the LEDs incrementing you need to insert a delay routine before
    incf PORTA .
     
Loading...