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.

PIC16F877

Status
Not open for further replies.

Celestie84

New Member
What I mean is that I want to send data (eg. 11110000) out from PIC16F877 and also read data in. Any sample codes to share?
 

Exo

Active Member
That's pretty basic, I think the best thing to do is do a google search for 'pic projects' and 'pic tutorials' to get to know pics...
 

Nigel Goodwin

Super Moderator
Most Helpful Member
Celestie84 said:
What I mean is that I want to send data (eg. 11110000) out from PIC16F877 and also read data in. Any sample codes to share?

Again, your meaning isn't clear, you don't mention what kind of interface, from or to what devices, or how fast it needs to be. In fact nothing we need to know to answer the question.

For a simple generic answer:

Code:
movwf PortB ;sends data out portb

movf PortB, W ;reads data from portb

I suggest you have a read of my tutorials, which will probably explain most of your questions.

When you've found a specific question, try asking again.
 

Celestie84

New Member
Is your tutorial codes meant for PIC16F877 as well? Coz you are written for PIC16F628, right?

I tried to use the code to test on the LED. But the LED didn't light up. I tested on the output pins and found that all pins are in open-loop (no voltage). I wonder is my code correct.

LIST p=16F877
include "P16F877.INC"

cblock 0X20
count1
counta
countb
countc
endc

org 0x00


bcf STATUS,RP0
bcf STATUS,RP1
bsf STATUS,RP0
movlw 0X00
movwf TRISD

movlw 0X05
movwf countc

loop movlw 0Xff
movwf PORTD
call Delay
call Delay
call Delay
call Delay
clrf PORTD
call Delay
call Delay
call Delay
call Delay
decfsz countc,f
goto loop


Delay movlw d'250'
movwf count1
d1 movlw 0xC7
movwf counta
movlw 0x01
movwf countb
Delay_0
decfsz counta, f
goto $+2
decfsz countb, f
goto Delay_0

decfsz count1, f
goto d1
retlw 0x00


END
 

Nigel Goodwin

Super Moderator
Most Helpful Member
As Exo says, you don't switch bank to bank 0, so you can't ever access PortD - instead it still writes to TRISD.

The tutorials are mainly based on the 16F628, but the code is essentially the same, only a few details need changing.

When you post code, enclose it using the 'code' button above, this maintains it's formatting, otherwise it's difficult to read.
 

Celestie84

New Member
Code:
                                List p=16F877
                                include "P16F877.inc"

#define		in0		PORTB,0
#define		in1		PORTB,1
#define		in2		PORTB,2
#define		in3		PORTB,3
#define		in4		PORTB,4
#define		in5		PORTB,5
#define		in6		PORTB,6
#define		in7		PORTB,7

#define		ot0		PORTC,0
#define		ot1		PORTC,1
#define		ot2		PORTC,2
#define		ot3		PORTC,3
#define		ot4		PORTC,4
#define		ot5		PORTC,5
#define		ot6		PORTC,6
#define		ot7		PORTC,7

                                org           0x00
                                goto          MAIN

                                org           0x11
MAIN
                                call           INIT
AGAIN
                                call           CHECK
                                goto         AGAIN

CHECK                      
                                btfss         in0
                                goto          OFF0
                                goto          ON0
ON0
                                bsf            ot0
                                goto          IN1
OFF0
                                bcf            ot0
IN1
                                btfss          in1
                                goto          OFF1
                                goto          ON1
ON1
                                bsf            ot1
                                goto          IN2
OFF1
                                bcf            ot1
IN2
                                btfss         in2
                                goto         OFF2
                                goto         ON2
ON2
                                bsf            ot2
                                goto          IN3
OFF2
                                bcf            ot2
IN3
                                btfss         in3
                                goto          OFF3
                                goto          ON3
ON3	
                                bsf            ot3
                                goto          IN4
OFF3
                                bcf           ot3	
IN4
                                btfss         in4
                                goto         OFF4
                                goto         ON4
ON4
                                bsf           ot4
                                goto         IN5
OFF4
                                bcf           ot4
IN5
                                btfss         in5
                                goto          OFF5
                                goto          ON5
ON5
                                bsf            ot5
                                goto          IN6
OFF5
                                bcf           ot5
IN6
                                btfss         in6
                                goto         OFF6
                                goto         ON6
ON6                          
                                bsf           ot6
                                goto         IN7
OFF6
                                bcf           ot6
IN7
                                btfss         in7
                                goto         OFF7
                                goto         ON7
ON7
                                bsf            ot7
                                goto          Finish
OFF7
                                bcf            ot7
Finish
                                return

INIT
                                bcf            STATUS,RP0
                                bcf            STATUS,RP1
                                clrf            PORTB
                                clrf            PORTC
                                bsf            STATUS,RP0
                                movlw       B'11111111'
                                movwf       TRISB
                                movlw       B'00000000'
                                movwf      TRISC
                                bcf           STATUS,RP0
                                return
END


Above is the code I written. I tried but it didn't give me the desired output. I grounded all the unused pins as well. But all the outputs I got are 0.3V or Open-loop although a HIGH is expected. Any idea solving
 

Nigel Goodwin

Super Moderator
Most Helpful Member
Celestie84 said:
Above is the code I written. I tried but it didn't give me the desired output. I grounded all the unused pins as well. But all the outputs I got are 0.3V or Open-loop although a HIGH is expected. Any idea solving

