uchar read_from_keyboard(void)
{
	uchar row=0,col=0;
	uchar value;
	uint i;

	//determine the row
	value = (PTT & 0x0F );
	if( value == 0x0E )
	{
		row = 0;
	}
	else if( value == 0x0D )
	{
		row = 1;
	}
	else if( value == 0x0B )
	{
		row = 2;
	}
	else if( value == 0x07 )
	{
		row = 3;
	}
	else
	{
		return 0xFF;
	}

	//determine the column
	value = 0x1F;
	for(i=0; i<4; i++)
	{
		PTT = value;
		delay_short();
		if( (PTT & 0x0F) == 0x0F)
		{
			col = i;
			break;
		}
		value = ((value<<1) | 0x0F);//fill with 1's from the right
	}

	if( i== 4)
	{
		return 0xFF;
	}

	return keyboard_code[4*row+col];
}
Example #2
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;
    }
  }
}
void main(void)
{   
	//IRQ INITIALIZATION
    INTCR_IRQEN = 1;     //enables IRQ# interrupts
    INTCR_IRQE  = 1;     //IRQ# interrupts edge-triggered
	////////
	
	//FUN BLINKING LIGHTS FOR FUNzies
	DDRA   = 0xFF;
	PORTA_BIT0 = 0;
	////////

	//KEYBOARD SETUP / PTT is also shared with Input Capture
    DDRT   = 0xF0;	//enable Port T, 7-4 bits for output, bits 3-0 for input
    PERT   = 0x0F;	//enable Port T, bits 3-0 for pull device
    PPST   = 0x00;	//set    Port T, bits 3-0 for Pull-Up resistors    
    PTT    = 0x00;	//initialize output pins to low
    //Port E, bits 1-0 are input always input
	//////// 

	//INPUT CAPTURE REGISTER INITIALIZATION
    TSCR1_TEN = 1;	//enable TCNT (timer counter)
	TSCR2     = (0xF8 & TSCR2) | TIMER_PRESCALE;
    TIOS_IOS0 = 1;	//set TC7 to output compare (TIOS)
    TFLG1_C0F = 1;	//reset TC7 interupt flags (TFLG1)
    TIE_C0I   = 1;	//enable TC7 interrupts (TIE)
	////////

	//PWM REGISTER INITIALIZATION
	PWMCLK_PCLK5 = 1;	//1 to select clock SA
	PWMPRCLK = (0xF8 & PWMPRCLK) | A_CLK_PRESCALE;
	PWMSCLA = SA_CLK_PRESCALE;	//SA = A/(2*SA_CLK_PRESCALE)
	PWMPOL_PPOL5 = 1;	//PPOL 1 to start high at beginning of period,
						//low when duty count is reached
	PWMCAE_CAE5 = 0;	//0 for left aligned
	PWME_PWME5 = 0;		//0 to disable initially
	PWMPER5 = PWM_PERIOD;//500 Hz
	PWMDTY5 = 0;	//scaled duty
	////////	

	//ANALOG TO DIGITAL INITIALIZATION
	ATDCTL2_ADPU   = 1; //Activate Power up
	ATDCTL2_AFFC   = 0; //Normal flag clear (default)
	ATDCTL2_AWAI   = 0; //Run in WAIT mode (default)
	ATDCTL2_ETRIGE = 0; //No external trigger (default)
//	ATDCTL2_ETRIGP = 0; //No external trigger (default)
	ATDCTL2_ASCIE  = 0; //No interrupts (default)
//  ATDCTL2_ASCIF       //interrupt flag
	delay_short();

	ATDCTL3_S8C  = 0;   //one conversion per sequence
	ATDCTL3_S4C  = 0;
	ATDCTL3_S2C  = 0;
	ATDCTL3_S1C  = 1;
	ATDCTL3_FIFO = 0;   //result in consecutive registers (default)
	ATDCTL3_FRZ  = 0;   //continue in freeze mode (default)

	ATDCTL4_SRES8 = 1;	//8-bit resolution (0=10 bit)
	ATDCTL4_SMP   = 3;	//16-clock (max) sample time for accuracy
	ATDCTL4_PRS   = 1;  //prescale fbus(4 MHz) by 4 for range [2MHz...500kHz]

	ATDCTL5 = 0x87;
	////////
	
	//LED DISPLAY INITIALIZATION
    DDRAD  = 0x7F;  //7 AD conversion, 6 data storage status (1==done),
                    //5 means PID control 1==ON
					//3-0 key pressed
	ATDDIEN_IEN7 = 1;
    PTAD   = 0x00;
	////////

	//GLOBAL VARIABLE INITIALIZATION
	keyboard_state = KEYBOARD_READY;
	measurement_state = MEASUREMENT_READY;
	control_state = CONTROL_STATE_OPEN;

	error[2] = 0;
	error[1] = 0;
	error[0] = 0;
	reference =0;
	adc_measurement[1] = 0;
	adc_measurement[0] = 0;
	last_ditch = 0;

	man_integral = 0;
	//initialize all the data to zeros
	for(measurement_index=0; measurement_index<MEASUREMENT_NUM_SAMPLES; measurement_index++)
	{
		measurement_storage[measurement_index] = 0; 
	}
	measurement_index=0;                
	////////	

    EnableInterrupts;     //enable IRQ
    while (1)
    {
        continue;
    }

	return;  
}
uchar read_from_keyboard(void) 
{
	//SCANNING ROUTINE EXPLANATION
    //PTT bits
    //correspond to...
    //out  in
    //cols rows
    //3210 3210
    
    //the point of this scanning routine is to not trigger a Falling edge on the
    //IRQ pin. In other words this routine looks for a rising edge on the input
    //from the columns

    //column scanning routine
    //0b0000 xxxx
    //0b0001 xxxx
        //if xxxx 1111 then
        // col = 0
    //0b0011 xxxx
        //if xxxx 1111 then
        // col = 1
    //0b0111 xxxx
        //if xxxx 1111 then
        // col = 2
    //0b1111 xxxx
        //if xxxx 1111 then
        // col = 3
	//otherwise invalid KeyPress
		//return 0xFF
	////////

    char row = 0, col = 0;
    uchar value;
    uint i;
    
    //determine the row
    value = (PTT & 0x0F);
    if(value == 0x0E) 
    {
        row = 0;
    } 
    else if (value == 0x0D)
    {
        row = 1;
    }
    else if (value == 0x0B)
    {
        row = 2; 
    }
    else if (value == 0x07)
    {
        row = 3;
    } 
  
	//determine the column
    value = 0x1F; //0b0001 1111
    for(i = 0; i <4; i++ )
    {
        PTT = value;
        delay_short();
        if( (PTT & 0x0F) == 0x0F )
        {
            col = i;
            break;
        }
        value = ((value<<1) | 0x0F);//fill with 1's from the right
    }
    if( i == 4)//could not read a correct value
    {
        return 0xFF;
    }
    //i=0   value=0b0001 1111
    //i=1   value=0b0011 1111
    //i=2   value=0b0111 1111
    //i=3   value=0b1111 1111
    
    return code[4*row+col];
}
Example #5
0
static int reset_init_mpu(void) {
	msg_t res = MSG_OK;

	palSetPadMode(SCL_GPIO, SCL_PAD,
			PAL_STM32_OTYPE_OPENDRAIN |
			PAL_STM32_OSPEED_MID1);

	for(int i = 0;i < 16;i++) {
		palClearPad(SCL_GPIO, SCL_PAD);
		delay_short();
		palSetPad(SCL_GPIO, SCL_PAD);
		delay_short();
	}

	palSetPadMode(SCL_GPIO, SCL_PAD,
			PAL_MODE_ALTERNATE(GPIO_AF_I2C1) |
			PAL_STM32_OTYPE_OPENDRAIN |
			PAL_STM32_OSPEED_MID1);

	I2C_DEV.state = I2C_STOP;
	i2cStart(&I2C_DEV, &i2cfg);
	chThdSleepMicroseconds(1000);

	// Set clock source to gyro x
	i2cAcquireBus(&I2C_DEV);
	tx_buf[0] = MPU9150_PWR_MGMT_1;
	tx_buf[1] = 0x01;
	res = i2cMasterTransmitTimeout(&I2C_DEV, mpu_addr, tx_buf, 2, rx_buf, 0, MPU_I2C_TIMEOUT);
	i2cReleaseBus(&I2C_DEV);

	// Try the other address
	if (res != MSG_OK) {
		if (mpu_addr == MPU_ADDR1) {
			mpu_addr = MPU_ADDR2;
		} else {
			mpu_addr = MPU_ADDR1;
		}

		// Set clock source to gyro x
		i2cAcquireBus(&I2C_DEV);
		tx_buf[0] = MPU9150_PWR_MGMT_1;
		tx_buf[1] = 0x01;
		res = i2cMasterTransmitTimeout(&I2C_DEV, mpu_addr, tx_buf, 2, rx_buf, 0, MPU_I2C_TIMEOUT);
		i2cReleaseBus(&I2C_DEV);

		if (res != MSG_OK) {
			return 0;
		}
	}

	// Set accelerometer full-scale range to +/- 16g
	i2cAcquireBus(&I2C_DEV);
	tx_buf[0] = MPU9150_ACCEL_CONFIG;
	tx_buf[1] = MPU9150_ACCEL_FS_16 << MPU9150_ACONFIG_AFS_SEL_BIT;
	res = i2cMasterTransmitTimeout(&I2C_DEV, mpu_addr, tx_buf, 2, rx_buf, 0, MPU_I2C_TIMEOUT);
	i2cReleaseBus(&I2C_DEV);

	if (res != MSG_OK) {
		return 0;
	}

	// Set gyroscope full-scale range to +/- 2000 deg/s
	i2cAcquireBus(&I2C_DEV);
	tx_buf[0] = MPU9150_GYRO_CONFIG;
	tx_buf[1] = MPU9150_GYRO_FS_2000 << MPU9150_GCONFIG_FS_SEL_BIT;
	res = i2cMasterTransmitTimeout(&I2C_DEV, mpu_addr, tx_buf, 2, rx_buf, 0, MPU_I2C_TIMEOUT);
	i2cReleaseBus(&I2C_DEV);

	if (res != MSG_OK) {
		return 0;
	}

	// Set low pass filter to 256Hz (1ms delay)
	i2cAcquireBus(&I2C_DEV);
	tx_buf[0] = MPU9150_CONFIG;
	tx_buf[1] = MPU9150_DLPF_BW_256;
	res = i2cMasterTransmitTimeout(&I2C_DEV, mpu_addr, tx_buf, 2, rx_buf, 0, MPU_I2C_TIMEOUT);
	i2cReleaseBus(&I2C_DEV);

	if (res != MSG_OK) {
		return 0;
	}

#if USE_MAGNETOMETER
	// Set the i2c bypass enable pin to true to access the magnetometer
	i2cAcquireBus(&I2C_DEV);
	tx_buf[0] = MPU9150_INT_PIN_CFG;
	tx_buf[1] = 0x02;
	res = i2cMasterTransmitTimeout(&I2C_DEV, mpu_addr, tx_buf, 2, rx_buf, 0, MPU_I2C_TIMEOUT);
	i2cReleaseBus(&I2C_DEV);

	if (res != MSG_OK) {
		return 0;
	}
#endif

	is_mpu9250 = read_single_reg(MPU9150_WHO_AM_I) == 0x71;

	return 1;
}