Sunteți pe pagina 1din 53

PIC PROGRAMMING IN C

Understand C Programming For Embedded System


C was developed in 1974 in order to write the UNIX operating system C is more "low level" than other high level languages (good for MCU programming) C is supported by compilers for a wide variety of MCU architectures C can do almost anything assembly language can do C is usually easier and faster for writing code than assembly language

The MPLAB C Compiler for PIC18 MCUs


The MPLAB C18 compiler is a free-standing, optimizing ANSI C compiler for the PIC18 PICmicro microcontrollers (MCU). The compiler deviates from the ANSI standard X3.159-1989 only where the standard conflicts with efficient PICmicro MCU support. The compiler is a 32-bit Windows console application and is fully compatible with Microchips MPLAB IDE, allowing sourcelevel debugging with the MPLAB ICE in-circuit emulator, the MPLAB ICD 2 in-circuit debugger or the MPLAB SIM simulator.

The compiler convert high level programming language to machine instruction of the target processor. Mixed language programming using C-language with assembly language is supported by the C18 compiler. Assembly blocks are surrounded with at _asm and a _endasm directives to the C18 compiler. Assembly code can also be located in a separate asm file

Structure Of C Program For PIC18 STRUKTUR ATURCARA BAHASA C


Komen tentang aturcara Compiler directive

Configuration word PIC (PIC setting) Aturcara utama

Input and Output(I/O) programming in C


Byte size I/O Ports PORTA-PORTD are byte accessible

Write a C 18 program to get a byte of data from Port B, wait 112 second, and then send it to Port C.

Bit-addressable I/O programming


The l/O ports of PIC 18 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. For example, PORTBbits.RB7 indicates PORTB.7. We access the TRISx registers in the same way where TRISBbits.TRISB7 indicates the D7 of the TRISB.

A door sensor is connected to the RB I pin, and a buzzer is connected to RC7. Write a CI8 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 <P18F4550 . inc > void MSDelay (unsigned int) ; //function prototype #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; //here to make square wave for buzzer output with 200ms interval MSDelay (5) ; // Hz,f = 1/t = 1/10m = 100Hz buzzer = 1; //cara untuk memberikan square wave kepada buzzer dengan 200ms interval MSDelay (5) ; // } while (1) ; //stay here forever } void MSDelay(unsigned int itime) { unsigned int i; unsigned char j; for(i=O;i <itime;i++ ) for (j=0;j<l65;j++) ; }

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 CI8 program to send "The Earth is but One Country" to this LCD.

LOGIC OPERATIONS IN C
One of the most important and powerful features of the C language is its ability to perform bit manipulation. While every C programmer is familiar with the logical operators AND (&&), OR (II), and NOT (!), many C programmers are less familiar with the bitwise operators AND (&), OR (I), EX-OR (^), inverter (~), shift right (>>), and shift left(<<)). These bit-wise operators are widely used in software engineering for embedded systems and control; consequently, their understanding and mastery are critical in microprocessor-based system design and interfacing.

DATA CONVERSION PROGRAMS IN C


Recall that BCD numbers were discussed earlier. As stated there, many newer microcontrollers have a real-time clock (RTC) where the time and date are kept even when the power is off. Very often the RTC provides the time and date in packed BCD. To display them, however, it must convert them to ASCII. In this section we show the application of logic and rotate instructions in the conversion of BCD and ASCII.

ASCII numbers
On ASCII keyboards, when the key "0" is activated, "0 II 0000" (30H) is provided to the computer. Similarly, 31H (0110001) is provided for the key "1", and so on.

Packed BCD to ASCII conversion


The RTC provides the time of day (hour, minute, second) and the date (year, month, day) continuously, regardless of whether the power is on or off. This data is provided in packed BCD, however. To convert packed BCD to ASCII, you must first convert it to unpacked BCD. Then the unpacked BCD is tagged with 011 0000 (30H).

ASCII to packed BCD conversion


To convert ASCII to packed BCD, you first convert it to unpacked BCD (to get rid of the 3), and then combine to make packed BCD. For example, 4 and 7 on the keyboard give 34H and 37H, respectively. The goal is to produce 47H or "0100 0111 ", which is packed BCD.

Understand Programming Timer


The PICI8 has two to five timers depending on the family member. They are referred to as Timers 0, I, 2, 3, and 4. They can be used either as timers to generate a time delay or as counters to count events happening outside the microcontroller.

