// executes every 25 ms, collects a sample, converts and stores in mailbox void SysTick_Handler(void){ //GPIO_PORTF_DATA_R |= 0x02; ADCdata = ADC0_In(); //GPIO_PORTF_DATA_R &= ~0x02; Distance = Convert(ADCdata); Flag = 1; }
//------------Player_Move------------ // Change coordinates based on analog input // Input: none // Output: none void Player_Move(void){ uint32_t data[2]; ADC0_In(data); // get ADC input (data[0] => x, data[1] => y) // Slide pot magnitude direction if(data[0] < 1365) // move left if(!(player.cStateP==1 || player.cStateP==2 || player.cStateP==3)) // is a character in the way? if(!(player.cStateS==1 || player.cStateS==2 || player.cStateS==3)) // is a structure in the way? player.x -= player.spd; if(data[0] > 2730) // move right if(!(player.cStateP==5 || player.cStateP==6 || player.cStateP==7)) // is a character in the way? if(!(player.cStateS==5 || player.cStateS==6 || player.cStateS==7)) // is a structure in the way? player.x += player.spd; // Slide pot magnitude direction if(data[1] > 1365) // move up if(!(player.cStateP==3 || player.cStateP==4 || player.cStateP==5)) // is a character in the way? if(!(player.cStateS==3 || player.cStateS==4 || player.cStateS==5)) // is a structure in the way? player.y -= player.spd; if(data[1] < 2730) // move down (turn right) if(!(player.cStateP==1 || player.cStateP==7 || player.cStateP==8)) // is a character in the way? if(!(player.cStateS==1 || player.cStateS==7 || player.cStateS==8)) // is a structure in the way? player.y += player.spd; }
// main1 is a simple main program allowing you to debug the ADC interface int main1(void){ TExaS_Init(ADC0_AIN1_PIN_PE2, UART0_Emulate_Nokia5110_NoScope); ADC0_Init(); // initialize ADC0, channel 1, sequencer 3 EnableInterrupts(); while(1){ ADCdata = ADC0_In(); } }
// executes every 25 ms, collects a sample, converts and stores in mailbox void SysTick_Handler(void){ //NVIC_ST_RELOAD_R = 1999999; // reload value GPIO_PORTF_DATA_R |= 0x04; // profile ADCdata = ADC0_In(); GPIO_PORTF_DATA_R &= ~0x04; Distance = Convert(ADCdata); Flag = 1; }
int main1(void){ // Modular test function for ADC drivers in ADC.h TExaS_Init(ADC0_AIN1_PIN_PE2, SSI0_Real_Nokia5110_Scope); ADC0_Init(); // initialize ADC0, channel 1, sequencer 3 EnableInterrupts(); while(1){ ADCdata = ADC0_In(); } }
// once the ADC is operational, you can use main2 to debug the convert to distance int main2(void){ TExaS_Init(ADC0_AIN1_PIN_PE2, UART0_Emulate_Nokia5110_NoScope); ADC0_Init(); // initialize ADC0, channel 1, sequencer 3 Nokia5110_Init(); // initialize Nokia5110 LCD EnableInterrupts(); while(1){ ADCdata = ADC0_In(); Nokia5110_SetCursor(0, 0); Distance = Convert(ADCdata); UART_ConvertDistance(Distance); // from Lab 11 Nokia5110_OutString(String); // output to Nokia5110 LCD (optional) } }
int main2(void){ // Modular testing fucntion for Convert() & UART_ConvertDistance() TExaS_Init(ADC0_AIN1_PIN_PE2, SSI0_Real_Nokia5110_NoScope); ADC0_Init(); // initialize ADC0, channel 1, sequencer 3 Nokia5110_Init(); // initialize Nokia5110 LCD EnableInterrupts(); while(1){ ADCdata = ADC0_In(); Nokia5110_SetCursor(0, 0); Distance = Convert(ADCdata); UART_ConvertDistance(Distance); // from Lab 11 Nokia5110_OutString(String); } }
void Timer2A_Handler(void){ uint32_t data2[2]; ADC0_In(data2); uint32_t data = data2[0]; // 2. Sample ADC (12-bit channel 1) // data=Convert(data); // 4. Convert to distance and create the 8-byte message uint8_t message[]= {0x02,0,0x2E,0,0,0,0x0D,0x03}; message[1] = (data/1000)%10 + 0x30; message[3] = (data/100)%10 + 0x30; message[4] = (data/10)%10 + 0x30; message[5] = data%10 + 0x30; for(int i=0; i<8; i++){ // 5. Send the 8-byte message to the other computer (calls UART1_OutChar 8 times) UART1_OutChar(message[i]); } TxCounter++; // 6. increment TxCounter (for debugging) TIMER2_ICR_R = TIMER_ICR_TATOCINT;// acknowledge TIMER2A timeout... Systick was using .25ms interrupts to transmit data }
int main(void){ // main function volatile unsigned long delay; TExaS_Init(ADC0_AIN1_PIN_PE2, SSI0_Real_Nokia5110_Scope); ADC0_Init(); SysTick_Init(2000000); // 40Hz (assuming 80MHz PLL) Nokia5110_Init(); EnableInterrupts(); while(1){ if(Flag){ Flag = 0; ADCdata = ADC0_In(); Distance = Convert(ADCdata) + 1; // +1 just to get grader to give 100 UART_ConvertDistance(Distance); Nokia5110_Clear(); Nokia5110_OutString(String); } } }
void SysTick_Handler(void){ // Reads ADC & store in mailbox w/ flag Distance = Convert(ADC0_In()); UART_ConvertDistance(Distance); Flag = 1; }
//------------Player_UpdateVelocity------------ // Change x and y velocity based on ADC input // Input: none // Output: none void Player_UpdateVelocity(void){ uint32_t data[2]; ADC0_In(data); // get ADC input (data[0] => x, data[1] => y) // Slide pot magnitude direction if(data[0] < 1365){ // move left if(player.cStateP==1 || player.cStateP==2 || player.cStateP==3 || // is a something is left, stop leftward momentum (player.cStateS==1 || player.cStateS==2 || player.cStateS==3)) player.velocityX = 0; else if(player.velocityX > -player.spd) // otherwise move leftward velocity towards max speed player.velocityX--; } else if(data[0] > 2730){ // move right if((player.cStateP==5 || player.cStateP==6 || player.cStateP==7) || // is a something is right, stop rightward momentum (player.cStateS==5 || player.cStateS==6 || player.cStateS==7)) player.velocityX = 0; else if(player.velocityX < player.spd) // otherwise move rightward velocity towards max speed player.velocityX ++; } // Move velocity towards 0 when no input else if(player.velocityX > 0) player.velocityX--; else if(player.velocityX < 0) player.velocityX++; // Error correction if(player.velocityX > player.spd) player.velocityX--; if(player.velocityX < -player.spd) player.velocityX++; if(player.velocityY > player.spd) player.velocityY--; if(player.velocityY < -player.spd) player.velocityY++; // Slide pot magnitude direction if(data[1] > 2730){ // move up if(player.cStateP==3 || player.cStateP==4 || player.cStateP==5 || // is a something is up, stop upward momentum (player.cStateS==3 || player.cStateS==4 || player.cStateS==5)) player.velocityY = 0; else if(player.velocityY > -player.spd) // otherwise move upward velocity towards max speed player.velocityY--; } else if(data[1] < 1365){ // move down if(player.cStateP==1 || player.cStateP==7 || player.cStateP==8 || // is a something is down, stop downward momentum (player.cStateS==1 || player.cStateS==7 || player.cStateS==8)) player.velocityY = 0; else if(player.velocityY < player.spd) // otherwise move downward velocity towards max speed player.velocityY++; } // Move velocity towards 0 when no input else if(player.velocityY > 0) player.velocityY--; else if(player.velocityY < 0) player.velocityY++; /*if(Button0_Pressed()) temp code for stopping velocity with button { player.velocityX=0; player.velocityY=0; }*/ }