Try   HackMD
tags: MPLAB Assembly PIC18F Breadboard LED PICKIT4

MPLAB - Assignment 6

PPT, Video, Hackmd
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

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More β†’

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
    ​​​​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
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More β†’
  3. 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
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More β†’

Circuit

  • all
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More β†’
  • Remember that led should be insert in breadboard by this direction
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More β†’
  • 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.
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More β†’

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

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More β†’

Code

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)


  • Prevent floating state : Use pull-up resistor

Result

demo


Advanced Example

Description

Code

  • just add delay between state5 and another press event

  • full code

    ​​​​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

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

demo


Reference

How to Use a Breadboard and Build a LED Circuit
How to write delay in MPLAB
PIC18Fxxx Instruction Set