void DisplayLCD(char string[], char string1[], char string2[]){ int number = 0; for(;;){ LCD_display(*string); string += 0x01; if(number == 16){ LCD_instruction(0xC0); } if( *string == 0 ){ break; } } for(;;){ LCD_display(*string); string1 += 0x01; if(number == 16){ LCD_instruction(0xC0); } if( *string1 == 0 ){ break; } } for(;;){ LCD_display(*string); string2 += 0x01; if(number == 16){ LCD_instruction(0xC0); } if( *string2 == 0 ){ break; } } }
void main( void ) { int i;//initialize loop counter char next;//store value of keypressed SPI1CR1=0x00; //Display initial value of speed and temperature into LCD Lcd2PP_Init(); LCD_instruction(0x01); Lcd2PP_Init(); printString(speed,temperature); //initialize KISR key = 0xFF;//refresh value of Key into 0xFF DDRP |= 0x0f; // bitset PP0-3 as outputs (rows) DDRH &= 0x0f; // bitclear PH4..7 as inputs (columns) PTP =0x0f; PIFH = 0xFF; // Clear previous interrupt flags PPSH = 0xF0; // Rising Edge PERH = 0x00; // Disable pulldowns PIEH |= 0xF0; // Local enable on columns inputs DDRM=0x00; asm("cli"); for (;;){ }//end for loop }//end main
void clearScreen(void){ LCD_instruction(0x00); DisplayLCD(" "," "," "); LCD_instruction(0xC0); DisplayLCD(" "," "," "); }
void RTI() { int temp;//temporary storage //4 tick approximately 1 second counter++;//do clock ticking SetOptCount++;//do clock ticking //obstacle when user press '5' if(obstacle==1) { RTIcounter++; //start count the RTI //Initialize 3 second delay if(RTIcounter==1) { duty=0; printf("DC motor off, Delay 3 second\n"); } //After 3 second Rotate the stepper motor to the right //(12 RTI counter= 3 second) if(RTIcounter==12) { printf("Rotate 90 degree right\n"); clockwise(1);//assume its rotate 90 degree right printf("wait 2 second\n"); } //After 5 second reset RTI counter and collision value to 0, then continue straight //(20 RTI counter = 5 second) if(RTIcounter==20)// { duty=20; printf("Continue straight\n"); PWMDTY7 = duty; RTIcounter=0;//reset RTI timer set to 0 obstacle=0;//reset collision to 0 // clear RTI flag } }//end obstacle //when heater is on, first LED from top is on and turn heater on if(heaterOn==1) { //heater on DDRM=0xff; PTM=0x81;//heater on delay(100);//delay 100ms allow heater to operate //LED on DDRK=0xff; PORTK=0x01; } else { //heater off DDRM=0xff; PTM=0x00;//heater off //first LED from top off DDRK=0xff; PORTK=0x00; } //for RTI, 4 tick is equal to 1 second if(SetOptCount==4) { optCount=0; } //refresh LCD every 3 second if(SetOptCount==12) { //refresh LCD, then print motor speed and temperature LCD_instruction(0x00); Lcd2PP_Init(); printString(optCount,temperature); SetOptCount=0;//reset RPS counter to 0 } //get temperature every 5 second if(counter==20) { //initialize interrupt for temperature sensor runTemperature(); //if temperature over 85, buzzer on, overheat LED on & turn off heater if(temperature>=85) { //turn on buzzer DDRK=0xff; PORTK=0x22; delay(100);//give time to buzzer to operate heaterOn=0; //turn off buzzer PORTK=0x00; DDRK=0x00; //heater off automatically DDRM=0xff; PTM=0x00; } counter=0;//reset counter for RTI } DDRM=0x00;//set DDRM on, to read from keypad CRGFLG=CRGFLG;//Acknowledge interrupt }
//print array of character //all character that will be printed into LCD will call this function void printString(int speedValue, int temp){ //All words below will be printed into LCD char *speed="Speed: "; char *temperature="Temp: "; char *kmperhour="RPS"; char *celsius="'F"; int i=0;//intitialize for loop //store digit of speed char stringSpeed[4]; //store digit of temperature char stringTemp[3]; //fill stringSpeed[4] with value of speed convertSpeedToChar(stringSpeed,speedValue); //fill stringTemp[2] with value of temperature convertTempToChar(stringTemp,temp); //Start display into LCD //Initialize LCD Lcd2PP_Init(); //print "Speed: ****RPS" for(i=0;i<7;i++) { LCD_display(speed[i]); } for(i=0;i<4;i++) { LCD_display(stringSpeed[i]); } for(i=0;i<3;i++) { LCD_display(kmperhour[i]); } //moves next line of LCD LCD_instruction(0xc0); //print "Temp: ***'F" for(i=0;i<6;i++) { LCD_display(temperature[i]); } for(i=0;i<3;i++) { LCD_display(stringTemp[i]); } for(i=0;i<2;i++) { LCD_display(celsius[i]); } }
//keypad interrupt void KISR(void) { PIEH = 0x00; // Local disable //get button pressed from line 1(key 1-A) PTM |= 0x08; //U7_EN enable PTP = 0x01; //PP0 writting PTM &= ~0x08; //U7_EN disable //option for 1 is pressed,port P is writting ,Port H reading the button pressed if(PTH==0x11) { printf("1\n"); PTM |= 0x08; //U7_EN enable PTP = 0x0f; //Write into all line(PP0-PP3) PTM &= ~0x08; //U7 disable } //option for 2 is pressed,port P is writting ,Port H reading the button pressed if(PTH==0x21) { printf("2\n"); PTM |= 0x08; //U7_EN enable PTP = 0x0f; //Write into all line(PP0-PP3) PTM &= ~0x08; //U7 disable } //option for 3 is pressed,port P is writting ,Port H reading the button pressed if(PTH==0x41) { //localDelay(); printf("3\n"); PTM |= 0x08; //U7_EN enable PTP = 0x0f; //Write into all line(PP0-PP3) PTM &= ~0x08; //U7 disable } //option for A is pressed,port P is writting ,Port H reading the button pressed if(PTH==0x81) { //localDelay(); printf("A\n"); PTM |= 0x08; //U7_EN enable PTP = 0x0f; //Write into all line(PP0-PP3) PTM &= ~0x08; //U7 disable } //get button pressed from line 2(key 4-B) PTM |= 0x08; //U7_EN enable PTP = 0x02; //PP1 writting PTM &= ~0x08; //U7_EN disable //option for 4 is pressed,port P is writting ,Port H reading the button pressed if(PTH==0x12) { printf("4\n"); PTM |= 0x08; //U7_EN enable PTP = 0x0f; //Write into all line(PP0-PP3) PTM &= ~0x08; //U7 disable } //option for 5 is pressed,port P is writting ,Port H reading the button pressed if(PTH==0x22) { printf("5\n"); PTM |= 0x08; //U7_EN enable PTP = 0x0f; //Write into all line(PP0-PP3) PTM &= ~0x08; //U7 disable } //option for 6 is pressed,port P is writting ,Port H reading the button pressed if(PTH==0x42) { printf("6\n"); PTM |= 0x08; //U7_EN enable PTP = 0x0f; //Write into all line(PP0-PP3) PTM &= ~0x08; //U7 disable } //option for B is pressed,port P is writting ,Port H reading the button pressed if(PTH==0x82) { printf("B\n"); PTM |= 0x08; //U7_EN enable PTP = 0x0f; //Write into all line(PP0-PP3) PTM &= ~0x08; //U7 disable } //get button pressed from line 3(key 7-C) PTM |= 0x08; //U7_EN enable PTP = 0x04; //PP2 writting PTM &= ~0x08; //U7_EN disable //option for 7 is pressed,port P is writting ,Port H reading the button pressed if(PTH==0x14) { printf("7\n"); PTM |= 0x08; //U7_EN enable PTP = 0x0f; //Write into all line(PP0-PP3) PTM &= ~0x08; //U7 disable } //option for 8 is pressed,port P is writting ,Port H reading the button pressed if(PTH==0x24) { printf("8\n"); PTM |= 0x08; //U7_EN enable PTP = 0x0f; //Write into all line(PP0-PP3) PTM &= ~0x08; //U7 disable } //option for 9 is pressed,port P is writting ,Port H reading the button pressed if(PTH==0x44) { printf("9\n"); PTM |= 0x08; //U7_EN enable PTP = 0x0f; //Write into all line(PP0-PP3) PTM &= ~0x08; //U7 disable } //option for C is pressed,port P is writting ,Port H reading the button pressed if(PTH==0x84) { printf("C\n"); PTM |= 0x08; //U7_EN enable PTP = 0x0f; //Write into all line(PP0-PP3) PTM &= ~0x08; //U7 disable } //get button pressed from line 4(key E-D) PTM |= 0x08; //U7_EN enable PTP = 0x08; //PP3 writting PTM &= ~0x08; //U7_EN disable //option for C is pressed,port P is writting ,Port H reading the button pressed if(PTH==0x18) { printf("E\n"); speed = speed+1;//increase speed everytime E is pressed //print new value of speed into LCD Lcd2PP_Init(); LCD_instruction(0x01); printString(speed,temperature); //reinitialize port M for U7_EN DDRM=0x00; } //option for 0 is pressed,port P is writting ,Port H reading the button pressed if(PTH==0x28) { printf("0\n"); PTM |= 0x08; //U7_EN enable PTP = 0x0f; //Write into all line(PP0-PP3) PTM &= ~0x08; //U7 disable //exit printf("Exit\n"); while(1) { PTP=0x00;//disable keypad } } //option for F is pressed,port P is writting ,Port H reading the button pressed if(PTH==0x48) { printf("F\n"); PTM |= 0x08; //U7_EN enable PTP = 0x0f; //Write into all line(PP0-PP3) PTM &= ~0x08; //U7 disable } //option for D is pressed,port P is writting ,Port H reading the button pressed if(PTH==0x88) { printf("D\n"); PTM |= 0x08; //U7_EN enable PTP = 0x0f; //Write into all line(PP0-PP3) PTM &= ~0x08; //U7 disable if(speed==0) { printf("Minimum speed limit is 0"); } else { speed=speed-1;//decrease speed //print new value of speed into LCD Lcd2PP_Init(); LCD_instruction(0x01); printString(speed,temperature); //reinitialize DDRM for keypad DDRM=0x00; } } //reinitialize interrupt for KISR DDRP |= 0x0f; // bitset PP0-3 as outputs (rows) DDRH &= 0x0f; // bitclear PH4..7 as inputs (columns) PTP =0x0f;//write into all lines PP0-PP3 PIFH = 0xFF; // Clear previous interrupt flags PPSH = 0xF0; // Rising Edge PERH = 0x00; // Disable pulldowns PIEH |= 0xF0; // Local enable on columns inputs }//end KISR