0% found this document useful (0 votes)
182 views2 pages

Delay Using Pic Microcontroller

This document contains code for a microcontroller program that uses timers and input/output ports to flash an LED. It initializes the oscillator, timers, and I/O ports. It uses a timer overflow flag to increment a counter, and flashes the LED when the counter reaches a threshold by toggling an output pin. It also waits for input pin state changes to control the LED behavior.

Uploaded by

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

Delay Using Pic Microcontroller

This document contains code for a microcontroller program that uses timers and input/output ports to flash an LED. It initializes the oscillator, timers, and I/O ports. It uses a timer overflow flag to increment a counter, and flashes the LED when the counter reaches a threshold by toggling an output pin. It also waits for input pin state changes to control the LED behavior.

Uploaded by

Basheer Naderi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

/*

* File: DLY1.c
*
*/

#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator).


#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled).
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled).
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR enabled).
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial
Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for
programming).
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data
EEPROM code protection off).
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write
protection off; all program memory may be written to by EECON control).
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code
protection off).
#include <xc.h> // Include the header file.

int COUNT = 0; // COUNT is declared as an integer with


value = 0
int DLY = 0; //

void main(void) //
{ //

PORTC = 0; // Clear PORTC bits


TRISC = 0x03; // Makes RC0 and RC1 of PORTC as inputs
PORTD = 0; // Clear PORTD bits
TRISD = 0xFE; // Make RD0 of PORTD as output
ADCON0 = 0x00; // This makes ADCON0 offTe various TMR1
control bits
OPTION_REG = 0x07; // This sets various TMR0 configuration
INTCON , TMR0IE = 1; //
while (1) // This makes the previous steps
executed only once

{ //

DLY: { //
while (!T0IF); // Stays here upto 256 and then T0IF =
1
T0IF = 0; // Resetting the T0IF over flow flag
COUNT ++; // Increments the counter
if (COUNT == 137) // When Count reaches 137, execute the
next instruction //
{ //
COUNT = 0; //
TMR0 = 0; // Timer starts from 0
while (TMR0 < 137); // do nothing while the TMR0 counts up
to 137. Execute the next instruction when it exceeds 137
} //
} //

PORTD, RD0 = 0; //
while (PORTC,RC0 == 0); // Do nothing while RC0 = low and stay
here. Executes the next instruction When RC0 = 1
goto DLY; //
PORTD, RD0 = 1; //
} //
} //

/*
RC1 = 1, RC0 = 0 LED OFF
RC0 = 0, RC1 = 0 LED OFF

RC1 = 1, RC0 = 1 LED Flashes


RC0 = 1, RC1 = 0 LED ON

*
There are two methods of writing program which I have got from an authorized
text book
But both are wrong. The MPLAB XC8 ver.5. will not identify those statements in
brackets.

while (PORTB.0 == 0); // Wait until PORTB.0 becomes 1


while (PORTB.0); // Same instruction as the previous one

The correct usage is:

while(PORTD,RD0 == 0)

*
Also a statement like PORTD,RD0 = 0 is same as (PORTD,RD0 = 0)
Also a statement like while (PORTC == 0x00); denotes the whole bits are zero and
while (PORTC,RC0 == 0) refers a particular bit which is RC0 of PORTC.
Also a statement like PORTD = 0x01; is same as PORTD,RD0 = 0

*/

You might also like