Sunteți pe pagina 1din 13

ELCT 912: Advanced Embedded Systems

Lecture 7: PIC Programming In C


Dr. Mohamed Abd El Ghany, Department of Electronics and Electrical Engineering

Why C programming?
It is easier and less time consuming to write in C than Assembly C is easier to modify and update You can use code available in function libraries C code is portable to other microcontrollers with little or no modification You can use Microchips C18 C compiler

www.microdigitaled.com
Dr. Mohamed Abd el Ghany Department of Electronics and Electrical Engineering ELCT 912: Advanced embedded Systems Winter 2011 2

C Data types for the PIC18


Data type Size in bits Data range/usage

Dr. Mohamed Abd el Ghany Department of Electronics and Electrical Engineering

ELCT 912: Advanced embedded Systems Winter 2011

Example
Write a C18 program to send values 00-FF to port B
# include <P18F458.h> Void main(void) // for TRISB and PORTB declarations

{
unsigned char z; TRISB =0; for (z=0;z<=255;z++) // make Port B an output

PORTB =Z;
while (1); }
Dr. Mohamed Abd el Ghany Department of Electronics and Electrical Engineering 4

// needed If running in hardware

ELCT 912: Advanced embedded Systems Winter 2011

Example
Write a C18 program to toggle all the bits of port B continuously # include <P18F458.h> Void main(void) // for TRISB and PORTB declarations

{
TRISB =0; for ( ; ; ) { // make Port B an output // repeat forever

PORTB = 0x55;
PORTB = 0xAA; } }
Dr. Mohamed Abd el Ghany Department of Electronics and Electrical Engineering ELCT 912: Advanced embedded Systems Winter 2011 5

Time delay

There are two ways to create a time delay in C18: Using a simple for loop Using the PIC18 timers

Dr. Mohamed Abd el Ghany Department of Electronics and Electrical Engineering

ELCT 912: Advanced embedded Systems Winter 2011

Time delay
In creating a time delay using a for loop, we must be mindful of two factors that can affect the accuracy of the delay: The crystal frequency connected to the OSC1-OSC2 input pins is the most important factor in the time delay calculation. The duration of the clock period for the instruction cycle is a function of this crystal frequency. The second factor that affects the time delay is the compiler used to compile the C program. In other words, if we compile a given C program with different compilers, each compiler produces different-size hex code. For the above reasons, when we write time delays for C, we must use the oscilloscope to measure the exact duration.
Dr. Mohamed Abd el Ghany Department of Electronics and Electrical Engineering ELCT 912: Advanced embedded Systems Winter 2011 7

Example
Write a C18 program to toggle all the bits of Port B continuously with a 250 ms delay. Assume that the system is PIC18F458 with XTAL = 10MHz.

Dr. Mohamed Abd el Ghany Department of Electronics and Electrical Engineering

ELCT 912: Advanced embedded Systems Winter 2011

Example
LEDs are connected to bits in port B and Port C. Write a C18 program that shows the count from 0 to FFH (0000 0000 to 1111 1111 in binary on the LEDs. # include <P18F458.h> # define LED PORTC Void main(void) { TRISB =0; TRISC =0; PORTB = 00; LED = 0; for( ; ; ) { PORTB ++; LED ++; } } // for TRISB and PORTB declarations

// make Port B an output // make Port C an output //clear Port B //clear Port C

// increment Port B // increment Port C

Dr. Mohamed Abd el Ghany Department of Electronics and Electrical Engineering

ELCT 912: Advanced embedded Systems Winter 2011

Example
Write a C18 program to get a byte of data from Port C. If it is less than 100, send it to port B; otherwise, send it to Port D.

Dr. Mohamed Abd el Ghany Department of Electronics and Electrical Engineering

ELCT 912: Advanced embedded Systems Winter 2011

10

Bit- addressable I/O programming

The I/O ports of PIC18 are bit-addressable. We can access a single bit without disturbing the rest of the port. We use PORTxbits.Rxy to access a single bit of Portx, where x is the port A, B, C, or D, and y is the bit (0-7) of that port.

Dr. Mohamed Abd el Ghany Department of Electronics and Electrical Engineering

ELCT 912: Advanced embedded Systems Winter 2011

11

Example
Write a C18 program to monitor bit PC5. If it is High, send 55H to Port B; otherwise, send AAH to Port D. # include <P18F458.h> // for TRISB and PORTB declarations # define mybit PORTCbits.RC4 // declare single bit Void main(void) { PORTBbits.TRISB4 =1; // make RC4 an intput TRISD =0; TRISB =0; while(1) { if(mybit == 1) PORTB = 0x55; else PORTD = 0xAA; } }
Dr. Mohamed Abd el Ghany Department of Electronics and Electrical Engineering ELCT 912: Advanced embedded Systems Winter 2011 12

Example
The data pins of an LCD are connected to Port B. The information is latched into the LCD whenever its Enable pin goes from HIGH to LOW. Write a C18 program to send The Earth is but One Country to this LCD.
# include <P18F458.h> // for TRISB and PORTB declarations # define En PORTCbits.RC2 // declare single bit the enable pin # define LCDData PORTB // LCDData declaration Void main(void) { unsigned char message[] = The Earth is But One Country; unsigned char z; PORTCbits.TRISC2 =0; // make PortC.2 as output TRISB =0; for(z=0;z<28;z++) { LCDData = message [z]; En=1; // a HIGHEn=0; // -to- LOW pulse to latch the LCD data } while(1); // stay here forever }
Dr. Mohamed Abd el Ghany Department of Electronics and Electrical Engineering ELCT 912: Advanced embedded Systems Winter 2011 13

S-ar putea să vă placă și