Sunday, January 25, 2015

Programs on PIC18

Example 7-1
Write a C18 program to send values 00--FF to Port B.
Solution:
#include <P1BF45B.h>  //for TRISB and PORTB declarations
void main(void)
{
unsigned char z;
TRISB = 0;        //make Port B an output                      
for(z=0;z<=255;z++)
PORTE = z;
while (1);  //NEEDED IF RUNNING IN HARDWARE
}
Run the above program on your simulator to see how Port B displays values 00--FFH in
binary. Notice that "while(l )" is needed if this program is running in hardware.

Example 7-2
Write a C18 program to send hex values for ASCII characters of 0, 1, 2, 3, 4, 5, A, B,
C, and D to Port B.
Solution:
#include <P18F458.h>
void main(void)
{
unsigned char mynum[]= "012345ABCD"; //data is stored in RAM
unsigned char z;
TRISB = 0;     //make Port B an output
for(z=0;z<10;z++)
PORTB = mynum[z];
while(l);
}           //stay here forever
Run the above program on your simulator to see how Port B displays values 30H,
31H, 32H, 33H, 34H, 35H, 41H, 42H, 43H, and 44H (the hex values for ASCII 0, 1,
2, etc.). Notice that the last statement "while(l)" is needed only if we run the program
in hardware. This is like "GOTO $" or "BRA$" in Assembly language.


Example 7-3
Write a C 18 program to toggle all the bits of Port B continuously.
Solution:
II Toggle PB forever
#include <P18F458.h>
void main(void)
{
TRISB = 0;
for (;;)
{
//make Port B an output
//repeat forever
PORTB
PORTB
Ox55; //Ox indicates the data is in hex (binary)
OxAA;
Run the above program on your simulator to see how Port B toggles continuously.

Example 7-4
Write a C18 program to send values of -4 to +4 to Port B.
Solution:
//sign numbers
#include <P18F45B.h>
void main(void)
{
char mynum[]= {+1,-1,+2,-2,+3,-3,+4,-4};
unsigned char z;
TRISB = 0; I /make Port B an output
for(z=O;z<S;z++)
PORTE = mynum[z];
while (1); //stay here forever
Run the above program on your simulator to see how PORTB displays values of 1, FFH,
2, FEH, 3, FDH, 4, and FCH (the hex values for +1, ~1, +2, ~2, etc.). See Chapter 5 for
discussion of signed numbers.




Example 7-5
Write a C18 program to toggle all bits of Port B 50,000 times.
Solution:
#include <P18F458.h>
void main(void)
{
unsigned int z;
TRISB = 0;
for(z=0;z<=50000;z++)
{
}
PORTB
PORTB
while(1);
Ox55;
OxAA;
}
//make Port B an output
//stay here forever
Run the above program on your simulator to see how Port B toggles continuously.
Notice that the maximum value for unsigned int is 65,535.

Example 7-6
Write a C 18 program to toggle all bits of Port B 100,000 times.
Solution:
//toggle PB 100,00 times
#include <P18F458.h>
void main(void)
{
256
unsigned short long z;
unsigned int x;
TRISB = 0;
for(z=0;Z<=10000;z++)
{
PORTB
PORTB
while (1);
Ox55;
OxAA;
}

Example 7-7
Write a C 18 program to toggle all the bits of Port B ports continuously with a 250 ms delay. Assume that the system is PIC18F458 with XTAL = 10 MHz.
Solution:
#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++);
}

Example 7-11
Write a C 18 program to get a byte of data from Port C. If it is less than I 00, send it to Port B; otherwise, send it to Port D.
Solution:
#include <P18F458.h>
void main(void)
{
260
unsigned char mybyte;
TRISC OxFF;
TRISE = 0;
TRISD = 0;
while (1)
{
}


Example 7-12
Write a C 18 program to toggle only bit RB4 continuously without disturbing the rest of the bits of Port B.
Solution:
#include <P18F458.h>
#define mybit PORTBbits.RB4 //declare single bit
void main(void)
{
TRISBbits.TRISB4=0;          // make RB4 an output
while (1)
{
mybit = 1;                 //turn on RB4
mybit = 0;                 //turn off RB4
}
}


Example 7-13
Write a Cl8 program to monitor bit PC5. If it is HIGH, send 55H to Port B; otherwise, send AAH to Port D.
Solution:
#include <P18F458.h>
#define mybit PORTCbits.RC5   //notice single-bit declaration
void main(void)
{
TRISCbits.TRISC5 = 1;         //RCS as input
TRISC = 0;
TRISB = 0;                             //Ports C and B output
while(1)
{
if (mybit ==1)
PORTB = 55H;
else
PORTC = AAH;
}
}





Example 7-14
A door sensor is connected to the RBI pin, and a buzzer is connected to RC7. Write a C18 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.
Solution:
#include <P18F458.h>
void MSDelay(unsigned int);
#define Dsensor PORTBbits.RB1
#define buzzer PORTCbits .RC7
void main(void)
{
TRISBbits.TRISB1 = 1;   //PORTB.1 as an input
TRISCbits.TRISC7 = 0;   //make PORTC.7 an output

while(Dsensor == 1)
{
buzzer = 0;
MSDelay(200) ;
buzzer = 1;
MSDelay(200);
while (1); //stay here forever
void MSDelay(unsigned int itime )
{
unsigned int i;
unsigned char j ;
for(i=0;i<itime;i++)
for(j=0;j<165;j++)
}


No comments: