DWMC - Lecture2 8051 Programming (ESD 606)
DWMC - Lecture2 8051 Programming (ESD 606)
23.1.2012
Lets C
Glow an LED at pin 3 of Port 1, based on a switch at pin1 of Port0.
Lets C
Set ports direction (i/p = 1, o/p =0) Read bit from a port pin (switch) Set bit of another port pin (LED)
Programming
C vs Assembly language Assembly language
Mnemonics for machine code Opcode and operand(s) Directives
25.1.2012
Directives
ORG
Origin address to start
DB
Define data Ascii data
EQU
Define constant no memory allocated
END
End of program, ignore anything that follows
Directives - Example
ORG 500H DATA1: DB 28 DATA2: DB 00110101B DATA3: DB 39H DATA4: DB 2591 DATA6: DB My name is COUNT EQU 25H MOV R3, COUNT END ;DECIMAL (1C in Hex) ;BINARY (35 in Hex) ;HEX ;ASCII NUMBERS Joe ;ASCII string ; Define Count as 25H ; Set R3 = 25H
Program flow
lst File
1 2 3 4 5 6 7 8 9 0000 0000 0002 0004 0006 7D25 7F34 7400 2D ORG 0H ;start at 0 MOV R5,#25H ;load 25H into R5 MOV R7,#34H ;load 34H into R7 MOV A,#0 ;load 0 into A ADD A,R5 ;add contents of R5 to A ;now A = A + R5 ADD A,R7 ;add contents of R7 to A ;now A = A + R7 ADD A,#12H ;add to A value 12H ;now A = A + 12H HERE: SJMP HERE ;stay in this loop END ;end of asm source file
27.1.2012
Instructions
MOV CJNE INC SJMP ACALL RET
Program-1
#include <reg51.h> void main(void) { while(1){ sbit port1bit2 = P1^2; sbit port2bit3 = P2^3; port1bit2 = 1; while (port1bit2 !=1); P0 = 0x45; port2bit3 = 1; port2bit3 = 0; }}
C program-1
#include <reg51.h> void main(void) { sbit port1bit2 = P1^2; sbit port2bit3 = P2^3; port1bit2 = 1; while(1){ while (port1bit2 !=1); low P0 = 0x45; port2bit3 = 1; port2bit3 = 0; }}
//set port1 bit2 as i/p //do nothing when portbit2 is //send 45H when portbit is hi //Create a hi to lo pulse
Program-2
C program-2
#include <reg51.h> void main(void) { sbit port1bit5 = P1^5; sbit port2bit3 = P2^3; port2bit3 = 1; while(1){ if (port2bit3 ==1) port1bit5 = 1; port1bit5 = 0; }}
Program-3
C program-3
#include <reg51.h> void main(void) { sbit sw = P1^7; sw =1; while(1){ if (sw ==0) p2 = N else p2 = Y; }}
Program-4
Program-5
Program 6
Create a square wave of 50% duty cycle (with equal portions high and low) on the P1.5 bit.
#include <reg51.h> void MSDelay(unsigned int); void main(void) {while (1) //repeat forever { P1^5=1; MSDelay(250); P1^5=0; MSDelay(250); } } void MSDelay(unsigned int itime) { unsigned int i,j; for (i=0;i<itime;i++) for (j=0;j<1275;j++); }
Program 6 (1)
HERE: SETB P1.5 ;set to high bit 0 of port 1 LCALL DELAY ;call the delay subroutine CLR P1.5 ;P1.5=0 LCALL DELAY ; Delay function call SJMP HERE ;keep doing it
Program 6(2)
HERE: CPL P1.0 ;set bit 0 of port 1 LCALL DELAY ;call the delay subroutine SJMP HERE ;keep doing it
Program 6(3)
HERE: CPL P1.0 ;set bit 0 of port 1 ACALL DELAY ;call the delay subroutine SJMP HERE ;keep doing it
Group Assignment
Explain the following set of instructions with examples
Call instructions Unconditional jump instructions Conditional jump instructions Logical instructions Arithmetic instructions
Read a datasheet
AT89C51
Keil demo
Review
Asm vs C program Directives Program flow Lst file Intel Hex format Data type formats Programming Asm and C