c programming language - german university in cairo
TRANSCRIPT
C Programming Language
Advantages over assembly language for microcontrollers:
More portable
Math functions
Readability
Maintainability
Editing C
End-of-line ignored
Use line breaks/tabs/indent for readability
Lines end in semicolon ;
Use Braces {} to group things
Comments: your code must have comments!
Standard C comment between /* and */ (no nesting)
C++ style common too, between // and end of line
Pre-Processor Performs text replacement before compile
Commands marked with “#” the “pound”
#include inserts entire file#include <stdio.h> // compiler library
#include “yourfile.h” //paste your file right here
.h files are header files Common declarations
Port, register names defined for chip (PORTB)
#define does search and replace#define this that // replace every “this” with “that”
#define MAX 100 // common for defining constants
Variables Names for memory locations
int: 16 bit
float: floating point (3.14)
char: 8 bit
unsigned char: 0-255
signed char: -128 to 127
long int or long: 32 bit
mikroC variable types
Logical Operators
x =10x > 8 // returns 1x ==10 // returns 1x < 100 // returns 1x > 20 // returns 0x != 10 // returns 0x >= 10 // returns 1x <=10 // returns 1
Logical Operatorsx = 7;x > 0 && x < 10 // returns 1x > 0 || x < 10 // returns 1x >=0 && x <=10 // returns 1x >=0 && x < 5 // returns 0a = 10; b = 20; c = 30; d = 40;a > b && c > d // returns 0b > a && d > c // returns 1a > b || d > c // returns 1
Bitwise Operators0xFA & 0xEE returns 0xEA0xFA: 1111 10100xEE: 1110 1110
0xEA: 1110 1010
0x01 | 0xFE returns 0xFF0x08: 0000 00010xFE: 1111 1110
0xFE: 1111 1111
0xAA ∧ 0x1F returns 0xB50xAA: 1010 10100x1F: 0001 1111
0xB5: 1011 0101
0xAA returns 0x550xAA: 1010 10100x55:
0x55: 0101 0101
0x14 >> 2 returns 0x05 (shift 0x14 right by 2 digits)0x14: 0001 0100>>2: 0000
0x05: 0000 0101
Data Accessing
/* Input/Output Ports initialization */
// Port A initialization
Accessing Registers
TRISA=0x01; // Initialize direction
TRISB=0b10110001;
PORTA=0x00; // Initialize state
Accessing Individual Bits
INTCON.GIE = 0; // Clear Global Interrupt Bit (GIE)
//GIE is defined in the definition file
INTCON.B0 = 0; // Clear bit 0 in INTCON register
INTCON.F5 = 1; // Set bit 0 in INTCON register
Accessing Individual Bits You can declare a sbit variable in a unit in such way that it
points to a specific bit in SFR register:
sbit Abit at PORTB.B0; // this is where Abit is fully defined
void main(){
Abit=1;
}
The mikroC compiler provides a bit data type that may be used for variable declarations. It can not be used for argument lists, and function-return values
bit bf; // bit variable
Arraysunsigned int Total[5];
Total[1] = 25;
unsigned char months[ ] ={31,28,31,30,31,30,31,31,30,31,30,31};
unsigned char Hex_Letters[ ] = {‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’};
unsigned char Mystring[ ]=“COMP”;
unsigned char Mystring[ ] = {‘C’, ‘O’, ‘M’, ‘P’, ‘\0’};
unsigned char Q[2][2] = { {1,0}, {0,1} };
Variable declarations Local variables
Declared inside a function
Names can be reused
Not shared with other functions
Global variables
Declared outside functions
All functions can see and use them
Programming style determines use
Math Operators Main reason to use C over assembly
Standard arithmetic: + - * /
Modulus % gives remainder: 7 % 2 = 1
++ and -- increment and decrement
x++; // same as x = x + 1
+= is a shortcut (-=, *=, /= etc work too)
x+=2; // same as x = x + 2
Branches and Loopsif (x > y) {
x = 1; // if true
} else {
x = 34; // if false
}
for (count=0; count<7; count++)
{
// count is 0 through 6
x = x + count; // calculated 7 times }
MechatronicsEngr 415 Spring 2007
Branches and Loops continuedwhile (x > y) {
x = 1; // infinite loop?
}
switch (numx) {
case 1: // this happens if x = 1
y = y+x;
break;
case 3: // if x is 3 this executes
y = 23;
break;
default: // otherwise this happens
y = 0;
break;
}
MechatronicsEngr 415 Sprig 2007
Pointers and Arrays Pointers: variable location rather than value
Not used much in small microcontrollersint x; // regular int
int *p; // pointer to int
p = &x; // p has address of x
x = *p; // x has value pointed to by p
Arrays: vector of variables, index in []int i, pwm_out[8]; // 8 ints
for (i=0;i<8;i++) {
pwm_out[i]=0; // clear array
}
MechatronicsEngr 415 Spring 2007
Functions: Return a number or VOID #include <math.h> // need sin
float sx, x; // declare variables (global)
int z, z2;
// Function square starts here
int square(int x)
// custom user-written function to do square of 2 numbers
{
int y; // local variable
y = x*x; // function calculation
return y; // send this back
}
// Function main starts here
void main(void)
// there is always a main function
{
x =1.57; // radians
sx = sin(x); // function call
z = 2; // units?
z2 = square(z); // function call
}
Write assembly in C codeasm
{
Assembly language instructions ...
}