Unit-6 Programming in C For PIC18
Unit-6 Programming in C For PIC18
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 z;
TRISB =0;
for(z=0;z<10;z++)
PORTB=mynum[z];
While(1);
Program:
#include<P18F458.h>
Void main(void)
TRISB =0;
for(;;)
PORTB=0X55;
PORTB=0XAA;
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);
Program:
#include<P18F458.h>
Void main(void)
Unsigned int z;
TRISB =0;
for(z=0;z<=50000;z++)
PORTB=0X55;
PORTB=0XAA;
While(1);
Program:
#include<P18F458.h>
Void main(void)
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 main(void)
TRISB = 0;
While(1)
PORTB = 0X55;
MSDelay(250);
PORTB = 0XAA;
MSDelay(250);
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 main(void)
TRISC = 0;
TRISD = 0;
While(1)
PORTC = 0X55;
PORTD = 0X55;
MSDelay(250);
PORTC = 0XAA;
PORTD = 0XAA;
MSDelay(250);
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>
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 main(void)
TRISB = 0XFF;
TRISC = 0;
While(1);
mybyte = PORTB;
MSDelay(500);
PORTC= mybyte;
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)
TRISC = 0xFF;
TRISB = 0;
TRISD = 0;
while(1)
mybyte = PORTC;
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>
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>
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 main(void)
TRISBbits.TRISB1 = 1;
TRISCbits.TRISC7 = 0;
While(Dsensor == 1)
buzzer = 0;
MSDelay(200);
buzzer = 1;
MSDelay(200);
while(1);
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 En PORTCbits.RC2
Void main(void)
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 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);
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>
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>
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 = ~0X55;
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 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 main(void)
TRISB = 0;
TRISC = 0;
TRISD = 0;
PORTB = 0X55;
PORTC = 0X55;
PORTD = 0X55;
while(1)
MSDelay(250);
unsigned int I;
Unsigned char j;
for(i=0;i< itime;i++)
for(j=0;j<165;j++)
Program:
#include<P18F458.h>
void main(void)
TRISBbits.TRISB0 = 1;
TRISCbits.TRISC7 = 0;
while(1)
outbit = ~inbit;
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)
TRISB = 0;
TRISC = 0;
PORTB = x | 0X30;
y=y>>4;
PORTC = y| 0X30;
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)
TRISB = 0;
w = w & 0X0F;
w = w << 4;
z = z & 0X0F;
bcdbyte = w | z;
PORTB = bcdbyte;