Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Need help starting with 16F877

Status
Not open for further replies.

janosandi

Full Member level 4
Joined
Jan 23, 2010
Messages
210
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,298
Activity points
2,788
hello Guys
i was using 12cXXX & 16FXXX & it was good for my small projects But now i need ore stuff of it so i've decided to move to 16F877.
but im facing problem starting with it at start i couldnt set trisA pins & mplab give me errors when i stat to compile it
im using assembly
can anyone help me with any assembly code to light a led on any port ?
ur help will b appriciated
John
 

Hello janosandi
using assembly is good but its its very old now. its very difficult to read the program flow,also you will always face this kind challenges when Ever you'll change the controller...
so my opinion is to Use C as your programming language.
its not that much difficult also is you know the assembly.
this link tech you how to code for en led to Blink
Click Here :cool:

OR this
Click Here :-D

and The following code do the same but using C
Code:
#include<xc.h>
#include<Delays.h>
#define LED1 RB0

void main(){
PORTB = 0x00; /// all Pin of PORTB to 0
TRISB = 0x00; /// to make all PORTB PIN output

while(1){
     LED1 = ~LED1; // Toggle the State of LED Every time
     Delay_ms(50)
     Delay_ms(50)
     Delay_ms(50)
     Delay_ms(50)
}

}
and if you want to read the Digital input PIN
Code:
#include<xc.h>
#include<Delays.h>
#define LED1 RB0
#define Key   RB1

void main(){
PORTB = 0x00; /// all Pin of PORTB to 0
TRISB0 = 0;   /// OUTPUT
TRISB1 = 1;   /// INPUT

while(1){
     if (key == 1){
     LED1 = ~LED1; // Toggle the State of LED Every time
     }
}

}
so when ever you connect PORTB pin 1 to VCC the PORTB0 will toggle its State

for more tutorial you can go to
this site

All The Best
HappyCoding :thumbsup:
 
thx Guys
the problem is tht i've used to use the Assembly code & i know about basic but i couldnt find free version of PICBASICi & in my country its hard & expensive to get one

- - - Updated - - -

thx Mohammad the second Link was useful
now i got an assembled simple program now i can go on programming

thx Again Guys
 

To set port A pins you first of all have to disable the A-D analogue controller.

Code:
	bsf	STATUS,5            ;Switch to Bank 1
	movlw	00h	          ;Set the Port A pins to outputs
	movwf	TRISA
	movlw	0ffh	          ;disable a-d
	movwf	ADCON1
	bcf	STATUS,5             ;Switch back to Bank 0
 
Last edited by a moderator:

Assembly is very simple and easy

Dear janosandi This program is written in mind and untested
MCU PIC 16f877a
Crystal is used 4Mhz
PORTC bit 7 LED will ON

Code:
#include <p16f877a.inc>
__config 3f39
Start    goto    main
main        call    sys_init
loop        call    Blink_LED
        goto    loop

Blink_LED    bsf    PORTC,7
        goto    Blink_LED

sys_init    clrf    PORTC
        bsf    STATUS,5
        bcf    TRISC,7
        bcf    STATUS,5
        clrf    PORTC
        return    
end
 

thx Guys its Done till Now
sure i'll ask for more help later with this

i had a mistake with the inc File
 

