/**************************************************** / Program: Port H Switch Inputs / Name: phia4evb.c / Date: 4 June 2002 / By: Jake Gamage / The code was adapted from the assembly language program / written by Steven J. Dombrowski called phia4evb.asm / / This program reads all 8 switches on the M68HC12A4EVB / and continuously updates all readings. / / To execute this program type G 5000 /****************************************************/ #include #include #include int __main() { int dummy = 100; /* Initialize our bit variables */ int bit0, bit1, bit2, bit3; int bit4, bit5, bit6, bit7; /* Set up the PORTH direction to all inputs */ _H12DDRH = 0x00; DB12->putchar(0x1A); while(1) { /* Read in the switch states and store them in the bit variables We are bitwise ANDing the PORTH variable with the position of the bit we are interested in. Then we are logic ANDing the result of that with a "1". This will result in either a 0 or a 1 stored in the bitx variables. There are other efficient ways to do this, and they could be done directly on the printf line. */ bit0 = (1 && (_H12PORTH & 0x01)); bit1 = (1 && (_H12PORTH & 0x02)); bit2 = (1 && (_H12PORTH & 0x04)); bit3 = (1 && (_H12PORTH & 0x08)); bit4 = (1 && (_H12PORTH & 0x10)); bit5 = (1 && (_H12PORTH & 0x20)); bit6 = (1 && (_H12PORTH & 0x40)); bit7 = (1 && (_H12PORTH & 0x80)); /* Print the first line. For an explanation of the "%c\b", please see "ada4evb.c" */ DB12->printf("%c\breading\tPH0:%3d\tPH1:%3d\tPH2:%3d\tPH3:%3d\n\r", bit0,bit1,bit2,bit3); /* At the end of the next line, we will print a Carriage Return but not a Line Feed. This will move the cursor to the beginning of the line without moving the cursor down one line */ DB12->printf("%c\breading\tPH4:%3d\tPH5:%3d\tPH6:%3d\tPH7:%3d\r", bit4,bit5,bit6,bit7); /* Move the cursor up to the previous line */ DB12->putchar(0x0B); /* This last part of code will clear the screen every 100 readings. It was added because the data can become offset on the terminal after many readings. This clears the old offset data and makes the terminal look nice again */ dummy--; if(!dummy) { dummy = 100; /* Clear the terminal screen */ DB12->putchar(0x1A); } } }