43
Welcome to AMUROBOCLUB

C is general-purpose structured programming language or high level language. It was developed by Dennis Ritchie in 1970s at Bell laboratories

Embed Size (px)

Citation preview

Page 1: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

Welcome to AMUROBOCLUB

Page 2: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

C programming basics

Page 3: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

C is general-purpose structured programming

language or high level language.

It was developed by Dennis Ritchie in 1970s at Bell

laboratories.

C supports a large no. of operators and a large no.

of library function.

C is most popular language used for system

programming ,such as development of compilers,

interpreters , assemblers , operating system like

UNIX.

C-Programming:

Page 4: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

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

}

C program structure

Page 5: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

#include <stdio.h>main(){ printf(“Hello World”);}

A simple Program

Page 6: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

A library function is a self-contained program

that carries out some specific as well as

defined tasks.

The function prototype or the reference is

defined in the header files section so we have

to include them in the beginning of program.

Example: #include, #define

Library function:

Page 7: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

The statements used to change the control

flow in a program are called the control

statements or control structures in C.

Control statements: Logical if structure

If-else structure

Nested if-else

Unconditional goto statement

Switch structures

Control statements:

Page 8: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

The logical if structure checks a given logical condition and transfers the control accordingly.

Syntax:If(condition){

Statement;}

Example: if(x==3){Y=2*x;}

If Statement:

Page 9: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

#include <stdio.h>int main(){

int a=1;If(a==1) {

printf(“true logic”); }If(a==0) {

printf(“false logic”); }return 0;

}

Example

Page 10: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

The if-else structure is more useful than logical if structure.

Syntax:if(expression){ S1; }

else { S2; }

Example:if(i==0){ s=s+1;}

else { s=s-1; }

If else Statement:

Page 11: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

#include <stdio.h>int main(){ int a=1;If(a==1) {printf(“true logic”); }else{printf(“false logic”); }return 0;}

Example

Page 12: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

C provides three loop structures to perform

looping operations or iterations, in which a

set of statements can be repeatedly

executed as long as condition is satisfied.

Loops are:

while

Do while

For

Loops:

Page 13: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

It is similar to the while-loop structure except that the condition is checked at the end of the loop.Syntax:

do{S1;} while(condition);

Example:do{ i=i+1;} while(i <2);

do-while loop:

Page 14: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

#include <stdio.h>int main(){int i=0;do{printf(“value of i=%d”,i);i++;}while(i<10);return 0;}

Example

Page 15: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

In while-loop structure condition is checked at the starting of the loop.

Syntax: while(condition)

{ s1;}

Example: while(i>0)

{ j=j+1;}

while loop:

Page 16: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

#include <stdio.h>int main(){int i=0;while (i<10){printf(“value of i=%d”,i);i++;} return 0;}

Example

Page 17: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

The expr1 is the statement assigning an initial

value to a variable,expr2 is a logical expression

and expr3 is a statement that is used to alter the

value was assigned in the initial expression.

Syntax:

For(expr1;expr2;expr3)

{ statements;

}

Example:

For(i=1;i<=5;i++)

{ j=j+1;}

For loop

Page 18: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

#include <stdio.h>int main(){ int i; for(i=0;i<10;i++){printf(“value of i=%d”,i);} return 0;}

Page 19: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

0; 1; Binary to Decimal Conversion Decimal to Binary Conversion

Binary Numbers

Page 20: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

0-9, A, B, C, D, E, F Conversion Decimal to Hexa decimal and vice versa Binary to Hexa Decimal and vice versa

HexaDecimal Numbers

Page 21: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

0-7 Conversion

Octal Numbers

Page 22: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

~ One’s Complement (unary operator) >> Right Shift << Left Shift & Bitwise AND | Bitwise OR ^ Bitwise XOR (Exclusive OR) Bitwise Compound Assignment

Operators(eg. <<=, >>=,|=,&=, and ^=)

Bitwise Operators

Page 23: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

#include <stdio.h>int main(){ unsigned char ch=32;unsigned char dh;dh=~ch;printf(“~ch=%d\n”,dh);printf(“~ch=%x\n”,dh);printf(“~ch=%X\n”,dh);return 0;}

Example for One’s Complement

Page 24: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

Ch=11010111;Ch>>1 gives output 01101011;Ch>>2 gives output 00110101;

Right Shift Operator

Page 25: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

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

Example

Page 26: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

Same as right shift operator <<

Left Shift Operator

Page 27: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

Bitwise OR operator (|) Bitwise AND operator (&) Bitwise XOR operator(^)

Page 28: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

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

Showbits function

Page 29: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

PORTS programming BASICS

Page 30: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

ROBOT:

Robot is a machine that gathers information

about its environment(senses) and uses that

information to follow instructions to do work.

MICROCONTROLLERS:

Microcontrollers is a single chip computer

containing a processor core , memory and

programmable input/output peripherals.

Robot & Microcontrollers:

Page 31: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

Atmega 16L pin diagram:

Page 32: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

DDRX: defines whether the port will act as i/p port or o/p port.

1 use for output 0 use for input

Example: DDRA = 0b11111111; (binary)

DDRA = 0xFF; (hexa decimal)DDRA = 1; (decimal)DDRA = 010; (octal)

Ports programming:

Page 33: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

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.

Page 34: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

PORTX:

Use to assign value to PORTX.

Example:

PORTA=27

decimal value 27 is assigned to the portA.

Page 35: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

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.

Page 36: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

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)

Page 37: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

PORTA is different from port A:

PORTA

DDRA

PINA

uCExternal

world

Port A

Page 38: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

PROGRAMS:

A program to o/p 33 (hex) on Port D.

#include <mega16.h>

void main( ){DDRD=0xFF; PORTD=0x33;}

Page 39: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

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

Page 40: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

LED:

+

-

+ VE

GND

Longer leg is +ve Terminal

Page 41: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

Glowing LED:

PortA

+

-01

PORTA=0b00000010;

PORTA=0x02;

Or

Or

For glowing LEDPORTA.0=0 (GND)PORTA.1=1 (+V)

Page 42: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

void main(){

DDRB=0XFF;while(1){

PORTB=0XFF;delay_ms(100);PORTB=0X00;delay_ms(100);

}}

Blinking of led:

Page 43: C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories

THANK YOU