###### tags: `MPLAB` `Assembly` `PIC18F` `Breadboard` `LED` `PICKIT4`
# MPLAB - Assignment 6
###### [PPT](https://www.youtube.com/redirect?event=video_description&redir_token=QUFFLUhqblRPLVZLUU9ULTh6ak5yV0N0aUVPbkkxbTlwd3xBQ3Jtc0tsQkdvVWgxcVBod1Njc3Byck9OOUw2QXFKVldYeG01QmFGVGNId1Q2R0EyZE51cUQzSzMxNktOWEJQYjFlOE90Z1Jlc1Q2NC1zME9mNDhXZTFDQUx6NXBHejlkWENYU3ZrbFVNZnpmN3FmSFB3RDQxbw&q=https%3A%2F%2Fdocs.google.com%2Fpresentation%2Fd%2F10ZMcQxYtCXkrXLtKLXVb12kVCReyMAF0%2Fedit%3Fusp%3Dsharing%26ouid%3D115453016868129368933%26rtpof%3Dtrue%26sd%3Dtrue&v=fjocqiF3gZE), [Video](https://www.youtube.com/watch?v=fjocqiF3gZE), [Hackmd](https://hackmd.io/@uXOsB_jURb6osPmOerwVig/B1Im7lyms)
##### Goal
###### 1. How to use Assembly control port on PIC18F4520
###### 2. How to control connect picket 4 to computer
###### 3. How to use jump wires on breadboard
---
## Simple Tutorial
### I/O ports of **PIC18F4520**

### Components
1. breadboard
2. pickit3 or pickit4
3. PIC18F4520 transistor
4. computer
5. jump wires : many
6. resistor : many
7. led : 5
8. button : 1
### Connect
here is a example for test to MPLAB
1. just copy this code
```assembly=1
LIST p=18f4520
#include<p18f4520.inc>
CONFIG OSC = INTIO67 ; 1 MHZ
CONFIG WDT = OFF
CONFIG LVP = OFF
L1 EQU 0x14
L2 EQU 0x15
org 0x00
; Total 2 + (2 + 7 * num1 + 2) * num2 cycles = C
; num1 = 200, num2 = 180, C = 252360
; Total delay ~= C/1M = 0.25s
DELAY macro num1, num2
local LOOP1
local LOOP2
MOVLW num2
MOVWF L2
LOOP2:
MOVLW num1
MOVWF L1
LOOP1:
NOP
NOP
NOP
NOP
NOP
NOP
DECFSZ L1, 1
BRA LOOP1
DECFSZ L2, 1
BRA LOOP2
endm
start:
int:
; let pin can receive digital signal
MOVLW 0x0f
MOVWF ADCON1
CLRF PORTB
BSF TRISB, 0
CLRF LATA
BCF TRISA, 0
; ckeck button
check_process:
BTFSC PORTB, 0
BRA check_process
BRA lightup
lightup:
BTG LATA, 0
DELAY d'200', d'180' ;delay 0.25s
BRA check_process
end
```
2. click Debug project

4. Warnning!
- If you don't have this warnning, congradulation, just skip this point
- If you have this problem, don't worry, The system just trying to tell you to be carefully sure that the transistor your use should be PIC18F4520. Thererfore, just click ==**`OK`**== 
### Circuit
- all

- Remember that led should be insert in breadboard by this direction

- Insert the resistor into the board with one end in the same horizontal row as the LED’s cathode (short) leg and the other a few rows down. The resistor is not polarized so it does not matter which direction you insert it.

### Run
Everytime press button, the led will light or turn off. If you are successful, go down and have chanllenge of other examples!
---
## Basic Example
#### Description