Every timer needs a clock pulse to tick. The clock source can be internal or external. If we use the internal clock source, then 1/4th of the frequency of the crystal oscillator on the OSCI and OSC2 pins (Fosc/4) is fed into the timer.
Therefore, it is used for time delay generation and for that reason is called a timer. By choosing the external clock option, we feed pulses through one of the PICI8s pins: this is called a counter.

Basic registers of the timer


Many of the PIC 18 timers are 16 bits wide. Because the PIC 18 has an 8-bit architecture, each 16-bit timer is accessed as two separate registers of low byte (TMRxL) and high byte (TMRxH). Each timer also has the TCON (timer control) register for setting modes of operation.

T0CON (Timer0 control) register


Each timer has a control register, called TCON, to set the various timer operation modes. T0CON is an 8-bit register used for control of Timer0.

TMR0IF flag bit


Notice that the TMR0IF bit (Timer0 interrupt flag) is part of the INTCON (interrupt control) register. when the timer reaches its maximum value of FFFFH, it rolls over to 0000, and TMR0IF is set to I.

16-bit timer programming


The following are the characteristics and operations of 16-bit mode: 1. It is a 16-bit timer; therefore, it allows values of 0000 to FFFFH to be loaded into the registers TMR0H and TMR0L. 2. After TMR0H and TMR0L are loaded with a 16-bit initial value, the timer must be started. This is done by "BSF T0CON, TMR0ON" for Timer0. 3. After the timer is started, it starts to count up. It counts up until it reaches its limit of FFFFH. When it rolls over from FFFFH to 0000, it sets HIGH a flag bit called TMR0IF (timer interrupt flag, which is part of the INTCON register). This timer flag can be monitored. When this timer flag is raised, one option would be to stop the timer.

4. After the timer reaches its limit and rolls over, in order to repeat the process, the registers TMR0H and TMR0L must be reloaded with the original value, and the TMR0IF flag must be reset to 0 for the next round.

Steps to program Timer0 in 16-bit mode


To generate a time delay using the Timer0 mode 16, the following steps are taken: Load the value into the T0CON register indicating which mode (8-bit or 16- bit) is to be used and the selected prescaler option. Load register TMR0H followed by register TMR0L with initial count values. Start the timer with the instruction "BSF T0CON, TMR0ON". Keep monitoring the timer flag (TMR0IF) to see if it is raised. Get out of the loop when TMR0IF becomes high. Stop the timer with the instruction "BCF T0CON, TMR0ON". Clear the TMR0IF flag for the next round. Go back to Step 2 to load TMR0H and TMR0L again.

1.

2.
3. 4. 5. 6. 7.

Write a CI8 program to generate a frequency of 2 Hz only on pin PORTB.5. Use Timer0, 8-bit mode to create the delay.

COUNTER PROGRAMMING
We used the timers of the PICI8 to generate time delays. These timers can also be used as counters to count events happening outside the PIC 18. When it is used as a counter, however, it is a pulse outside the PIC 18 that increments the TH, TL registers. In counter mode, notice that registers such as T0CON, TMR0H, and TMR0L are the same as for the timer discussed in the last section; they even have the same names.

T0CS bit in T0CON register


Recall from the last section that the T0CS bit (Timer0 clock source) in the T0CON register decides the source of the clock for the timer. If T0CS = 0, the timer gets pulses from the crystal oscillator connected to the OSC I and OSC2 pins (Fosc/4). In contrast, when T0CS = I, the timer is used as a counter and gets its pulses from outside the PIC 18. Therefore, when T0CS = I, the counter counts up as pulses are fed from pin RA4 (PORTA.4). The pin is called T0CKI (Timer0 clock input). Notice that the pin belongs to Port A. In the case of Timer0, when T0CS = I, pin RA4 (PORTA.4) provides the clock pulse and the counter counts up for each clock pulse coming from that pin. Similarly, for Timer I, when TMRI CS = I, each clock pulse coming in from pin RC0 (PORTC.0) makes the counter count up.

Example - counter
Assume that a 1Hz external clock is being fed into pin T0CKI (RA4). Write a CI8 program for Counter0 in 8-bit mode to count up and display the state of the TMR0L count on PORTB. Start the count at 0H.

example
Assume that a 1Hz external clock is being fed into pin T0CKI (RA4). Write a C program for Counter 0 in mode 1 (16-bit) to count the pulses and display the TMR0H and TMR0L registers on PORTD and PORTB, respectively.

example
Assume that a 60Hz external clock is being fed into pin T0CKI (RA4). Write a C program for Counter 0 in 8-bit mode to display the seconds and minutes on PORTB and PORTD, respectively.

End chapter 5. Please read/study the c programming language

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