chp2_LEDBlink.pdf
(
262 KB
)
Pobierz
Chapter
Chapter
2.1
2
The First Program
AT89S52-gLCD-STK1
by John Leung
2
The First Program
Blink a LED by software delay
The first program is a simple one: just to blink a LED. Figure 2.1.1 is an extract from the
full schematic. The driving port of LED D3 is P2.0.
Figure 2.1.1
The source code is located under cd:\src\chp2\src2_1\. Project name is blinkLED.uv2.
Launch Vision2 IDE, under
Project
manual (Figure 2.1.2), browse to blinkLED.uv2 and
click open.
Figure 2.1.2
(Revision a)
1
© 2006 TechToys Co.
www.TechToys.com.hk
All Rights Reserved
Chapter
2
The First Program
AT89S52-gLCD-STK1
by John Leung
#include <reg52.h>
#define ON 0
#define OFF 1
sbit LED = P2^0;
void DelayMs(unsigned int msec);
void DelayMs(unsigned int msec)
{
unsigned int x,y;
for(x=0; x<=msec;x++)
{
for(y=0;y<=110;y++);
}
}
void main(void)
{
for(;;)
{
(1)
(2)
(3)
(4)
(5)
}
Listing 2.1
}
LED = ON;
DelayMs(50);
LED = OFF;
DelayMs(50);
(6)
(7)
Line (1)
is the register definition file provided by Keil C. It is a map of the registers of
89C52 compatible mcu.
Line (2)
is a constant definition for portability.
Line (3)
defines the hardware pin P2.0 to be the LED driving port. The data type ‘sbit’ is
provided by Keil C for the 8051 family and is used to access an individual I/O pin.
Line (4)
is the prototype for a delay function.
Line (5)
is the body of the delay function. It has been adjusted for an 11.0592MHz
crystal.
(Revision a)
2
© 2006 TechToys Co.
www.TechToys.com.hk
All Rights Reserved
Chapter
2
The First Program
AT89S52-gLCD-STK1
by John Leung
Line (6)
is the heart of this program. AT89S52 is not the same as other microcontrollers
like Microchip’s PIC or Texas Instrument’s MSP430. There is no register to control I/O
direction. To turn on the LED, we need writing 0 to P2.0 to output low.
Line (7)
turns off the LED by writing 1 to P2.0 to make it a high impedance input.
To get a better idea of how lines 6&7 work, we need to refer to the basic port structure
of 8051. There is an Atmel 8051 Microcontrollers Hardware Manual Rev. 4316D-8051-
05105 available from Atmel web site. Figure 2.1.3 is an extract from the manual on Port
2.
Figure 2.1.3 P2 structure
Connect a LED’s cathode via a current limiting resistor to P2.0 is like Figure 2.1.4.
R2
D3
Vcc
I
d
Figure 2.1.4
By running the code
LED= ON (eq. P2^0=0),
we write 0 to the P2.0 latch and thus
turning ON the output driver FET. Current flows from Vcc to anode of D3, passing
through the current limiting resistor R2 (470Ω) and finally through the internal FET to
ground; therefore, turning LED ON.
(Revision a)
3
© 2006 TechToys Co.
www.TechToys.com.hk
All Rights Reserved
Chapter
2
The First Program
AT89S52-gLCD-STK1
by John Leung
On the contrary, writing 1 to the port latch by the code
LED=OFF (eq. P2^0=1)
will
turn off the output driver FET. Port P2^0 is pulled up by the internal pull-up resistor to
Vcc. No current flow through D3 thus turning off the LED.
Now, let’s check all options for proper HEX file generation. Right click on
Target 1
under
the current Project Workspace (Figure 2.1.5).
Figure 2.1.5
(Revision a)
4
© 2006 TechToys Co.
www.TechToys.com.hk
All Rights Reserved
Chapter
2
The First Program
AT89S52-gLCD-STK1
by John Leung
The Xtal frequency should be 11.0592MHz which is the crystal frequency onboard. Check
Use On-chip ROM
option because we are running code from the internal flash of
AT89S52. Memory Model is Small and Code Rom Size is also Small.
Figure 2.1.6
Target option
The next important option is the
Output
option. Make sure the
Create HEX File
radio
button is checked, otherwise, no hex file will be generated.
Figure 2.1.7
(Revision a)
Output option
© 2006 TechToys Co.
www.TechToys.com.hk
All Rights Reserved
5
Plik z chomika:
vegasic
Inne pliki z tego folderu:
Dobry_Darmowy_Kompilator_C_8051.zip
(23927 KB)
8255_example.zip
(290 KB)
Czytaj(9).txt
(0 KB)
asc2bin(1).asm
(0 KB)
Algoritmy_principy.zip
(8 KB)
Inne foldery tego chomika:
8051
Amiga
Dokumenty
filmy dokumentalne
Galeria
Zgłoś jeśli
naruszono regulamin