Exp.1: LED, Buzzer and Switch interfacing withPIC18Fcontroller







CODE

#include<P18F4550.h>

void delay(unsigned int time)

{

unsigned int i, j;

for(i=0;i<time;i++)

for(j=0;j<1000;j++);

}

void main( )

{

unsigned char i, key = 0;

TRISB = 0x00; //LED pins as output

LATB = 0x00;

ADCON1 = 0x0F; //set pins as Digital

TRISAbits.TRISA2 = 1; //set RA2 as input

TRISAbits.TRISA3 = 1; //set RA3 as input

TRISAbits.TRISA5 = 0; //set buzzer pin RA5 as output

while(1)

{

if(PORTAbits.RA2 == 0) key =0; //If switch1 pressed

if(PORTAbits.RA3 == 0) key =1; //If switch2 pressed

if(key = = 0)

{

LATAbits.LATA5 = 0; //Buzzer OFF

for(i=0; i<8; i++) //Chase LED right to left

{

LATB = 1<<i;

delay(100);

LATB = 0x00;

delay(100);

}

}

if(key = = 1)

{

LATAbits.LATA5 = 1; //Buzzer ON

for(i=7; i> 0 ;i--) //Chase LED left to right

{

LATB = 1<<i;

delay(100);

LATB = 0x00;

delay(100);

}

}

}

}

Comments