You don't mention your hardware, I notice you are using PortB for inputs, do you have pullup resistors on the pins?.
 

Exo

Active Member
Yes, more info about how you connected the inputs is required...

Also, there is a much easyer way to accomplish what you are doiing

Code:
Again

     MOVF     PORTB, W      ;copy portB to W
     MOVWF    PORTC         ;and copy W to portC
     GOTO     Again
 

Celestie84

New Member
How actually should I connect the PORTB pins as input? Why the pull-up resistors are necessary? What I did was applying 5V to each PORTB pins and both VDD. Of course the Vss and all the unused pins are grounded. I tried to measure the output voltage of PORTC pins and 0.3V / open-loop were what I got. Is the hardware the one not working or my coding? Please correct me if the hardware is wrong.
 

Exo

Active Member
well, if that is your hardware setup, and with the code above it should work, portC pins should be high.

are you sure the pic is running, what oscillator are you using and what configuration settings? have you tried a simple blink-a-led program to see it is actually running ?
 

Celestie84

New Member
I'm doing the flashing of LED as well. However, the output that I got was very weird. I got 2.5-3.5V for a HIGH and 1.2V for a LOW. Also, the LED was always ON and didn't blink at all no matter how much delay I have put. I don't understand why. The oscillator that I'm using is 20MHz with 22pF capacitances. I think the configuration setting is correctly set.

My questions for today are:
1) How to calculate the delay time for 20MHz crystal oscillator in a cascaded delay subroutine?
2) The flow of execution of the program goes from one line after another,right? If I want to set PORTB,7 as HIGH at the same time output all PORTD as HIGH, how should I write the coding?
 

Nigel Goodwin

Super Moderator
Most Helpful Member
Celestie84 said:
I'm doing the flashing of LED as well. However, the output that I got was very weird. I got 2.5-3.5V for a HIGH and 1.2V for a LOW. Also, the LED was always ON and didn't blink at all no matter how much delay I have put. I don't understand why. The oscillator that I'm using is 20MHz with 22pF capacitances. I think the configuration setting is correctly set.

It could be many things, either hardware or software, it's always possible you've wired it wrong somewhere.

My questions for today are:
1) How to calculate the delay time for 20MHz crystal oscillator in a cascaded delay subroutine?

The easiest way is to use the "delay code generator" on the PICList, you simply enter your required delay and the clock speed, the generator gives you the code you need.

2) The flow of execution of the program goes from one line after another,right? If I want to set PORTB,7 as HIGH at the same time output all PORTD as HIGH, how should I write the coding?

You can't, there will have to be a tiny delay between them, with your 20MHz clock it will be a minimum of 200nS.
 

Celestie84

New Member
I finally got the flashing LED working~!

My PIC output is connected to a LATCH. PORTB,7 is used to triggered on the LATCH and PORTD is connected to the input pins of the LATCH.
PORTB,7 has to be HIGH output to trigger on the LATCH then at the same time only the data from PORTD will flow through the LATCH.

If like what Nigel said, how should I program it in order to trigger on the LATCH and get the PORTD data flowing thru the LATCH? Please advice.
 

Nigel Goodwin

Super Moderator
Most Helpful Member
Celestie84 said:
I finally got the flashing LED working~!

My PIC output is connected to a LATCH. PORTB,7 is used to triggered on the LATCH and PORTD is connected to the input pins of the LATCH.
PORTB,7 has to be HIGH output to trigger on the LATCH then at the same time only the data from PORTD will flow through the LATCH.

If like what Nigel said, how should I program it in order to trigger on the LATCH and get the PORTD data flowing thru the LATCH? Please advice.

I'm not very sure why you have an external latch?, all PIC ports have internal latches anyway - so it's not required, unless you are multiplexing the port to do more than one job?.

Anyway, assuming you want to do it, simply output your data on PortD, then pulse your PortB pin high - you don't want them to happen at the same time!. Something like this:

Code:
movwf PortD
bsf PortB, 7
;add a delay here if a longer pulse is required, perhaps a 'nop' or two?
bcf PortB, 7

But if you're not using PortD for anything else, the first line is all that's needed, and no external latch.
 

Celestie84

New Member
I have some codes coded for PIC18F452. However, I need to adapt the codes for my PIC16F877. Since PIC16F877 only has 35 instruction sets, some of the instructions of the PIC18F452 has to be recoded in order to perform the same task. I am not sure whether the conversion I did is correct or not. Pls correct me if I am wrong.

Code:
(PIC18F452)           BRA     MAIN
(PIC16F877)           GOTO    MAIN


(PIC18F452)           BZ       CONTROL_TRANSMIT_OK
                      MOVF     TRANS_REG_CTRL_IN
                      XORLW    41H
                      BZ       CONTROL_TRANSMIT_OK
                      GOTO     ERR

(PIC16F877)           BTFSC    STATUS,Z
                      GOTO     CONTROL_TRANSMIT_OK
                      MOVF     TRANS_REG_CTRL_IN,0
                      XORLW    41H
                      BTFSC    STATUS,Z
                      GOTO     CONTROL_TRANSMIT_OK
                      GOTO     ERR
Also, how to convert the following code to PIC16F877 code?
Code:
(PIC18F452)           LFSR      FSR0,0x100
Thankz[/code]
 
Status
Not open for further replies.

EE World Online Articles

Loading
Top