#### Code
```clike=1
LIST p=18f4520
#include<p18f4520.inc>
CONFIG OSC = INTIO67 ; 1 MHZ
CONFIG WDT = OFF
CONFIG LVP = OFF
L1 EQU 0x14
L2 EQU 0x15
count EQU 0x01
org 0x00
; Total 2 + (2 + 7 * num1 + 2) * num2 cycles = C
; num1 = 200, num2 = 180, C = 252360
; Total delay ~= C/1M = 0.25s
DELAY macro num1, num2
local LOOP1
local LOOP2
MOVLW num2
MOVWF L2
LOOP2:
MOVLW num1
MOVWF L1
LOOP1:
NOP
NOP
NOP
NOP
NOP
NOP
DECFSZ L1, 1
BRA LOOP1
DECFSZ L2, 1
BRA LOOP2
endm
initial:
MOVLW 0x01
MOVWF count
; let pin can receive digital signal
MOVLW 0x0f
MOVWF ADCON1
CLRF PORTB
BSF TRISB, 0
CLRF LATA ; output value
BCF TRISA, 0 ; tell if its output(0) or input(1)
BCF TRISA, 1 ; tell if its output(0) or input(1)
BCF TRISA, 2 ; tell if its output(0) or input(1)
BCF TRISA, 3 ; tell if its output(0) or input(1)
; ckeck button
check_process:
BTFSC PORTB, 0 ; Bit Test f, Skip if clear
BRA check_process ; not press
BRA lightup ; if press
;light up oled
lightup:
MOVLW 0x0f
CPFSEQ LATA
BRA light
CLRF LATA
BRA end_
light:
RLCF LATA
MOVF count,w
IORWF LATA
DELAY d'200', d'180' ;delay 0.25s
BRA check_process
end_:
end
```
#### Circuit
- Button : Connect a push-button at RB0 port with pull-up or pull-down resistor.
- LED : Connect four LEDs at RA0 ~ RA3 port. Press the button to change the blinking pattern of LEDs, as in the figure below.
- Remember that every oled usually used in tandem with a resistance to protect led from too large voltage and prevent floating state(Use [pull-down resistor](https://bmet.fandom.com/wiki/Pull-down_Resistor))

- Prevent floating state : Use [pull-up resistor](https://en.wikipedia.org/wiki/Pull-up_resistor)

### Result
[:video_camera: demo](https://youtu.be/4DHEU-eOWJM)
---
## Advanced Example
### **Description**

### **Code**
- just add delay between state5 and another press event 
- full code
```assembly=1
LIST p=18f4520
#include<p18f4520.inc>
CONFIG OSC = INTIO67 ; 1 MHZ
CONFIG WDT = OFF
CONFIG LVP = OFF
L1 EQU 0x14
L2 EQU 0x15
count EQU 0x01
org 0x00
; Total 2 + (2 + 7 * num1 + 2) * num2 cycles = C
; num1 = 200, num2 = 180, C = 252360
; Total delay ~= C/1M = 0.25s
DELAY macro num1, num2
local LOOP1
local LOOP2
MOVLW num2
MOVWF L2
LOOP2:
MOVLW num1
MOVWF L1
LOOP1:
NOP
NOP
NOP
NOP
NOP
NOP
DECFSZ L1, 1
BRA LOOP1
DECFSZ L2, 1
BRA LOOP2
endm
initial:
MOVLW 0x01
MOVWF count
; let pin can receive digital signal
MOVLW 0x0f
MOVWF ADCON1
CLRF PORTB
BSF TRISB, 0
CLRF LATA ; output value
BCF TRISA, 0 ; tell if its output(0) or input(1)
BCF TRISA, 1 ; tell if its output(0) or input(1)
BCF TRISA, 2 ; tell if its output(0) or input(1)
BCF TRISA, 3 ; tell if its output(0) or input(1)
; ckeck button
check_process:
BTFSC PORTB, 0 ; Bit Test f, Skip if clear
BRA check_process ; not press
BRA lightup ; if press
;light up oled
lightup:
MOVLW 0x0f
CPFSEQ LATA
BRA light
CLRF LATA
DELAY d'200', d'180' ;delay 0.25s
BRA check_process
light:
RLCF LATA
MOVF count,w
IORWF LATA
DELAY d'200', d'180' ;delay 0.25s
BRA check_process
end
```
---
## Bonus Example
### **Description**

### **Code**
```assembly=1
LIST p=18f4520
#include<p18f4520.inc>
CONFIG OSC = INTIO67 ; 1 MHZ
CONFIG WDT = OFF
CONFIG LVP = OFF
R1 EQU 0x14
R2 EQU 0x15
R3 EQU 0x16
count EQU 0x00
state EQU 0x01
state_count EQU 0x02
org 0x00
delay macro num
local L_1
local L_2
local L_3
MOVLW num
MOVWF R3
L_1: ; MAIN loop in the subroutine to generate a delay of R3 50x msecs
MOVLW D'50'
MOVWF R2
L_2: ; outer loop generates a delay of 50 msecs
MOVLW D'199'
MOVWF R1
L_3: ; inner loop generates a delay of 1 msecs
NOP
NOP
DECF R1
BNZ L_3
DECF R2
BNZ L_2
DECF R3
BNZ L_1
endm
add_state macro s
INCF s
MOVLW 0x04
CPFSLT s
CLRF s
endm
check_light macro s
local s0
local s1
local s2
local s3
local end_
MOVLW 0x03
MOVWF LATA
MOVLW 0x00
s0:
CPFSEQ s
BRA s1
BRA end_
s1:
INCF WREG
CPFSEQ s
BRA s2
RLCF LATA
BRA end_
s2:
INCF WREG
CPFSEQ s
BRA s3
; RLCF LATA
MOVLW 0x0C
MOVWF LATA
BRA end_
s3:
INCF WREG
CPFSEQ s
BRA end_
MOVLW 0x09
MOVWF LATA
end_:
endm
initial:
MOVLW 0x00
MOVWF count
MOVWF state
; let pin can receive digital signal
MOVLW 0x0f
MOVWF ADCON1
CLRF PORTB
BSF TRISB, 0
CLRF LATA ; output value
BCF TRISA, 0 ; tell if its output(0) or input(1)
BCF TRISA, 1 ; tell if its output(0) or input(1)
BCF TRISA, 2 ; tell if its output(0) or input(1)
BCF TRISA, 3 ; tell if its output(0) or input(1)
; ckeck button
check_process:
MOVLW 0x00
MOVWF count
MOVFF state, state_count
BTFSC PORTB, 0 ; Bit Test f, Skip if clear
BRA check_process ; not press
BRA lightup ; if press
;light up oled
lightup:
check_light state_count
delay 0x01 ;delay 500ms
INCF count
add_state state_count
MOVLW 0x04
CPFSEQ count
BRA lightup
add_state state
delay 0x02 ;delay 500ms
CLRF LATA
BRA check_process
end
```
### Result
[:video_camera:demo](https://youtu.be/XtZkuRmriGw)
---
# Reference
[:paperclip:How to Use a Breadboard and Build a LED Circuit](https://computers.tutsplus.com/tutorials/how-to-use-a-breadboard-and-build-a-led-circuit--mac-54746)
[:paperclip:How to write delay in MPLAB](https://engineeringxpert.com/how-to-create-a-delay-for-pic18-using-a-loop-in-assembly-language/)
[:paperclip:PIC18Fxxx Instruction Set](http://technology.niagarac.on.ca/staff/mboldin/18F_Instruction_Set/)