Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <EEPROM.h>
- #include <Wire.h>
- #include <Adafruit_SI5351.h>
- Adafruit_SI5351 clockgen = Adafruit_SI5351();
- // C16/PLUS4 Kernel Switcher and Clock Generator
- // version 1.1 -- 26-March-2019
- // By Adrian Black
- // SCL = A5 --> This might change on different ATMEL MCU
- // SDA = A4 --> This might change on different ATMEL MCU
- const int PowerLED = 4; // Output Power LED
- const int PowerLEDAlt = 13; // Output Power LED (onboard LED)
- const int A14 = 5; // Output EPROM Address Line 14
- const int ResetLine = 6; // Input Reset line key
- int ResetDelay = 2000; // 2000ms delay for Reset key (2 seconds)
- const int FlashSpeed = 100; // LED Flash delay
- const unsigned long repeatdelay = 500; // used for debouncing
- int CurrentROM; // which rom is select (0-3)
- int debouncecounter = 0; // how many times we have seen new value (for debounce)
- int debouncereading;
- int debounce_count;
- int ResetHeld;
- unsigned long TimeHeld; // amount of time Resotre is held down
- int buttonDuration = 0; // for keeping track of how long Reset is held down
- boolean buttonHeld = 0; // for keeping track when you are holding down
- boolean Released = 0; // Keeping track when the Reset key is released
- boolean holdingReset = 0; // Keeping track if you are holding Reset
- boolean resetSystem = 0; // keep track wheter to reset
- int buttonInput; // used to return if Reset is held
- unsigned long time; //used to keep track of millis output
- unsigned long htime; //used to keep track of millis output
- unsigned long btime; //used to keep track of bounce millis output
- void setup() {
- clockgen.begin();
- pinMode(PowerLED, OUTPUT);
- pinMode(PowerLEDAlt, OUTPUT);
- pinMode(A14, OUTPUT);
- pinMode(ResetLine, INPUT);
- digitalWrite(PowerLED, HIGH); // turn on the power LED
- digitalWrite(ResetLine, LOW); // keep the system reset
- pinMode(ResetLine, OUTPUT); // switch reset line to OUTPUT so it can hold it low
- digitalWrite(ResetLine, LOW); // keep the system reset
- CurrentROM = EEPROM.read(1);
- SetSlot(CurrentROM);
- delay(200);
- pinMode(ResetLine, INPUT); // set the reset pin back to high impedance which releases the INPUT line
- delay(1000); // wait 1000ms
- FlashLED(CurrentROM); // flash the power LED to show the current state
- // all set!
- }
- void loop() {
- buttonInput = readButton(); delay(500);
- time = millis(); // load the number of milliseconds the arduino has been running into variable time
- if (buttonInput == 1) {
- if (!buttonHeld) {
- htime = time; TimeHeld = 0; buttonHeld = 1; } //reset button is pushed and held
- else {
- TimeHeld = time - htime; } // button is being held down, keep track of total time held.
- }
- if (buttonInput == 0) {
- if (buttonHeld) {
- Released = 1; buttonHeld = 0; htime = millis(); TimeHeld = 0; //reset button not being held anymore
- }
- }
- if (TimeHeld > ResetDelay && !Released) { // do this when the time the button is held is longer than the delay
- htime = millis();
- if (CurrentROM == 0) { CurrentROM = 1; SaveSlot(CurrentROM); resetSystem = 1;} // or you've already been holding Reset, so increment the current ROM slot otherwise reset it to 0
- else { CurrentROM = 0; SaveSlot(CurrentROM); resetSystem = 1;}
- if (TimeHeld > ResetDelay) { TimeHeld = 0;} // reset the time held
- FlashLED(CurrentROM); //flash the LED
- }
- if (Released) {
- //if time held greater than Reset delay, reset the system, set the current rom slot, reselt the time held and holding Reset
- if (resetSystem) { // on do this if the reset system has been set above
- htime = millis();
- resetSystem = 0;
- holdingReset = 0;
- Released = 0;
- digitalWrite(ResetLine, LOW); // keep the system reset
- pinMode(ResetLine, OUTPUT);
- digitalWrite(ResetLine, LOW); // keep the system reset
- delay(1000); //
- SetSlot(CurrentROM); // select the appropriate kernal ROM and change the clock appropriately
- delay(200); // wait 200ms before releasing RESET line
- pinMode(ResetLine, INPUT); // set the reset pin back to high impedance so computer boots
- } else { //otherwise do nothing
- htime = millis(); Released = 0; resetSystem = 0; holdingReset = 0;
- }
- }
- // finished with loop
- }
- int readButton() {
- if (!digitalRead(ResetLine) && (millis() - btime >= repeatdelay)) {
- for(int i = 0; i < 10; i++)
- {
- debouncereading = !digitalRead(ResetLine);
- if(!debouncereading && debouncecounter > 0)
- {
- debouncecounter--;
- }
- if(debouncereading)
- {
- debouncecounter++;
- }
- // If the Input has shown the same value for long enough let's switch it
- if(debouncecounter >= debounce_count)
- {
- btime = millis();
- debouncecounter = 0;
- ResetHeld = 1;
- }
- delay (10); // wait 10ms
- }
- } else {
- ResetHeld = 0;
- }
- return ResetHeld;
- }
- void SaveSlot(int CurrentRomSlot) {
- // Save Current ROM selection (0-3) into EPROM
- EEPROM.write(1,CurrentRomSlot);
- }
- void FlashLED(int flashcount) {
- // Flash the LED to represent which ROM slot is selected
- switch (flashcount) {
- case 0:
- digitalWrite(PowerLED, LOW);
- digitalWrite(PowerLEDAlt, LOW);
- delay(FlashSpeed);
- digitalWrite(PowerLED, HIGH);
- digitalWrite(PowerLEDAlt, HIGH);
- break;
- case 1:
- digitalWrite(PowerLED, LOW);
- digitalWrite(PowerLEDAlt, LOW);
- delay(FlashSpeed);
- digitalWrite(PowerLED, HIGH);
- digitalWrite(PowerLEDAlt, HIGH);
- delay(FlashSpeed);
- digitalWrite(PowerLED, LOW);
- digitalWrite(PowerLEDAlt, LOW);
- delay(FlashSpeed);
- digitalWrite(PowerLED, HIGH);
- digitalWrite(PowerLEDAlt, HIGH);
- break;
- default:
- digitalWrite(PowerLED, LOW);
- digitalWrite(PowerLEDAlt, LOW);
- delay(FlashSpeed);
- digitalWrite(PowerLED, HIGH);
- digitalWrite(PowerLEDAlt, HIGH);
- break;
- }
- }
- void SetSlot(int DesiredRomSlot) {
- // Select the actual ROM slot being used
- switch (DesiredRomSlot) {
- case 0:
- digitalWrite(A14, LOW); //select lower half of EPROM
- clockgen.setupPLL(SI5351_PLL_A, 30, 579543, 625000); // setup the clock for NTSC
- clockgen.setupMultisynth(0, SI5351_PLL_A, 54, 0, 1);
- clockgen.enableOutputs(true);
- delay(50);
- break;
- case 1:
- digitalWrite(A14, HIGH); //select higher half of EPROM
- clockgen.setupPLL(SI5351_PLL_A, 26, 478201, 500000); //setup the clock for PAL
- clockgen.setupMultisynth(0, SI5351_PLL_A, 38, 0, 1);
- clockgen.enableOutputs(true);
- delay(50);
- break;
- default: // in case of EPROM error, default to NTSC
- digitalWrite(A14, LOW); //select lower half of EPROM
- clockgen.setupPLL(SI5351_PLL_A, 30, 579543, 625000); // setup the clock for NTSC
- clockgen.setupMultisynth(0, SI5351_PLL_A, 54, 0, 1);
- clockgen.enableOutputs(true);
- delay(50);
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement