Experimenting the 2051 withC Programming
Wichit Sirichote, kswichit@kmitl.ac.th

Learn yourself, how to write a simple program using Clanguage for the 89C2051/89C4051. Write a C source program, compile,and download the HEX code to the chip directly, connect DC adapter, seewhat happen after power up the board. No need ICE, in-circuit programmer,everything can be made by yourself easily.



Introduction

This page was provided forBeginnerswho interested in using C and Assembly languageto do simple experiment with the 89C2051/89C4051 Flash microcontrollers.Exemplary of experiments are;

Hardware & Software Tools

Let begin with soldering the basic circuit. The hardware& software tools that needed for getting start are;

A Bit Modification of Micro-C Startup Code for TINY Model

The startup code for TINY memory model was modified byputting a service routine for timer0 interrupt that generating 10ms timebasefor many experiments. The service routine of timer0 increments CPUTICKvariable by one every 10ms. The others application programs that need thisvariable may used by using modifier EXTERN REGISTER CHAR CPUTICK; say.Initial codes have also set the timer interrupt, if user program won'tuse timer interrupt, in the C source, just put DISABLE( ) functions, itwill clear EA bit.

also for serinit(9600) function

serinit(9600) initializes SERIAL PORT to 9600 by usingTIMER1, but not for TIMER0. Since Dave used MOV immediate value for settingTIMER MODE, only for TIMER1, but not for TIMER0. I have modified withinthis 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 shownbelow. The program will make a complement bit 7 of P1's latch every 500ms.

/*
 * simple.c
 * Simple demonstration programwritten in C Language
 * Copyright 1999 W.Sirichote
 * Compiled with DDS Micro-CTINY 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 fileand ASM listing file output, home directory is c:\mc, and memory modelis 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 studyinghow 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 theEASY-DOWNLOADERV1.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 translatedinto 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 withany text editors
2    use cc51 to compile source programinto hex file
3    download hex file to the 2051/4051chips
4    put the programmed 2051/4051 intoapplication board
5    connect DC adapter to the board
6    if nothing happen (I provide a dotLED (P1.7) for testing the program running)
      goto step 1
We start with build a Basic2051's Circuit

Build YourOwn Microcontrollers Projects
 

Last updated, 29 November 2542