100% found this document useful (1 vote)
289 views17 pages

Unit-6 Programming in C For PIC18

The document contains 22 C programming examples for PIC 18 microcontroller programming. The programs demonstrate various I/O operations including sending values to ports, toggling port bits, monitoring ports, logical operations on ports, and data conversion routines. Specifically, programs 1-8 show toggling and sending values to ports, programs 9-14 involve I/O interfacing and monitoring ports, programs 15-21 perform logical operations on ports, and programs 23-24 demonstrate packed BCD to ASCII and ASCII to packed BCD conversions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
100% found this document useful (1 vote)
289 views17 pages

Unit-6 Programming in C For PIC18

The document contains 22 C programming examples for PIC 18 microcontroller programming. The programs demonstrate various I/O operations including sending values to ports, toggling port bits, monitoring ports, logical operations on ports, and data conversion routines. Specifically, programs 1-8 show toggling and sending values to ports, programs 9-14 involve I/O interfacing and monitoring ports, programs 15-21 perform logical operations on ports, and programs 23-24 demonstrate packed BCD to ASCII and ASCII to packed BCD conversions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 17

Unit-6: PIC 18 Microcontroller Programming in C

1. Write a C program to send values 00-FFH to PortB.

Program:

#include<P18F458.h>

Void main(void)

Unsigned char z;

TRISB =0;

for(z=0;z<=255;z++)

PORTB=z;

While(1);

2. Write a C program to send hex values for ASCII characters of 0,1,2,3,4,5,A,B,C and D to PortB.

Program:
#include<P18F458.h>

Void main(void)

Unsigned char mynum[]=“012345ABCD”;

Unsigned char z;

TRISB =0;

for(z=0;z<10;z++)

PORTB=mynum[z];

While(1);

3. Write a C program to Toggle all the bits of PortB continuously.

Program:

#include<P18F458.h>

Void main(void)

TRISB =0;

for(;;)

PORTB=0X55;

PORTB=0XAA;

4. Write a C program to send values of -4 to +4 to PortB.

Program:

#include<P18F458.h>

Void main(void)

char mynum[]={+1,-1,+2,-2,+3,-3,+4,-4};
Unsigned char z;

TRISB =0;

for(z=0;z<8;z++)

PORTB=mynum[z];

While(1);

5. Write a C Program to toggle all bits of PortB 50,000 times.

Program:

#include<P18F458.h>

Void main(void)

Unsigned int z;

TRISB =0;

for(z=0;z<=50000;z++)

PORTB=0X55;

PORTB=0XAA;

While(1);

6. Write a C Program to toggle all bits of PortB 10,000 times.

Program:

#include<P18F458.h>

Void main(void)

Unsigned short long z;

Unsigned int x;

TRISB =0;
for(z=0;z<=10000;z++)

PORTB=0X55;

PORTB=0XAA;

While(1);

7. Write a C program to toggle all the bits of PortB continuously with a 250 ms delay. Assume that the system is
PIC18F458 with XTAL=10 MHz.

Program:

#include<P18F458.h>

Void MSDelay(unsigned int);

Void main(void)

TRISB = 0;

While(1)

PORTB = 0X55;

MSDelay(250);

PORTB = 0XAA;

MSDelay(250);

Void MSDelay(unsigned int itime)

Unsigned int i;

Unsigned char j;

For(i=0;i<itime;i++)

For(j=0;j<165;j++);
}

8. Write a C program to toggle all the bits of PortC and PortD continuously with a 250 ms delay.

Program:

#include<P18F458.h>

Void MSDelay(unsigned int);

Void main(void)

TRISC = 0;

TRISD = 0;

While(1)

PORTC = 0X55;

PORTD = 0X55;

MSDelay(250);

PORTC = 0XAA;

PORTD = 0XAA;

MSDelay(250);

Void MSDelay(unsigned int itime)

Unsigned int i;

Unsigned char j;

For(i=0;i<itime;i++)

