0% found this document useful (0 votes)
70 views

20 C Programming 1

The document discusses C language programming for the 8051 microcontroller. It covers an overview of using C with microcontrollers including C basics, the compilation flow, C extensions, inline assembly, and interfacing C with hardware. It provides examples of arrays, pointers, I/O circuitry, functions, and multitasking in C. It also discusses C compilers available for the 8051 and the basics of C programming including variables, functions, statements, operators, and loop structures like while, for, and do-while loops.

Uploaded by

DanialMZaki
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

20 C Programming 1

The document discusses C language programming for the 8051 microcontroller. It covers an overview of using C with microcontrollers including C basics, the compilation flow, C extensions, inline assembly, and interfacing C with hardware. It provides examples of arrays, pointers, I/O circuitry, functions, and multitasking in C. It also discusses C compilers available for the 8051 and the basics of C programming including variables, functions, statements, operators, and loop structures like while, for, and do-while loops.

Uploaded by

DanialMZaki
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

C Language Programming

for the 8051

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Overview
•  C for microcontrollers
–  Review of C basics
–  Compilation flow for SiLabs IDE
–  C extensions
–  In-line assembly
–  Interfacing with C
•  Examples
•  Arrays and Pointers
•  I/O Circuitry
•  Functions and Header Files
•  Multitasking and multithreading
Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers
C for Microcontrollers
•  Of higher level languages, C is the closest
to assembly languages
– bit manipulation instructions
– pointers (indirect addressing)
•  Most microcontrollers have available C
compilers
•  Writing in C simplifies code development
for large projects.

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Available C Compilers
•  Kiel – integrated with the IDE we have been
using for labs.
•  Reads51 – available on web site (http://
www.rigelcorp.com/reads51.htm)
•  Freeware: SDCC - Small Device C
Compiler (http://sdcc.sourceforge.net/)
•  Other freeware versions …

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Compilation Process (Keil)
program.c

no SRC compile
option
program.LST program.OBJ

build/make

program.M51

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Modular Programming
•  Like most high level languages, C is a
modular programming language (but NOT
an object oriented language)
•  Each task can be encapsulated as a function.
•  Entire program is encapsulated in “main”
function.

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Basic C Program Structure
1.  Compiler directives and include files
2.  Declarations of global variables and constants
3.  Declaration of functions
4.  Main function
5.  Sub-functions
6.  Interrupt service routines

Example: blinky.c

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Back to C Basics
•  All C programs consists of:
– Variables
– Functions (one must be “main”)
•  Statements

•  To define the SFRs as variables:


#include <c8051F020.h>

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Variables
•  All variables must be declared at top of program, before
the first statement.
•  Declaration includes type and list of variables.
Example: void main (void) {
int var, tmp; must go HERE!
•  Types:
–  int (16-bits in our compiler)
–  char (8-bits)
–  short (16-bits)
–  long (32-bits)
–  sbit (1-bit) not standard C – an 8051 extension
–  others that we will discuss later

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Variables
•  The following variable types can be signed
or unsigned:
signed char (8 bits) –128 to +127
signed short (16 bits) –32768 to +32767
signed int (16 bits) –32768 to +32767
signed long (32 bits) –2147483648 to +2147483648

unsigned char (8 bits) 0 to + 255


unsigned short (16 bits) 0 to + 65535
unsigned int (16 bits) 0 to + 65535
unsigned long (32 bits) 0 to + 4294967295
NOTE: Default is signed – it is best to specify.
Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers
Statements
•  Assignment statement:

variable = constant or expression or variable

examples: upper = 60;


I = I + 5;
J = I;

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Operators
•  Arithmetic: +, -, *, /
•  Relational comparisons: >, >=, <, <=
•  Equality comparisons: ==, !=
•  Logical operators: && (and), || (or)
•  Increment and decrement: ++, --
•  Example:
if (x != y) && (c == b)
{
a=c + d*b;
a++;
}

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Example – Adder program
(add 2 16-bit numbers)
$INCLUDE (C8051F020.inc) #include <c8051f020.h>
XL equ 0x78 void main (void) {
XH equ 0x79 int x, y, z; //16-bit variables
YL equ 0x7A
// disable watchdog timer
YH equ 0x7B
cseg at 0 WDTCN = 0xde;
ljmp Main WDTCN = 0xad;
cseg at 100h z = x + y;
; Disable watchdog timer }
Main: mov 0xFF, #0DEh
mov 0xFF, #0ADh
mov a, XL
add a, YL
mov XL, a mov a, XH The C version
addc a, YH
mov XH, a
nop The assembly version
end

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Compilation Process (Keil)
Use the #pragma CODE
compiler directive to
get assembly code adder.c
generated in SRC file.

compile look here in RAM


when debugging
adder.SRC adder.OBJ

assemble build/make

adder.M51
Map file shows where variables Symbol Table in M51 file:
------ DO
are stored. One map file is D:0008H SYMBOL x
generated per project. D:000AH SYMBOL y
D:000CH SYMBOL z
------- ENDDO

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


x?040: DS 2 adder.SRC
y?041: DS 2
z?042: DS 2
main:
; SOURCE LINE # 12
; int x, y, z;
; WDTCN = 0xde; // disable watchdog timer
; SOURCE LINE # 14
MOV WDTCN,#0DEH
; WDTCN = 0xad;
; SOURCE LINE # 15
MOV WDTCN,#0ADH
; z = x + y;
; SOURCE LINE # 17
MOV A,x?040+01H
ADD A,y?041+01H
MOV z?042+01H,A
MOV A,x?040
ADDC A,y?041
MOV z?042,A
; } ; SOURCE LINE # 18
RET
; END OF main
END

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Bitwise Logic Instructions
Examples:
•  AND & n = n & 0xF0;
•  OR |
•  XOR ^
•  left shift n = n & (0xFF << 4)
<<
•  right shift >>
•  1’s complement ~ n = n & ~(0xFF >> 4)

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Example – Logic in Assembly and C
Main: void main (void) {
mov WDTCN, #0DEh char x;
mov WDTCN, #0ADh WDTCN = 0xDE;
xrl a, #0xF0 ; invert bits 7-4 WDTCN = 0xAD;
orl a, #0x0C ; set bits 3-2 x = x ^ 0xF0;
anl a, #0xFC ; reset bits 1-0 x = x | 0x0C;
mov P0, a ; send to port0 x = x & 0xFC;
P0 = x;
}

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Loop Statements - While
•  While loop:

while (condition) { statements }

while condition is true, execute statements

if there is only one statement, we can lose the {}

Example: while (1) ; // loop forever

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Loop Statements - For
•  For statement:

for (initialization; condition; increment) {statements}

initialization done before statement is executed

condition is tested, if true, execute statements


do increment step and go back and test condition again

repeat last two steps until condition is not true

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Example: for loop
for (n = 0; n<1000; n++)
n++ means n = n + 1

Be careful with signed integers!

for (i=0; i < 33000; i++) LED = ~LED;

Why is this an infinite loop?


Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers
Loops: do - while

do
statements
while (expression);

Test made at the bottom of the loop

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Decision – if statement
if (condition1)
{statements1}
else if (condition2)
{statements2}

else
{statementsn}

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Decision – switch statement
switch (expression) {
case const-expr: statements
case const-expr: statements
default: statements
}

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Example: switch
Need a statement
switch (unibble) { like “return” or
“break” or execution
case 0x00 : return (0xC0); falls through to the
case 0x01 : return (0xF9); next case (unlike
VHDL)
case 0x02 : return (0xA4);
case 0x03 : return (0xC0);
default : return (0xFF);
}

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Revisit Toggle and Blink5

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


C Extensions: Additional Keywords
For accessing SFRs

Specify where variables go


in memory
Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers
Accessing Specific Memory

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


C Access to 8051 Memory
code: program
memory accessed by
movc @a + dptr data

bdata

idata
xdata

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


C Extensions for 8051 (Cygnal)
•  New data types:

Example:
bit bit new_flag; //stored in 20-2F
sbit sbit LED = P1^6;
sfr sfr SP = 0x81; //stack pointer
sfr16 sfr16 DP = 0x82; // data pointer

$INCLUDE (c8051F020.h)

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


C Data Types With Extensions

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Declaring Variables in Memory

char data temp;


char idata varx;
int xdata array[100];
char code text[] = “Enter data”;

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Example: Accessing External Memory

•  Program defines two 256 element arrays in


external memory
•  First array is filled with values that increase
by 2 each location.
•  First array is copied to second array.
•  Similar to block move exercise done in
assembly.
•  xdata_move.c

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Interrupts – Original 8051

Specify register bank 2

void timer0 (void) interrupt 1 using 2 {


if (++interruptcnt == 4000) { /* count to 4000 */
second++; /* second counter */
interruptcnt = 0; /* clear int counter */
}
}
Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers
Other Interrupt Numbers

Interrupt number is same as “Priority Order” in datasheet


Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers

You might also like