Introduction To 8051 Microcontroller
Introduction To 8051 Microcontroller
Introduction To 8051 Microcontroller
How to program 8051. 1. In assembly. 2. In C 3. Basic programming (delay and other pins basic I/O functions).
4. Timer/counter. 1. Basic timer operations 2. Timer interrupts. 3. Counter programming. 4. Some basic codes. 5. LCD and Keypad interfacing. 6. Serial Port Programming 1. Basic serial communication. 2. Serial communication modes. 3. Serial communication registers. 4. Basic serial communication programming in C. 7. Other Interrupts Programming. 1. External interrupt programming.
Definition of microcontroller.
CPU General Purpose Microprocessor RAM ROM I/O Ports Timers Serial COM Port
CPU
RAM
ROM
I/O Ports
Timer
Microcontroller
128 2 32 1 6
256 3 32 1 8
128 2 32 1 6
Part Number
ROM
RAM
I/O Pins
Timer
Interrupt
Vcc
Packaging
4K 4K 1K
128 128 64
32 32 15
2 2 1
6 6 3
5V 3V 3V
40 40 20
AT89C2051
2K
128
15
3V
20
AT89C52 AT89LV52
8K 8K
128 128
32 32
3 3
8 8
5V 3V
40 40
Interrupt Vector Table for 8051 Interrupt Reset External hardware interrupt 0 (INT0) ROM location (HEX) 0000 0003 Flag Clearing Auto Auto
Timer 0 interrupt
000B
Auto
0013
Auto
001B 0023
EA
IE7
Disable all interrupts. If EA=0, no interrupt is acknowledged. If EA=1 each interrupt source is individually enabled or disabled by setting or clearing its enable bit. Not Implemented Enable or disable Timer 2 overflow or capture interrupt (8052 only). Enables or disable serial port interrupt Enables or disable Timer 1 overflow interrupt Enable or disable external interrupt 1 Enables or disable Timer 0 overflow interrupt Enable or disable external interrupt 0
---ET2
IE6 IE5
MOV Instruction mov destination,source; ADD Instruction add A,sourc; add source operand to the accumulator and store the result in A. JUMP Instruction Conditional Jumps. JZ(Jump if Zero if A=0) DJNZ(Decrement and jump if not zero) JNC( Jump if not Carry set CY=0) JC(Jump if carry set CY=1)
Unconditional Jumps LJMP(Long Jump) It is a 3byte instruction 1st byte is used for opcode the remaining 2 bytes are used for address. SJMP(Short Jump) It is a 2 byte instruction 1st byte is used for opcode the remaining 1 byte is used for address. CALL Instructions LCALL(Long Call) It is also a 3 byte instruction. ACALL(Absolute Call) It is a Two byte instruction used to call a function outside the function being executed.
Include header file #include<reg51.h> The Main Function Void main(void){ } Decision Making Instructions while loop while(condition) //the program remains in the loop while the condition is true for loop for(initialization;condition;increment/decrement)
GATE gating control when set. The timer/counter is enabled only when the INTx pin is high and the TRx pin is set. When cleared, the timer is enabled whenever the TRx control bit is set. C/T Timer or Counter selected cleared for timer operation (input from internal system clock). Set for counter operation (input from Tx input pin). M1 Mode bit 1 M0 Mode bit 0
Timer 1 overflow flag. Set by hardware when timer/counter 1 overflows. Cleared by hardware as the processor vectors to the interrupt service routine. Timer 1 run control bit. Set/cleared by the software to turn timer/counter 1 on/off. Timer 0 overflow flag. Set by hardware when timer/counter 0 overflows. Cleared by hardware as the processor vectors to the interrupt service routine. Timer 0 run control bit. Set/cleared by the software to turn timer/counter 0 on/off. External interrupt 1 edge flag. Set by CPU when the external interrupt edge (H-to-L transition) is detected. Interrupt 1 type control bit. Set/cleared by software specify falling edge/low level triggered external interrupt External interrupt 0 edge flag. Set by CPU when the external interrupt edge (H-to-L transition) is detected. Interrupt 0 type control bit. Set/cleared by software specify falling edge/low level triggered external interrupt
Transmitter
Receiver
Full Duplex Serial Communication System 1 Transmitter Receiver System 2 Receiver Transmitter
Serial port mode specifier Serial port mode specifier Used for multiprocessor communication. (Make it 0.) Set/cleared by software to enable/disable reception. Not widely used (Make it 0) Not widely used (Make it 0) Transmit interrupt flag. Set by the hardware at the beginning of the stop bit in mode 1, must be
Receive interrupt flag. Set by the hardware halfway through the stop bit time in mode 1. Must be
1 1
0 1