Zilog Z80 microcontroller UART serial end

This is the second post in the series “serial port on android”. It is about using platform based design, a micro-controller (zilog z80) system that does nothing more than just communicate with the android phone. Later, we will see how to do sensing and actuation for the interface to the physical world.

The hardware

This is a custom board made for general purposes in embedded applications with sensors, actuators,an LCD screen and serial communication. I haven’t given details of how to develop the circuit here, may be on a later post….

a z80

z80 microcontroller board with a serial UART

The Software

The code is entirely written in C. This is sometimes considered a high level language. For the folks coding in Java and scripting(Python, Perl, Php), you might take that differently. If you were to consider assembly programming, this is high level stuff. The simplest software we can have is what we call a single loop operating system. It has a main method with initialization code, then a single infinite while loop within which all the possible functions of the system happen until the hardware is powered down.  To be able to write software for a micro-controller chip, you need the manufacturers documentation that indicates its architecture. Most important is the layout of control, status and data registers. Lets keep it silly simple. we have the following files:

/* main.h - declarations of global variables & functions, and includes libraries. */ 
int byte0, flag; 
void init_uart(); 
void send(char ch); 
void read(); 
/* uart.h - The communications driver for initialization and simple I/O routines */ 
#define CRYSTAL_FREQ 20000000 // we have a 20MHz external crystal oscillator 
#define BAUDRATE 9600 // communication baud rate. Default value is 57600 baud. 
                      //User can change to desired value supported by controller. 
#define BRG0 ( (CRYSTAL_FREQ + (BAUDRATE * 8)) / (BAUDRATE * 16 ) ) 
#define BAUD_H BRG0 / 256 
#define BAUD_L BRG0 % 256 
extern int byte0, flag; 

void init_uart() { 
    PAADDR = 0x02; // select alternate function 
    PACTL = 0x30; // pins PA4 is configured as RXD and PA5 as TXD 
    U0BRH = BAUD_H; // 9600 baud default 
    U0BRL = BAUD_L; 
    U0CTL1 |= 0x00; // Normal mode of operation 
    U0CTL0 |= 0x50; // Receive and transmitt enable,no parity,one stop bit 
    IRQ0ENH |= 0x10; // Rx interrupts 
    IRQ0ENL |= 0x10; // Rx priority 2 

void read(void) { 
    byte0 = (char)U0RXD; //get the receive data byte 
    flag = 1; 

void send(char ch){ 
    while ((U0STAT0 & 0x04) != 0x04 ); /* Transmit Data register enabled*/ 
    U0TXD = ch; 
/* main.c - The single loop OS. */
char data = 0x00;//the data holding variable 

//LED indicator functions 
void lights_on(){ 
    PCOUT |= 0x04; // pin 2 of port C set high, LED on 
void lights_off(){ 
    PCOUT &= ~0x04; // pin 2 of port C set low, LED off 

//the main method 
int main(void){ 
    init_uart(); //configure the UART by initializing registers 
    flag = 0;//flag to show when there's data 
        data = 0x01; 
        send(data); //send a character(01) 
        if(flag!=0){ //check if there is data 
            int cpy_data = byte0; 
                lights_on();//indicate a one was sent in response 
            lights_off();//indicate something else other than a one was sent 
    return 0; 
#pragma interrupt // interupt service routine for receive 
void ISR_Receive(void) { 
    SET_VECTOR(UART0_RX,ISR_Receive); //defining ISR for receive 

That’s all the code we need to be able to send a 0x01 and wait for a reply, if we receive a 0x01, we echo it back and light the LED, if we receive anything else, we send a 0x02 character and put the LED off. This is not a really useful communication protocol but, it does test the hardware. You can modify it further to meet maturity required in a project.
I mostly use this to forward data from a sensor reading to a PC when requested. Say if the micro gets a 0x01 to 0x09, it reads and forwards data from the channel requested, within channels 1 through 9.
NB: Zilog kits come with a Windows IDE which has the toolchains for compiling the code to run on the micro. I have no Idea whether there exists linux, opensource toolchain for z8 and I am not interested because I am planning to move to arm based micros, specifically from NXP (still reading the mannuals, hehe!!!).

Articles in this series

5 responses to “Zilog Z80 microcontroller UART serial end”

  1. mica

    where can I find the schematic diagram of this project?

  2. mica gutierrez

    where is the schem?

  3. JeffreyEdward


    Your code looks like it will be very usefull. However I do not know what is included in your main.h header. I feel as if there is some valuable information in their. Also, I am trying to export data out of my Z80 all the time with out the need of recieve data… Do you have any Application notes or suggestions??? Thanks again for the usefull information you have supplied already…:)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: