/* * BankSwitcher.c * * Author: Özay Turay a.k.a Simon/CGTr ([url]http://www.commodore.gen.tr[/url]) */ #ifndef F_CPU #define F_CPU 9600000UL // Use 9.6MHz Internal Oscillator#endif#include <avr/io.h>#include <inttypes.h>#include <util/delay.h>#include <avr/eeprom.h>#define T13_PORT PORTB // Register for Button Output#define T13_PIN PINB // Register for Button Input#define T13_DDR DDRB // Data Direction Register#define ADDR_13 PB0 // Address Pin 13 of the EPROM#define ADDR_14 PB1 // Address Pin 14 of the EPROM#define ADDR_15 PB2 // Address Pin 15 of the EPROM#define BANK_BTN PB3 // Bank Switcher Button Pin#define HOST_RST PB4 // Host Device Reset Pin#define DEBOUNCE 25 // Time to wait while "de-bouncing" button#define REPEATER 250 // Time to wait after a button press#define set_output(port, pin) (port |= (1<<pin))#define clear_output(port, pin) (port &= ~(1<<pin))#define toggle_output(port, pin) (port ^= (1<<pin))uint8_t currentbank; // Initialize Current Bank Global Variableint button_is_pressed(){ if (bit_is_clear(T13_PIN, BANK_BTN)) { _delay_ms(DEBOUNCE); if (bit_is_clear(T13_PIN, BANK_BTN)) return 1; } return 0;}int button_is_released(){ if (bit_is_set(T13_PIN, BANK_BTN)) { _delay_ms(DEBOUNCE); if (bit_is_set(T13_PIN, BANK_BTN)) return 1; } return 0;}int main(void){ T13_DDR |= (1<<HOST_RST); // Configure Host Device Reset Pin as OUTPUT clear_output(T13_PORT, HOST_RST); // Pull Host Device Reset Pin LOW so that it doesn't startup while we are preparing T13_DDR |= (1<<ADDR_13); // Configure Address Pin 13 of the EPROM as OUTPUT T13_DDR |= (1<<ADDR_14); // Configure Address Pin 14 of the EPROM as OUTPUT T13_DDR |= (1<<ADDR_15); // Configure Address Pin 15 of the EPROM as OUTPUT T13_PORT |= (1<<BANK_BTN); // Configure Bank Switcher Button Pin as INPUT_PULLUP currentbank = eeprom_read_byte((uint8_t*)0); // Read value from EPROM if (currentbank == 0xFF) currentbank = 0; // In case of no value make it 0 if (bit_is_set(currentbank, 0)) set_output(T13_PORT, ADDR_13); else clear_output(T13_PORT, ADDR_13); // Set Address Pin 13 of the EPROM using Current Bank Bit 0 if (bit_is_set(currentbank, 1)) set_output(T13_PORT, ADDR_14); else clear_output(T13_PORT, ADDR_14); // Set Address Pin 14 of the EPROM using Current Bank Bit 1 if (bit_is_set(currentbank, 2)) set_output(T13_PORT, ADDR_15); else clear_output(T13_PORT, ADDR_15); // Set Address Pin 15 of the EPROM using Current Bank Bit 2 _delay_ms(REPEATER); // Wait some time to finish Host Device Reset operation set_output(T13_PORT, HOST_RST); // Pull Host Device Reset Pin HIGH so that it starts up as preparing is done while (1) { if (button_is_pressed()) { clear_output(T13_PORT, HOST_RST); // Pull Host Device Reset Pin LOW so that it resets while we switch banks currentbank++; // Select next bank if (currentbank > 7) currentbank = 0; // In case of overflow make it 0 eeprom_write_byte((uint8_t*)0, currentbank); // Write new value to EPROM if (bit_is_set(currentbank, 0)) set_output(T13_PORT, ADDR_13); else clear_output(T13_PORT, ADDR_13); // Set Address Pin 13 of the EPROM using Current Bank Bit 0 if (bit_is_set(currentbank, 1)) set_output(T13_PORT, ADDR_14); else clear_output(T13_PORT, ADDR_14); // Set Address Pin 14 of the EPROM using Current Bank Bit 1 if (bit_is_set(currentbank, 2)) set_output(T13_PORT, ADDR_15); else clear_output(T13_PORT, ADDR_15); // Set Address Pin 15 of the EPROM using Current Bank Bit 2 _delay_ms(REPEATER); // Wait some time as a key repeat delay } if (button_is_released()) { set_output(T13_PORT, HOST_RST); // Pull Host Device Reset Pin HIGH so that it starts up as switching banks is done } }}
if (button_is_pressed()){ clear_output(T13_PORT, HOST_RST); // Pull Host Device Reset Pin LOW so that it resets while we switch banks.....if (button_is_released()){ set_output(T13_PORT, HOST_RST); // Pull Host Device Reset Pin HIGH so that it starts up as switching banks is done... }
#ifndef F_CPU #define F_CPU 9600000UL // Use 9.6MHz Internal Oscillator#endif