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.

help with 16f877a

Status
Not open for further replies.

o2thesama

New Member
Hi all,

I'm new to this forum as well as microcontrollers and am looking for some guidance/tips on the learning process. I've purchased a very cheap development kit consisting of leds, number pads, etc etc etc. It also came with a 16f877a, which I find a bit complex to start with and I am having problems.

The led's are connected to port d, where current flows from power source, thru resistor, thru led, into port d pins (so port d must be grounded to power leds). I know everything works because if I program the pic with the sample program provided in C, the leds work.

However I am trying to do a simple test in assembly and am unable to set any of the pins on any ports high or low, and I've played around with this for a week. No matter what I set the pins (on any port), nothing is affected (port RD1 is always about 3.5V and the supply is 5V). I've tried _RC_OSC and _HS_OSC (its using a 20mhz crystal).

If anyone can point out what the problem is, that would be awesome, thanks!

My program:
----------------------------------------------------------------------------------

list p=16f877a ; list directive to define processor
#include <p16f877a.inc> ; processor specific variable definitions

__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_OFF & _HS_OSC & _WRT_OFF & _LVP_OFF & _CPD_OFF



MAIN_PROG CODE

start
banksel TRISD
CLRF TRISD
banksel PORTD
CLRF PORTD


; remaining code goes here

goto $
END ; directive 'end of program'
 
hi,
The problem may be in the code section that you have not posted, please post your full code listing.
 
Have you loaded the reset vector to point to "start"

ie.
Code:
     list p=16f877a ; list directive to define processor
     #include <p16f877a.inc> ; processor specific variable definitions

     __CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_OFF & _HS_OSC & _WRT_OFF & _LVP_OFF & _CPD_OFF

    org   0
    goto start
    org    4
    goto start
   
   'MAIN_PROG CODE I haven't seen this directive before

start	
   banksel TRISD
   CLRF TRISD
   banksel PORTD
   CLRF PORTD


; remaining code goes here

   goto $
END ; directive
 
Last edited:
You should reduce the actual code to a very minimum. Blinking the proverbial LED is the way or a good one at least.

THe board came populated already?
 
Hi,

Thanks for your reply. The "MAIN_PROG CODE" was actually from the mplab template for the 16f877a, which I've posted here, do you suggest I replace that with something else?:

----------------------------------------------------------------------------------

list p=16f877a ; list directive to define processor
#include <p16f877a.inc> ; processor specific variable definitions

__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _RC_OSC & _WRT_OFF & _LVP_ON & _CPD_OFF

; '__CONFIG' directive is used to embed configuration data within .asm file.
; The labels following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.

;***** VARIABLE DEFINITIONS (examples)

; example of using Shared Uninitialized Data Section
INT_VAR UDATA_SHR
w_temp RES 1 ; variable used for context saving
status_temp RES 1 ; variable used for context saving
pclath_temp RES 1 ; variable used for context saving

; example of using Uninitialized Data Section
TEMP_VAR UDATA ; explicit address specified is not required
temp_count RES 1 ; temporary variable (example)

; example of using Overlayed Uninitialized Data Section
; in this example both variables are assigned the same GPR location by linker
G_DATA UDATA_OVR ; explicit address can be specified
flag RES 2 ; temporary variable (shared locations - G_DATA)

G_DATA UDATA_OVR
count RES 2 ; temporary variable (shared locations - G_DATA)

;**********************************************************************
RESET_VECTOR CODE 0x0000 ; processor reset vector
nop ; nop for icd
pagesel start
goto start ; go to beginning of program


INT_VECTOR CODE 0x0004 ; interrupt vector location

INTERRUPT

movwf w_temp ; save off current W register contents
movf STATUS,w ; move status register into W register
movwf status_temp ; save off contents of STATUS register
movf PCLATH,w ; move pclath register into w register
movwf pclath_temp ; save off contents of PCLATH register

; isr code can go here or be located as a call subroutine elsewhere

movf pclath_temp,w ; retrieve copy of PCLATH register
movwf PCLATH ; restore pre-isr PCLATH register contents
movf status_temp,w ; retrieve copy of STATUS register
movwf STATUS ; restore pre-isr STATUS register contents
swapf w_temp,f
swapf w_temp,w ; restore pre-isr W register contents
retfie ; return from interrupt

MAIN_PROG CODE

start

