/******************************************************************** * Filename: 555CalcPi.c * * This program calculates the frequency, high time, low time, and * duty cycle for an astable oscillator on a 555 timer chip when * given the capacitance and resistance values. * * It also calculates the high time for a mono-stable "One Shot". * * ********************************************************************/ #include #include #define clrscr() printf("\e[1;1H\e[2J") // Clear screen. int onpin = 0; int offpin = 1; int button = 2; long mult = 1000000; // Multiplier for capacitance. /******************************************************************** * capmult() function - Determine multiplier for capacitor value. ********************************************************************/ void capmult() { int choice = 0; while(choice < 1 || choice > 3) // Choice must be between 1 and 4. { clrscr(); // Clear screen. printf("\n Calculator for 555 Timer\n\n"); printf("\n Determine multiplier for capacitance\n\n"); printf(" The formula needs the capacitance to be in Farads.\n"); printf(" How do you want to enter the capacitance?\n\n\n"); printf(" 1 - In nano Farads (nF)\n\n"); printf(" 2 - In micro Farads (uF)\n\n"); printf(" 3 - In Farads (F)\n\n"); printf("\n\n\n\n Enter Selection: "); scanf("%d", &choice); // Get selection. (choice) switch(choice) { case 1: mult = 1000000000; // Multiplier for nano Farads. break; case 2: mult = 1000000; // Multiplier for micro Farads. break; case 3: mult = 1; // Multiplier for Farads. break; } } } /******************************************************************** * astgpio() function - Run GPIO simulation of astable oscillator. ********************************************************************/ void astgpio(long ondelay, long offdelay) { long ontime; long offtime; int done = 0; if(ondelay > 30000) ondelay = 30000; // If light stays the same for over if(offdelay > 30000) offdelay = 30000; // thirty seconds it would be boring, // so change it after thirty seconds. while(digitalRead(button) == HIGH); // Wait for buttonpress while(digitalRead(button) == LOW); // Debounce delay(10); while(done == 0) { ontime = millis(); while((millis() - ontime) < ondelay) { digitalWrite(onpin, HIGH); digitalWrite(offpin, LOW); if(digitalRead(button) == LOW) // If button pressed end GPIO simulation. { while(digitalRead(button) == LOW); // Debounce done = 1; ontime = 0; delay(10); } } offtime = millis(); while(((millis() - offtime) < offdelay) && (done == 0)) { digitalWrite(offpin, HIGH); digitalWrite(onpin, LOW); if(digitalRead(button) == LOW) // If button pressed end GPIO simulation. { while(digitalRead(button) == LOW); // Debounce done = 1; offtime = 0; delay(10); } } } digitalWrite(onpin, LOW); digitalWrite(offpin, LOW); } /******************************************************************** * monogpio() function - Run GPIO simulation of mono-stable mode. ********************************************************************/ void monogpio(long ondelay) { long ontime; int done = 0; if(ondelay > 30000) ondelay = 30000; // If light stays the same for over // thirty seconds it would be boring, // so change it after thirty seconds. digitalWrite(onpin, LOW); digitalWrite(offpin, HIGH); while(done==0) { while(digitalRead(button) == HIGH); // Wait for buttonpress long down = millis(); while(digitalRead(button) == LOW); // Debounce { if((millis() - down) > 1000) // If button held down for one second { // force end of GPIO simulation. while(digitalRead(button) == LOW); // Debounce done = 1; ondelay = 0; } } delay(10); ontime = millis(); down = millis(); while((millis() - ontime) < ondelay) { digitalWrite(onpin, HIGH); digitalWrite(offpin, LOW); if(digitalRead(button) == LOW) // If button pressed end GPIO simulation { if((millis() - down) > 1000) // If button held down for one second { // force end of GPIO simulation. while(digitalRead(button) == LOW); // Debounce done = 1; ondelay = 0; } } } digitalWrite(onpin, LOW); digitalWrite(offpin, HIGH); } digitalWrite(onpin, LOW); digitalWrite(offpin, LOW); } /******************************************************************** * astable() function - The code for an astable oscillator. ********************************************************************/ void astable() { float r1 = 0; float r2 = 0; float c1 = 0; while(r1 == 0) { clrscr(); // Clear screen. printf("\n Calculator for 555 Timer\n"); printf(" in an Astable Oscillator\n\n"); printf("\n Enter R1: "); scanf("%f",&r1); } while(r2 == 0) { clrscr(); // Clear screen. printf("\n Calculator for 555 Timer\n"); printf(" in an Astable Oscillator\n\n"); printf("\n R1 = %7.0f Ohms\n",r1); printf("\n Enter R2: "); scanf("%f",&r2); } while(c1 ==0) { clrscr(); // Clear screen. printf("\n Calculator for 555 Timer\n"); printf(" in an Astable Oscillator\n\n"); printf("\n R1 = %7.0f Ohms\n",r1); printf("\n R2 = %7.0f Ohms\n",r2); if(mult==1) printf("\n Enter C1 in F: "); else if(mult==1000000) printf("\n Enter C1 in uF: "); else if(mult==1000000000) printf("\n Enter C1 in nF: "); scanf("%f",&c1); } clrscr(); // Clear screen. printf("\n Calculator for 555 Timer\n"); printf("\n in an Astable Oscillator\n\n"); printf("\n R1 = %7.0f Ohms\n",r1); printf("\n R2 = %7.0f Ohms\n",r2); if(mult==1) printf("\n C1 = %7.3f F\n", c1); else if(mult==1000000) printf("\n C1 = %7.3f uF\n", c1); else if(mult==1000000000) printf("\n C1 = %7.3f nF\n", c1); printf(" ----------------------------------------\n"); c1/=mult; // Peform calculations float freq = 1.44 / ((r1 + r2 + r2) * c1); float hi = 0.693 * (r1 + r2) * c1; float lo = 0.693 * r2 * c1; float duty = (hi / (hi + lo)) * 100; printf("\n Frequency = %7.5f\n",freq); // Display results printf("\n Time high = %7.5f\n",hi); printf("\n Time low = %7.5f\n",lo); printf("\n Duty Cycle = %3.1f Percent\n",duty); long lhi = hi*1000; // Convert high and low times if(lhi<1) lhi = 1; long llo = lo*1000; // from float to long milliseconds if(llo<1) llo = 1; astgpio(lhi, llo); // Pass high time and low time to GPIO simulation. } /******************************************************************** * mono() function - The code for a mono-stable "One Shot". ********************************************************************/ void mono() { float r1 = 0; float c1 = 0; while(r1 == 0) { clrscr(); // Clear screen. printf("\n Calculator for 555 Timer\n"); printf(" in Mono-Stable (One shot) mode\n\n"); printf("\n Enter R1: "); scanf("%f",&r1); } while(c1 == 0) { clrscr(); // Clear screen. printf("\n Calculator for 555 Timer\n"); printf(" in Mono-Stable (One shot) mode\n\n"); printf("\n R1 = %7.0f Ohms\n",r1); if(mult==1) printf("\n Enter C1 in F: "); else if(mult==1000000) printf("\n Enter C1 in uF: "); else if(mult==1000000000) printf("\n Enter C1 in nF: "); scanf("%f",&c1); } clrscr(); // Clear screen. printf("\n Calculator for 555 Timer\n"); printf(" in Mono-Stable (One shot) mode\n\n"); printf("\n R1 = %7.0f Ohms\n",r1); if(mult==1) printf("\n C1 = %7.3f F\n", c1); else if(mult==1000000) printf("\n C1 = %7.3f uF\n", c1); else if(mult==1000000000) printf("\n C1 = %7.3f nF\n", c1); printf(" ----------------------------------------\n"); c1/=mult; float hi = 1.1 * r1 * c1; printf("\n Time high = %7.5f\n",hi); long lhi = hi*1000; // Convert high times from float to long milleseconds if(lhi<1) lhi = 1; monogpio(lhi); // Pass high time to GPIO simulation. } /******************************************************************** * main() function ********************************************************************/ int main() { wiringPiSetup(); // Setup required by wiringPi pinMode(onpin, OUTPUT); // Set LED pins to output pinMode(offpin, OUTPUT); pinMode(button, INPUT); // Set button pin to input pullup pullUpDnControl(button, PUD_UP); digitalWrite(onpin, LOW); // Inital state of LEDs off digitalWrite(offpin, LOW); int choice; while(choice != 4) // Display main menu. { clrscr(); // Clear screen. printf("\n Calculator for 555 Timer\n\n"); printf("\n\n\n\n\n 1 - Astable Oscillator\n"); printf("\n 2 - Mono-stable\n"); printf("\n 3 - Change multiplier for C1 "); if(mult==1) printf("(F)\n"); else if(mult==1000000) printf("(uF)\n"); else if(mult==1000000000) printf("(nF)\n"); printf("\n 4 - EXIT\n"); printf("\n\n\n\n\n\n Enter Selection: "); scanf("%d", &choice); // Get selection. (choice) clrscr(); // Clear screen. switch(choice) { case 1: astable(); // Run code for astable oscillator. break; case 2: mono(); // Run code for mono-stable "One Shot". break; case 3: capmult(); // Get multiplier for capacitance. break; case 4: clrscr(); // Clear screen and exit. break; } } return 0; }