For(j=0;j<165;j++);

I/O Programming in C
9. Write a C program that shows the count from 00 to FFH on the LEDs. LEDs are connected to bits in Port B and
PortC.

Program:

#include<P18F458.h>

#define LED PORTC

Void main(void)

TRISB = 0;

TRISC = 0;

PORTB = 0X00;

LED = 0;

For(;;)

PORTB++;

LED++;

10. Write a C program to get a byte of data from PortB, wait ½ second, and then send it to PortC.

Program:

#include<P18F458.h>

Void MSDelay(unsigned int);

Void main(void)

Unsigned char mybyte;

TRISB = 0XFF;

TRISC = 0;

While(1);

mybyte = PORTB;
MSDelay(500);

PORTC= mybyte;

Void MSDelay(unsigned int itime)

Unsigned int i;

Unsigned char j;

For(i=0;i<itime;i++)

For(j=0;j<165;j++);

11. Write a C program to get a byte of data from PortC. If it is less than 100, send it to PortB; otherwise, send it to
Port D.

Program:

#include<P18F458.h>

Void main(void)

unsigned char mybyte;

TRISC = 0xFF;

TRISB = 0;

TRISD = 0;

while(1)

mybyte = PORTC;

if(mybyte < 100)

PORTB = mybte;

else

PORTD = mybyte;

}
}

12. Write a C program to toggle only bit RB4 continuously without disturbing the rest of the bits of Port B.

Program:

#include<P18F458.h>

#define mybit PORTBbits.RB4

Void main(void)

TRISBbits.TRISB4 = 4;

while(1)

mybit = 1;

mybit = 0;

13. Write a C program to monitor bit PC5. If it is HIGH, send 55H to Port B; otherwise, send AAH to Port D.

Program:

#include<P18F458.h>

#define mybit PORTCbits.RC5

Void main(void)

TRISCbits.TRISC5 = 1;

TRISD = 0;

while(1)

if(mybit == 1)

PORTD = 0X55;

else

PORTD = 0XAA;
}

14. A door sensor is connected to the RB1 pin, and a buzzer is connected to RC7. Write a C program to monitor the
door sensor, and when it opens, sound the buzzer. You can sound the buzzer by sending a square wave of a few
hundred Hz frequency to it.

Program:

#include<P18F458.h>

Void MSDelay(unsigned int);

#define Dsensor PORTBbits.RB1

#define buzzer PORTCbits.RC7

Void main(void)

TRISBbits.TRISB1 = 1;

TRISCbits.TRISC7 = 0;

While(Dsensor == 1)

buzzer = 0;

MSDelay(200);

buzzer = 1;

MSDelay(200);

while(1);

Void MSDelay(unsigned int itime)

Unsigned int I;

Unsigned char j;

For(i=0;i<itime;i++)

For(j=0;j<165;j++);
}

15. The data pins of an LCD are connected to PORTB. The information is latched into the LCD whenever its enable
pin goes from HIGH to LOW. Write a C program to send “The Earth is but one Country” to this LCD.

Program:

#include<P18F458.h>

#define LCDData PORTB

#define En PORTCbits.RC2

Void main(void)

unsigned char message[] = “The Earth is but One Country”;

unsigned char z;

TRISB = 0;

TRISCbits.TRISC2 = 0;

for(z=0;z<28;z++)

LCDData = message[z];

En = 1;

En = 0;

while(1);

16. Write a C program to toggle all the bits of Port B, Port C and Port D continuously with a 250 ms delay.

Program:

#include<P18F458.h>

Void MSDelay(unsigned int)

Void main(void)

TRISB = 0;

TRISC = 0;
TRISD = 0;

while(1)

PORTB = 0X55;

PORTC = 0X55;

PORTD = 0X55;

MSDelay(250);

PORTB = 0XAA;

PORTC = 0XAA;

PORTD = 0XAA;

MSDelay(250);

Void MSDelay(unsigned int itime)

Unsigned int I;

