Example #1
0
void ADC_Init(const uint8_t Vref, const uint8_t DataBit, const uint8_t Psc)
{
	ADC_SetVref(Vref);
	ADC_SetChannel(ADC_CH0);
	ADC_SetDataBit(DataBit);
	ADC_SetPrescalar(Psc);
	ADC_StartSingleConversion();
	ADC_ONOFF(1);
}
Example #2
0
uint16_t ADC_ReadData(const uint8_t aChannel)
{
	ADC_SetChannel(aChannel);
	ADC_StartSingleConversion();
	ADC_WaitConversionComplete();
	ADC_ClearFlagComplete();
	
	if (bit_is_set(ADMUX,ADLAR))	//8bit
	{
		return ADCH;
	}
	else
	{
		return ADC;
	}
}
Example #3
0
File: adc.c Project: c0lin91/EE445M
// ************** ADC_ReadXVal *****************************
// - 
// *********************************************************
// Input: none
// Output: none
// *********************************************************
unsigned long Touch_ReadX(void){
    long i = 0;
    long sum = 0;
    long result = 0;
    
    GPIO_PORTA_DATA_R   &= ~PA2;

    // Configure PA3 (XP) for GPIO HIGH
    GPIO_PORTA_AFSEL_R  &= ~PA3;       // ASFEL = 0
    GPIO_PORTA_DEN_R    |=  PA3;       // DEN = 1
    GPIO_PORTA_DIR_R    |=  PA3;       // DIR = 1
    GPIO_PORTA_DATA_R   |=  PA3;       // DATA = 1
    
    // Configure PE4 (XN) for GPIO LOW
    GPIO_PORTE_AFSEL_R  &= ~PE4;       // ASFEL = 0
    GPIO_PORTE_DEN_R    |=  PE4;       // DEN = 1
    GPIO_PORTE_DIR_R    |=  PE4;       // DIR = 1
    GPIO_PORTE_DATA_R   &= ~PE4;       // DATA = 0
    
    // Configure PA2 (YN) for analog hi-Z
    GPIO_PORTA_AFSEL_R  &= ~PA2;       // AFSEL = 0
    GPIO_PORTA_DEN_R    &= ~PA2;       // DEN = 0
    GPIO_PORTA_DIR_R    &= ~PA2;       // DIR = 0
    
    // Configure PE5 (YP) for ADC use
    GPIO_PORTE_AFSEL_R  |=  PE5;       // AFSEL = 1
    GPIO_PORTE_DEN_R    &= ~PE5;       // DEN = 0
    GPIO_PORTE_AMSEL_R  |=  PE5;       // AMSEL = 1
   
    // Configure ADC to read from AIN8 (PE5, YP)
    ADC_SetChannel(8);
    
    // Take some samples to average
    for (i = 0; i < NUM_SAMPLES; i++){
        sum += ADC_Read();
    }

    GPIO_PORTE_AMSEL_R  &= ~PE5;       // AMSEL = 0
     
    // Compute average
    result = sum / NUM_SAMPLES;
    
    Touch_XVal = result;

    return result;
}
Example #4
0
//GAME
int main(void){unsigned long i=64,j=0;
  PLL_Init();  // Set the clocking to run at 80MHz from the PLL.
  LCD_Init();  // Initialize LCD
	Piano_Init();
	Sound_Init();
	ADC_Init();		//initialize ADC
	ADC_SetChannel(1); //PE2
	
	Missile_Init();
	Enemy1_Init();
	
  LCD_Goto(10,0);
  LCD_SetTextColor(255,0,0); // yellow= red+green, no blue
  printf("Welcome to Pokemon Capture!");
	delay_long();
	LCD_DrawFilledRect(0,0,320,80,0x00);
	delay_short();
	
	while((GPIO_PORTE_DATA_R&0x01)==0){
		LCD_Goto(10,0);
		printf("Press any button to begin...");
		delay_blink();
		LCD_DrawFilledRect(0,0,320,80,0x00);
		delay_blink();
	}
	delay_short();
	
	LCD_SetTextColor(255,255,0);
	LCD_Goto(0,0);
	printf("Tragedy has struck Pallet Town! Four Pokemon have    escaped from their Pokeballs, and it's your job to   put them back where they belong.");
	delay_long();
	delay_long();
	delay_long();
	LCD_DrawFilledRect(0,0,320,80,0x00);
	delay_short();
	
	LCD_Goto(0,0);
	printf("Use the slider to aim, and when you think you have a shot, press the button to throw your Pokeball.");
	delay_long();
	delay_long();
	delay_long();
	LCD_DrawFilledRect(0,0,320,80,0x00);
	delay_blink();
	
	LCD_Goto(0,0);
	printf("You only have so many Pokeballs, so be smart where   you throw them!");
	delay_long();
	delay_long();
	LCD_DrawFilledRect(0,0,320,80,0x00);
	delay_short();
	
	LCD_Goto(0,0);
	printf("Good luck!");
	delay_long();
	LCD_DrawFilledRect(0,0,320,80,0x00);
	delay_short();
	
	LCD_SetTextColor(255,0,0);
	LCD_Goto(0,0);
	printf("Captured:");
	
	LCD_SetTextColor(0,255,0);
	LCD_Goto(14,0);
	printf("Pokeballs Left:");
	
	LCD_SetTextColor(0,0,255);
	LCD_Goto(35,0);
	printf("Accuracy:");
  LCD_DrawLine(10,16,310,16,BURNTORANGE);
  
  Timer2_Init(7256); // 11kHz
	SysTick_Init(2666667);	//30 Hz
  EnableInterrupts();
	
	Pokeballs = 25;
  while(1){
		if((25-(hits+misses))==0){
			if(kill!=4){
				DisableInterrupts();
				LCD_SetTextColor(255,0,0);
				
				LCD_DrawFilledRect(0,0,320,80,0x00);
				delay_short();
				
				LCD_Goto(0,0);
				printf("Sorry, you were not able to catch all the Pokemon.");
				delay_long();
				delay_long();
				LCD_DrawFilledRect(0,0,320,80,0x00);
				delay_short();
				
				LCD_Goto(0,0);
				printf("Try again.");
				delay_long();
				LCD_DrawFilledRect(0,0,320,80,0x00);
				delay_short();
				
				while(1){
				LCD_Goto(0,0);
				printf("Press RESET to play again.");
				delay_blink();
				LCD_DrawFilledRect(0,0,320,80,0x00);
				delay_blink();
				}
			}
		}
				
		if(kill==4){
			DisableInterrupts();
			LCD_SetTextColor(0,255,255);
			
			LCD_DrawFilledRect(0,0,320,80,0x00);
			delay_short();
			
			LCD_Goto(0,0);
			printf("Congratulations! You have captured all the Pokemon!");
			delay_long();
			delay_long();
			LCD_DrawFilledRect(0,0,320,80,0x00);
			delay_short();
			
			LCD_Goto(0,0);
			printf("You are now a Pokemon Master!");
			delay_long();
			delay_long();
			LCD_DrawFilledRect(0,0,320,80,0x00);
			delay_short();
			
			while(1){
				LCD_Goto(0,0);
				printf("Press RESET to play again.");
				delay_blink();
				LCD_DrawFilledRect(0,0,320,80,0x00);
				delay_blink();
			}
		}
		
		
    if(Semaphore){
			
			LCD_SetTextColor(255,0,0);
			LCD_Goto(10,0);
			LCD_PrintInteger(kill);
			
			LCD_SetTextColor(0,255,0);
			LCD_Goto(30,0);
			LCD_PrintInteger(25-(hits+misses));
			if((25-(hits+misses))<10){
				LCD_Goto(31,0);
				printf(" ");
			}
			
			LCD_SetTextColor(0,0,255);
			LCD_Goto(45,0);
			LCD_PrintInteger(hits/(hits+misses));
			
			//draw ship
			if(Ship_Position>288){
				Ship_Position=288;
			}
      LCD_DrawBMP(PlayerShip,Ship_Position,220);
			LCD_DrawFilledRect(0,220,Ship_Position,9,0x00);
			LCD_DrawFilledRect(Ship_Position+32,220,320-(Ship_Position+32),9,0x00);
			
			
      for(j=0;j<1;j++){
				if(Missile[j].life==1){
					LCD_DrawBMP(Missile[j].image, Missile[j].x, Missile[j].y);
				} else{
					LCD_DrawFilledRect(Missile[j].x,Missile[j].y,16,18,0x00);
				}
				if(Missile[j].y<=18){
					LCD_DrawFilledRect(Missile[j].x,Missile[j].y,16,18,0x00);
					Missile[j].life=0;
					misses++;
				}
			}
			
      for(i=0;i<4;i++){
				if(Enemy1[i].life!=0){
					LCD_DrawBMP(Enemy1[i].image,Enemy1[i].x,Enemy1[i].y);
				}
				else{
					LCD_DrawFilledRect(Enemy1[i].x,Enemy1[i].y,32,32,0x00);
				}
			}
			
			LCD_DrawFilledRect(0,17,320,4,0x00);
			
      Semaphore = 0;
    }
  }
}
Example #5
0
void PowerSwitch::scanSenseValue()
{      
    ADC_SetChannel(senseChNum); 
    
    senseValue = ADC_GetAdcResult();    
}  
Example #6
0
/*****************************************************************************//*!
   *
   * @brief start a conversion and get conversion result
   *        
   * @param[in]  pADC point to ADC module type. 
   * @param[in]  u8Channel adc channel to conversion. 
   *
   * @return ADC conversion result.
   *
   * @ Pass/ Fail criteria: none
   *****************************************************************************/
unsigned int ADC_PollRead( ADC_Type *pADC, uint8_t u8Channel )
{
		ADC_SetChannel(pADC,u8Channel);
		while( !ADC_IsCOCOFlag(pADC) );
		return ADC_ReadResultReg(pADC);
}
Example #7
0
/*****************************************************************************//*!
   *
   * @brief disable ADC module.
   *        
   * @param[in]  pADC point to ADC module type. 
   *
   * @return none.
   *
   * @ Pass/ Fail criteria: none.
   *****************************************************************************/
void ADC_DeInit( ADC_Type *pADC )
{
    ADC_SetChannel(pADC,ADC_CHANNEL_DISABLE);

    SIM->SCGC &= ~SIM_SCGC_ADC_MASK;
}