c programming language - german university in cairo

19

Upload: others

Post on 16-Oct-2021

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C Programming Language - German University in Cairo
Page 2: C Programming Language - German University in Cairo

C Programming Language

Advantages over assembly language for microcontrollers:

More portable

Math functions

Readability

Maintainability

Page 3: C Programming Language - German University in Cairo

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

Page 4: C Programming Language - German University in Cairo

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

Page 5: C Programming Language - German University in Cairo

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

Page 6: C Programming Language - German University in Cairo

mikroC variable types

Page 7: C Programming Language - German University in Cairo

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

Page 8: C Programming Language - German University in Cairo

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

Page 9: C Programming Language - German University in Cairo

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

Page 10: C Programming Language - German University in Cairo

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

Page 11: C Programming Language - German University in Cairo

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

Page 12: C Programming Language - German University in Cairo

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} };

Page 13: C Programming Language - German University in Cairo

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

Page 14: C Programming Language - German University in Cairo

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

Page 15: C Programming Language - German University in Cairo

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

Page 16: C Programming Language - German University in Cairo

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

Page 17: C Programming Language - German University in Cairo

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

Page 18: C Programming Language - German University in Cairo

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

}

Page 19: C Programming Language - German University in Cairo

Write assembly in C codeasm

{

Assembly language instructions ...

}