8051 basic programming

23
8051 Programming

Upload: anjusha-r

Post on 22-Jan-2018

294 views

Category:

Engineering


1 download

TRANSCRIPT

Page 1: 8051 basic programming

8051 Programming

Page 2: 8051 basic programming

Assembly language----- includes mnemonics

Example: ADD, SUB, MUL, RR…

Assembly Language-----Assembler---Binary

Disadvantage:

We should know about architecture of microcontroller or microcontroller

Page 3: 8051 basic programming

Higher level languages : c ,java

( c, c++,java)------compiler----binary

Advantage:

No need to consider about architecture of µp/µc….just few features are considered

Page 4: 8051 basic programming

STEPS FOLLOWED IN CONVERTING SOURCE FILE TO HEX. FILE

Page 5: 8051 basic programming

EMBEDDED C

DATA TYPES USED IN EMBEDDED C

Integer int x;Unsigned integer unsigned int x;Char char x;Unsigned char unsigned char x;

Page 6: 8051 basic programming

1) BASIC PORT PROGRAMMING

P0 -------

P0=____________?

MSB LSB

Page 7: 8051 basic programming

BASIC PORT PROGRAMMING……………………

P0-------8 4 2 1 8 4 2 1

ANS: P0=0X55

Page 8: 8051 basic programming

Syntax to be followed:

Pin declaration:

sbit variablename = PX ^Y;Where,

x=0,1,2,3 porty=0,1,2,3,4,5,6,7 pins

Example: sbit led = P1^0;

Page 9: 8051 basic programming

WAP to blink a led which is connected to port2 , pin 0

#include<reg52.h>sbit led = P2^0;unsigned int i;void main()

{P2=0x00;led =1;for(i=0; i<1000; i++);

led =0;for(i=0; i<1000; i++);

}

Page 10: 8051 basic programming

Syntax to be followed:

port declaration:

#define variablename Px

Where,x=0,1,2,3

Example: #define segment P1

Page 11: 8051 basic programming

WAP to blink all led’s connected to port2

#include<reg52.h>#define leds P2unsigned int i;void main()

{leds=0x00;for(i=0; i<1000; i++);

leds =0xFF;for(i=0; i<1000; i++);

}

Page 12: 8051 basic programming

INTRODUCTION TO

KEIL IDE FOR

80XX PROGRAMMING

Page 13: 8051 basic programming

PROGRAM 1: WAP to blink a led which is connected to port1 , pin 0

PROGRAM 2: WAP to blink the led’s which are connected to port1

PROGRAM 3: WAP to blink the led’s alternatively which are

connected to port1

Practice examples

Page 14: 8051 basic programming

pin7 pin6 pin5 pin4 pin3 pin2 pin1 pin0

0 0 0 0 0 0 0 10 0 0 0 0 0 1 00 0 0 0 0 1 0 00 0 0 0 1 0 0 00 0 0 1 0 0 0 00 0 1 0 0 0 0 00 1 0 0 0 0 0 01 0 0 0 0 0 0 0

PROGRAM 4:WAP to shift a bit of port1 from pin0 to pin7

Page 15: 8051 basic programming

PROGRAM 5: WAP to shift a bit of port1 from pin7 to pin0

Pin7 pin6 pin5 pin4 pin3 pin2 pin1 pin01 0 0 0 0 0 0 00 1 0 0 0 0 0 00 0 1 0 0 0 0 00 0 0 1 0 0 0 00 0 0 0 1 0 0 00 0 0 0 0 1 0 00 0 0 0 0 0 1 00 0 0 0 0 0 0 1

Page 16: 8051 basic programming

PROGRAM 6: WAP to shift a bit of port1 from pin0 to pin7

pin7 pin6 pin5 pin4 pin3 pin2 pin1 pin00 0 0 0 0 0 0 10 0 0 0 0 0 1 10 0 0 0 0 1 1 10 0 0 0 1 1 1 10 0 0 1 1 1 1 10 0 1 1 1 1 1 10 1 1 1 1 1 1 11 1 1 1 1 1 1 1

Page 17: 8051 basic programming

16X2 LCD Display

Page 18: 8051 basic programming

8 data pins

3 control pins

supply pins and a potentiometer

1. Two modes

-----Command mode (RS=0)

-----Data mode (RS=1)

2. RW is always logic zero

3. EN=1 to EN=0 for data transfer

Page 19: 8051 basic programming

LCD INITIALIZATION Using commands

0X38 =========2 lines and 5x7 matrix

0x01 ========= clear the display

0x06 ========= shift cursor to right i.e. increment cursor

0x05 ========= shift display right

0x0E ========= display on, cursor blinking

0x80 ========= force cursor to beginning of first line

0xC0 ========= force cursor to beginning of first line

Page 20: 8051 basic programming

To pass command to lcd

8datapins = dataRS=0RW=0EN=1Delay()EN=0

To pass command to lcd

8datapins = dataRS=1RW=0EN=1Delay()EN=0

Page 21: 8051 basic programming

void lcd_cmd(unsigned char d){

dataport =d;rs= 0;rw=0;e=1;delay(150);e=0;delay(150);

} void lcd_data(unsigned char f){

dataport =f;rs= 1;rw=0;e=1;delay(150);e=0;delay(150);

}

Sample code:

Page 22: 8051 basic programming

WAP to display your name on LCD

Program6:

#include<reg51.h>#define dataport P2 // for LCDvoid delay(int a);void lcd_cmd(unsigned char d);void lcd_data(unsigned char f);sbit rs =P1^0; // rs pin of LCDsbit rw =P1^2; // rw pin of LCDsbit e = P1^1; // enable pin of LCDvoid delay(int a){

int i;for(i=0;i<a;i++);

}

Page 23: 8051 basic programming

void main(void){lcd_cmd(0x38); //function setdelay(50);lcd_cmd(0x0C); //display on,cursor off,blink off delay(50);lcd_cmd(0x80); //first rowdelay(50);lcd_cmd(0x06); //entry mode, set incrementdelay(50);lcd_cmd(0x80); //first rowdelay(10);dataport=0;P1=0X00;lcd_data('E');delay(10);lcd_data('C');delay(10);lcd_data('E');delay(1500);}