Correct way to program ports on 16F628A - Help

Sep 08, 2007 2 Replies

I am trying to learn PIC assember. Can someone please give me the correct assembler code to program a



16F628A with all pins on PORTA and PORTB as outputs and then toggle them Hi and Lo. My efforts to date using MPLAB has always resulted in an error. Thanks Syd

If it were the 16F88, I'd be able to help, because that's what I have. Either way, it seems as if the following might be a lead to what you want (one of the programs does exactly what you asked for):

formatting link

Don't fear the Google. PSM

Here is some code that will toggle the output pins at quite a clip. IT IS UNTESTED, but was cut from working code, so it should work fine. ;-) I assume a 4MHz external crystal is connected. Watch out for line wraps from the comments when you copy and paste this code.

;--------------------------------------------------------------------------------------------- ; HEADER: ;--------------------------------------------------------------------------------------------- LIST P=16f628 RADIX DEC INCLUDE "p16f628.inc" __CONFIG _CP_OFF & _WDT_OFF & _XT_OSC & _PWRTE_ON & _BODEN_OFF & _LVP_OFF

;--------------------------------------------------------------------------------------------- ; EQUATES: ;---------------------------------------------------------------------------------------------

STARTOFRAM equ 0x20 ;First Usable RAM Location for

16f628 ENDOFRAM equ 0x6F ;Last Usable RAM Location for 16f628 FIXEDAREA equ 0x70 ;Start of shared memory between banks BANK2RAM equ 0xA0 ;Start of Bank 2 RAM

;--------------------------------------------------------------------------------------------- ; General Purpose Temp Storage ;---------------------------------------------------------------------------------------------

cblock STARTOFRAM LoopCounter endc

;--------------------------------------------------------------------------------------------- ; Common Area Temp Storage (visible from any bank) ;---------------------------------------------------------------------------------------------

cblock FIXEDAREA

; ; ISR Register Context Save Areas ; INT_FLAGS W_TEMP STATUS_TEMP FSR_TEMP PCLATH_TEMP

endc ;--------------------------------------------------------------------------------------------- ; START: ;--------------------------------------------------------------------------------------------- org 0x000 goto Start

; Interrupt handler org 0x004 movwf W_TEMP ;Save everything swapf STATUS, W movwf STATUS_TEMP movfw FSR movwf FSR_TEMP movfw PCLATH movwf PCLATH_TEMP

goto IntExit ; ; Restore everything and return ; IntExit movfw PCLATH_TEMP movwf PCLATH movfw FSR_TEMP movwf FSR swapf STATUS_TEMP, W movwf STATUS swapf W_TEMP, F swapf W_TEMP, W retfie

;--------------------------------------------------------------------------------------------- ; ; Main level code begins here ; ;---------------------------------------------------------------------------------------------

Start bsf STATUS, RP0 ; Switch to Bank 1 movlw b'00000000' ; Define I/O on Port B (all output) movwf TRISB & 0x07F movlw b'00000000' ; Define I/O on Port A (all output) movwf TRISA & 0x07F bcf STATUS, RP0 ; Back to Bank 0

movlw 0x07 ; Turn off the comparators (common newbie mistake to miss this) movwf CMCON

clrf PORTA ; Drive all pins low initially clrf PORTB

Main

; Drive the pins high movlw 0xFF movwf PORTA movwf PORTB

; A couple of no-ops to square up the output waveform nop nop

; Drive the pins low movlw 0x00 movwf PORTA movwf PORTB

; Loop back forever goto Main

end ; Don't forget to include one of these

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required