Académique Documents
Professionnel Documents
Culture Documents
AMUROBOCLUB
C programming basics
C-Programming:
laboratories.
of library function.
UNIX.
C program structure
A typical C program has 3 sections:
#include< stdio.h > //Header file section
#include< stdio.h >
void main( )
{
int a,b; //Type declaration section
//Instruction section
}
A simple Program
#include <stdio.h>
main()
{
printf(Hello World);
}
Library function:
Control statements:
to change the
Control statements:
Logical if structure
If-else structure
Nested if-else
Unconditional goto statement
Switch structures
If Statement:
Syntax:
If(condition)
{
Statement;
}
Example:
if(x==3)
{
Y=2*x;
}
Example
#include <stdio.h>
int main()
{
int a=1;
If(a==1)
{
printf(true logic);
}
If(a==0)
{
printf(false logic);
}
return 0;
}
If else Statement:
Example
#include <stdio.h>
int main()
{ int a=1;
If(a==1) {
printf(true logic); }
else
{
printf(false logic); }
return 0;
}
Loops:
of
statements
can
be
repeatedly
do-while loop:
Example
#include <stdio.h>
int main()
{int i=0;
do{
printf(value of i=%d,i);
i++;
}while(i<10);
return 0;
}
while loop:
In while-loop structure condition
checked at the starting of the loop.
Syntax:
while(condition)
{ s1;
}
Example:
while(i>0)
{ j=j+1;
}
is
Example
#include <stdio.h>
int main()
{int i=0;
while (i<10){
printf(value of i=%d,i);
i++;
} return 0;
}
For loop
#include <stdio.h>
int main()
{ int i;
for(i=0;i<10;i++)
{
printf(value of i=%d,i);
} return 0;
}
Binary Numbers
0;
1;
Binary to Decimal Conversion
Decimal to Binary Conversion
HexaDecimal Numbers
0-9,
A,
B,
C,
D,
E,
F
Conversion
Decimal to Hexa decimal and vice versa
Binary to Hexa Decimal and vice versa
Octal Numbers
0-7
Conversion
Bitwise Operators
~ Ones Complement (unary operator)
>> Right Shift
<< Left Shift
& Bitwise AND
| Bitwise OR
^ Bitwise XOR (Exclusive OR)
Bitwise Compound Assignment
Operators(eg. <<=, >>=,|=,&=, and ^=)
Example
#include <stdio.h>
void showbits(unsigned char);
int main()
{ unsigned char num=225,i,k;
printf(\n Decimal %d is same as hexa
%x,num,num);
for(i=0;i<=5;i++)
{k=num>>i;
printf(\n %d right shift %d gives %x,num,I,k);
//showbits(k);}
return 0;}
Showbits function
void showbits(unsigned char n)
{ unsigned char i,k,andmask;
for(i=7;i>=0;i--)
{ andmask=1<<i;
k=n & andmask;
If(k==0)
printf(0);
else printf(1);
}
}
MICROCONTROLLERS:
Ports programming:
0b11111111;
0xFF;
1;
010;
(binary)
(hexa decimal)
(decimal)
(octal)
DDRX.Y:
Define individual pin (pin Y of port X) acts
as the i/p pin or the o/p pin
Example:
DDRA.3=1;
pin 3 of port A is o/p port.
PORTX:
Use to assign value to PORTX.
Example:
PORTA=27
decimal value 27 is assigned to the portA.
PORTX.Y:
Use to assign value to individual pins(y) of
any port (X).
Example:
PORTA.0=1
assign value 1 to the pin0 of the port A.
PINX:
Read 8-bit integer value from the port X.
Example:
X=PINA;
Read the 8-bit integer value from the portA.
0<X<255
PINX.Y :
Read 1-bit value (individual pin value) from PORTX.
Example:
X= PINA.2; ( value may be 0 or 1)
uC
P
O
R
T
A
D
D
R
A
P
I
N
A
External
world
PROGRAMS:
A program to o/p 33 (hex) on Port D.
#include <mega16.h>
void main( )
{
DDRD=0xFF;
PORTD=0x33;
}
PROGRAMS:
A program read pins 2 and 7 of Port A.
#include <mega16.h>
void main( )
{
unsigned int x,y;
DDRA=0b01111011;
x=PINA.2;
y=PINA.7;
}
LED:
GND
+ VE
Glowing LED:
0
1
P
o
r
t
A
Blinking of led:
void main()
{
DDRB=0XFF;
while(1)
{
PORTB=0XFF;
delay_ms(100);
PORTB=0X00;
delay_ms(100);
}
}
THANK YOU