You are on page 1of 20

Understanding 8051

with C
By,
Sandip Nair
sandipnair06@yahoomail.com
sandipnair.hpage.com
Embedded microcontroller

• Microcontroller has inbuilt peripherals such as memory,


UART, timers etc where as microprocessor has only the
processing unit and the peripherals, has to be connected
externally
• Application of microcontroller: mobile phones, automatic
washing machine, digital watch, A/C, automobiles,
microwave oven etc
Embedded design philosophy

• An efficient microcontroller application involves


designing with a minimum external hardware necessary to
allow the software to keep up with all of its tasks
• An efficient software is easy-to-understand software that
runs faster and requires less code
Understanding 8051
architecture
About 8051
• The first device was developed by intel
• The architecture had dominated for more than 20 years
• Other versions are developed keeping the architecture
and instruction set same for low power, larger memory
and with many peripherals added
• 8051 can be programmed using C, BASIC, C++, forth
and PL/M languages
Atmel AT89S52

• It is a 8-bit microcontroller built on 80C51


architecture
• 4.0 V to 5.5V operating voltage
• It has 8K bytes of ISP flash memory
• Three level program memory lock
• 256 X 8bit internal RAM
• 32 programmable I/O lines
• Three 16 bit timer/counter
• Eight interrupt sources
• Full duplex UART serial channel
Cont….

• Low power idle and power down modes


• Interrupt recovery from power down
• Watch dog timer
• Dual data pointer
• Power-off flag
• Fast programming time
• Flexible ISP programming
Pin diagram
Pin description

• Pin 40 =VCC = supply voltage 4.0V to 5.5 V


• Pin 20 = GND = ground
• Pin 9 = RST = reset input
• Pin 30 =ALE/ PROG = ALE for latching the low byte address
Program pulse input during flash programming
• Pin 29 = PSEN =
Port 0

• P0.0 to P0.7 = Pin 39 to Pin 32


• Has internal pull up resistor for external program
memory
• Low order address/data bus for external program
memory
• Receives code bytes for flash programming
• Outputs code bytes for verification
• Requires external pull up register during program
verification
Port 1

• Pin 1 to 8 = P1.0 to 1.7


• Has an internal pull up resistor
• P1.0 = T2 = external count input to timer/counter 2
• P1.1 = T2EX = Timer/counter 2 capture/ reload trigger
• P1.5 = MOSI = used for in-system programming
• P1.6 = MISO = used for in-system programming
• P1.7 = SCK = used for in-system programming
Port 2

• Pin 21 to 28 = P2.0 to 2.7


• Has internal pull ups
• Used for higher order bus for external memory
• Also receives some control signal during flash
programming
Port 3

• Pin 10 to 17 = P3.0 to P3.7


• Receives control signals for flash programming and
verification
• P3.0 = RXD = serial input port
• P3.1 = TXD = serial output port
• P3.2 = INT0 = external interrupt 0 = inverted
• P3.3 = INT1 = external interrupt 1 = inverted
• P3.4 = T0 = timer 0 external input
• P3.5 = T1 = timer 1 external input
• P3.6 = WR = external memory data write strobe = inverted
• P3.7 = RD = external memory data read strobe = inverted
C basics
C variables
Bit:
• It is a variable type to store a single bit value
• 8 bit variables can be stored in one byte

Byte:
•Is a variable type to store 8 bit data
• A bit variable declared can be a part of the byte
variable declared

Signed Character:
• 8 bit
• Ranges from -127 to +127
• -ve numbers are stored in 2’s complement
C variables
Integer:
• It is 16 bit variable type to store integer values
• It is default unsigned

Float:
• It is a 32 bit variable type
• Double is a 64 bit variable type
• The calculation with these variable types requires
library function
C variables

Static and volatile

Constants:
• Doesn’t vary during the program cycle
• It is stored in program memory

#define
• used at the top of the program to create short cut to save
typing
• Eg, #define uchar unsigned char
#define buffersize 100
Tips for variable

• Calculations to be done on signed variable requires


library function which adds extra code to the program,
making it lengthy and slow. Thus where ever possible
use unsigned variable.
• Variable naming should be according to the purpose it
is used for, such that it helps in understanding the code
for others
Reference

• AT89S52 datasheet
• C & the 8051 by Thomas Schultz

You might also like