/*Algorithm: This code display a centrifuge with five variable speed RPM by increaseing */ /*the speed the cell which are less dense can float and the cell that are denser can sink*/ /*the user has five option of speed 1)100 RPM 2)200 RPM 3)300 RPM 4)500RPM 5)above 500RPM error */ /*In progress:temprature shouldn't exceed 50C if temprature exceeds it will stop it will be measured with a termostat */ /*In Progress: There is an on and off button which will allow the user to turn on and off the centrifuge*/ /*In Progress: There is an auto turn off like power saving mechanism if the user didn't use it for a certain period of time */ #include <stdlib.h> //standard include files #include <pic.h>//library files for microcontroller #include <htc.h>//detrmine which complier should be run #include<time.h>//add a time function library CONFIG(DUNPROT & PWRTDIS & XT & WDTDIS & BORDIS & LVPDIS); #ifndef_rcal_freq // Unless already defined assume 4MHz system frequency // Used to calibrate delay_us() and delay_ms() #define_rcal_freq 7372800 #endif #define PRESS_BUTTON RE2//Sets PRESS_BUTTON to the value of pin RE2 to turn on the Centrifuge #define PRESS_bUTTON RE1//Sets PRESS_BUTTON to the value of pin RE1 to leave the Centrifuge at auto control
#define TEMPERATURE RB1 //Sets TEMPERATURE to the value of RB1 to display the temprature #define BUTTON1 RB2 //Sets the BUTTON1 to the value of RB2 to set the speed #define BUTTON2 RB3 //Sets the BUTTON2 to the value of RB3 to set the speed #define BUTTON3 RB4 //Sets the BUTTON3 to the value of RB4 to set the speed #define BUTTON4 RB5 //Sets the BUTTON4 to the value of RB5 to set the speed #define BUTTON5 RB6 //Sets the BUTTON5 to the value of RB6 to set the speed void InitLCD(void); void DisplayC(unsigned char position, const char *str); void clear_display(void); int ChangeState, DispMode, resetlcd, advalue; //Declare the variablea //;;;;;;; LoopTime subroutine ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; //; //; This subroutine waits for Timer2 to complete its ten millisecond count //; sequence. void LoopTime(void) for (;;)
if ( TMR2IF ) // Check whether ten milliseconds are up TMR2IF=0; // Clear flag //;;;;;;; Initial subroutine ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; //; //; This subroutine performs all initializations of variables and registers. //; It is important to refer to the PIC datasheet to understand what is happening. void Initial(void) ADCON1 = 0b00000100; // Setup A/D converter and enable PORTE as digital I/O ADCS0 = 1; // Use A/D FOSC/8 TRISA = 0b00001011; // Set I/O for PORTA TRISB = 0b11100001; // Set I/O for PORTB TRISC = 0b10110111; // Set I/O for PORTC TRISD = 0b00000000; // Set I/O for PORTD TRISE = 0b00000100; // Set I/O for PORTE PR2 = 95; // Set up Timer2 for a looptime of 10 ms PORTD = 0; // Turn off LEDs T2CON = 0b01011111; // Finish set up of Timer2 DispMode = 0;
ChangeState = 0; resetlcd = 1; OPTION = 0b00000100;// Timer 0 prescale set 32:1 InitLCD(); // normally call just before setting up INTCON INTCON = 0b10000000; // Enable interrupts //;;;;;;; Mainline program ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; void LCD() //This function uses the DisplayC function to write text to the LCD screen if(resetlcd) resetlcd = 0; clear_display(); //This function clears the LCD display //so it can accomodate new characters if(!dispmode) (0x80) DisplayC(0x80, "BLINK"); //This function writes "BLINK" starting at the top left of the screen DisplayC(0xC0, "PRESS ENTER"); //This writes "PRESS ENTER" starting at the bottom left of the screen (0xC0) PORTD = 0b11111111; //This line resets the LEDs for Blink
else if(dispmode==1) DisplayC(0x80, "A/D CONVERT"); DisplayC(0xC0, "TURN RP1"); void Blink() //This function blinks the 8 LEDs at half second intervals int i; PORTD ^= 0b11111111; //Toggles LEDs of PORTD on/off for(i=50;i>0;i--) //For loop calls the 10 ms LoopTime routine 50 times //this takes 500 ms to complete LoopTime(); void ShowAD() //This function displays the result of the A/D converter //by incrementing LEDs as the A/D number increases if(advalue<128) //It checks the value against a series of IF statements //chooses the appropriate level and sets the correct PORTD = 0b00000001; //number of pins high else if(advalue<128*2)
PORTD = 0b00000011; else if(advalue<128*3) PORTD = 0b00000111; else if(advalue<128*4) PORTD = 0b00001111; else if(advalue<128*5) PORTD = 0b00011111; else if(advalue<128*6) PORTD = 0b00111111; else if(advalue<128*7) PORTD = 0b01111111; else
PORTD = 0b11111111; int BUTTON1() //Choose BUTTON1 for the first speed ChangeState = 1 //ChangeStates remember that the button was pressed scanf("%d", &BUTTON1); switch case BUTTON1: DisplayC(0x80"RUNNING:SPEED 100RPM");//Displays "RUNNING SPEED 100RPM in top left corner for(i=100,i,i--) // runs the centrifuge for 100 loop LoopTime(); clear_display(); //clears the display once complete DisplayC(0x80, "Completed!!"); //inform the user the cycle is completed
if(changstate == 1) // resets the ChangeState for next Button to be Pressed ChangeState = 0; resetlcd = 1; if(dispmode == 1) //If the final state is currently in use the counter is reset to 0 DispMode = 0; else DispMode = DispMode++; break; int BUTTON2() //Choose BUTTON2 for the first speed ChangeState = 1 //ChangeStates remember that the button was pressed scanf("%d", &BUTTON2);
switch case BUTTON2: DisplayC(0x80"RUNNING:SPEED 200RPM");//Displays "RUNNING SPEED 200RPM in top left corner for(i=200,i,i--) // runs the centrifuge for 200 loop LoopTime(); clear_display(); //clears the display once complete DisplayC(0x80, "Completed!!"); //inform the user the cycle is completed if(changstate == 1) // resets the ChangeState for next Button to be Pressed ChangeState = 0; resetlcd = 1; if(dispmode == 1) //If the final state is currently in use the counter is reset to 0 DispMode = 0; else
DispMode = DispMode++; break; int BUTTON3() //Choose BUTTON3 for the first speed ChangeState = 1 //ChangeStates remember that the button was pressed scanf("%d", &BUTTON3); switch case BUTTON3: DisplayC(0x80"RUNNING:SPEED 300RPM");//Displays "RUNNING SPEED 300RPM" in top left corner for(i=300,i,i--) // runs the centrifuge for 300 loop LoopTime(); clear_display(); //clears the display once complete DisplayC(0x80, "Completed!!"); //inform the user the cycle is completed
if(changstate == 1) // resets the ChangeState for next Button to be Pressed ChangeState = 0; resetlcd = 1; if(dispmode == 1) //If the final state is currently in use the counter is reset to 0 DispMode = 0; else DispMode = DispMode++; break; int BUTTON4() //Choose BUTTON4 for the first speed ChangeState = 1 //ChangeStates remember that the button was pressed
scanf("%d", &BUTTON4); switch case BUTTON4: DisplayC(0x80"RUNNING:SPEED 500RPM");//Displays "RUNNING SPEED 500RPM" in top left corner for(i=500,i,i--) // runs the centrifuge for 500 loop LoopTime(); clear_display(); //clears the display once complete DisplayC(0x80, "COMPLETED!!"); //display "COMPLETED" and inform the user the cycle is completed if(changstate == 1) // resets the ChangeState for next Button to be Pressed ChangeState = 0; resetlcd = 1; if(dispmode == 1) //If the final state is currently in use the counter is reset to 0 DispMode = 0;
else DispMode = DispMode++; break; default: corner DisplayC(0X80"NUMBER EXCEEDS ERROR")//Displays"NUMBER EXCEEDS ERROR" in top left break; int main(void) //This is the main body of the program //All functions of the program start here and are called either by the endless FOR loop or a subsidiary. for(;;) Initial(); //This function initializes necessary registers TEMPURATURE(); //Calls a function to check the TEMPURATURE BUTTON1(); //Calls a function of BUTTON1
BUTTON2(); //Calls a function of BUTTON2 BUTTON3(); //Calls a function of BUTTON3 BUTTON4(); //Calls a function of BUTTON4 BUTTON5(); //Calls a function of BUTTON5 if(!dispmode) //If the Enter Button was in State 0 the functions //LCD and Blink will be called LCD(); Blink(); else //If the Enter Button was not in State 0 the LCD //ADConvert and ShowAD functions will be called LCD(); ADConvert(); ShowAD();