[go: up one dir, main page]

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

PIC Codes

The document provides programming examples for interfacing microcontrollers, specifically the PIC18F452, with LEDs and switches using the C18 programming language. It includes code snippets for toggling ports, using arrays for LED control, and interfacing a 7-segment display. Additionally, it explains how to configure ports as input or output and includes delay functions for timing control.

Uploaded by

hamsa
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)
4 views4 pages

PIC Codes

The document provides programming examples for interfacing microcontrollers, specifically the PIC18F452, with LEDs and switches using the C18 programming language. It includes code snippets for toggling ports, using arrays for LED control, and interfacing a 7-segment display. Additionally, it explains how to configure ports as input or output and includes delay functions for timing control.

Uploaded by

hamsa
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

Electronics &Communication Engineering

Microcontrollers and
Applications Lab

 To configure any port (8bits) as output write TRISD = 0x00;


 To configure any port as output write TRISD = 0xFF;

1a LED interface(8 bit) to PIC18F452

Write C18 program to toggle Port D continuously with some delay:

#include<p18f452.h>
void delay(void);
void main()
{
TRISD=0x00;
while(1)
{
PORTD=0xff;
delay();
PORTD=0x00;
delay();
}
}

void delay(void)
{
unsigned int i;
for(i=0;i<=20000;i++);
}
Electronics &Communication Engineering
Microcontrollers and
Applications Lab

Write C18 program to alternate toggle Port D continuously with some delay and
array method:

#include<p18f452.h>
void delay(void);
void main()
{

int j;
int a[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
TRISD=0x00;
while(1)
{
for(j=0;j<=7;j++)
{
PORTD=a[j];
delay();
}
}
}

void delay(void)
{
unsigned int i;
for(i=0;i<=10000;i++);
}
Electronics &Communication Engineering
Microcontrollers and
Applications Lab

1b Switch and LED interface (1 bit)

 Any bit configured as output TRISDbits.TRISD0=0;


 Any bit configured as input TRISDbits.TRISD0=1;
 To write a data to any bit PORTDbits.RD0=1;

#include<p18f452.h> include<p18f452.h>
void delay(void); #define sw PORTDbits.RD0
void main() #define led PORTDbits.RD1
{ void main()
TRISDbits.TRISD0=0; {
while(1) TRISDbits.TRISD0=1;
{ TRISDbits.TRISD1=0;
PORTDbits.RD0=1; while(1)
delay(); {
PORTDbits.RD0=0; if(sw==1)
delay(); {
} led=1;
} }
else
void delay(void) {
{ led=0;
int i;
}
for(i=0;i<=10000;i++);
} }
}
Electronics &Communication Engineering
Microcontrollers and
Applications Lab

2 7 Segment Display to PIC18F452

#include<p18F452.h>
#pragma config WDT=OFF
void delay(void);
void main()
{
unsigned int k;
unsigned int a[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
TRISD=0x00;

while(1)
{
for(k=0;k<=9;k++)
PORTD=a[k];
delay();
}

}
void delay(void)
{
unsigned int i,j;
for(j=0;j<5;j++)
for(i=0;i<2000;i++);
}

You might also like