+ All Categories
Home > Documents > Chapter5-Pic Programming in c

Chapter5-Pic Programming in c

Date post: 28-Oct-2014
Category:
Upload: adamwaiz
View: 885 times
Download: 14 times
Share this document with a friend
Popular Tags:
53
PIC PROGRAMMING IN C
Transcript
Page 1: Chapter5-Pic Programming in c

PIC PROGRAMMING IN C

Page 2: Chapter5-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

Page 3: Chapter5-Pic Programming in c

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 Microchip’s MPLAB IDE, allowing source-level debugging with the MPLAB ICE in-circuit emulator, the MPLAB ICD 2 in-circuit debugger or the MPLAB SIM simulator.

Page 4: Chapter5-Pic Programming in c

• 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

Page 5: Chapter5-Pic Programming in c
Page 6: Chapter5-Pic Programming in c
Page 7: Chapter5-Pic Programming in c

STRUKTUR ATURCARA BAHASA C

• Komen tentang aturcara

• Compiler directive

• Configuration word PIC (PIC setting)

• Aturcara utama

Structure Of C Program For PIC18

Page 8: Chapter5-Pic Programming in c

Input and Output(I/O) programming in C

Byte size I/OPorts PORTA-PORTD are byte accessible

Page 9: Chapter5-Pic Programming in c

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

Page 10: Chapter5-Pic Programming in c
Page 11: Chapter5-Pic Programming in 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.

Page 12: Chapter5-Pic Programming in c
Page 13: Chapter5-Pic Programming in c
Page 14: Chapter5-Pic Programming in c

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.

Page 15: Chapter5-Pic Programming in c

#include <P18F4550 . inc >void MSDelay (unsigned int) ; //function prototype#define Dsensor PORTBbits.RB1#define buzzer PORTCbits . RC7void main (void){TRISBbits.TRISB1 = 1; //PORTB.1 as an inputTRISCbits . TRISC7 = 0; //make PORTC . 7 an outputwhile (Dsensor ==1)

{buzzer = 0; //here to make square wave for buzzer output with 200ms intervalMSDelay (5) ; // Hz,f = 1/t = 1/10m = 100Hzbuzzer = 1; //cara untuk memberikan square wave kepada buzzer dengan 200ms intervalMSDelay (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++) ;}

Page 16: Chapter5-Pic Programming in c
Page 17: Chapter5-Pic Programming in c

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.

Page 18: Chapter5-Pic Programming in c
Page 19: Chapter5-Pic Programming in c
Page 20: Chapter5-Pic Programming in c

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.

Page 21: Chapter5-Pic Programming in c
Page 22: Chapter5-Pic Programming in c
Page 23: Chapter5-Pic Programming in c
Page 24: Chapter5-Pic Programming in c

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.

Page 25: Chapter5-Pic Programming in c

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.

Page 26: Chapter5-Pic Programming in c

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).

Page 27: Chapter5-Pic Programming in c

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.

Page 28: Chapter5-Pic Programming in c
Page 29: Chapter5-Pic Programming in c
Page 30: Chapter5-Pic Programming in c

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 PICI8‘s pins: this is called a counter.

Page 31: Chapter5-Pic Programming in c

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.

Page 32: Chapter5-Pic Programming in c

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.

Page 33: Chapter5-Pic Programming in c
Page 34: Chapter5-Pic Programming in c
Page 35: Chapter5-Pic Programming in c

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.

Page 36: Chapter5-Pic Programming in c

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.

Page 37: Chapter5-Pic Programming in c

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.

Page 38: Chapter5-Pic Programming in c

Steps to program Timer0 in 16-bit mode

To generate a time delay using the Timer0 mode 16, the following steps are taken:

1. Load the value into the T0CON register indicating which mode (8-bit or 16- bit) is to be used and the selected prescaler option.

2. Load register TMR0H followed by register TMR0L with initial count values.

3. Start the timer with the instruction "BSF T0CON, TMR0ON".4. Keep monitoring the timer flag (TMR0IF) to see if it is raised.

Get out of the loop when TMR0IF becomes high.5. Stop the timer with the instruction "BCF T0CON, TMR0ON".6. Clear the TMR0IF flag for the next round.7. Go back to Step 2 to load TMR0H and TMR0L again.

Page 39: Chapter5-Pic Programming in c
Page 40: Chapter5-Pic Programming in c
Page 41: Chapter5-Pic Programming in c

• 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.

Page 42: Chapter5-Pic Programming in c
Page 43: Chapter5-Pic Programming in c
Page 44: Chapter5-Pic Programming in c

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.

Page 45: Chapter5-Pic Programming in c

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.

Page 46: Chapter5-Pic Programming in c

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.

Page 47: Chapter5-Pic Programming in c
Page 48: Chapter5-Pic Programming in c
Page 49: Chapter5-Pic Programming in c

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.

Page 50: Chapter5-Pic Programming in c
Page 51: Chapter5-Pic Programming in c

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.

Page 52: Chapter5-Pic Programming in c
Page 53: Chapter5-Pic Programming in c

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


Recommended