0% found this document useful (0 votes)
17 views4 pages

h230600n Assignment 1

Uploaded by

greatambsi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

h230600n Assignment 1

Uploaded by

greatambsi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <xc.

h>

// Configuration bits

#pragma config FOSC = HS // High-speed oscillator

#pragma config WDTE = OFF // Watchdog timer off

#pragma config PWRTE = OFF // Power-up timer off

#pragma config MCLRE = ON // MCLR pin enabled

#pragma config CP = OFF // Code protection off

#pragma config BOREN = OFF // Brown-out reset off

#pragma config IESO = OFF // Internal/External Switchover mode off

#pragma config FCMEN = OFF // Fail-Safe Clock Monitor off

// Define the clock frequency

#define _XTAL_FREQ 20000000 // 20 MHz

// Function to generate a 100ms delay using TMR0

void delay_100ms(void) {

T0CON = 0x80; // Enable TMR0, 1:256 prescaler

TMR0H = 0x3C; // Set TMR0 to count to 100ms

TMR0L = 0xB0;

while (TMR0IF == 0); // Wait for TMR0 to overflow

TMR0IF = 0; // Clear TMR0IF

// Main function

void main(void) {

TRISAbits.TRISA0 = 0; // Set RA0 as output

while (1) {

PORTAbits.RA0 = 1; // Set RA0 high


delay_100ms(); // 100ms delay

PORTAbits.RA0 = 0; // Set RA0 low

delay_100ms(); // 100ms delay

return;

}
; Configuration bits

CONFIG FOSC = HS ; High-speed oscillator

CONFIG WDTE = OFF ; Watchdog timer off

CONFIG PWRTE = OFF ; Power-up timer off

CONFIG MCLRE = ON ; MCLR pin enabled

CONFIG CP = OFF ; Code protection off

CONFIG BOREN = OFF ; Brown-out reset off

CONFIG IESO = OFF ; Internal/External Switchover mode off

CONFIG FCMEN = OFF ; Fail-Safe Clock Monitor off

; Define the clock frequency

DEFINE _XTAL_FREQ 20000000 ; 20 MHz

; Delay subroutine

DELAY_100MS

MOVLW D'156' ; Load 156 into WREG (20 MHz / 256 = 78.125 kHz, 1/78.125 kHz = 12.8 us, 100 ms
/ 12.8 us = 7812.5, 7812.5 / 50 = 156.25)

MOVWF TMR0L ; Load WREG into TMR0L

CLRF TMR0H ; Clear TMR0H

BSF T0CON, TMR0ON ; Enable TMR0

DELAY_LOOP

BTFSS T0CON, TMR0IF ; Check if TMR0 has overflowed

GOTO DELAY_LOOP ; If not, loop back

BCF T0CON, TMR0IF ; Clear TMR0IF

RETURN ; Return from subroutine

; Main program

ORG 0x00 ; Start of program

GOTO MAIN ; Jump to main program


MAIN

BCF TRISA, TRISA0 ; Set RA0 as output

LOOP

BSF PORTA, RA0 ; Set RA0 high

CALL DELAY_100MS ; Call delay subroutine

BCF PORTA, RA0 ; Set RA0 low

CALL DELAY_100MS ; Call delay subroutine

GOTO LOOP ; Loop back

END

You might also like