| Products | Support | Sales | Company | |
User-contributed C Program for Serial CommsC does not have built-in support for serial communication. Most programmers use hardware- and OS-specific libraries of serial I/O routines. One of our customers contibuted this standalone serial communication program for a DOS-based single-board computer. The program initializes comm ports and sends data without use of a serial comm library. It is designed to talk to our G12864 graphics LCD, but the principles are the same for virtually any serial device. This program was compiled under Borland C++ V4.52. If you need more in-depth information on serial comms under C, check with user groups that support your compiler, or follow some of the helpful links from Lakeview Research. This information is offered as-is. Neither Scott Edwards Electronics, Inc. nor the author bear any responsibility for consequences arising from its use.
// This is a program that I cobbled together from various other programs to get my G12864 LCD to
// display text and bitmaps. I also use it to control a keypad on COM2. This is about as simple
// as it gets to control an LCD display and keypad. Not all of the code was written by me.
// If you have a question about this code, e-mail me and I'll try and help out. I can't guarantee
// that I'll be extremely helpful, but I can more than likely point you in the right direction
// go get things working properly.
#include < stdio.h >
#include < dos.h >
#include < ctype.h >
#define KEYINT 0x0016
#define GETKEY 0x010
int Port = 1;
int PortAddr = 0x03F8;
long int Speed = 9600;
int SpeedDiv = 12;
int EndFlag = 0;
int Port2Addr = 0x02F8;
int Ans;
// This routine initializes the COM1 port for the LCD display
PortInit()
{
outp(PortAddr + 3, inp(PortAddr + 3) | 0x080);
outp(PortAddr, SpeedDiv & 0x0FF);
outp(PortAddr + 1, SpeedDiv >> 8);
outp(PortAddr + 3, inp(PortAddr + 3) & 0x07F);
outp(PortAddr + 4, inp(PortAddr + 4) | 0x003);
}
// This routine initializes the COM2 port for the keypad
Port2Init()
{
outp(Port2Addr + 3, inp(Port2Addr + 3) | 0x080);
outp(Port2Addr, SpeedDiv & 0x0FF);
outp(Port2Addr + 1, SpeedDiv >> 8);
outp(Port2Addr + 3, inp(Port2Addr + 3) & 0x07F);
outp(Port2Addr + 4, inp(Port2Addr + 4) | 0x003);
}
// This routine shows a bitmap stored in EEPROM as image #2. Each time something is written
// to the port, you must wait until the port is ready before sending any additional information
// or the port will not respond and transmit the data.
SendBmp()
{
PortInit();
for(int d = 0; d < 1; d++)
{
outpw(PortAddr, 27);
while((inp(PortAddr + 5) & 0x020) == 0);
}
for(int a = 0; a < 1; a++)
{
outp(PortAddr, 69);
while((inp(PortAddr + 5) & 0x020) == 0);
}
for(int b = 0; b < 1; b++)
{
outp(PortAddr, 50);
while((inp(PortAddr + 5) & 0x020) == 0);
}
for(int c = 0; c < 1; c++)
{
outp(PortAddr, 32);
while((inp(PortAddr + 5) & 0x020) == 0);
}
}
// This routine displays 4 lines of text on the LCD. Each text line is 16 characters long.
ShowScreen()
{
char far * Line1 = "1 Calibrate ";
char far * Line2 = "2 Configure ";
char far * Line3 = "3 Diagnostic ";
char far * Line4 = "4 Run/Stop ";
int k;
for (k = 0;k < 17;k++)
{
outp(PortAddr, Line1[k]);
while ((inp(PortAddr + 5) & 0x020) == 0);
}
for (k = 0;k < 17;k++)
{
outp(PortAddr, Line2[k]);
while ((inp(PortAddr + 5) & 0x020) == 0);
}
for (k = 0;k < 17;k++)
{
outp(PortAddr, Line3[k]);
while ((inp(PortAddr + 5) & 0x020) == 0);
}
for (k = 0;k < 17;k++)
{
outp(PortAddr, Line4[k]);
while ((inp(PortAddr + 5) & 0x020) == 0);
}
}
// GetPortSel() is the code to get the value of a key press from a keypad. The keypad is setup
// with a BASIC Stamp chip and BASIC Stamp code to return the ASCII values for the following
// keys: 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, ENTER, A, B, C, D, BACKSPACE. This code returns the
// value of the returned keypress to the calling routine.
GetPortSel(int Ans)
{
Ans = 0;
mark:
Ans = inp(Port2Addr);
if (Ans != 0)
{
outp(Port2Addr, 0);
return(Ans);
}
else
goto mark;
}
// Use SendData() to show a message on the scren and use a keypad to respond to the message.
// The keypad was set up to return an ASCII number based on which key was pressed. The code
// resonds to the key pressed and shows displays the appropriate text. I haven't included the
// ClearScreen(), ShowCalib(), ShowConfig(), ShowDiag(), ShowRunStop() becase they are the same
// as ShowScreen() with different text.
SendData()
{
int i;
PortInit();
Port2Init();
ShowScreen();
switch(GetPortSel(Ans))
{
case 49:
ClearScreen();
ShowCalib();
break;
case 50:
ClearScreen();
ShowConfig();
break;
case 51:
ClearScreen();
ShowDiag();
break;
case 52:
ClearScreen();
ShowRunStop();
break;
default:;
}
}
main(argc, argv, envp)
int argc;
char *argv[];
char *envp;
{
ShowBmp(); // only if you are showing a bitmap from EEPROM
SendData(); // only if you are sending text to the LCD
}
|