int main() { // unsigned char onechar = 'a'; PLL_Init(); PortA_Init(); PortC_Init(); SysTick_Init(); Nokia5110_Init(); // Зеленый светодиод выкл, PC9. Синий сетодиод вкл, PC8. Положительная логика GPIOC_BSRR = 0x2000100; Nokia5110_Clear(); while(1){ if((GPIOA_IDR & 0x1)){ // Проверяем нажатие кнопки, PA0. Положительная логика GPIOC_BSRR = 0x1000200; // Синий LED off. Зеленый LED on SysTick_Wait10ms(20); // задержка 200 мс GPIOC_BSRR = 0x3000000; // Синий LED off. Зеленый LED off SysTick_Wait10ms(20); // задержка 200 мс Nokia5110_OutString("Hello,World!"); } else GPIOC_BSRR = 0x2000100; // Синий светодиод вкл. Зеленый сетодиод выкл } }
void GPIOPortF_Handler(void) { unsigned int i; // SW1(PF4) -> Green if(GPIO_PORTF_RIS_R & 0x10) { GPIO_PORTF_ICR_R |= 0x10; for(i = 0; i < 3; i++) { GPIO_PF3_Data |= 0x08; // Delay 300ms SysTick_Wait10ms(30); GPIO_PF3_Data &= ~0x08; // Delay 300ms SysTick_Wait10ms(30); } } // SW2(PF0) -> Red if(GPIO_PORTF_RIS_R & 0x01) { GPIO_PORTF_ICR_R |= 0x01; for(i = 0; i < 3; i++) { GPIO_PF1_Data |= 0x02; // Delay 300ms SysTick_Wait10ms(30); GPIO_PF1_Data &= ~0x02; // Delay 300ms SysTick_Wait10ms(30); } } }
void followWall(void){ // includes avoiding other robots ////get sensor values frontSensor = ADCRead(adc[0])*1000; rightSensor = ADCRead(adc[1])*1000; leftSensor = ADCRead(adc[2])*1000; if (rightSensor>300 && rightSensor>leftSensor) { ///follow whichever wall is closer //////if right wall is closer if(frontSensor > 900){ ////back up and turn if wall ahead SetMotor(leftMotor, -1); SetMotor(rightMotor, -1); SysTick_Wait10ms(70); //reverse for 3 seconds turn90Degrees(LEFT); wasRightWall = true; wasLeftWall = false; } else if(rightSensor > 650){ SetMotor(leftMotor, -.25); SetMotor(rightMotor, .8); } else{ SetMotor(leftMotor, 1); SetMotor(rightMotor, .25); } } /////////if left wall is closer else if(leftSensor>300 && leftSensor>rightSensor){ if(frontSensor > 900){ ////back up and turn if wall ahead SetMotor(leftMotor, -1); SetMotor(rightMotor, -1); SysTick_Wait10ms(70); //reverse turn90Degrees(RIGHT); wasLeftWall = true; wasRightWall = false; } else if(leftSensor > 650){ SetMotor(leftMotor, .8); SetMotor(rightMotor, -.25); } else{ SetMotor(leftMotor, .25); SetMotor(rightMotor, 1); } } else { if (wasLeftWall) { SetMotor(leftMotor, 1); SetMotor(rightMotor, .7); } else { SetMotor(leftMotor, .7); SetMotor(rightMotor, 1); } } }
void squareDance(void){ int turns = 0; bool postTurn = true; bool clockWise; float sensor; rightSensor = ADCRead(adc[1])*1000; leftSensor = ADCRead(adc[2])*1000; if (rightSensor>leftSensor){ clockWise = true; }else{ clockWise = false; } while(turns < 4) { if (clockWise) {sensor = ADCRead(adc[1])*1000;}else{sensor = ADCRead(adc[2])*1000;} if (postTurn){ //////after we make a turn SetMotor(leftMotor, 1); SetMotor(rightMotor, 1); SysTick_Wait10ms(250); // go straight for a short time to get back by object do { if (clockWise) {sensor = ADCRead(adc[1])*1000;}else{sensor = ADCRead(adc[2])*1000;} }while(sensor > 400); postTurn = false; } else { SetMotor(leftMotor, 1); SetMotor(rightMotor, 1); /////keep going straight while no wall is close do { if (clockWise) {sensor = ADCRead(adc[1])*1000;}else{sensor = ADCRead(adc[2])*1000;} }while(sensor < 400); /////now that we have encountered a wall /////keep going straight until we have passed the wall so we can make turn do { if (clockWise) {sensor = ADCRead(adc[1])*1000;}else{sensor = ADCRead(adc[2])*1000;} }while(sensor > 400); SysTick_Wait10ms(70); /////stop briefly SetMotor(leftMotor, 0); SetMotor(rightMotor, 0); SysTick_Wait10ms(40); /////turn corner if (clockWise) {turn90Degrees(RIGHT);} else { turn90Degrees(LEFT);} turns += 1; postTurn = true; } } //////once we have made 4 turns we will stop SetMotor(leftMotor, 0); SetMotor(rightMotor, 0); while(true); }
void turn90Degrees(int dir){ if (dir == LEFT) { SetMotor(leftMotor, -1); SetMotor(rightMotor, 1); SysTick_Wait10ms(72); } if (dir == RIGHT){ SetMotor(leftMotor, 1); SetMotor(rightMotor, -1); SysTick_Wait10ms(72); } stage = 1; }
int main(void){ uint8_t n; // state number uint32_t Input; PLL_Init(Bus80MHz); // initialize 50 MHz system clock SysTick_Init(); // initialize SysTick timer SYSCTL_RCGCGPIO_R |= 0x12; // activate clock for Port E and Port B // allow time for clock to stabilize while((SYSCTL_PRGPIO_R&0x12) == 0){}; GPIO_PORTB_DIR_R |= 0x3F; // make PB5-0 out GPIO_PORTB_AFSEL_R &= ~0x3F; // disable alt funct on PB5-0 GPIO_PORTB_DEN_R |= 0x3F; // enable digital I/O on PB5-0 // configure PB5-0 as GPIO GPIO_PORTB_PCTL_R = (GPIO_PORTB_PCTL_R&0xFF000000)+0x00000000; GPIO_PORTB_AMSEL_R &= ~0x3F; // disable analog functionality on PB5-0 GPIO_PORTE_DIR_R &= ~0x03; // make PE1-0 in GPIO_PORTE_AFSEL_R &= ~0x03; // disable alt funct on PE1-0 GPIO_PORTE_DEN_R |= 0x03; // enable digital I/O on PE1-0 // configure PE1-0 as GPIO GPIO_PORTE_PCTL_R = (GPIO_PORTE_PCTL_R&0xFFFFFF00)+0x00000000; GPIO_PORTE_AMSEL_R &= ~0x03; // disable analog functionality on PE1-0 n = goN; // initial state: Green north; Red east while(1){ LIGHT = FSM[n].Out; // set lights to current state's Out value SysTick_Wait10ms(FSM[n].Time); // wait 10 ms * current state's Time value Input = SENSOR; // get new input from car detectors n = FSM[n].Next[Input]; // transition to next state } }
{ GR, S, 50, { S_ge, S_ge, S_ye, S_ye, S_ye, S_ye, S_ye, S_ye } }, //S_ge { YR, S, 10, { S_gn, S_ge, S_gn, S_gn, S_wa, S_wa, S_gn, S_gn } }, //S_ye - Moves to gn { RR, W, 50, { S_h0, S_h0, S_h0, S_h0, S_wa, S_h0, S_h0, S_h0 } }, //Walk { RR, S, 02, { S_h1, S_h1, S_h1, S_h1, S_h1, S_h1, S_h1, S_h1 } }, //Hurry0 - can only go to h1 { RR, 0, 02, { S_h2, S_h2, S_h2, S_h2, S_h2, S_h2, S_h2, S_h2 } }, //Hurry1 - can only go to h2 { RR, S, 02, { S_h3, S_h3, S_h3, S_h3, S_h3, S_h3, S_h3, S_h3 } }, //Hurry2 - can only go to h3 { RR, 0, 10, { S_ge, S_ge, S_gn, S_ge, S_wa, S_ge, S_gn, S_ge } }, //Hurry3
int main(void) {volatile unsigned long delay; SysTick_Init(); SYSCTL_RCGC2_R |= 0x32; // 1) B E delay = SYSCTL_RCGC2_R; // 2) no need to unlock GPIO_PORTE_AMSEL_R &= ~0x07; // 3) disable analog function on PE1-0 GPIO_PORTE_PCTL_R &= ~0x00000FFF; // 4) enable regular GPIO GPIO_PORTE_DIR_R &= ~0x07; // 5) inputs on PE1-0 GPIO_PORTE_AFSEL_R &= ~0x07; // 6) regular function on PE1-0 GPIO_PORTE_DEN_R |= 0x07; // 7) enable digital on PE1-0 GPIO_PORTB_AMSEL_R &= ~0x3F; // 3) disable analog function on PB5-0 GPIO_PORTB_PCTL_R &= ~0x00FFFFFF; // 4) enable regular GPIO GPIO_PORTB_DIR_R |= 0x3F; // 5) outputs on PB5-0 GPIO_PORTB_AFSEL_R &= ~0x3F; // 6) regular function on PB5-0 GPIO_PORTB_DEN_R |= 0x3F; // 7) enable digital on PB5-0 GPIO_PORTF_LOCK_R = 0x4C4F434B; // 2) unlock GPIO Port F GPIO_PORTF_CR_R = 0x1F; // allow changes to PF4-0 GPIO_PORTF_AMSEL_R &= ~0x0A; GPIO_PORTF_PCTL_R &= ~0x000FFF0; GPIO_PORTF_DIR_R |= 0x0A; GPIO_PORTF_AFSEL_R &= ~0x0A; GPIO_PORTF_DEN_R |= 0x0A; S = goN; while(1){ LIGHT = FSM[S].Out; // set lights PED_LIGHT = FSM[S].Out2; SysTick_Wait10ms(FSM[S].Time); Input = SENSOR; // read sensors S = FSM[S].Next[Input]; } }
//------------Fixed_uBinOut8_test------------ // Test case for Fixed_sDecOut3 function. Unigned 32-bit binary fixed-point with a resolution of 1/256. void Fixed_uBinOut8_test(void){ Output_Clear(); ST7735_SetCursor(0,0); printf("Fixed_uBinOut8_test\n"); for(int i = 0; i < ARRAY_SIZE; i++){ Fixed_uBinOut8(BinCases[i]); SysTick_Wait10ms(10); } printf("Press SW2 to cont."); }
// 3. Subroutines Section // MAIN: Mandatory for a C Program to be executable int main(void){ PLL_Init(); // 80 MHz // initialize output and interrupts EnableInterrupts(); PortB_Init();/*Initialize ports and timers*/ SysTick_Init(); while(1){ /*Your code goes here*/ GPIO_PORTB_DATA_R=0X05; SysTick_Wait10ms(1); GPIO_PORTB_DATA_R=0X06; SysTick_Wait10ms(1); GPIO_PORTB_DATA_R=0X0A; SysTick_Wait10ms(1); GPIO_PORTB_DATA_R=0X09; SysTick_Wait10ms(1); } }
int main(void){ TExaS_Init(SW_PIN_PE210, LED_PIN_PB543210,ScopeOff); // activate grader and set system clock to 80 MHz EnableInterrupts(); Port_Init(); SysTick_Init(); S = GoWest; while(1){ GPIO_CarLED = FSM[S].CarLED; // set Car LED GPIO_WalkLED = FSM[S].WalkLED; // set Walk LED SysTick_Wait10ms(FSM[S].Time); Input = GPIO_Sensor; // read sensors S = FSM[S].Next[Input]; } }
int main() { RCGC2::GPIOF::write(1); // turn on GPIOF GPIOHBCTL::GPIOF::write(0x1); // turn on AHB access to GPIOF GPIOF::DEN::DEN::write(BIT1 | BIT2 | BIT3); // digital mode bits 1,2,3 of GPIOF GPIOF::DIR::DIR::write(BIT1 | BIT2 | BIT3); // make bits 1,2,3 outputs on GPIOF PLL_Init(); SysTick_Init(); lights = BIT1; while (1) { GPIOF::DATA::DATA::write(lights); SysTick_Wait10ms(1000); GPIOF::DATA::DATA::write(0x0); SysTick_Wait10ms(1000); // Toggle Led color lights <<= 1; if (lights > BIT3) lights = BIT1; } return 0; }
void Xbee_Init(unsigned char ChannelNum){ unsigned char nextStep = 0; SysTick_Init(); // printf("Initializing...%c",NEWLINE); while(nextStep == 0){ UART_OutChar('x'); SysTick_Wait10ms(110);//wait 1.1ms UART_OutChar('+'); UART_OutChar('+'); UART_OutChar('+'); SysTick_Wait10ms(110);//wait 1.1ms nextStep = lookforCR(); } // printf("okay1%c",NEWLINE); nextStep = 0; while(nextStep == 0){ UART_OutString(ATCMD1); SysTick_Wait10ms(2); nextStep = lookforCR(); } // printf("okay2%c",NEWLINE); nextStep = 0; while(nextStep == 0){ UART_OutString(ATCMD2); SysTick_Wait10ms(2); nextStep = lookforCR(); } // printf("okay3%c",NEWLINE); nextStep = 0; while(nextStep == 0){ UART_OutString(ATCMD3); SysTick_Wait10ms(2); nextStep = lookforCR(); } // printf("okay4%c",NEWLINE); nextStep = 0; while(nextStep == 0){ UART_OutString(ATCMD4); SysTick_Wait10ms(2); nextStep = lookforCR(); } //printf("okay5%c",NEWLINE); nextStep = 0; while(nextStep == 0){ UART_OutString(ATCMD5); SysTick_Wait10ms(2); nextStep = lookforCR(); } // printf("okay6%c",NEWLINE); }
int main(void){ unsigned char input; PLL_Init(); // Program 10.1 SysTick_Init(); // Program 10.2 PortB_Init(); // initialize motor outputs on Port B PortE_Init(); // initialize sensor inputs on Port E cState = 0; // initial state = 0 while(1){ // output based on current state GPIO_PORTB_DATA_R = Fsm[cState].out; // step motor // wait for time according to state SysTick_Wait10ms(Fsm[cState].wait); // get input input = GPIO_PORTE_DATA_R&0x03; // Input 0,1,2,3 // change the state based on input and current state cState = Fsm[cState].next[input]; } }
int main(void){ TExaS_Init(SW_PIN_PE210, LED_PIN_PB543210,ScopeOff); // activate grader and set system clock to 80 MHz ports_Init(); SysTick_Init(); EnableInterrupts(); S = goW; while(1){ TRAFLIGHT = FSM[S].trafOut; PEDLIGHT = FSM[S].pedOut; SysTick_Wait10ms(FSM[S].delay); Input = SENSORS; S = FSM[S].next[Input]; } }
int main(void){ TExaS_Init(SW_PIN_PE210, LED_PIN_PB543210); // activate grader and set system clock to 80 MHz SYSCTL_RCGC2_R |= 0x32; // 1) B E F initB(); initF(); initE(); SysTick_Init(); // Program 10.2 EnableInterrupts(); CurrentState = 2; while(1){ CAR_LIGHT = FSM[CurrentState].CarOut; // set car lights PEDES_LIGHT = FSM[CurrentState].WalkOut; // set pedestrian lights SysTick_Wait10ms(FSM[CurrentState].Time); Input = SENSOR; // read sensors CurrentState = FSM[CurrentState].Next[Input]; } }
// ************* CheckContact ************* // checks if any of the four corners of link are inside the boss // when one of the four corners are hit by the boss, link will then // be knocked back and lose health void CheckContact(void){ char hit = 0; // hit from lower right if(CheckInBoss(Link.Xpos, Link.Ypos)){ hit = 1; RetreatLeft(); RetreatUp(); } // hit from lower left else if(CheckInBoss(Link.Xpos + Link.Width - 1, Link.Ypos)){ hit = 1; RetreatRight(); RetreatUp(); } // hit from upper right else if(CheckInBoss(Link.Xpos, Link.Ypos + Link.Height - 1)){ hit = 1; RetreatLeft(); RetreatDown(); } // hit from upper left else if(CheckInBoss(Link.Xpos + Link.Width - 1, Link.Ypos + Link.Height - 1)){ hit = 1; RetreatRight(); RetreatDown(); } if (hit){ linkHealth -=1; // redraws link after he retreats LCD12864ImageDraw(&display[0],&room[0],Link.Graphic,Link.Xpos,Link.Ypos,Link.Width,Link.Height); LCD12864PartUpdate(&display[0],Link.Xpos,Link.Ypos,Link.Width,Link.Height); SysTick_Wait10ms(1); } // user died :( if (linkHealth == 0){ gameEnd = LOST; } }
int main(void){ volatile unsigned long delay; STyp *Pt; // state pointer unsigned long Input; PLL_Init(); // configure for 50 MHz clock SysTick_Init(); // initialize SysTick timer // activate port A SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOA; delay = SYSCTL_RCGC2_R; // allow time to finish activating GPIO_PORTA_DIR_R &= ~0x01; // make PA0 in GPIO_PORTA_DIR_R |= 0x06; // make PA2-1 out GPIO_PORTA_AFSEL_R &= ~0x07; // disable alt func on PA2-0 GPIO_PORTA_DEN_R |= 0x07; // enable digital I/O on PA2-0 // configure PA2-0 as GPIO GPIO_PORTA_PCTL_R = (GPIO_PORTA_PCTL_R&0xFFFFF000)+0x00000000; GPIO_PORTA_AMSEL_R = 0; // disable analog functionality on PA Pt = Stop; // initial state: stopped while(1){ Input = INPUT; // get new input from Control OUTPUT = Pt->Out[Input]<<1;// output to Brake and Gas SysTick_Wait10ms(Pt->Delay);// wait 10 ms * current state's Delay value Pt = Pt->Next[Input]; // transition to next state } }
int main(void){ volatile unsigned long delay; PLL_Init(); // 80 MHz, Program 10.1 SysTick_Init(); // Program 10.2 SYSCTL_RCGC2_R |= 0x12; // 1) B E delay = SYSCTL_RCGC2_R; // 2) no need to unlock GPIO_PORTE_AMSEL_R &= ~0x03; // 3) disable analog function on PE1-0 GPIO_PORTE_PCTL_R &= ~0x000000FF; // 4) enable regular GPIO GPIO_PORTE_DIR_R &= ~0x03; // 5) inputs on PE1-0 GPIO_PORTE_AFSEL_R &= ~0x03; // 6) regular function on PE1-0 GPIO_PORTE_DEN_R |= 0x03; // 7) enable digital on PE1-0 GPIO_PORTB_AMSEL_R &= ~0x3F; // 3) disable analog function on PB5-0 GPIO_PORTB_PCTL_R &= ~0x00FFFFFF; // 4) enable regular GPIO GPIO_PORTB_DIR_R |= 0x3F; // 5) outputs on PB5-0 GPIO_PORTB_AFSEL_R &= ~0x3F; // 6) regular function on PB5-0 GPIO_PORTB_DEN_R |= 0x3F; // 7) enable digital on PB5-0 S = goN; while(1){ LIGHT = FSM[S].Out; // set lights SysTick_Wait10ms(FSM[S].Time); Input = SENSOR; // read sensors S = FSM[S].Next[Input]; } }
int main(void){ PLL_Init(); SysTick_Init(); UART4_Init(); Edge_Detect_Init(); Timer_Capture_Init(); UART_OutUDec(1,4); while(1) { SysTick_Wait10ms(100); if(((1<<TIMER0_RIS_R)&(0x1))) { TIMER0_ICR_R|=(1<<1); UART_OutUDec(TIMER0_TAR_R,4); New_Line(4); } } }
// attacks with Link's sword void PressA(void){ if(Link.Direction == UP){ // make sure sword can fit within top side of LCD screen if(Link.Ypos <= (LCD_HEIGHT-Link.Height-SWORD_LENGTH-PIXEL_BUFFER)){ Sword.Width = SWORD_BREADTH; Sword.Height = SWORD_LENGTH; Sword.Direction = UP; Sword.Ypos = Link.Ypos + (Link.Height-PIXEL_BUFFER); Sword.Xpos = Link.Xpos + (OFFSET_UP_SWORD); Sword.Graphic = &swordUp[0]; } else{ return; } } else if(Link.Direction == DOWN){ // make sure sword can fit within bottom side of LCD screen if(Link.Ypos >= SWORD_LENGTH){ Sword.Width = SWORD_BREADTH; Sword.Height = SWORD_LENGTH; Sword.Direction = DOWN; Sword.Ypos = Link.Ypos - (Sword.Height - PIXEL_BUFFER); Sword.Xpos = Link.Xpos + (OFFSET_DOWN_SWORD); Sword.Graphic = &swordDown[0]; } else{ return; } } else if(Link.Direction == LEFT){ // make sure sword can fit within left side of LCD screen if(Link.Xpos <= (LCD_WIDTH-SWORD_LENGTH-Link.Width-PIXEL_BUFFER)){ Sword.Width = SWORD_LENGTH; Sword.Height = SWORD_BREADTH; Sword.Direction = LEFT; Sword.Ypos = Link.Ypos + (OFFSET_LEFT_SWORD); Sword.Xpos = Link.Xpos + (Link.Width - PIXEL_BUFFER); Sword.Graphic = &swordLeft[0]; } else{ return; } } else if (Link.Direction == RIGHT){ // make sure sword can fit within right side of LCD screen if(Link.Xpos >= (SWORD_LENGTH)){ Sword.Width = SWORD_LENGTH; Sword.Height = SWORD_BREADTH; Sword.Direction = RIGHT; Sword.Ypos = Link.Ypos + (OFFSET_RIGHT_SWORD); Sword.Xpos = Link.Xpos - (Sword.Width - PIXEL_BUFFER); Sword.Graphic = &swordRight[0]; } else{ return; } } // draws the swords LCD12864ImageDraw(&display[0],&room[0],Sword.Graphic,Sword.Xpos,Sword.Ypos,Sword.Width,Sword.Height); LCD12864PartUpdate(&display[0],Sword.Xpos,Sword.Ypos,Sword.Width,Sword.Height); Timer1A_Disable(); // boss stops moving (so it doesn't run into the sword) SysTick_Wait10ms((SWORD_DELAY)/10); // how long the sword will stay out CheckSword(); Timer1A_Enable(); // removes the sword LCD12864ImageClear(&display[0],&room[0],Sword.Xpos,Sword.Ypos,Sword.Width,Sword.Height); LCD12864PartUpdate(&display[0],Sword.Xpos,Sword.Ypos,Sword.Width,Sword.Height); }
void XBeeInit(){ char * commands [] = {"ATDL66", "ATDH0", "ATMY6D", "ATAP1", "ATCN", ""}; int i = 0; int j; UART_Init(); while (RxFifo_Size()>0){ //flush FIFO UART_InChar(); } ID = 1; UART_OutString("x"); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); //SysTick_Wait10ms(110); //wait waitTime number of ms; sendATCommand("+++", 110, 0); //UART_InString(response, 5); //RIT128x96x4StringDraw(response, 10, 10 , 15); for (i=0;i<5;i++){ sendATCommand(commands[i], 20, 1); } SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); }
void findObject(void) { int item = 0; int close =0; if (wasLeftWall){ SetMotor(rightMotor, -.1); //rotate SetMotor(leftMotor, .1); }else{ SetMotor(rightMotor, .1); //rotate SetMotor(leftMotor, -.1); } while (item == 0) { frontSensor = ADCRead(adc[0])*1000; if (frontSensor > 300) { item = 1; } } SetServo(servo,1); SetMotor(leftMotor, 0); SetMotor(rightMotor, 0); SysTick_Wait10ms(40); SetMotor(leftMotor, 1); SetMotor(rightMotor, 1); //SysTick_Wait10ms(100); // while (close == 0) { // frontSensor = ADCRead(adc[0])*1000; // if (frontSensor<300) { // SetMotor(rightMotor, -1); // SetMotor(leftMotor, 0); // SysTick_Wait10ms(50); // frontSensor = ADCRead(adc[0])*1000; // SetMotor(rightMotor, 0.1); // while(frontSensor<300){ // frontSensor = ADCRead(adc[0])*1000; // } // } // else if(frontSensor>300&&frontSensor<850) { // SetMotor(rightMotor, 1); // SetMotor(leftMotor, 1); // } // else if(frontSensor>850) { // close = 1; // } // } // if (frontSensor<400){ // SysTick_Wait10ms(200); // } // else if (frontSensor<600){ // SysTick_Wait10ms(150); // } // else if (frontSensor<800){ // SysTick_Wait10ms(100); // } // else if (frontSensor<1000){ // SysTick_Wait10ms(50); // } SysTick_Wait10ms(158); SetMotor(rightMotor, 0); //STOP SetMotor(leftMotor, 0); SetPin(PIN_F3, 1); SetServo(servo,0); SysTick_Wait10ms(50); SetMotor(rightMotor, -.5); SetMotor(leftMotor, .5); while(true){ SetPin(PIN_F3, 1); SetPin(PIN_F2, 0); SetPin(PIN_F1, 0); SysTick_Wait10ms(50); SetPin(PIN_F3, 0); SetPin(PIN_F2, 1); SetPin(PIN_F1, 0); SysTick_Wait10ms(50); SetPin(PIN_F3, 0); SetPin(PIN_F2, 0); SetPin(PIN_F1, 1); SysTick_Wait10ms(50); } }
// ************* Buttons ************* // this is the major function that handles the button presses // such as movements, attack, and mute, this function will // also check if the game has been won or lost and restart // the input is an int containing buttons that were pressed void Buttons(int keyPressed){ if (gameEnd == WON){ // win DisableInterrupts(); ClearLCDgameStarted(); // displays win banner LCD12864ImageDraw(&display[0],&room[0],&youWin[0],WIN_X,WIN_Y,WIN_WIDTH,WIN_HEIGHT); LCD12864PartUpdate(&display[0],WIN_X,WIN_Y,WIN_WIDTH,WIN_HEIGHT); SysTick_Wait10ms((GAME_RESTART_DELAY)/10); // wait 2 seconds to start game over Game_Init(); keyPressed = 0; EnableInterrupts(); Music_Play(); } else if (gameEnd == LOST){ // lose DisableInterrupts(); ClearLCDgameStarted(); // displays lose banner LCD12864ImageDraw(&display[0],&room[0],&gameOver[0],LOSE_X,LOSE_Y,LOSE_WIDTH,LOSE_HEIGHT); LCD12864PartUpdate(&display[0],LOSE_X,LOSE_Y,LOSE_WIDTH,LOSE_HEIGHT); SysTick_Wait10ms((GAME_RESTART_DELAY)/10); // wait 2 seconds to start game over Game_Init(); keyPressed = 0; EnableInterrupts(); Music_Play(); } // the following handles the inputs from the user // up button moves link up if ((keyPressed&UP) && gameStarted){ MoveUp(); } // down button moves link down if ((keyPressed&DOWN) && gameStarted){ MoveDown(); } // left button moves link to the left if ((keyPressed&LEFT) && gameStarted){ MoveLeft(); } // right button moves link to the right if ((keyPressed&RIGHT) && gameStarted){ MoveRight(); } // "A" button attacks with sword if ((keyPressed&A) && gameStarted){ PressA(); } // starts the game else if((keyPressed&A) && !gameStarted){ LCD12864ImageDraw(&display[0],&room[0],&room[0],0,0,LCD_WIDTH,LCD_HEIGHT); // draw room LCD12864ImageDraw(&display[0],&room[0],Link.Graphic,Link.Xpos,Link.Ypos,Link.Width,Link.Height); // draw link LCD12864Refresh(&display[0]); // display SysTick_Wait10ms(50); LCD12864ImageDraw(&display[0],&room[0],Boss.Graphic,Boss.Xpos,Boss.Ypos,Boss.Width,Boss.Height); // draw boss LCD12864Refresh(&display[0]); Change_Song(1); LCD12864GameStart(); Timer1A_Enable(); gameStarted = 1; } // "B" button mutes the game music if ((keyPressed&B) && gameStarted){ PressB(); ClearB(); } // updates the screen when Link moves if(keyPressed && gameStarted){ LCD12864ImageDraw(&display[0],&room[0],Link.Graphic,Link.Xpos,Link.Ypos,Link.Width,Link.Height); LCD12864PartUpdate(&display[0],Link.Xpos,Link.Ypos,Link.Width,Link.Height); CheckContact(); SysTick_Wait10ms((LINK_SPEED)/10); // time to wait until next movement } }