/* ********************************************************************** * File S1deb1.C - Rel. 1.1 compile with uC/51 V. 1.10.10 * * GRIFO(R) via Dell'Artigiano 8/6 40016 S. Giorgio di Piano (BO) * * Cards: GPC(R) F2 d.s. 130688 and DEB01 or TIO16 * * Tel. +39 051 892052 Fax. +39 051 893661 * * http://www.grifo.com http://www.grifo.it * * sales@grifo.it tech@grifo.it grifo@grifo.it * * by Graziano Gaiba 29.09.03 * ********************************************************************** Rel. 1.1 - 29.09.2003 by Graziano Gaiba Connect CN15 of DEB01 or CN1 of TIO16 through a 20 pins flat to CN3 of GPC(r) F2. This program shows the hexadecimal code of a key pressed on the PC's keyboard on the (red) LEDs of port A. */ #include #include #include // Clock frequency of CPU used #define FCLOCK 11059200 // On board peripherals registers allocation addresses. static unsigned char PORTA @ 0xFA00, PORTB @ 0xFA01, PORTC @ 0xFA02, CNT @ 0xFA03; /********* General purpose utilities and hw sections management ***********/ void iniser(unsigned long baud) /* Initialise serial port with: Bit x chr = 8 Stop bit = 1 Parity = None Baud rate = baud using timer 1 as baud rate generator. */ { SCON=0x052; // Mode 1, receiver enabled TMOD&=0x00F; // Timer 1 in auto-reload mode TMOD|=0x020; TR1=0; // Stop TIMER 1 TH1=256-((2*FCLOCK)/(384*baud)); // baud at 14.7456 MHz PCON=PCON|0x080; // Set SMOD=1 for higher baud rate TR1=1; // Start TIMER 1 } // ************************ Procedure HexAdjust **************************** // This procedure accepts in input an unsigned char and checks that it is // an hexadecimal figure (number from 0 to 9 or letter from 'a' to 'f' or // from 'A' to 'F'. If check is postive, the value of hexadecimal figure is // returned (from 0 to 15) or 16 is returned. // Input parameters: // c, type unsigned char: character code to examine // Returns value of c as hexadecimal figure or 16. unsigned char HexAdjust(unsigned char c) { c-=48; // If c if a figure from '0' to '9', subtract 48 // (ASCII '0') to obtain its value if(c>15) // If c is still greater than 15, maybe it was a // character from 'A' to 'F' { c-=7; // After subtracting 48 and 7 ora, (ASCII 'A', // (65)+10)... if(c<10 || c>15) // ...maybe if was from 'a' to 'f' { c-=32; // Total value subtracted: ASCII of 'a' if(c<10 || c>15) // If still it is not between 0 and 15... { // ...then c was not an hexadecimal figure c=16; // Set to return 16 } } } return c; // Return value of c or 16 } // Wait for signals settling void setup(void) { unsigned int i; for(i=10000; i!=0; i--) ; } // Initialise 82c55 with value in parameter void init8255(unsigned char c) { CNT=c; } // Set port A of 82c55 void setPortA(unsigned char v) { PORTA=v; } // Set port B of 82c55 void setPortB(unsigned char v) { PORTB=v; } // Set port C of 82c55 void setPortC(unsigned char v) { PORTC=v; } // **************************** Main program ***************************** void main(void) { unsigned char tasto; setup(); // Wait for signals settling iniser(9600); TI=0; RI=0; // Comment next two lines if serial port is used in polling (SIOTYPE=p or k) ES=1; // Enable serial port interrupt EA=1; // Enable all interrupts // Initialisation of PPI 8255. init8255(0x80); // PORT A, B, C in OUTPUT. setPortA(0xff); // Clear PORT A. setPortC(0xff); // Clear PORT C. puts("\n********************* Demo S1DEB1 **********************"); for (;;) // Loop forever { puts("\nPress a key corresponding to an hexadecimal figure."); putc(tasto = getc()); // Wait a key pressed and echo. tasto = HexAdjust(tasto); if(tasto<=15) // Hexadecimal figure? { setPortA(~tasto); // Show its code on PORT A. } else { puts("\nError !"); // Not an hexadecimal figure setPortA(0xff); // Clear PORT A. } } }