/********************************************************************/ /* DIY Arduino Mega Pinball Machine */ /*__________________________________________________________________*/ /* */ /* port bit type item description */ /*_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ */ /* */ /* PORTA 0 input, pullup - bumper 0 */ /* PORTA 1 input, pullup - bumper 1 */ /* PORTA 2 input, pullup - bumper 2 */ /* PORTA 3 input, pullup - bumper 3 */ /* PORTA 4 input, pullup - bumper 4 */ /* PORTA 5 input, pullup - bumper 5 */ /* PORTA 6 input, pullup - bumper 6 */ /* PORTA 7 input, pullup - bumper 7 */ /* */ /* PORTB 0 output - LCD RS register select */ /* PORTB 1 output - LCD RW read/!write */ /* PORTB 2 output - LCD E enable */ /* */ /* PORTL 0 output - LCD D0 data 0 */ /* PORTL 1 output - LCD D1 data 1 */ /* PORTL 2 output - LCD D2 data 2 */ /* PORTL 3 output - LCD D3 data 3 */ /* PORTL 4 output - LCD D4 data 4 */ /* PORTL 5 output - LCD D5 data 5 */ /* PORTL 6 output - LCD D6 data 6 */ /* PORTL 7 output - LCD D7 data 7 */ /*__________________________________________________________________*/ /* */ /* This code was run on an ATmega2560 processor mounted to an */ /* Arduino Mega 2560. The inputs were simple foil contacts, and */ /* the outputs were several LEDs and a 1602A LCD module. */ /********************************************************************/ //program setup #define F_CPU 16000000UL #include #include #include #include //global variables uint16_t score = 0; char line_1[16]; char line_2[16]; //function prototypes void instruction_LCD(unsigned char); void data_LCD(char *); //main function int main(void) { //port initialization DDRA = 0x00; //PORTA bits 0-7 input PORTA = 0xFF; //PORTA bits 0-7 pullup enabled DDRB = 0x07; //PORTB bits 0-2 output PORTB = 0x00; //PORTB bits 0-2 off DDRL = 0xFF; //PORTL bits 0-7 output PORTL = 0x00; //PORTL bits 0-7 off //LCD setup instruction_LCD(0x38); //function set: interface data bits 8, number of lines 2, font size 5x8 instruction_LCD(0x0C); //display on/off: entire display on, cursor off, cursor position off instruction_LCD(0x06); //entry mode set: cursor move increment, display shift none instruction_LCD(0x01); //clear display //LCD line 1 strcpy(line_1, "Pinball Machine"); //set first line message instruction_LCD(0x80); //move to start of first line data_LCD(line_1); //print first line message //LCD line 2 strcpy(line_2, "ECET 38001"); //set second line message instruction_LCD(0xC0); //move to start of second line data_LCD(line_2); //print second line message //delay 3s for splash screen _delay_ms(3000); //wait 3s //LCD setup instruction_LCD(0x01); //clear display //LCD line 1 strcpy(line_1, "SCORE:"); //set first line message instruction_LCD(0x80); //move to start of first line data_LCD(line_1); //print first line message //loop while (1) { //LCD line 2 snprintf(line_2, 15, "%d", score); //set char representation of score as second line message instruction_LCD(0xC0); //move to start of second line data_LCD(line_2); //print second line message //increment score if bumper sensor is pressed if ((~PINA & 0x01) || (~PINA & 0x02) || (~PINA & 0x04) || (~PINA & 0x08) || (~PINA & 0x10) || (~PINA & 0x20) || (~PINA & 0x40) || (~PINA & 0x80)) { score++; //increment score } //delay 50ms for button debounce system _delay_ms(50); //wait 50ms } } //instruction_LCD function void instruction_LCD(unsigned char instruction) { PORTB = 0x00; //disable, write, instruction PORTL = instruction; //set instruction PORTB |= 0x04; //enable _delay_ms(2); //wait PORTB &= ~(0x04); //disable _delay_ms(2); //wait } //data_LCD function void data_LCD(char *char_array_ptr) { PORTB = 0x01; //disable, write, data while(*char_array_ptr != '\0') //while char at char_array_ptr is not NULL { PORTB |= 0x04; //enable PORTL = *char_array_ptr; //set data char_array_ptr++; //increment char_array_ptr _delay_ms(2); //wait PORTB &= 0x01; //disable _delay_ms(2); //wait } }