0% found this document useful (0 votes)
260 views

Pic 18 Microcontroller Notes

The document initializes registers on a microcontroller. It loads hex values 30H and 02H into registers REG1 and REG2. It then performs a multiplication by shifting and adding REG1 to REG4 if the carry flag is set from shifting REG2. The program ends and configures bits for an LED program that blinks the LED by delaying with Timer0 interrupts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
260 views

Pic 18 Microcontroller Notes

The document initializes registers on a microcontroller. It loads hex values 30H and 02H into registers REG1 and REG2. It then performs a multiplication by shifting and adding REG1 to REG4 if the carry flag is set from shifting REG2. The program ends and configures bits for an LED program that blinks the LED by delaying with Timer0 interrupts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

03 April 2023 09:25 PM

Initialize registers Page 1


Initialize registers
MOVLW 30H ; Load hex number 30H into WREG
MOVWF REG1 ; Store WREG value in reg1
MOVLW 02H ; Load hex number 02H into WREG
MOVWF REG2 ; Store WREG value in reg2

; Perform multiplication
CLRF REG4 ; Clear reg4
CLRF REG5 ; Clear reg5
MOVLW 08H ; Load loop counter with value 08H
MOVWF REG3 ; Store loop counter in reg3
LOOP:
RRF REG2,F ; Right shift reg2 to get the next bit
BTFSC STATUS,C ; Check carry flag
ADDWF REG4,F ; Add reg1 to reg4 if carry flag is set
RLF REG1,F ; Left shift reg1
DECFSZ REG3,F ; Decrement loop counter and skip next instruction if it is zero
GOTO LOOP ; Jump to LOOP label if loop counter is not zero

; Terminate program
END ; End of program

Initialize registers Page 2


; Set up configuration bits
list p=18F4550
#include <p18f4550.inc>
__CONFIG(0x3F32)

; Define constants
LED_PIN equ PORTB,1 ; LED connected to RB1
DELAY_TIME equ 62500 ; 1 second delay at 4MHz

; Set up initialization routine


org 0x0000 ; Start of program memory
nop ; Required reset delay

; Set up I/O ports


banksel TRISB ; Select TRISB register
bcf TRISB,1 ; RB1 is output

; Main program loop


main:
banksel LED_PIN ; Select LED_PIN
bsf LED_PIN ; Set RB1 high
call delay ; Delay for 1 second
bcf LED_PIN ; Set RB1 low
call delay ; Delay for 1 second
goto main

; Delay routine
delay:
movlw .61 ; Load high byte of delay time
movwf TMR0H ; Set Timer0 high byte
movlw .28 ; Load low byte of delay time
movwf TMR0L ; Set Timer0 low byte
bsf INTCON,T0IF ; Clear Timer0 interrupt flag
bsf T0CON,TMR0ON ; Turn on Timer0
delay_loop:
btfss INTCON,T0IF ; Check for timer interrupt
goto delay_loop ; Wait for timer interrupt
bcf T0CON,TMR0ON ; Turn off Timer0
retlw 0 ; End of delay routine

Prescaler is a name for the part of a microcontroller which divides oscillator clock before it will reach logic that increases timer status. The range of the prescaler id is from 1 to 256 and the value of the Prescaler can be set using the OPTION
Register (The same one that we used for pull up resistors). For example if the value of prescaler is 64, then for every 64th pulse the Timer will be incremented by 1.

As the timer increments and when it reaches to its maximum value of 255, it will trigger an interrupt and initialize itself ot 0 back again. This interrupt is called as the Timer Interrupt. This interrupt informs the MCU that this particular time has
lapped.

From <https://circuitdigest.com/microcontroller-projects/pic-microcontroller-timer-tutorial>

Initialize registers Page 3


Initialize registers Page 4
Initialize registers Page 5
Timer rollover is an event that happens when a timer register in a PIC18F
microcontroller overflows its maximum value and returns to zero. This is a
natural characteristic of binary arithmetic, and it is intended to ensure that the
timer always maintains an accurate count.

After a timer rollover, the microcontroller will typically generate an interrupt or


trigger an event. This can be used by the programmer to perform a specific
action, such as updating a display or triggering a device.

Initialize registers Page 6


Initialize registers Page 7
Initialize registers Page 8
Initialize registers Page 9
Initialize registers Page 10
Initialize registers Page 11
Initialize registers Page 12
Initialize registers Page 13
Initialize registers Page 14
Initialize registers Page 15

You might also like