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

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;

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 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

  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;


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?


1    edit source program with any text editors
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
We start with build a Basic 2051's Circuit

Build Your Own Microcontrollers Projects

Last updated, 29 November 2542