IO Programming
IO Programming
.
Write a C program to send values 00-FFH to PortB.
#include<P18F458.h>
Void main(void)
{
Unsigned char z;
TRISB =0;
for(z=0;z<=255;z++)
PORTB=z;
While(1);
}
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.
#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);
}
Write a C program to Toggle all the bits of PortB continuously
#include<P18F458.h>
Void main(void)
{
TRISB =0;
for(;;)
{
PORTB=0X55;
PORTB=0XAA;
}
}
Write a C program to send values of -4 to +4 to PortB
#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);
}
Write a C Program to toggle all bits of PortB 50,000 times.
#include<P18F458.h>
Void main(void)
{
Unsigned int z;
TRISB =0;
for(z=0;z<=50000;z++)
{
PORTB=0X55;
PORTB=0XAA;
}
While(1);
}
Write a C Program to toggle all bits of PortB 10,000 times.
#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);
}
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.
#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++);
}
Write a C program to toggle all the bits of PortC and PortD
continuously with a 250 ms delay.
#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
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.
#include<P18F458.h>
#define LED PORTC
Void main(void)
{
TRISB = 0;
TRISC = 0;
PORTB = 0X00;
LED = 0;
For(;;)
{
PORTB++;
LED++;
}
}
Write a C program to get a byte of data from PortB, wait ½
second, and then send it to PortC
#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++);
}
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.
#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;
}
}
Write a C program to toggle only bit RB4 continuously without
disturbing the rest of the bits of Port B.
#include<P18F458.h>
#define mybit PORTBbits.RB4
Void main(void)
{
TRISBbits.TRISB4 = 4;
while(1)
{
mybit = 1;
mybit = 0;
}
}
Write a C program to monitor bit PC5. If it is HIGH, send 55H to
Port B; otherwise, send AAH to Port D.
#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;
}
}
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.
#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++);
}
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.
#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);
}
Write a C program to toggle all the bits of Port B, Port C and Port
D continuously with a 250 ms delay.
#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++);
}
Write a C program to turn bit 5 of PortB on and off 50000 times.
#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);
}
Write a C program to get the status of bit RB0 and send it to RC7
continuously.
#include<P18F458.h>
#define inbit PORTBbits.RB0
#define outbit PORTCbits.RC7
Void main(void)
{
TRISBbits.TRISB0 = 1;
TRISCbits.TRISC7 = 0;
while(1)
{
outbit = inbit;
}
}
Bitwise Operators
Write a C program to perform Logical operations on Port B,C and
D.
#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);
}
Write a C program to toggle all the bits of Port B and Port C
continuously with a 250 ms delay.
#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++)
}
Write a C program to toggle all the bits of PortB, C and D
continuously with a 250 ms delay. Use EX-OR operator.
#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++)
}
Write a C program to get bit RB0 and send it to RC7.
#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
#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;
}
Write a C program to convert ASCII digits of ‘4’ and ‘7’ to packed
BCD and display it on PORTB.
#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;
}