Experimenting the MTK-85 with Micro-C for 8085
We may develop the code using c language and run it on the MTK-85 easily. However we will need the program that translates c code into assembly code and the program that translates assembly code to hex code or machine code. Then we can use serial port for hex code loading to the board memory. The program that translates c code to the assembly code is called c compiler. And the program that translates assembly code to hex code is called Assembler. The Micro-C for 8085 includes the compiler and the assembler. The flow of such process can be viewed like this.
C program ----> Assembly code ----> Hex code ----> load to memoryStartup Modification
Before we can use the Micro-C with our board, we must modify the startup code to let the compiler knows the memory space allocation. Since the MTK-85 has built-in monitor program in ROM, so we can the test running the program in RAM (0x8000-0xFFFF).
ORG $8000 Place code in memory here
JMP startORG $8100
* LXI SP,$6000
start XRA A
STA ?heap
CALL main
exit RST 7We set the origin to 0x8000. We comment the stack pointer loading and replace place RTS 1 to RST 7. The stack pointer is now set by our monitor program. This startup code is the beginning of our code. The c code will have the main function that is called from the instruction CALL main.
Example 1. Your First Program
The first program will write the 8-bit data to the onboard output port at the I/O address 0.
/*
Test GPIO LED with Micro-C
*/#include c:\mc85\8085io.h
#define gpio 0 // GPIO I/O address
delay(int j)
{
int n;
for(n=0; n<j; n++)
continue;
}main()
{
int i;
i=0;
while(1) // repeat below block forever
{
out(gpio,i++); // write to 4-bit output port
delay(200); // delay
}}
The first program will write the 8-bit data to the onboard output port at the I/O address 0. The compilation process can be studied from the Micro-C technical manual. You can also test the HEX code by downloading it to the MTK-85 memory. To run the program, press RESET key. If we use NVRAM, DS1230Y the code will not lost when the board is power off. When we power the board it will run the code automatically. To get back to monitor program, push USER1 key and then press RESET key. Below shows the running of the GPIO LED.
Example 2. 7-segment Display Scanning
The 2nd examples is to use the 7-segment LED for displaying the number.
/*
Scan display on 7-segment
*/#include c:\mc85\8085io.h
#define port_a 0x10
#define port_b 0x11
#define port_c 0x12
#define port_control 0x13
char buffer[6];
int i;// 7-segment LED conversion table
char convert[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};delay(int j)
{
int n;
for(n=0; n<j; n++)
continue;
}scan_display()
{char digit;
for(digit=0 ; digit<6; digit++) // loop for 6-digit
{
out(port_c,digit|0xf0); // enable digit first
out(port_b,buffer[digit]); // write segment
delay(10); // delay for electron transition
out(port_b,0); // turn off the digit
}
}main()
{
buffer[0]=convert[0]; // load buffer with 12345
buffer[1]=convert[1];
buffer[2]=convert[2];
buffer[3]=convert[3];
buffer[4]=convert[4];
buffer[5]=convert[5];while(1)
{
scan_display();
}
}
The onboard 6-digit LED display is multiplex display. Each digit will ON a short period of time. The function scan_display( ) provides 6-round looping for 6-digit display. Micro-C provides function out(port_address,data) for writing the 8-bit data to the output port. Please refer to the hardware schematic of the display in user manual. The scanning process is done by digit activation followed with segment and delay a short time. Then turn off the segment before switch to the next digit. The main loop repeats scan_display( ) function forever. We will see the number, 012345 on the display. The member of array convert[ ] are 7-segment pattern for displaying the number 0 to 9. You may check the bit designation for segment a, b, c, to dp at PORTB. With c code, we can convert the value 0 to 9 to the 7-segment pattern easily using indexing method. HEX code
Example.3 Number Counting on 7-segment Display
How can we display the real number as variable, not the fixed pattern?
/*
Display counting number on 7-segment
*/#include c:\mc85\8085io.h
#define port_a 0x10
#define port_b 0x11
#define port_c 0x12
#define port_control 0x13
char buffer[6];
int i;unsigned int temp;
char convert[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
delay(j)
int j;
{
int n;
for(n=0; n<j; n++)
continue;
}scan_display()
{char digit;
for(digit=0 ; digit<6; digit++)
{
out(port_c,digit|0xf0);
out(port_b,buffer[digit]);
delay(2);
out(port_b,0);
}
}
num2buffer(int n)
{buffer[0]=0;
buffer[1]=convert[n/10000];
temp=n%10000;
buffer[2]=convert[temp/1000];
temp%=1000;
buffer[3]=convert[temp/100];
temp%=100;
buffer[4]=convert[temp/10];
buffer[5]=convert[temp%10];
}
main()
{
int k;
k=0;
while(1)
{num2buffer(k++);
for(i=0;i<10; i++)
scan_display();}
}Since the display has 6-digit, thus we will need the method to convert the number into display pattern. For 16-bit integer number, we can have 5-digit display. To separate the value into the 5-digit LED, we can use division method. The function num2buffer( ) separates the 16-bit integer number into 5-digit by finding the value for each power of ten. The first digit is found by dividing the value with 10000. The 2nd digit, by dividing the remainder with 1000 and so on for the rest. HEX code See the program running below.
Example.4 Hello World
The serial port with RS232 standard can be used to connect the terminal for program debugging or some application. Our board provides speed of 9600 8n1 asynchronous format controlled by the 8250 UART chip. To let Micro-C uses such serial port, we can modify the library source, serio.asm easily.
We may test the serial port with this program.
/*
Hello world from MTK-85
*/#include c:\mc85\8085io.h
int i;
main()
{
for(i=0; i<100;i++)
putstr("\nHello world from MTK-85");
}The function putstr( ) will write the string by sending its ASCII code for each letter to the RS232 port one by one from H, e, l, l,o.... and terminated at the end of string. The for loop will repeat 100 times. HEX code
Example.5 Kaypad Scanning
This example will display the key position when pressed on the terminal. The function that prints the position value in HEX is printf ( ) statement./*
Kaypad scanning
Display scan code on terminal
*/#include c:\mc85\8085io.h
#define port_a 0x10
#define port_b 0x11
#define port_c 0x12
#define port_control 0x13
int i;
char temp;
delay(int j)
{
int n;
for(n=0; n<j; n++)
continue;
}char scan_key()
{char digit;
char key_position;
char code;code=-1;
key_position=0;
for(digit=0 ; digit<6; digit++) // loop for 6-digit
{
out(port_c,digit|0xf0); // enable digit firsttemp=in(port_a); // read from PORTA
for(i=0; i<8; i++)
{
if((temp&1)==0) code=key_position;
{
temp>>=1;
key_position++;
}}
}
return code;
}
char read_key()
{while(scan_key()!=-1)
continue;
delay(1); // debounce when key releasedwhile(scan_key()==-1)
continue; // scan until key pressed
delay(1); // debounce when pressedreturn scan_key();
}
main()
{putstr("\nPress a key on the MTK-85");
while(1)
{
printf("\nKey position=%02x",read_key());
}
}
The keypad is connected in 6x4 matrix. The low level function that scans keypad is scan_key( ). Key position is the value that reads by counting up to the number from the first key. The detection logic is ZERO when key was pressed. The method is done by making the column line to logic '0' The column lines are the output from U18. Then the ROW signals are read through PORTA. Detecting the key being pressed checks which bit will be read as '0'. If there is no key pressed, all bits are '1'.The key position will be incremented. If there is a key pressed, the key position will be saved to variable named as code. The function scan_key( ) when called will be executed only one time. So in order to check key pressed, we will need the upper level function. The function read_key( ) will return key position when key was pressed.The key position will display on terminal screen when pressed. HEX code
Written
by Wichit Sirichote, kswichit@kmitl.ac.th
Last updated: May 13, 2009.
< CONTENTS