Vous êtes sur la page 1sur 4

PORTX: 1 1 1 1 1 1 1 1

PORTX: 0 0 0 0 0 0 0 0

EMBEDDED Programming Technique
By Arun Barman
Program Skeleton
Ex.












Program with User Defined Function
Ex.















DATATYPE BITs BYTE VALUE RANGE
bit 1 ----- 0 to 1
char 8 1 -128 to +127
int 16 2 -32768 to +32767
short 16 2 -32768 to +32767
long 32 4 -2147483648 to +2147483647
unsigned char 8 1 0 to 255
unsigned int 16 2 0 to 65535
unsigned short 16 2 0 to 65535
unsigned long 32 4 0 to 4294967295
float 32 4 +1.175494x10
-38
to +3.402823x10
+38

#include<XXXX.h>
void YYYY();

void main()

{
YYYY();
}

void YYYY()
{
//source code
}

#include<XXXX.h>
void DELAY();
void main()
{
while(1)
{
PORTX=0XFF;
DELAY();
PORTX=0X00;
DELAY();
}
}

void DELAY()
{
unsigned int i;
for(i=0;i<=1000;i++);
}

#include<XXXX.h>
void main()
{
//source code
}

* Start with #include to add header file XXXX.h of a particular Microcontroller to access PORT and other Registers inside C code.
* 0X (zero X) it indicates HexaDecimal value like 0X5F (01010101), 0XAA (10101010), 0XFF (11111111), 0X00 (00000000)
* PORT is generally of 8 bit

Decimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
HexaDecimal 0 1 2 3 4 5 6 7 8 9 A B C D E F
Binary 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111

Decimal value: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
HexaDecimal value: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
Binary value: 0000


* Please save the file with XYZ.C, i.e., BLINKING.C

* You can write C code inside your own defined function like void DELAY(), write source code inside this Function and call this
function from main function void main() for infinite time.
* Function Prototype: say to C compiler that there is an User Defined Function written after main() function otherwise that
Function can not be called.
#include<XXXX.h>
void main()
{
unsigned int i;
while(1)
{
PORTX=0XFF;
for(i=0;i<=1000;i++);
PORTX=0X00;
for(i=0;i<=1000;i++);
}
}

5 F
Range of i: 0 to 65535
Self Loop: till it reaches from i=0 to
1000, it acts as a DELAY and then
execute the next statement.
Function Prototype
Function Call
Function Definition
Infinite Loop (always True)
#include<XXXX.h>
void main()
{
char i=0;
while(i<=10)
{
PORTX=0XFF;
DELAY();
PORTX=0X00;
DELAY();
i++;
}
while(1);
}

#include<XXXX.h>
void main()
{
unsigned int i;
for(i=0;i<=10;i++)
{
PORTX=0XFF;
for(i=0;i<=2000;i++);
PORTX=0X00;
for(i=0;i<=2000;i++);
}
while(1);
}

#include<XXXX.h>
void main()
{
unsigned int i;
for(;;)
{
PORTX=0XFF;
for(i=0;i<=2000;i++);
PORTX=0X00;
for(i=0;i<=2000;i++);
}
}



while Loop

Ex.










do while Loop





for Loop












while(1) Executes the statement for infinite time (condition is always true), it is called SUPER LOOP.
while(1); - It will be executing itself for infinite time.
Compiler checks the while condition, if it is True, execute the statements inside the while block till it is True. If it is False, skip the
while block.
while(conditional exp)
{
//statements
}
for (initialisation; condition; modification)
{
//statements
}
for(i=0;i<=10;i++) in the first statement store initial value into i variable; then it checks the condition, execute statements inside for
block (if the condition is true) and increment the value of i with 1, again check the condition, execute statements, increment i . . . . .till
the condition is False

for(i=0;i<=2000;i++); the loop on its own, start with i=0 to i=2000, it can be used for creating DELAY

for(;;) infinite Loop same as while(1)


do
{
//statements
}
while(conditional exp);

* if condition is TRUE: first execute the statements and then check the condition ..till the condition is FALSE
* if condition is FALSE: at least once it will execute the statements and then check the condition (which is FALSE) and Terminate

Loop execute 11 times i=0 to 10
It will get stuck, self Loop for
infinite time
while(conditional exp);

The for Loop execute 11 times
and then get stuck in while
Loop forever
This operation executes for
infinite time
for (initialisation; condition; modification);
The whole block will be on Loop
till the condition is False


if and else






switch and case















Goto






Both the examples are same

if(conditional exp)
{
//statements1
}
else
{
//statements2
}

Note- {} braces are for executing multiple Statements, but for single Statement, braces are optional

#include<XXXXX.h>
void FONT(unsigned char);
void main()
{
unsigned char i;
while(1)
{
for(i=0;i<=7;i++)
{
FONT(i);
}
}
}
void FONT(unsigned char F)
{
switch(F)
{
case 0: PORTX=0XC0;
break;
case 1: PORTX=0XF9;
break;
default: PORTX=0XFF;
break;
}
}
#include<XXXXX.h>
Function Prototype
Main function()
{
variable definition
infinite Loop(always True)
{
For Loop 0 to 7
{
Function Call by value
}
}
}
Function definition(the value is copied)
{
switch(variable)
{
case value1: statement1
break;
case value2: statement2
break;
default: statementN
break;
}
}
If value inside
variable is
matched
If value inside
variable is not
matched
#include<XXXX.h>
void main()
{
while(1)
{
PORTX=0XFF;
DELAY();
PORTX=0X00;
DELAY();
}
}

#include<XXXX.h>
void main()
{
YYYY:
{
PORTX=0XFF;
DELAY();
PORTX=0X00;
DELAY();
goto YYYY;
}
}

If condition is TRUE executes statements1.
Otherwise executes statements2

Value of i
will be
copied to F
each time
the FONT
function is
called, here
it is 8 times
0 to 7

PRACTICE

Keil IDE (8052) MPLab IDE with H-tech C Compiler (PIC) Code Vision IDE + ISP (AVR)
BYTE OPERATION [any changes on PORT which is input reflects to output PORT]
#include<reg52.h>
void main()
{
P2=0xFF; //configure as input
P3=0x00; //configure as output
While(1)
{
P3=P2;
}
}
#include<htc.h>
void main()
{
TRISC=0xFF; //configure as input
TRISD=0x00; //configure as output
While(1)
{
PORTD=PORTC;
}
}
#include<mega32.h>
void main()
{
DDRC=0x00; //configure as input
DDRD=0xFF; //configure as output
While(1)
{
PORTD=PORTC;
}
}
Bit OPERATION [take input from switch and output to LED]
#include<reg52.h>
sbit SW=P2^0; //single bit declaration
sbit LED=P2^1; //single bit declaration
void main()
{
SW=1; //configure as input
LED=0; //configure as output
While(1) //infinite loop
{
if(SW==0)
LED=1;
else
LED=0;
}
}
#include<htc.h>
#define SW RC0 //R-register C-PORTC
#define LED RC1
void main()
{
TRISC0=1; //configure as input
TRISC1=0; //configure as output
While(1) //infinite loop
{
if(SW==0)
LED=1;
else
LED=0;
}
}
#include<mega32.h>
#define SW PINC.0 //i/p from PIN
#define LED PORTC.1 //o/p to PORT
void main()
{
DDRC.0=0; //configure as input
DDRC.1=1; //configure as output
While(1)
{
if(SW==0)
LED=1;
else
LED=0;
}
}

SENDING HEX VALUE TO PORT- GETTING BINARY VALUE ON PORT
PX.7 PX.6 PX.5 PX.4 PX.3 PX.2 PX.1 PX.0
PORTX 0 1 0 1 0 1 0 1
MSB LSB

PX.7 PX.6 PX.5 PX.4 PX.3 PX.2 PX.1 PX.0
PORTX 1 0 1 0 1 0 1 0
MSB LSB

PX.7 PX.6 PX.5 PX.4 PX.3 PX.2 PX.1 PX.0
PORTX 0 0 0 0 0 0 1 0
MSB LSB

PX.7 PX.6 PX.5 PX.4 PX.3 PX.2 PX.1 PX.0
PORTX 0 0 0 1 1 0 0 0
MSB LSB

NOTE: Logic 1 means Vcc (3v or 5v)
Logic 0 means Gnd
PORTX=0X55
PORTX=0XAA
PORTX=0X02
PORTX=0X18

Vous aimerez peut-être aussi