Learn yourself, how to write a simple program using C
language for the 89C2051/89C4051. Write a C source program, compile,
and download the HEX code to the chip directly, connect DC adapter, see
what happen after power up the board. No need ICE, in-circuit programmer,
everything can be made by yourself easily.
This page was provided forBeginners who interested in using C and Assembly language to do simple experiment with the 89C2051/89C4051 Flash microcontrollers. Exemplary of experiments are;
Let begin with soldering the basic circuit. The hardware & software tools that needed for getting start are;
The startup code for TINY memory model was modified by putting a service routine for timer0 interrupt that generating 10ms timebase for many experiments. The service routine of timer0 increments CPUTICK variable by one every 10ms. The others application programs that need this variable may used by using modifier EXTERN REGISTER CHAR CPUTICK; say. Initial codes have also set the timer interrupt, if user program won't use timer interrupt, in the C source, just put DISABLE( ) functions, it will clear EA bit.
also for serinit(9600) function
serinit(9600) initializes SERIAL PORT to 9600 by using TIMER1, but not for TIMER0. Since Dave used MOV immediate value for setting TIMER MODE, only for TIMER1, but not for TIMER0. I have modified within this function by setting the TIMER0 to 16-bit counter and RUN both timers. The application program that needs CPUTICK, must then have to invoked, say serinit(9600) before, see example in driving dot LED experiment.
Editing C source program
Use DOS editor, edits the source program, example is shown below. The program will make a complement bit 7 of P1's latch every 500ms.
/*
* simple.c
* Simple demonstration program
written in C Language
* Copyright 1999 W.Sirichote
* Compiled with DDS Micro-C
TINY model
*/
#include c:\mc\8051io.h
#include c:\mc\8051reg.h
main()
{
while(1)
// loop continuously
{
P1 ^= 0x80; //
exclusive or P1's latch with 0x80
delay(500); //
delay 500 ms
}
}
Compiling using CC51
Simply invoke;
c:\mc\cc51 simple -il h=c:\mc m=t
compile C source program, simple.(C) with intel HEX file and ASM listing file output, home directory is c:\mc, and memory model is TINY.
To compile with output C as comments and Assembly program,
try -iac
or with output listing file, -il
The listing file is very useful for debugging and studying how the compiler work.
The output HEX file of the simple.c program is shown below;
:0300000002000EED
:03000300020028D0
:20000B0002001F758108D2AFD2A912002B80FE12001B80FB758CDC758A00050832050832FD
:20002B00E5906480F59074F475F001C0E0C0F01200451581158102002B2278FB120062867A
:20004B0003088604BB0004BC00012279E5A3D9FD1BBBFFF01C80F4C82581C822C92581C9A6
:20006B007A0022D083D082CF2581F581CFC082C08322CF2581F581CF221200976009E4F50C
:20008B00F02212009760F7E4F5F00422C5F0C39C7003E5F09B22FBE493CB22FCE493FB74FA
:0D00AB000193CC22FAE493F9740193CA2268
:00000001FF
Downloading the HEX file to the Chips
The output HEX file is INTEL HEX file suitable for the EASY-DOWNLOADER V1.1
With a DOS version EZ program, just simply invoke;
c:\mc\tint\ez myfirst
more easy under WINDOW via EZ3 UPLOADER.
Now the program that you wrote in C language was translated into HEX code and was programmed into the code memory, ready to run. Just put the programmed chip to the application board, plug the adapter, and see what happen?
Summary
1 edit source program with any text editorsWe start with build a Basic 2051's Circuit
2 use cc51 to compile source program into hex file
3 download hex file to the 2051/4051 chips
4 put the programmed 2051/4051 into application board
5 connect DC adapter to the board
6 if nothing happen (I provide a dot LED (P1.7) for testing the program running)
goto step 1
Build Your
Own Microcontrollers Projects
Last updated, 29 November 2542