int main(void){

	uint8_t received_data[2];
	
	init_I2C1(); // initialize I2C peripheral

	
	

	while(1){

		I2C_start(I2C1, SLAVE_ADDRESS, I2C_Direction_Transmitter); // start a transmission in Master transmitter mode
		I2C_write(I2C1, 0x20); // write one byte to the slave
		I2C_write(I2C1, 0x03); // write another byte to the slave
		I2C_stop(I2C1); // stop the transmission
	}
}
Example #2
0
int main(void)
{
	int timerID = 1;
	int timerID1 = 2;

	/*A Timer used to count how long there is no signal come in*/
	xTimerNoSignal = xTimerCreate("TurnOffTime", 10000 / portTICK_RATE_MS, pdFALSE,  (void *) timerID, vTimerSystemIdle);

	xTimerSampleRate = xTimerCreate("SensorSampleRate", 4 / portTICK_RATE_MS, pdTRUE,  (void *) timerID1, vTimerSample);

	/*a queue for tansfer the senddate to USART task*/
	xQueueUARTSend = xQueueCreate(15, sizeof(serial_str_msg));
   	xQueueUARTRecvie = xQueueCreate(15, sizeof(serial_ch_msg));
   	xQueueShell2PWM = xQueueCreate(3, sizeof(pwm_ch_msg));

   	xQueuePitchdirection = xQueueCreate(3, sizeof(pitch_direction_msg));
   	xQueueRolldirection = xQueueCreate(3, sizeof(roll_direction_msg));

	/* initialize hardware... */
	prvSetupHardware();
	init_I2C1();
	xTimerStart(xTimerNoSignal, 0);


	/* Start the tasks defined within this file/specific to this demo. */
	xTaskCreate(vPWMctrlTask, ( signed portCHAR * ) "pwmctrl", configMINIMAL_STACK_SIZE, NULL,tskIDLE_PRIORITY, NULL );
	xTaskCreate(vUsartSendTask, ( signed portCHAR * ) "USART", configMINIMAL_STACK_SIZE, NULL,tskIDLE_PRIORITY, NULL);
	xTaskCreate(shell, ( signed portCHAR * ) "shell", configMINIMAL_STACK_SIZE, NULL,tskIDLE_PRIORITY + 5, NULL);
	xTaskCreate(vBalanceTask, ( signed portCHAR * ) "Balance", configMINIMAL_STACK_SIZE, NULL,tskIDLE_PRIORITY, NULL);

	xTaskCreate(vPitchctrlTask, ( signed portCHAR * ) "Pitchctrl", configMINIMAL_STACK_SIZE, NULL,tskIDLE_PRIORITY, NULL );
	xTaskCreate(vRollctrlTask, ( signed portCHAR * ) "Rollctrl", configMINIMAL_STACK_SIZE, NULL,tskIDLE_PRIORITY, NULL );

	/* Start the scheduler. */
	vTaskStartScheduler();

	/* Will only get here if there was not enough heap space to create the idle task. */
	return 0;  
}
Example #3
0
int main(void)
{

	SystemInit();

	STM32F4_Discovery_LEDInit(LED3); //Orange
	STM32F4_Discovery_LEDInit(LED4); //Green
	STM32F4_Discovery_LEDInit(LED5); //Red
	STM32F4_Discovery_LEDInit(LED6); //Blue

	STM32F4_Discovery_PBInit(BUTTON_USER, BUTTON_MODE_GPIO);

	USBD_Init(&USB_OTG_dev,USB_OTG_FS_CORE_ID,&USR_desc,&USBD_CDC_cb,&USR_cb);
	SystemCoreClockUpdate(); // inicjalizacja dystrybucji czasu procesora
		init_I2C1(); // na podstawie: http://eliaselectronics.com/stm32f4-tutorials/stm32f4-i2c-mastertutorial/

		//acc
			I2C_start(I2C1, LSM303DL_A_ADDRESS, I2C_Direction_Transmitter);
			I2C_write(I2C1,0x20);   // LSM303_CTRL_REG1_A  0x20
			I2C_write(I2C1,0x27);   // Enable Accelerometer
			  	  	  	  	  	  	// 0x27 = 0b00100111
									// Normal power mode, all axes enabled
			I2C_stop(I2C1); // stop the transmission
		//acc
		//mag
				I2C_start(I2C1, LSM303DL_M_ADDRESS, I2C_Direction_Transmitter);
				I2C_write(I2C1,0x02);	  //LSM303_MR_REG_M   0x02
				I2C_write(I2C1,0x00);     // Enable Magnetometer
										  // 0x00 = 0b00000000
				  	  	  	  	  	  	  // Continuous conversion mode
				I2C_stop(I2C1);
		//mag
		//gyro
				I2C_start(I2C1, LSM303DL_G_ADDRESS, I2C_Direction_Transmitter);
				I2C_write(I2C1, 0x20);   //L3G_CTRL_REG1 0x20
				I2C_write(I2C1, 0x0F);   // 0x0F = 0b00001111
					  	  	  	  	     // Normal power mode, all axes enabled
				I2C_stop(I2C1);
		//gyro
	char start='0';
	while(1)
	{
					Delay(5);
					read_acc();
					read_mag();
					read_gyro();
					start='0';

					while(1)
					{
						start = usb_cdc_getc();
						if(start=='1')
						{
							break;
						}
					}

	}
	/*while (1){

		if(usb_cdc_kbhit()){
			char c, buffer_out[15];
			c = usb_cdc_getc();
			switch(c){
				case '3':
					STM32F4_Discovery_LEDToggle(LED3);
					sprintf(buffer_out,"LED%c = %u\r\n",c,GPIO_ReadInputDataBit(GPIOD,LED3_PIN));
					usb_cdc_printf(buffer_out);
					break;
				case '4':
					STM32F4_Discovery_LEDToggle(LED4);
					sprintf(buffer_out,"LED%c = %u\r\n",c,GPIO_ReadInputDataBit(GPIOD,LED4_PIN));
					usb_cdc_printf(buffer_out);
					break;
				case '5':
					STM32F4_Discovery_LEDToggle(LED5);
					sprintf(buffer_out,"LED%c = %u\r\n",c,GPIO_ReadInputDataBit(GPIOD,LED5_PIN));
					usb_cdc_printf(buffer_out);
					break;
				case '6':
					STM32F4_Discovery_LEDToggle(LED6);
					sprintf(buffer_out,"LED%c = %u\r\n",c,GPIO_ReadInputDataBit(GPIOD,LED6_PIN));
					usb_cdc_printf(buffer_out);
					break;
			}
		}

		button_sts = STM32F4_Discovery_PBGetState(BUTTON_USER);

		if(button_sts){
			STM32F4_Discovery_LEDOff(LED3);
			STM32F4_Discovery_LEDOff(LED5);
			STM32F4_Discovery_LEDOff(LED3);
			STM32F4_Discovery_LEDOff(LED5);
		}
	}*/
}
Example #4
0
int main(void)
{
    	int i=1;
    	USART3_Config();	
   	SysTick_Init();
    	init_I2C1();
    	sensor_ayarla();
    	printf("l3g4200d\r\n");
    	while (1){

    		if((I2C_readreg(L3G4200D_ADDR,STATUS_REG)&0x08)==0x08) 
    		{

			/* read the rate from the register */
        		gyrox_l_temp=I2C_readreg(L3G4200D_ADDR,OUT_X_L);
        		gyrox_h_temp=I2C_readreg(L3G4200D_ADDR,OUT_X_H);
        		gyrox_temp=((gyrox_h_temp << 8) | gyrox_l_temp);

        		gyroy_l_temp=I2C_readreg(L3G4200D_ADDR,OUT_Y_L);
        		gyroy_h_temp=I2C_readreg(L3G4200D_ADDR,OUT_Y_H);
        		gyroy_temp=((gyroy_h_temp << 8) | gyroy_l_temp);

        		gyroz_l_temp=I2C_readreg(L3G4200D_ADDR,OUT_Z_L);
        		gyroz_h_temp=I2C_readreg(L3G4200D_ADDR,OUT_Z_H);
        		gyroz_temp=((gyroz_h_temp << 8) | gyroz_l_temp);
			printf("gyrox_temp=%d   gyroy_temp=%d   gyroz_temp=%d\r\n",gyrox_temp,gyroy_temp,gyroz_temp);

			
			/* compute the value of rate */
			gyrox = gyrox_temp * gyro_sensitivity_250 /1000 ;  
			gyroy = gyroy_temp * gyro_sensitivity_250 /1000 ;
			gyroz = gyroz_temp * gyro_sensitivity_250 /1000 ;
			
			
			/* print the value of rate*/			
			gyrox_int = (int16_t)gyrox;
			gyrox_decimal = (gyrox - gyrox_int)*1000;
			if(gyrox_decimal < 0)
				gyrox_decimal=-gyrox_decimal;

			gyroy_int = (int16_t)gyroy;
			gyroy_decimal = (gyroy - gyroy_int)*1000;
			if(gyroy_decimal < 0)
				gyroy_decimal=-gyroy_decimal;

			gyroz_int = (int16_t)gyroz;
			gyroz_decimal = (gyroz - gyroz_int)*1000;
			if(gyroz_decimal < 0)
				gyroz_decimal=-gyroz_decimal;

			printf("gyrox_float=%d.%d\r\n",gyrox_int,gyrox_decimal);
			printf("gyroy_float=%d.%d\r\n",gyroy_int,gyroy_decimal);
			printf("gyroz_float=%d.%d\r\n",gyroz_int,gyroz_decimal);


			/* 頃角計算 */	
			i=1;
			for(i;i<=50;i++)
				gyro_sample();		

			//gyrox_angle=gyrox_angle+gyrox*0.5;
			//gyroy_angle=gyroy_angle+gyroy*0.5;
			//gyroz_angle=gyroz_angle+gyroz*0.5;


			/* print the value of angle */
			gyrox_angle_int = (int16_t)gyrox_angle;
			gyrox_angle_decimal = (gyrox_angle - gyrox_angle_int)*1000;
			if(gyrox_angle_decimal < 0)
				gyrox_angle_decimal=-gyrox_angle_decimal;
			
			gyroy_angle_int = (int16_t)gyroy_angle;
			gyroy_angle_decimal = (gyroy_angle - gyroy_angle_int)*1000;
			if(gyroy_angle_decimal < 0)
				gyroy_angle_decimal=-gyroy_angle_decimal;
			
			gyroz_angle_int = (int16_t)gyroz_angle;
			gyroz_angle_decimal = (gyroz_angle - gyroz_angle_int)*1000;
			if(gyroz_angle_decimal < 0)
				gyroz_angle_decimal=-gyroz_angle_decimal;


			
 			printf("gyrox_angle_float=%d.%d\r\n",gyrox_angle_int,gyrox_angle_decimal);
			printf("gyroy_angle_float=%d.%d\r\n",gyroy_angle_int,gyroy_angle_decimal);
			printf("gyroz_angle_float=%d.%d\r\n",gyroz_angle_int,gyroz_angle_decimal);
			printf("\r\n-------------------------------------------------------\r\n");
			Delay(500);
    }
   }
}