PIC 18 Introduction: GCS250 Computer Architecture Gustavo Rodriguez-Rivera Purdue University
PIC 18 Introduction: GCS250 Computer Architecture Gustavo Rodriguez-Rivera Purdue University
Delay1 res 2
Start:
CLRF PORTD ; Initialize with 0's output D.
CLRF TRISD ; Make port D output
CLRF Delay1; Clear delay variables
CLRF Delay2
SETF TRISC ; Make port c an input
MOVLW H'40' ; Initialize delay3 to 0x40. This is the delay used to rotate the segments.
MOVWF Delay3
BSF PORTD,RD0 ;Turn on bit 0 in RD0
Example (cont.)
MainLoop:
RRNCF PORTD ; Rotate bits in D. This causes the segments in display to shift.
MOVF Delay3,0 ; Reload Delay2 eith the value of Delay3. Delay2 controls the rate the
MOVWF Delay2 ; rotate takes place.
MOVLW H'F0' ; Test if Delay3 is at the maximum of 0xF0 or more. If that is the case, do not
CPFSLT Delay3 ; read the left switch.
goto noincrement
MOVLW 4 ; Read the left switch.
BTFSS PORTC,0 ; If the switch is 0 (gnd), then increase Delay3 by 4, otherwise skip the increment.
ADDWF Delay3,1
noincrement:
MOVLW H'05' ; Test if Delay3 is at the minimum pf 0x5 or less. If that is the case do not
CPFSGT Delay3 ; read the right switch.
goto Delay
MOVLW 4 ; Read the right switch.
BTFSS PORTC,1 ; If the switch is 0, then decrement Delay3 by 4, otherwise skip the decrement
operation.
SUBWF Delay3,1
Delay:
DECFSZ Delay1,1 ;Decrement Delay1 by 1, skip next instruction if Delay1 is 0
GOTO Delay
DECFSZ Delay2,1 ;Decrement Delay1 by 1, skip next instruction if Delay1 is 0
GOTO Delay
GOTO MainLoop
end
Example: Driving a Full-Color LED
• To drive the full-color LED you will use Pulse
Width Modulation (PWM).
• PWM sends pulses to the LED with different
widths to the three color LEDs.
• If for example, the width of the pulse is small for
the red LED, then the red LED will display a low
intensity red light.
• If the red LED receives a pulse with a wide width,
then the red LED will display a high intensity red
light.
Pulse Width Modulation
• Short Width = Low Intensity
• Long Width = High Intensity
Algorithm for Driving Full Color
LED
• Start
– Initialize Ports and Registers
– Initialize colors and counters
• MainLoop
– Put in a variable val the current color value (red, green, blue)
– Read button 1 and 2. If they are “on” increase or decrease val. Make
sure that val is not increased beyond maxColor and is not decreased
beyond 0.
– Update “msg” (the display buffer) with:
• msg[0]= c[currentColor]
– where c is an array with the characters “r”, “g” or b” in seven-segment values.
• msg[1]= “=“
– in seven segment value “=“ is(0x48)
• msg[2] = digit[(val>>4)&0xFF]
– Displays most significant nibble of val
– digit is an array with the hex digits in seven segment value.
• msg[3]=digit[val&0xFF]
– Displays least significant nibble of val
Algorithm for Driving Full Color
LED (cont.)
– Store val in currentColor red, green or blue
– Update Display. See example code.
– Read button 3 to change color if necessary. Use a
variable previouslyPressed to store the previous status
of the button.
– Only update the color name if previouslyPressed is
false and button3 is pressed.
– To update the color name write into msg (the display
buffer” the name of the color in seven-segment values.
– Now refresh the red, green, blue LEDs PWM See
example code.
• Goto MainLoop