分类: C/C++
2009-06-16 00:22:45
/* * filename: bujinji.c * Copyleft (C) lizeliang * Date: * 6.9.2009 */ #include #include #include #include "bujinji.h" //debug //#define DEBUG //gobal var int buffer = 0x33; FLAG flag = RIGHT; //declare func void set_speed(int speed,FLAG flag); void do_key(int key); void set_speed(int speed,FLAG flag) { outb(P8255_A,buffer); if (flag == RIGHT) { asm { mov ax,buffer; ror al,1; mov buffer,ax; } } else { asm { mov ax,buffer; rol al,1; mov buffer,ax; } } #ifdef DEBUG printf("buffer = %x\n",buffer); #endif delay_func(speed); } void do_key(int key) { int time = 0; if (key == 0) { buffer = 0x33; outb(P8255_A,buffer); return; } else { #ifdef DEBUG printf("key = %d\t",key); #endif flag = ((key & 0x0080) == 0? RIGHT: LEFT); #ifdef DEBUG printf("flag = %d\t",flag); #endif time = (key & 0x007f); #ifdef DEBUG printf("time = %d\t",time); #endif if (time&0x01) { #ifdef DEBUG printf("time&0x01=%d S0\t",time&0x01); #endif set_speed(S0,flag); } else if (time&0x02) { #ifdef DEBUG printf("time=%d S1\t",time); #endif set_speed(S1,flag); } else if(time&0x04) { #ifdef DEBUG printf("time=%d S2\t",time); #endif set_speed(S2,flag); } else if (time&0x08) { set_speed(S3,flag); #ifdef DEBUG printf("S3\t"); #endif } else if (time&0x10) { set_speed(S4,flag); #ifdef DEBUG printf("S4\t"); #endif } else if(time&0x20) { set_speed(S5,flag); #ifdef DEBUG printf("S5\t"); #endif } else if (time&0x40) { set_speed(S6,flag); #ifdef DEBUG printf("S6\t"); #endif } else { #ifdef DEBUG printf("time&0x02=%d\tstop\t",time&0x80); #endif } } } int main() { int key; outb(P8255_MODE,0x90); //10000001,A:方式0,输出;B:方式0,输出;C高:输出,C低:输入 outb(P8255_A,buffer); //init buffer = 33h while (!is_key()) { key = inb(P8255_C); do_key(key); } return 0; } |
#ifndef __BUJINJI_ #define __BUJINJI_ /* * filename: bujinji.h * Copyleft (C) lizeliang * Date: * 6.8.2009 */ //port address #define IOY0 0xE800 #define IOY1 0xE840 #define IOY2 0xE880 #define IOY3 0xE8C0 //address offset #define OFFSET 4 //8255 address #define P8255_A IOY3+0x00*OFFSET #define P8255_B IOY3+0x01*OFFSET #define P8255_C IOY3+0x02*OFFSET #define P8255_MODE IOY3+0x03*OFFSET #define SPEED 0x10 //rotray speed #define S0 SPEED*1 #define S1 SPEED*2 #define S2 SPEED*3 #define S3 SPEED*4 #define S4 SPEED*5 #define S5 SPEED*6 #define S6 SPEED*7 //deine flag type typedef enum { RIGHT, /*0*/ LEFT /*1*/ }FLAG; //declare func extern void outb(int addr,int mode); extern int inb(int addr); extern void display(char *msg); extern int is_key(void); extern void delay_func(int dly); #endif |
/* * filename: comm.c * Copyleft (C) lizeliang * Date: * 6.9.2009 */ #include #include #include //declare void outb(int addr,int mode); int inb(int addr); void display(char *msg); int is_key(void); void delay_func(int dly); void outb(int addr,int mode) { asm mov dx,addr; asm mov ax,mode; asm out dx,ax; } int inb(int addr) { asm mov dx,addr; asm in al,dx; asm xor ah,ah; return (_AX); } void display(char *msg) { asm mov dx,msg; asm mov ah,09h; asm int 21h; } int is_key(void) { asm mov ah,06h; asm mov dl,0ffh; asm int 21h; asm je nokey; asm mov ax,1; return (_AX); nokey: asm mov ax,0; return (_AX); } void delay_func(int dly) { asm mov dx,dly; delay: asm mov cx,0ffffh; thus: asm loop thus; asm dec dx; asm jnz delay; } |
# Makefile for bujinji # Copyleft (C) lizeliang # Date: # 6.8.2009 TARGET= bujinji CC= tcc CFLAGS= -B -DDEBUG TCDIR= C:\JMSOFT\CYuYan\tc3 INCLUDE= $(TCDIR)\INCLUDE LIB= $(TCDIR)\LIB $(TARGET).exe: $(TARGET).obj comm.obj $(CC) $(CFLAGS) -I$(INCLUDE) -L$(LIB) $(TARGET).obj comm.obj $(TARGET).obj: $(TARGET).c $(TARGET).h $(CC) $(CFLAGS) -I$(INCLUDE) -L$(LIB) -c $(TARGET).c comm.obj: comm.c $(CC) $(CFLAGS) -I$(INCLUDE) -L$(LIB) -c comm.c clean: @del *.obj distclean: @del $(TARGET).exe |