Celestie84
New Member
Does anyone have sample codes for input / output data from PIC16F877?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Celestie84 said:Does anyone have sample codes for input / output data from PIC16F877?
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?
movwf PortB ;sends data out portb
movf PortB, W ;reads data from portb
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
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
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.
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?
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.
movwf PortD
bsf PortB, 7
;add a delay here if a longer pulse is required, perhaps a 'nop' or two?
bcf PortB, 7
(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
(PIC18F452) LFSR FSR0,0x100