Hi Guys
thx for your ideas it was usefull & now im starting to read about the 4 Ports of the 16F877
my questions is can i use the 4 ports as digital inputs or outputs ?
my project needs a port for 7 segment displays(wich i've used portB) ,atleast 5 inputs & 8 ouputs,
1 Analog input to read a thermost
what you suggest?
 
Last edited:

The short answer to your question is yes.
But it depends how you make the circuit. 7 segment require 1 port for digits (unless you could use a shift register), 1 I/O for each segment.
Say you want to use 4 segments and 1 A/D. PORT A and PORT B would do the job easily.
Now you need 8 Outputs port C or D would be enough, and same goes to 5 inputs.
Go through the data sheet of 16F877A it is a comprehensive document, answers to lot of your future questions and questions are in there.
 

i tried alot with the documents but im stopping somewhere
wht i should disable to activate the digital pins on ports C & D ?
 

from the datasheet
PORTC and the TRISC Register
PORTC is an 8-bit wide, bidirectional port. The corresponding data direction register is TRISC. Setting a TRISC bit (= 1) will make the corresponding PORTC pin an input (i.e., put the corresponding output driver in a High-Impedance mode). Clearing a TRISC bit (= 0) will make the corresponding PORTC pin an output (i.e.,put the contents of the output latch on the selected pin).

Code:
TRISC = 0b00001111; /// will make Port C4:7 outputs and C0:3 inputs

Same with PORTD
 

i've read about it & already used port C but i'll include small Counter program to drive 2 7segment displays
Which it works on 16f628 but its not here
would u plz check for corrections
Code:
	List	 P=16F877A
	include <P16F877A.inc>

	__CONFIG _CP_OFF& _WRT_OFF& _CPD_OFF& _LVP_OFF& _BODEN_OFF& _PWRTE_ON& _WDT_OFF& _XT_OSC

org 	0x00
						 			
temp1		equ 20h	;for delay
temp2		equ 21h	;for delay
SwUp		equ 22h	;
SwDwn		equ	23h	;
units		equ	24h	;
tens		equ	25h	;
Sw_Flag		equ	26h	;

status		equ	0x03
adcon		equ	0x1F
ADCON1		equ	0x9F
rp1			equ	0x06
rp0			equ	0x05
portA		equ 0x05
portB		equ 0x06
portC		equ 0x07

option_reg	equ 0x81

;****************************************************************
;Beginning of program
;****************************************************************

goto	SetUp			;goto SetUp

;****************************************************************
;* Delay 10mS 		10 x 1,000uS			*
;****************************************************************
D_10mS	movlw	0Ah
		movwf	temp2
D_a		nop
		decfsz temp1,1
		goto 	D_a
		decfsz temp2,1
		goto 	D_a	
		retlw 	00

;****************************************************************
;* port A and B initialisation					*
;****************************************************************
SetUp	bsf		STATUS,RP0	
		movlw	b'00000000'	;turn portB all outputs connected to 7segments
		movwf	TRISB			
		movlw	b'00000011'	;turn portC 0,1 as inputs 2,3 as outputs to drive the segments 
		movwf	TRISC
		bcf		STATUS,RP0		
		clrf 	portB			
		clrf	units			
		clrf	tens			
		clrf	Sw_Flag
		goto 	Main						
				
Up		btfsc	Sw_Flag,2
		retlw	00
		bsf		Sw_Flag,2
		incf	units,1
		movlw	0Ah			;put 10 into w
		xorwf	units,0		;compare units file with 10
		btfss	status,2	;zero flag in status file. Will be set if units is 10
		retlw	00
		clrf	units
		incf	tens,1
		movlw	0Ah			;put 10 into w
		xorwf	tens,0		;compare units file with 10
		btfsc	status,2	;zero flag in status file. Will be set if tens is 10
		clrf	tens				
		retlw	00			;display passes 99 but not below 0
		
Dwn		btfsc	Sw_Flag,3
		retlw	00
		bsf		Sw_Flag,3
		decf	units,1
		movlw	0FFh		;put FFh into w
		xorwf	units,0		;compare units file with FFh
		btfss	status,2	;zero flag in status file. Will be set if units is 10
		retlw	00
		movlw	09
		movwf	units		;put 9 into units file
		decf	tens,1
		movlw	0FFh		;put 0FFh into w
		xorwf	tens,0		;compare tens file with 0FFh
		btfsc	status,2	;zero flag in status file. Will be set if tens is 0FFh
		goto	$+2			;tens file is 0FFh
		retlw	00						
		clrf	tens
		clrf	units
		retlw	00			;display  not below 0		
		
Main	btfss	portC,0			;test switch-press for UP
		call	Up				;UP switch pressed
		btfss	portC,1			;test switch-press for Down
		call	Dwn				;Down switch pressed
		movlw	b'00000100'		;Make RC2 HIGH for units drive 	
		movwf	portC	
		movf	units,0			;copy unit value into w
		call	table			;unit display value will return in w
		movwf	portB			;output units value
		call	D_10mS			;call delay
		clrf	portB			;clear display
		movlw	b'00001000'		;Make RC3 HIGH for tens drive 	
		movwf	portC			
		movf	tens,0			;copy tens value into w		
		call	table			;tens display value will return in w
		movwf	portB			;output tens value
		call	D_10mS			;call delay
		clrf	portB			;clear display
		btfsc	portC,0			;bit will be zero when sw is pressed
		bcf		Sw_Flag,2
		btfsc	portC,1			;bit will be zero when sw is pressed
		bcf		Sw_Flag,3				
		goto	Main		

table	addwf   PCL,F           ;02h,1  add W to program counter 
        retlw   b'00111111'     ; "0"   -|F|E|D|C|B|A
        retlw   b'00000110'     ; "1"   -|-|-|-|C|B|-
        retlw   b'01011011'     ; "2"   G|-|E|D|-|B|A
        retlw   b'01001111'     ; "3"   G|-|-|D|C|B|A 
        retlw   b'01100110'     ; "4"   G|F|-|-|C|B|-
        retlw   b'01101101'     ; "5"   G|F|-|D|C|-|A
        retlw   b'01111101'     ; "6"   G|F|E|D|C|-|A
        retlw   b'00000111'     ; "7"   -|-|-|-|C|B|A
        retlw   b'01111111'     ; "8"   G|F|E|D|C|B|A
        retlw   b'01101111'     ; "9"   G|F|-|D|C|B|A



end
 

This is working
Please check your configuration word _
Code:
_CONFIG _CP_OFF& _WRT_OFF& _CPD_OFF& _LVP_OFF& _BODEN_OFF& _PWRTE_ON& _WDT_OFF& _XT_OSC

Or else use __config 3f39 for ( 4Mhz)
What is your crystal Frequency

Eda1.png
 

wht program is this ?
any free version of pic basic is there ?
im using mpasm anyway thx

- - - Updated - - -

thx guys
now im sure everything with ports is ok for me
i've used a small code to test if PORTC works as i want
Code:
Main2	clrf	portC
		btfss	portC,0
		call	on1
		BTFSS	portC,1
		call	on2
goto	Main2

on1		movlw	b'00001000'
		movwf	portC
retlw 	0x00

on2		movlw	b'00000100'
		movwf	portC
retlw	0x00
& its done Now lets move to the main problem
in the code which i've included before i have a 2 digits counter which its working on 16f628
why its not working with 16f877 ? at the start r the files at 20h to 26h free to use for variables?
any idea will help
thx guys
 

=janosandi;
& its done Now lets move to the main problem
in the code which i've included before i have a 2 digits counter which its working on 16f628
why its not working with 16f877 ? at the start r the files at 20h to 26h free to use for variables?
any idea will help
thx guys

Tray to change configuration word as i mention in the above and what is the Crystal is using for your board
 

im using 4mhz Crystal & other functions is working great
i just want to convert the pic16f628 code into 16f877
just converted the ports i thing there is something else with the registers which i've used for variables
or if u have 2 segments counter ready for this will b great
 

One possible problem, you have not shown your circuit diagram, have you got pull up resistors on the switches on PortB? alternatively instead of adding pull up resistors you can enable the internal pull up resistors in your code RBPU (OPTION_REG<7>) and set it to low to enable the pull up resistors.
 

PIC16F628 doesn't have ADC and PIC16F877 has ADC and so you have to disable ADC pins and configure them as digital IO pins.
 

im using 4mhz Crystal & other functions is working great
i just want to convert the pic16f628 code into 16f877
just converted the ports i thing there is something else with the registers which i've used for variables
or if u have 2 segments counter ready for this will b great

What is your Seven segments are displaying, Is it showing at least Zero digit on both Seven segments. can we see your hardware or circuit diagram as explained by pjmelect
 

its ok now it was a silly problem i wouldnt notice about it
im connecting positive to inputs when im using btfss
so its Solved now
its solved atleast now & i'll include a pic of my development board today
thx for all comments & help

John
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top