Unsigned char j;

For(i=0;i<itime;i++)

For(j=0;j<165;j++);

17. Write a C program to turn bit 5 of PortB on and off 50000 times.

Program:

#include<P18F458.h>

#define MYBIT PORTBbits.RB5

Void main(void)

Unsigned int z;

TRISBbits.TRISB5 = 0;
for(z=0;z<50000;z++)

MYBIT = 1;

MYBIT = 0;

while(1);

18. Write a C program to get the status of bit RB0 and send it to RC7 continuously.

Program:

#include<P18F458.h>

#define inbit PORTBbits.RB0

#define outbit PORTCbits.RC7

Void main(void)

TRISBbits.TRISB0 = 1;

TRISCbits.TRISC7 = 0;

while(1)

outbit = inbit;

Logical Operations:
19. Write a C program to perform Logical operations on Port B,C and D.

Program:

#include<P18F458.h>

Void main(void)

TRISB = 0;
TRISC = 0;

TRISD = 0;

PORTB = 0X35 & 0X0F;

PORTC = 0X04 | 0X68;

PORTD = 0X54 ^ 0X78;

PORTB = ~0X55;

PORTC = 0X9A >> 3;

PORTD = 0X77 >>4;

PORTB = 0X6 << 4;

while(1);

20. Write a C program to toggle all the bits of Port B and Port C continuously with a 250 ms delay.

Program:

#include<P18F458.h>

void MSDelay(unsigned int);

void main(void)

TRISB = 0;

TRISC = 0;

PORTB = 0X55;

PORTC = 0XAA;

while(1)

PORTB = ~ PORTB;

PORTC = ~ PORTC;

MSDelay(250);

}
void MSDelay(unsigned int itime)

unsigned int I;

Unsigned char j;

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

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

21. Write a C program to toggle all the bits of PortB, C and D continuously with a 250 ms delay. Use EX-OR operator.

Program:

#include<P18F458.h>

void MSDelay(unsigned int);

void main(void)

TRISB = 0;

TRISC = 0;

TRISD = 0;

PORTB = 0X55;

PORTC = 0X55;

PORTD = 0X55;

while(1)

PORTB = ~PORTB^ 0XFF;

PORTC = ~PORTC^ 0XFF;

PORTD = ~PORTD^ 0XFF;

MSDelay(250);

void MSDelay(unsigned int itime)


{

unsigned int I;

Unsigned char j;

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

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

22. Write a C program to get bit RB0 and send it to RC7.

Program:

#include<P18F458.h>

#define inbit PORTBbits.RB0

#define outbit PORTCbits.RC7

void main(void)

TRISBbits.TRISB0 = 1;

TRISCbits.TRISC7 = 0;

while(1)

outbit = ~inbit;

Data Conversion Programs in C

Packed BCD to ASCII Conversions

Packed BCD →UnPacked BCD → ASCII


0X29 → 0X02,0X09 → 0X32,0X39

23. Write a C program to convert packed BCD 0X29 to ASCII and display the bytes on PORTB and PORTC.

Program:
#include<P18F458.h>

void main(void)

unsigned char x,y,z;

unsigned char mybyte = 0X29;

TRISB = 0;

TRISC = 0;

x=mybyte & 0X0F;

PORTB = x | 0X30;

y= mybyte & 0XF0;

y=y>>4;

PORTC = y| 0X30;

ASCII to Packed BCD Conversion

ASCII → UnPacked BCD → Packed BCD


0X34,0X37 → 0X04,0X07 → 0X47

24. Write a C program to convert ASCII digits of ‘4’ and ‘7’ to packed BCD and display it on PORTB.

Program:

#include<P18F458.h>

void main(void)

unsigned char bcdbyte;

unsigned char w=‘4’;


unsigned char z=‘7’;

TRISB = 0;

w = w & 0X0F;

w = w << 4;

z = z & 0X0F;

bcdbyte = w | z;

PORTB = bcdbyte;

You might also like