Memory, Input/Output Ports, Clocking, Timers. • Writing C code for 8051 based
microcontrollers. – C51 language extensions. • Sample Diploma Project.
P89LPC935 Microcontroller Tutorial Agenda • • •
Basic Architecture of the P89LPC935 microcontroller – Memory, Input/Output Ports, Clocking, Timers Writing C code for 8051 based microcontrollers – C51 language extensions Sample Diploma Project – The Keyless lock
Diploma Project Tutorial
1
MCB900 Board
Diploma Project Tutorial
2
MCB900 Features • • • • • • •
RS232 driver provides a standard COM port interface. 8 buffered LEDs display I/O port 2 status information. Potentiometer and Jumper for ADC1 channel 2 analog input. 3.3V low-drop regulator allows wide power supply range (5V-9VDC). Jumpers for flexible ISP and debug configurations. All P89LPC935/P89LPC932 pins available on flexible connection area. Prototyping area: 55mm x 25mm (2.1" x 1.0").
Diploma Project Tutorial
3
P89LPC935 Microcontroller Basic Architecture • • • • • • • • • •
•
8-bit microcontroller 8 KByte Flash Program memory 256 Byte Data Memory (RAM) + 512 bytes auxiliary RAM (XDATA) 512 Byte EEPROM 3V device but I/O pins are 5V tolerant Up to 26 input/output pins (4 ports P0 to P3) 2 16-bit timer/counters UART for serial interfacing 15 interrupt sources, 5 priority levels 2 clock machine cycle – All instructions execute in 1, 2 or 4 machine cycles – Instructions execute 6 times faster than 8051 Program and data memory may not be expanded externally
Diploma Project Tutorial
4
P89LPC935 Microcontroller Advanced Architecture • • • • • • • • • •
In-System Programmable (ISP) In-Application Programmable (IAP) Watchdog timer Real Time Clock On-chip power-on reset I2C port SPI port Dual 4-input multiplexed 8-bit ADCs Dual DAC 2 Analog Comparators
Diploma Project Tutorial
5
P89LPC935 Microcontroller Advanced Architecture
Diploma Project Tutorial
6
Memory Organisation •
The 8051 architecture supports 2 parallel memory spaces – Program memory used for code and constant storage – Data memory used for variable storage
Diploma Project Tutorial
7
Memory Organisation •
•
Data Memory – 256 bytes of on-chip data RAM • Used for stack, registers, bit addressable memory and variables • Upper 128 bytes is indirectly addressable only – SFRs (Special Function Registers) • Used for control and status – 512 bytes of on-chip auxiliary data RAM • Duplicates 8051 off-chip data memory • Slower access than directly accessible memory – 512 bytes data EEPROM • Non-volatile data memory • Accessed by SFR only Program Memory – 8KByte flash memory – Used for application code and optionally for user data – ISP serial loader stored here
Diploma Project Tutorial
8
Input/Output Ports • •
P89LPC935 is a 28 pin device Up to 26 of the pins are available for input/Output – 3 8-bit ports P0, P1, P2 • Port pins are labeled Px.0 to Px.7 where x is the port number • P1.5 is input only and is unavailable if using an external reset • P1.2 and P1.3 may be configured as open-drain or input only – 2-bit port P3 • P3 is unavailable if using an external crystal
Diploma Project Tutorial
9
Input/Output Ports
Diploma Project Tutorial
10
Input/Output Ports •
P89LPC935 is a 3V device but the pins are 5V tolerant – Can interface directly to 5V TTL devices – Connecting a pin directly to 5V will increase power consumption – Each input can sink up to 20mA but the limit for the IC is 100mA – Configuration of the port pins may be changed to suit the hardware interface by writing to the Port Mode SFR registers.
Diploma Project Tutorial
11
Port Configurations • •
•
•
Input Only – This is the default configuration at reset Quasi-bidirectional – Standard 80C51 outputs – Strong pull-down can sink 20mA when output = ‘0’ – Weak pull-up can source 20uA when output = ‘1’ – Can be used to interface to TTL devices Push-Pull – Used when more source current is required • Can source 3.2mA when outputting a ‘1’ – Strong pull-down can sink 20mA when output = ‘0’ Open Drain – Needs an external pull-up to output a logic ‘1’ – Best solution when interfacing to 5V circuits
Diploma Project Tutorial
12
Input/Output Port Configuration • • •
On reset all port pins default to input To change a port configuration the Port Output Mode Registers 1 & 2 must be written to. A port may contain a mix of input and output pins. – e.g. configure P0.0 to P0.2 as input, all other Port 0 pins as quasibidirectional – P0M1 = 00000111 (0x07) – P0M2 = 00000000 (0x00)
Diploma Project Tutorial
13
Interfacing Inputs to 5V Logic •
• •
Although the supply voltage is lower, the I/O-pins are 5V-tolerant. – i.e., the I/O stages cannot actively drive the outputs higher than the supply voltage, but they may be pulled to 5V externally. The LPC935 inputs can be driven directly from the outputs of 5V logic families. For a 3.3V power supply, the worst case Schmitt trigger input switching levels are VIL = 0.726V and VIH = 2.31V.
Diploma Project Tutorial
14
Interfacing Outputs to 5V Logic • • •
LPC935 output can drive a TTL input. The output high level voltage (VOH) is not high enough to drive a CMOS input. When outputting a 0, the LPC935 port pin can sink up to 20mA – total limit for the chip is 100mA. Bigger loads require external interface circuitry.
Diploma Project Tutorial
15
Hardware Interfacing 3.3V
5V
3.3V
3.3V
VDD
VDD
micro
P0.1
VSS
Direct hardware interface 20mA max sink current Configure as quasi-bidir or Push-pull
Diploma Project Tutorial
micro
P0.1
VSS
Interfacing to 5V hardware Configure as open-drain 5V supply drives load
16
89LPC935 Clock •
•
A machine cycle is the minimum amount of time in which any P89LP935 operation can execute – A machine cycle is 2 clock periods in duration – All instructions will execute in 1, 2 or 4 machine cycles – The internal timers operate every machine cycle if enabled 3 clock source options – External crystal up to 12MHz – Internal RC 7.373MHz oscillator • Used on MCB900 board • Allows 2 XTAL pins to be used for input/output – Internal watchdog oscillator (400KHz)
Use 33pF capacitor for 12MHz crystal
Diploma Project Tutorial
17
Clock Configuration
Diploma Project Tutorial
18
Keil C51 Cross Compiler • •
•
ANSI C Compiler – Generates fast compact code for the 8051 and it’s derivatives Advantages of C over Assembler – Do not need to know the microcontroller instruction set – Register allocation and addressing modes are handled by the compiler – Programming time is reduced – Code may be ported easily to other microcontrollers C51 supports a number of C language extensions that have been added to support the 8051 microcontroller architecture e.g. – Data types – Memory types – Pointers – Interrupts
Diploma Project Tutorial
19
C51 Data Types Data Type bit unsigned char signed char unsigned int signed int unsigned long signed long sbit
Diploma Project Tutorial
Bits 1 8 8 16 16 32 32 1
Range 0,1 0 to 255 -128 to +127 0 to 65535 -32768 to +32767 0 to 4294967296 -2147483648 to + 2147483647 0,1
20
C51 Memory Types • •
•
• • •
Memory type extensions allow access to all 8051 memory types. – A variable may be assigned to a specific memory space code – Program memory. – unsigned char code const1 = 0x55; //define a constant – char code string1[] = “hello”; //define a string data – Lower 128 bytes of internal data memory – unsigned int data x; //16-bit variable x idata – All 256 bytes if internal data memory bdata – Bit addressable area of internal data memory xdata – External data memory (512 bytes of on-chip auxiliary data RAM)
Diploma Project Tutorial
21
Example Code (1) //program to add 2 8-bit variables void main() { unsigned char data num1, num2, result; num1 = 10; num2 = 25; result = num1 + num2; } • This code will use 3 bytes of internal data memory (00 to 7FH) • What happens if the result exceeds 255?
Diploma Project Tutorial
22
Example Code (2) //program to add 2 8-bit variables //a flag should be set if the result exceeds 100 void main() { unsigned char data num1, num2; unsigned int data result; bit overflow; num1 = 10; num2 = 25; result = num1 + num2; if (result >100) overflow = 1; }
Diploma Project Tutorial
23
Accessing Port Pins •
•
•
The microcontroller port should be configured at the start of the program. //configure port 2 to be quasi bi-directional P2M1 = 0x00; P2M2 = 0x00; //all 8 pins are quasi bi-directional Writing to an entire port P2 = 0x12; //Port 2 = 12H (00010010 binary) P2 &= 0xF0; //clear lower 4 bits of Port 2 P2 |= 0x03; //set P2.0 and P2.1 Reading from a port unsigned char data status; status = P1; //read from Port 1
Diploma Project Tutorial
24
Example Code (3) //Program to read from 8 switches connected to Port 1. The status of the switches //is written to 8 LEDs connected to Port 2. #include //SFR address definitions void main() { unsigned char data status; //variable to hold switch status P1M1 = 0xFF; P1M2 = 0; //port 1 is input P2M1 = 0; P2M2 = 0xFF; //port 2 is output //continuous loop to read switches and display the status while(1) { status = P1; P2 = status; } } Diploma Project Tutorial
25
Accessing Individual Port Pins •
•
•
Writing to a port pin //write a logic 1 to port 0 pin 1 P0^1 = 1; In a program using lots of individual port pins it is better coding practice to name the pins according to their function sbit power_led = P0^1; power_led = 1; Reading from a port pin //Turn on LED if switch is active sbit switch_input = P2^0; if (switch_input) power_led = 1; else power_led = 0;
Diploma Project Tutorial
26
Example Code (4) //program to flash an LED connected to Port 2 pin 0 every second #include sbit LED = P2^0; void delay(); void main() { P2M1 = 0; P2M2 = 0; while (1) { LED = 0; //LED off delay(); LED = 1; //LED on delay(); } } //Delay function void delay() { ……. } Diploma Project Tutorial
27
Generating Delays (1) •
•
Use a for loop – Does not use any timer resources – Uses CPU resources while running • i.e. no other task can be performed during delay loop – Can result in large amounts of machine code being generated – Results may be different for different compilers The following code results in a half second delay for the P89LPC935 operating off the 7.373MHz RC oscillator
void delay() { unsigned int i; for (i=0; i < 50000; i++); }
Diploma Project Tutorial
28
Generating Delays (2) •
Use assembly code – Use DJNZ loops to produce very compact code – Does not use timer resources – Uses CPU resources while running • i.e. no other task can be performed during delay loop
•
Use timer 0 or timer 1 – Very efficient mechanism of producing accurate delays – Timer mode, control and counter registers must be configured – Timer run bit is used to start/stop the timer – The timer flag can be polled to determine when required time delay has elapsed
Diploma Project Tutorial
29
Generating Delays (3) •
When enabled the timer counts from an initial value to FFFFH and then overflows to 0000H. The count is incremented every 2 clocks.
•
Delay = 2 x (216 – initial value)/fosc
•
Max. delay for a 7.373MHz crystal is 217/7373000 = 17.118msec.
•
Calculate the initial value for a delay of 10msec – initial value = [216 – (0.01 x 7373000)]/2 – Initial value = 28671 = 6FFFH – Load TL0 with FFH – Load TH0 with 6FH
Diploma Project Tutorial
30
Using Timer 0 to Generate a Delay //function to generate a delay of x times 10msec void delay(unsigned char x) { unsigned char z; TR0 = 0; //stop timer TF0 = 0; //clear timer flag TMOD = 0x01; //mode 1 - 16 bit timer TAMOD = 0; for (z=0;z