4) PIC IO Port Programming
4) PIC IO Port Programming
Chapter 3
QDELAY MOVLW D 200 MOVWFR1 D1MOVLW D 250 MOVWFR1 D2NOP NOP DECF R2, F BNZD2 DECFR1 BNZD1 RETURN END
10
BIT-ORIENTED OPERATIONS
1. Some times, we are interested to access only 1 or 2 bits of the port instead of the entire 8 bits. 2. BIT Oriented operation are supporting to do the above things in PIC18
11
12
Study Example:
An LED is connected to each pin of PORT D. Write a program to turn on each LED from pin D0 to pin D7. Call a delay module before turning on the next LED CLRFTRISD;Make PORT D an output BSFPORTD, 0; Bit set turns on RD 0 CALL DELAY BSF PORTD,1 ; Bit set turns on RD 1 CALL DELAY ----------------------------------------------------------------------------------------BCFTRISB, 2 ;bit = 0, make RB2 an output pin AGAINBSFPORTB,2 ; bit set (RB2 = high) CALL DELAY BCFPORTB, 2 ; bit clear (RB2=low) CALL DELAY BRA AGAIN
13
Exercise 1
Write a program to generate 1kHz square wave with 80% duty cycle on bit3 of PORTC. Prepare the flowchart.
14
BTFSS (bit test fileReg, skip if set) BTFSC (bit test fileReg, skip if clear)
In order to make a decision on based on the status of a given bit in the file register, we use the following instructions.
BTFSS - to monitor the status of single bit HIGH
15
Example 4-4
Write a program to perform the following: a)Keep monitoring the RB2 bit until it becomes HIGH b)When RB2 becomes HIGH, write value 45H to PORTC, and also send a HIGH-to-LOW pulse to RD3
16
17
Example 4-5
BSFTRISB,3 BCFTRISC,5 HEREBTFSC PORTB,3 BRAHERE BSFPORTC,5 BCFPORTC,5 BRAHERE
Assume RB3 is an input. If it goes LOW, it means that the door open. Monitor the bit continuously. Whenever it goes LOW, sent a HIGH-to-LOW pulse to port RC5.
18