Sample C file text
#define BASE 0x378
#define I_REGISTER 2
#define D_REGISTER 0
#define WRITE_DATA 8
#define Superio_index 0x2e
#define Superio_data 0x2f
#define BANK_REG 0x07
#define timeout_value 5 //Set time out value, the default value is 5
seconds
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include <unistd.h>
#include <sys/io.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include
<sys/perm.h>
#include <sys/time.h>
int status;
pid_t pid;
// LCD display control functions
void display_on(void);
void display_off(void);
void clear_display(void);
void return_home(void);
void entry_mode(void);
void function_set(void);
void
initialize_display(void);
void print_character(int character);
int detect_all_press();
void open_LCD(void);
void close_LCD(void);
void timeout(void);
int btn_pressed=0, count_end=1;
// Functions for making screens to send to the LCD
void main_loop(void);
//
Main function
int main(void)
{
int width; // LCD screen width
int height; // LCD screen height
int i; // Just a counter
width = 16;
height = 2;
ioperm(BASE, 3, 1); // Get permission to the prallel port
outb(0, BASE + 2); // Make sure there is power on
C0
initialize_display(); // Initialize the display
main_loop();
display_off();
ioperm(BASE, 3, 0);
return 0;
}
void open_LCD(void)
{
int data_temp;
ioperm(Superio_index, 2, 1); //Get permission to the GPIO port
outb(0x87, Superio_index);
outb(0x87,
Superio_index);
outb(BANK_REG, Superio_index);
outb(BANK_REG, Superio_data);
outb(0xf1, Superio_index);
data_temp=inb(Superio_data);
data_temp&=~(0x10);
outb(data_temp, Superio_data);
ioperm(Superio_index, 2, 0); //Cloase permission to the GPIO port
}
void
close_LCD(void)
{
int data_temp;
ioperm(Superio_index, 2, 1); //Get permission to the GPIO port
outb(0x87, Superio_index);
outb(0x87, Superio_index);
outb(BANK_REG, Superio_index);
outb(BANK_REG, Superio_data);
outb(0xf1,
Superio_index);
data_temp=inb(Superio_data);
data_temp|=0x10;
outb(data_temp, Superio_data);
ioperm(Superio_index, 2, 0); //Cloase permission to the GPIO port
}
void timeout(void)
{
btn_pressed=1;
count_end=0;
}
void
display_on(void)
{
outb(12, BASE); //Turn on D3 and D4
outb(I_REGISTER + WRITE_DATA, BASE + 2); //Turn off C1 and C3
outb(4 + I_REGISTER + WRITE_DATA, BASE + 2); //Turn on C2 while C1 and C3 remains off
outb(I_REGISTER + WRITE_DATA, BASE + 2); //Reset Control
pins
usleep(0);
}
void display_off(void)
{
outb(8, BASE); //Turn on D3 and D4
outb(I_REGISTER + WRITE_DATA, BASE + 2); //Turn off C1 and C3
outb(4 + I_REGISTER + WRITE_DATA, BASE + 2); //Turn on C2 while C1 and C3 remains off
outb(I_REGISTER + WRITE_DATA,
BASE + 2); //Reset Control pins
usleep(0);
}
void clear_display(void)
{
outb(1, BASE); //Turn on D0
outb(I_REGISTER + WRITE_DATA, BASE + 2); //Turn off C1 and C3
outb(4 + I_REGISTER + WRITE_DATA, BASE + 2); //Turn on C2 while C1 and C3 remains off
outb(I_REGISTER
+ WRITE_DATA, BASE + 2); //Reset Control pins
usleep(0);
}
void return_home(void)
{
outb(2, BASE); //Turn on D1
outb(I_REGISTER + WRITE_DATA, BASE + 2); //Turn off C1 and C3
outb(4 + I_REGISTER + WRITE_DATA, BASE + 2); //Turn on C2 while C1 and C3 remains off
//
usleep(1); //Wait while Enable (C2) is high so LCD will have time to load the info
outb(I_REGISTER + WRITE_DATA, BASE + 2); //Reset Control pins
usleep(0);
}
void entry_mode(void)
{
outb(6, BASE); //Turn on D1 and D2
outb(I_REGISTER + WRITE_DATA, BASE + 2); //Turn
off C1 and C3
outb(4 + I_REGISTER + WRITE_DATA, BASE + 2); //Turn on C2 while C1 and C3 remains off
outb(I_REGISTER + WRITE_DATA, BASE + 2); //Reset Control pins
usleep(0);
}
void function_set(void)
{
outb(56, BASE); //Turn on D3, D4 and D5
outb(I_REGISTER +
WRITE_DATA, BASE + 2); //Turn off C1 and C3
outb(4 + I_REGISTER + WRITE_DATA, BASE + 2); //Turn on C2 while C1 and C3 remains off
outb(I_REGISTER + WRITE_DATA, BASE + 2); //Reset Control pins
usleep(0);
}
void
initialize_display(void)
{
function_set();
function_set();
entry_mode();
display_on();
clear_display();
}
void print_character(int character)
{
outb(D_REGISTER + WRITE_DATA, BASE + 2); //Set register to Write Data
outb(character, BASE); // Set the
character code
outb(4 + D_REGISTER + WRITE_DATA, BASE + 2); //Enable High
outb(D_REGISTER + WRITE_DATA, BASE + 2); //Disable High
usleep(0); //Wait while Enable (C2) is high so LCD will have time to load the info
}
int detect_all_press()
{
unsigned char
b123;
b123 = 0xF0 & inb(0x379);
switch(b123){
case 0x70: //don't press any button
return 0;
break;
case 0x30: //button left
return 1;
break;
case 0x60: //button up
return 2;
break;
case 0xF0: //button right
return
3;
break;
case 0x50: //button down
return 4;
break;
case 0xB0: //button left & right to exit
return 5;
break;
default:
return 0;
break;
}
}
void main_loop(void)
{
int x, retval, timer_counter=0;
char
clearbr[]=" up"; //show word
char bypassoff[]=" right";
char aewinos[]=" down";
char bypasson[]=" left";
unsigned char
d123=0, c123;
pid=fork();
// printf("PID= %d\n", pid);
if(pid==0)
{
// printf("Signal Alarm timeout!\n");
signal(SIGALRM,timeout); //excute timeout func as soon as receive the SIGALRM
// printf("Process Signal timeout\n");
while(1)
{
if(btn_pressed)
{
btn_pressed=0;
timer_counter=timeout_value;
}
else if(!count_end)
{
timer_counter--;
sleep(1);
if(!timer_counter) //If timer count out, Excute
close_LCD,
{
close_LCD();
timer_counter=timeout_value;
count_end=1;
}
}
// printf("it is in child process\n");
}
exit(0);
}
else
if(pid==-1)
{
printf("The Timer subprocess was not created! \n");
}
else
{
// printf("PID= %d\n",pid);
while(1){
c123=detect_all_press(); //?sbuttonA
if ((d123 != 1) && (c123 ==
1)){
clear_display();
for(x = 0; x < strlen(clearbr); x++)
print_character(clearbr[x]); //display "left" in lcm
open_LCD();
kill(pid,SIGALRM);
printf("up\n"); //print
in computer
}
if ((d123 != 2) && (c123 == 2)){
clear_display();
for(x = 0; x < strlen(bypassoff); x++)
print_character(bypassoff[x]); //display
"up"
open_LCD();
kill(pid,SIGALRM);
printf("right\n");
}
if ((d123 != 3) && (c123 == 3)){
clear_display();
for(x = 0; x < strlen(aewinos); x++)
print_character(aewinos[x]); //display
"right"
open_LCD();
kill(pid,SIGALRM);
printf("down\n");
}
if ((d123 != 4) && (c123 == 4)){
clear_display();
for(x = 0; x < strlen(bypasson); x++)
print_character(bypasson[x]); //display
"down"
open_LCD();
kill(pid,SIGALRM);
printf("left\n");
}
if ((d123 != 5) && (c123 == 5)){
clear_display();
close_LCD();
retval = kill( pid,SIGKILL );
if ( retval )
{
puts( "kill failed." );
perror( "kill" );
waitpid( pid, &status, 0 );
}
else
{
printf("LCD test is exited!\n");
}
break; //?kP?loopAM?`{
}
return_home();
d123=c123; //OUbuttonA d?Uj
}
}
}
over 1 year ago