Date post: | 02-Apr-2018 |
Category: |
Documents |
Upload: | minh-hoang |
View: | 216 times |
Download: | 0 times |
of 33
7/27/2019 ESD-Ch3
1/33
3/12/2012
1
I HC QUC GIA TP.H CH MINHTRNG I HC BCH KHOA
KHOAIN-IN TB MN K THUT IN T
11
Embedded System Design
Chapter 3: C Programming for PICMicrocontroller
B mn K Thut in T - HBK
References
Textbook
Martin Bates, Programming 8-bit PIC Microcontrollers inC, Newnes, 2008
Many C compilers for PIC:MikroC (www.mikroe.com)
PICC18 (www.htsoft.com)
MPLAB C18, C30 (www.microchip.com)
CCS C (www.microchipc.com/reviews/CCS_C/)
2
7/27/2019 ESD-Ch3
2/33
3/12/2012
2
B mn K Thut in T - HBK
Outline
2.1 PIC16 C Getting Started
Simple program and test circuit
Variables, looping, and decisions
SIREN program
2.2 PIC16 C Program Basics
Variables
Looping
Decisions
2.3 PIC16 C Data Operations
Variable types
Floating point numbers
Characters
Assignment operators
2.4 PIC16 C Sequence Control
While loops Break, continue, goto If, else, switch
2.5 PIC16 C Functions and Structure
Program structure Functions, arguments Global and local variables
2.6 PIC16 C Input and Output
RS232 serial data Serial LCD Calculator and keypad
2.7 PIC16 C More Data Types
Arrays and strings Pointers and indirect addressing Enumeration
3
B mn K Thut in T - HBK
2.1 PIC16 C Getting Started
Microcontroller programs contain three main features:
Sequences of instructions Conditional repetition of sequences Selection of alternative sequences
Listing 2.1 A program to output a binary code
/* Source code file: OUTNUM.C
Author, date, version: MPB 11-7-07 V1.0
Program function: Outputs an 8-bit code
Simulation circuit: OUTBYTE.DSN
*******************************************************/
#include "16F877A.h" // MCU select
void main() // Main block
{
output_D(255); // Switch on outputs
}
4
7/27/2019 ESD-Ch3
3/33
3/12/2012
3
B mn K Thut in T - HBK
Figure 2.1 MPLAB IDE Screenshot
5
B mn K Thut in T - HBK
Figure 2.2 ISIS dialogue to attach program
6
7/27/2019 ESD-Ch3
4/33
3/12/2012
4
B mn K Thut in T - HBK
Setup IO Ports Port modes
#use fast_io(port): leaves the state of the port the same unless re-configured
#use fixed_io(port_outputs=pin, pin): permanently sets up the datadirection register for the port
#use standard_io(port): default for configuring the port every time its used
Set directions set_tris_a(value);
value = get_tris_a();
Read / write IO ports value = input_A();
output_A(value);
output_high(pin); //set an output to logic 1
output_low(pin); //set an output to logic 0
7
B mn K Thut in T - HBK
PIC16 C Program Basics Variables Looping Decisions
The purpose of an embedded program is to read in data or control inputs, to process them and operate the outputs as required.
The program for processing the data usually contains repetitive loopsand conditional branching, which depends on an input or calculatedvalue.
2.2 PIC16 C Program Basics
8
7/27/2019 ESD-Ch3
5/33
3/12/2012
5
B mn K Thut in T - HBK
Variables
Variables: is a label attached to the memory location where the variable value is
stored.
automatically assigned to the next available location or locations (manyvariable types need more than 1 byte of memory).
must be declared at the start of the program block, so that the compilercan allocate a corresponding set of locations.
Only alphanumeric characters (az, AZ, 09) can be used for variablenames
Variable values
in decimal by default in hexadecimal with the prefix 0x, for example, 0xFF
By default, the CCS compiler is not case sensitive,
9
B mn K Thut in T - HBK
Listing 2.2 Variables
/* Source code file: VARI.C
Author, date, version: MPB 11-7-07 V1.0
Program function: Outputs an 8-bit variable
Simulation circuit: OUTBYTE.DSN
*******************************************************/
#include "16F877A.h"
void main()
{
int x; // Declare variable and type
x=99; // Assign variable value
output_D(x); // Display the value in binary
}
10
7/27/2019 ESD-Ch3
6/33
3/12/2012
6
B mn K Thut in T - HBK
Looping Most real-time applications need to execute continuously until the
processor is turned off or reset.
In C this can be implemented as a while loop, as in Listing 2.3 .
/* Source code file: ENDLESS.C
Author, date, version: MPB 11-7-07 V1.0
Program function: Outputs variable count
Simulation circuit: OUTBYTE.DSN
*******************************************************/
#include "16F877A.h
void main()
{
int x; // Declare variable
while(1) // Loop endlessly
{ output_D(x); // Display value
x++; // Increment value
}
}
11
B mn K Thut in T - HBK
Decision Making
The effect of the programis to switch on the output ifthe input is high.The switch needs to beclosed before running tosee this effect.The LED cannot beswitched off again until theprogram is restarted.
The simplest way to illustrate basic decision making is to change an outputdepending on the state of an input.
Figure 2.4 show test circuit with input switch
12
7/27/2019 ESD-Ch3
7/33
3/12/2012
7
B mn K Thut in T - HBK
Listing 2.4 IF statement
/* Source code file: IFIN.C
Author, date, version: MPB 11-7-07 V1.0
Program function: Tests an input
Simulation circuit: INBIT.DSN
*******************************************************/
#include "16F877A.h"
void main()
{
int x; // Declare test var.
output_D(0); // Clear all outputs
while(1) // Loop always
{x = input(PIN_C0); // Get input
if(x==1)output_high(PIN_D0); // Change out
}
}
13
B mn K Thut in T - HBK
Loop Control
The program can be simplified by combining the input functionwith the condition statement as follows:
if (input(PIN_C0)) output_high(PIN_D0);
The conditional sequence can also be selected by a whilecondition.
In Program WHILOOP.C ( Listing 2.5 ) the input is tested in the loop condition statement and the output flashed on
and off while the switch is open (input high).
If the switch is closed, the flash loop is not executed and the LED is switched off.
14
7/27/2019 ESD-Ch3
8/33
3/12/2012
8
B mn K Thut in T - HBK
Listing 2.5 Conditional loop
/* Source code file: WHILOOP.C
Author, date, version: MPB 11-7-07 V1.0
Program function: Input controls output loop
Simulation circuit: INBIT.DSN
*******************************************************/
#include "16F877A.h"
#use delay (clock=1000000) // MCU clock = 1MHz
void main(){
while(1)
{
while(input(PIN_C0)); // Repeat while switch open
{ output_high(PIN_D0);
delay_ms(300); // Delay 0.3s
output_low(PIN_D0);delay_ms(500); // Delay 0.5s
}
output_low(PIN_D0); // Switch off LED
}
}
15
B mn K Thut in T - HBK
FOR Loop
The WHILE loop repeats until some external event or internallymodified value satisfies the test condition.
In other cases, we need a loop to repeat a fixed number oftimes.
The FOR loop uses a loop control variable, which is set to aninitial value and modified for each iteration while a defined
condition is true.
In the demo program FORLOOP.C ( Listing 2.6 ), the loop controlparameters are given within the parentheses that follow the for
keyword.
16
7/27/2019 ESD-Ch3
9/33
3/12/2012
9
B mn K Thut in T - HBK
FOR Loop
17
B mn K Thut in T - HBK
SIREN Program
A program combining some of these basic features is shown inSIREN.C ( Listing 2.7 ).
This program outputs to a sounder rather than an LED,operating at a higher frequency.
The output is generated when the switch is closed (input C0low).
The delay picks up the incrementing value of step giving alonger pulse each time the for loop is executed.
This causes a burst of 255 pulses of increasing length (reducingfrequency), repeating while the input is on.
Note that 255 is the maximum value allowed for step, as it isan 8-bit variable.
18
7/27/2019 ESD-Ch3
10/33
3/12/2012
10
B mn K Thut in T - HBK
Listing 2.7 Siren Program
/* Source code file: SIREN.C
Author, date, version: MPB 11-7-07 V1.0
Program function: Outputs a siren sound
Simulation circuit: INBIT.DSN
*******************************************************/
#include "16F877A.h"
#use delay (clock=1000000)
void main()
{
int step;
while(1)
{
while(!input(PIN_C0)) // loop while switch ON
{
for(step=0;step
7/27/2019 ESD-Ch3
11/33
3/12/2012
11
B mn K Thut in T - HBK
Blank Program
A blank program is shown in Listing 2.8 , which could be usedas a general template.
We should try to be consistent in the header commentinformation, so a standard comment block is suggested.
Compiler directives are preceded by hash marks and placedbefore the main block.
Other initialization statements should precede the start of themain control loop. Inclusion of the unconditional loop option
while(1) assumes that the system will run continuously until
reset.
21
B mn K Thut in T - HBK
Table 2.1 A basic set of CCS C components
Compiler Directives#include source files Include another source code or header file#use functions(parameters) Include library functions
C Blocks
main(condition) {statements } Main program block
while(condition) {statements } Conditional loopif(condition) {statements } Conditional sequencefor(condition) {statements } Preset loop
C Functions
delay_ms(nnn) Delay in millisecondsdelay_us(nnn) Delay in microsecondsoutput_x(n) Output 8-bit code at Port Xoutput_high(PIN_nn) Set output bit highoutput_low(PIN_nn) Set output bit lowinput(PIN_nn) Get input
22
7/27/2019 ESD-Ch3
12/33
3/12/2012
12
B mn K Thut in T - HBK
Variable types Floating point numbers Characters Assignment operators
A main function of any computer program is to carry out calculationsand other forms of data processing. Data structures are made up ofdifferent types of numerical and character variables, and a range ofarithmetical and logical operations are needed.
Microcontroller programs do not generally need to process largevolumes of data, but processing speed is often important.
2.3 PIC16 C Data Operations
23
B mn K Thut in T - HBK
Table 2.1 Integer Variables
Name Type Min Max
int1 1 bit 0 1
unsigned int8 8 bits 0 255
signed int8 8 bits -127 +127
unsigned int16 16 bits 0 65525
signed int16 16 bits -32767 +32767
unsigned int32 32 bits 0 4294967295
signed int32 32 bits -2147483647 +2147483647
24
7/27/2019 ESD-Ch3
13/33
3/12/2012
13
B mn K Thut in T - HBK
Table 2.2 Microchip/CCS Floating Point Number Format
Exponent Sign Mantissa
xxxx xxxx x xxx xxxx xxxx xxxx xxxx xxxx
8 bits 1 23 bits
Table 2.4 Example of 32-bit floating point number conversion
Mantissa: 101 0010 0000 0000 0000
0000
Exponent: 1000 0011
Sign: 1 = negative number
FP number: 1000 0011 1101 0010 0000 0000 0000
0000
25
B mn K Thut in T - HBK
Figure 2.5 Variable Types
26
7/27/2019 ESD-Ch3
14/33
7/27/2019 ESD-Ch3
15/33
3/12/2012
15
B mn K Thut in T - HBK
Figure 2.6 Variable Operations
29
B mn K Thut in T - HBK
Table 2.7 Conditional Operators
Operation Symbol EXAMPLE
Equal to == if(a == 0) b=b+5;
Not equal to != if(a != 1) b=b+4;
Greater than > if(a > 2) b=b+3;
Less than < if(a < 3) b=b+2;
Greater than or equal to >= if(a >= 4) b=b+1;
Less than or equal to
7/27/2019 ESD-Ch3
16/33
3/12/2012
16
B mn K Thut in T - HBK
2.4 PIC16 C Sequence Control
While loops
Break, continue, goto
If, else, switch
Conditional branching operations are a basic feature of anyprogram.
These must be properly organized so that the programstructure is maintained and confusion avoided.
The program then is easy to understand and more readilymodified and upgraded.
31
B mn K Thut in T - HBK
While Loops
The basic while(condition) provides a logical test at the start ofa loop, and the statement block is executed only if the
condition is true.
It may, however, be desirable that the loop block be executedat least once, particularly if the test condition is affected
within the loop.
This option is provided by the do..while(condition) syntax. Thedifference between these alternatives is illustrated in Figure
2.7 .
32
7/27/2019 ESD-Ch3
17/33
3/12/2012
17
B mn K Thut in T - HBK
ConditionTrue?
StatementBlock Condition
True?
StatementBlock
(a) While loop (b) Do..While loop
Figure 2.3.1 Comparison of While and Do..While Loop
33
B mn K Thut in T - HBK
Listing 2.9 DOWHILE.C contains both types ofwhile loop
// DOWHILE.C
// Comparison of WHILE and DO WHILE loops
#include "16F877A.H
main()
{
int outbyte1=0;
int outbyte2=0;
int count;
count=0; // This loop is notwhile (count!=0) // executed
{ output_C(outbyte1);
outbyte1++;
count--;
}
count=0; // This loop is
do // executed
{ output_C(outbyte2);
outbyte2++;
count--;
} while (count!=0);
while(1){};
}
34
7/27/2019 ESD-Ch3
18/33
3/12/2012
18
B mn K Thut in T - HBK
Break, Continue, and Goto
Break and continue It may sometimes be necessary to breakthe execution of a loop or
block in the middle of its sequence.
The block must be exited in an orderly way, and it is useful to have theoption of restarting the block (continue) or proceeding to the next one
(break).
Goto Occasionally, an unconditional jump may be needed, but this should
be regarded as a last resort, as it tends to threaten the program
stability.
It is achieved by assigning a label to the jump destination andexecuting a goto..label.
35
B mn K Thut in T - HBK
label
StatementBlock
ContinueGoto
Break
Figure 2.8 Break, continue and goto
36
7/27/2019 ESD-Ch3
19/33
3/12/2012
19
B mn K Thut in T - HBK
Listing 2.10 Continue, Break & Goto
// CONTINUE.C
// Continue, break and goto jumps
#include "16F877A.H"
#use delay(clock=4000000)
main()
{
int outbyte;
again: outbyte=0; // Goto destination
while(1)
{
output_C(outbyte); // Loop operation
delay_ms(10);
outbyte++;
if (!input(PIN_D0)) continue; // Restart loopif (!input(PIN_D1)) break; // Terminate loop
delay_ms(100);
if (outbyte==100) goto again; // Unconditional jump
}
}
37
B mn K Thut in T - HBK
Figure 2.9 Comparison of If and If..Else
Ifblock
ConditionTrue?
YES
NO
ConditionTrue?
Ifblock
Elseblock
YES NO
If..Else and Switch..Case
If .. Else: allows a block to be executed or skipped conditionally.
Switch..case : When we need a multi-choice selection
38
7/27/2019 ESD-Ch3
20/33
3/12/2012
20
B mn K Thut in T - HBK
Test Variable
Value = 3? Procedure 3YES
NO
Value = n?Procedure nYES
NO
DefaultProcedure
Value = 2? Procedure 2YES
NO
Value = 1? Procedure 1YES
NO
Figure 2.10 Switch..case branching structure
39
B mn K Thut in T - HBK
Listing 2.11 Comparison of Switch and If..Else control
// SWITCH.C
// Switch and if..else sequence control
// Same result from both sequences
#include "16F877A.h
void main()
{
int8 inbits;
while(1) {inbits = input_D(); // Read input byte
// Switch..case option........................
switch(inbits) // Test input byte
{
case 1: output_C(1); // Input = 0x01,output = 0x01
break; // Quit block
case 2: output_C(3); // Input = 0x02,output = 0x03
break; // Quit block
case 3: output_C(7); // Input = 0x03,output = 0x07
break; // Quit block
default:output_C(0); // If none of these,output= 0x00
}
40
7/27/2019 ESD-Ch3
21/33
7/27/2019 ESD-Ch3
22/33
3/12/2012
22
B mn K Thut in T - HBK
2.5 PIC16 C Functions and Structures
Program structure
Functions, arguments
Global and local variables
The structure of a C program is created using functions. This is a block of code written
and executed as a self-contained process, receiving the required parameters (data
to be processed) from the calling function and returning results to it. Main() is the
primary function in all C programs, within which the rest of the program is
constructed.
When running on a PC, main() is called by the operating system, and control is
returned to the OS when the C program is terminated. In the microcontroller,
main() is simply used to indicate the start of the main control sequence, and more
care needs to be taken in terminating the program.Normally, the program runs in a continuous loop, but if not, the final statement should
be while(1);, which causes the program to wait and prevents the program running
into undefined locations following the application code.
43
B mn K Thut in T - HBK
Figure 2.11 Hierarchical C program structure
Main(){
statementsfun1()statementsstatements................statementsfun2(arg)statements
}
void fun1(){
statements......
}
void fun2(arg){
statements...
fun3...return(val)
}
void fun3{
statements......
}
LEVEL 0 LEVEL 1 LEVEL 2
44
7/27/2019 ESD-Ch3
23/33
3/12/2012
23
B mn K Thut in T - HBK
Basic Functions
A simple program using a function is shown in FUNC1.C, Listing 2.12 . Themain block is very short, consisting of the function call out() and a while
statement, which provides the wait state at the end of main(). In this case,
the variables are declared before the main block. This makes them global
in scope; that is, they are recognizedthroughout the whole program and
within all function blocks. The function out() is also defined before main() ,
so that, when it is called, the function name is recognized. The function
starts with the keyword void , which indicates that no value is returned by
the function. The significance of this is explained shortly.
The function itself simply increments Port C from 0 to 255. It contains a forloop to provide a delay, so that the output count is visible. This is a simple
alternative to the built-in delay functions seen in previous examples and isused here to avoid the inclusion of such functions while we study user-
defined functions. It simply counts up to a preset
value to waste time. The delay time is controlled by this set value.
45
B mn K Thut in T - HBK
Listing 2.12 Basic function call
// FUNC1.C
// Function call structure
#include "16F877A.H
int8 outbyte=1;
int16 n;
void out() // Start of function block
{
while (outbyte!=0) // Start loop, quit when output =0{
output_C(outbyte); // Output code 1 0xFF
outbyte++; // Increment output
for(n=1;n
7/27/2019 ESD-Ch3
24/33
7/27/2019 ESD-Ch3
25/33
3/12/2012
25
B mn K Thut in T - HBK
Listing 2.14 Local variables
// FUNC3.C// Use of local variables
#include "16F877A.H
int8 outbyte=1; // Declare global variables
int16 count;
int out(int16 t) // Declare argument types
{
int16 n; // Declare local variable
while (input(PIN_D0)) // Run output at speed t
{ outbyte++;
for(n=1;n
7/27/2019 ESD-Ch3
26/33
3/12/2012
26
B mn K Thut in T - HBK
Serial LCD
CCS C provides an RS232 driver routine that works with anyI/O pin (that is, the hardware port need not be used).
This is possible because the process for generating the RS232data frame is not too complex and can be completed fast
enough to generate the signal in real time.
At the standard rate of 9600 baud, each bit is about 100 slong, giving an overall frame time of about 1 ms.
The data can be an 8-bit integer or, more often, a 7-bit ASCIIcharacter code.
51
B mn K Thut in T - HBK
Serial LCD
52
7/27/2019 ESD-Ch3
27/33
3/12/2012
27
B mn K Thut in T - HBK
Serial LCD
Control Codes
53
B mn K Thut in T - HBK
Serial LCD Format Codes
54
7/27/2019 ESD-Ch3
28/33
3/12/2012
28
B mn K Thut in T - HBK
Example
55
B mn K Thut in T - HBK
2.7 PIC16 C More Data Types
Arrays and strings
Pointers and indirect addressing
Enumeration
The data in a C program may be most conveniently handled assets of associated variables. These occur more frequently as
the program data becomes more complex, but only the basics
are mentioned here.
56
7/27/2019 ESD-Ch3
29/33
3/12/2012
29
57
B mn K Thut in T - HBK
2.8 PIC16 C Compiler Directives
Include and use directives
Header file listing and directives
Compiler directives are typically used at the top of the program to set up
compiler options, control project components, define constant labels, and
so on before the main program is created. They are preceded by the hashsymbol to distinguish them from other types of statements and do not
have a semicolon to end the line.
58
7/27/2019 ESD-Ch3
30/33
3/12/2012
30
59
60
7/27/2019 ESD-Ch3
31/33
3/12/2012
31
61
62
7/27/2019 ESD-Ch3
32/33
3/12/2012
32
63
64
7/27/2019 ESD-Ch3
33/33
3/12/2012
65