nop ; code starts here (example)
banksel count ; example
clrf count ; example

; remaining code goes here

goto $
END ; directive 'end of program'

------------------------------------------------------------------------------

Have you loaded the reset vector to point to "start"

ie.
Code:
     list p=16f877a ; list directive to define processor
     #include <p16f877a.inc> ; processor specific variable definitions

     __CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_OFF & _HS_OSC & _WRT_OFF & _LVP_OFF & _CPD_OFF

    org   0
    goto start
    org    4
    goto start
   
   'MAIN_PROG CODE I haven't seen this directive before

start	
   banksel TRISD
   CLRF TRISD
   banksel PORTD
   CLRF PORTD


; remaining code goes here

   goto $
END ; directive
 
Ericgibbs,

Thanks for your reply. I'm unsure of what you're refering to as my code section, but this is all I've got in my source file, which was a modification to the template for the 16f877a provided by MPLAB. I'm also using the linker file that is provided by MPLAB.

hi,
The problem may be in the code section that you have not posted, please post your full code listing.
 
atferrari,

Thanks for your reply. I figured this is as reduced as my code can get, as I'm only trying to light one led up (as supposed to the more complicated blinking). The board came populated already, and I've posted the schematic (there are some notes in chinese as I bought this part from china but they don't say much from google translate)

I know for one thing the board works, because when I load the demo program on it the leds flash to a pattern (the demo is in C and I'm trying to do this in assembly). So it seems to me that it is purely a problem with my code. However when im debug it using MPLAB SIM the pins of port d changes as I would expect it to.

The problem is that whatever I set to the output pins of port d, or any port for that matter, it does not change. pins in port d always measure about 3.5v and the supply is 5v.

You should reduce the actual code to a very minimum. Blinking the proverbial LED is the way or a good one at least.

THe board came populated already?
 

Attachments

  • sch.zip
    67.7 KB · Views: 174
hi o2,

I have removed the relative coding and replaced with absolute addressing.

While you are learning I would recommend that you use absolutes.

The program code now assembles OK, but as the Interrrupts have not been enabled , nothing happens.:D

Code:
 list    p=16f877a    ; list directive to define processor
    #include <p16f877a.inc>    ; processor specific variable definitions
    errorlevel -302, -207

    __config _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _RC_OSC & _WRT_OFF & _LVP_ON & _CPD_OFF

; '__CONFIG' directive is used to embed configuration data within .asm file.  
; The labels following the directive are located in the respective .inc file.  
; See respective data sheet for additional information on configuration word.  

;***** VARIABLE DEFINITIONS (examples)  

TEMP_VAR equ    0x20        ;  
temp_count equ    0x21        ; temporary variable  
flag    equ    0x22        ; temporary variable 
count    equ    0x23        ; temporary variable  

w_temp    equ    0x70        ; variable used for context saving
status_temp equ    0x71        ; variable used for context saving
pclath_temp equ    0x72        ; variable used for context saving

;************************************************* *********************  
    org    0x0000        ; processor reset vector
    nop            ; nop for ic
    goto    start        ; go to beginning of program


    org    0x0004        ; interrupt vector location

INTERRUPT: 

    movwf    w_temp        ; save off current W register contents
    movf    STATUS,w    ; move status register into W register
    movwf    status_temp    ; save off contents of STATUS register
    movf    PCLATH,w    ; move pclath register into w register
    movwf    pclath_temp    ; save off contents of PCLATH register

; isr code can go here or be located as a call subroutine elsewhere  

    movf    pclath_temp,w    ; retrieve copy of PCLATH register
    movwf    PCLATH        ; restore pre-isr PCLATH register contents
    movf    status_temp,w    ; retrieve copy of STATUS register
    movwf    STATUS        ; restore pre-isr STATUS register contents
    swapf    w_temp,f
    swapf    w_temp,w    ; restore pre-isr W register contents
    retfie            ; return from interrupt

start:

    nop            ; code starts here (example)
    banksel    count        ; example
    clrf    count        ; example

; remaining code goes here  

    goto    $
    end            ; directive 'end of program'
 
Hey everyone,

I figured out what my problem was! I was missing the reset vector as Ian mentioned. I added this in and it works!

RESET_VECTOR CODE 0x0000 ; processor reset vector
nop ; nop for icd
pagesel start
goto start ; go to beginning of program
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top