コード例 #1
0
ファイル: main.c プロジェクト: numkang/STM32F429_Training
int main(void)
{
    RCC_Configuration();
    GPIO_Configuration();
    USART1_Configuration();
    LED_Initialization();
    USART1_puts("Hello World!\r\n");
    USART1_puts("Just for STM32F429I Discovery verify USART1 with USB TTL Cable\r\n");
    while(1)
    {
        //LED3_Toggle();

        //Recieve
        if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) != RESET){ // == SET
        char t = USART_ReceiveData(USART1);
        if(t == 'a') LED3_Toggle();
        else if(t == 'b') LED4_Toggle();

        //Transmitt
        while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
        USART_SendData(USART1, t);

        }

    }

    while(1); // Don't want to exit
}
コード例 #2
0
ファイル: i2c_ops.c プロジェクト: iGaryGu/Painter
void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction)
{
	// wait until i2c_dev is not busy anymore
//	while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));
	// Send i2c_dev START condition
	I2C_GenerateSTART(I2Cx, ENABLE);
	
	USART1_puts("send start!\r\n");
	// wait for i2c_dev EV5 --> Slave has acknowledged start condition
//	while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
	// Send slave Address for write
	I2C_Send7bitAddress(I2Cx, address, direction);

	USART1_puts("send address!\r\n");
	/* wait for i2c_dev EV6, check if
	 * either Slave has acknowledged Master transmitter or
	 * Master receiver mode, depending on the transmission
	 * direction
	 */
	if(direction == I2C_Direction_Transmitter)
	{
		while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
//		Delay(100);

		USART1_puts("master transmit!\r\n");
	}
	else if(direction == I2C_Direction_Receiver)
	{
//		Delay(100);
//		while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
		USART1_puts("master receive!\r\n");
	}
}
コード例 #3
0
ファイル: i2c_ops.c プロジェクト: iGaryGu/Painter
uint8_t I2C_writereg(uint8_t reg, uint8_t data)
{
	uint8_t tmp;

	USART1_puts("start!\r\n");

	I2C_start(i2c_dev, SLAVE_ADDRESS, I2C_Direction_Transmitter); // start a transmission in Master transmitter mode
	USART1_puts("write start!\r\n");
	I2C_write(i2c_dev, (uint8_t) reg); // write one byte to the slave
	USART1_puts("write!one \r\n");
	delay(100);
	I2C_write(i2c_dev, (uint8_t) data); // write one byte to the slave
	USART1_puts("write!two \r\n");
	I2C_stop(i2c_dev); // stop the transmission
	USART1_puts("stop!\r\n");
	delay(100);
	I2C_start(i2c_dev, SLAVE_ADDRESS, I2C_Direction_Receiver); // start a transmission in Master receiver mode
	USART1_puts("start!\r\n");
	tmp = I2C_read_nack(i2c_dev);
	USART1_puts("write nack!\r\n");
	I2C_stop(i2c_dev); // stop the transmission
	USART1_puts("stop!\r\n");
	delay(100);

	return tmp;
}
コード例 #4
0
ファイル: i2c_ops.c プロジェクト: iGaryGu/Painter
/* This function transmits one byte to the slave device
 * Parameters:
 *		I2Cx --> the I2C peripheral e.g. i2c_dev
 *		data --> the data byte to be transmitted
 */
void I2C_write(I2C_TypeDef* I2Cx, uint8_t data)
{
	I2C_SendData(I2Cx, data);
	USART1_puts("i2c send!\r\n");
	// wait for i2c_dev EV8_2 --> byte has been transmitted
//	while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
//	Delay(100);
}
コード例 #5
0
ファイル: i2c_ops.c プロジェクト: iGaryGu/Painter
/* This function reads one byte from the slave device
 * and doesn't acknowledge the received data
 */
uint8_t I2C_read_nack(I2C_TypeDef* I2Cx){
	uint8_t data;
	// disable acknowledge of received data
	I2C_AcknowledgeConfig(I2Cx, DISABLE);
	// wait until one byte has been received
	while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED) );
//	Delay(100);
	// read data from I2C data register and return data byte
	data = I2C_ReceiveData(I2Cx);
	USART1_puts("i2c received!\r\n");
	return data;
}
コード例 #6
0
ファイル: main.c プロジェクト: numkang/STM32F429_Training
int main(void)
{
  
    RCC_Configuration();
    LED_Initialization();     //PG13 (GREEN) & PG14 (RED)
    USART1_Initialization();  //PA9 (TX) & PA10 (RX)
    SPI1_Initialization();    //PB3 (SCK) & PB4 (MISO) & PB5 (MOSI)
    Delay_1us(1000000);
    NRF2401_Initialization();
    Timer2_Initialization();

    // LED3_On();
    USART1_puts("\r\nHello World\r\n");
    // Delay_1us(500000);
    // LED3_Off();
    // Delay_1us(500000);    

    while(1)
    {
      if(task == 1){
        LED4_Toggle();

        /* NRF2401 TX Mode */
        NRF2401_SendData_1CH("KU");

        /* NRF2401 RX Mode */
        // uint8_t i;
        // for(i = 0; i < 24; i++){
        //   receivedData = NRF2401_ReceiveData_1CH()[i];
        //   USART_SendData(USART1, receivedData);
        // }
        // USART1_puts("\r\n");

        // NRF2401_ReceiveData_1CH();
        task = 0;
      }
    }

    while(1); // Don't want to exit
}
コード例 #7
0
ファイル: game.c プロジェクト: colin8930/freertos-stm32
int
UART_EventHandler1()  //for game
{
	USART_SendData(USART1, '1');
        while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
        char t = USART_ReceiveData(USART1);

	arrowKey=t;
	USART_SendData(USART1, t);
	portBASE_TYPE status;
	if(arrowKey=='p')  {		
		status = xQueueSendToBack(t_queue, &states[GAME_PAUSE],0);	
		if ( status != pdPASS)	USART1_puts("queue error");		
 	}

	if(arrowKey=='r')  {		
		status = xQueueSendToBack(t_queue, &states[GAME_RESUME],0);	
		if ( status != pdPASS)	USART1_puts("queue error");		
 	}

	if(arrowKey==ESC)  {		
		status = xQueueSendToBack(t_queue, &states[GAME_STOP],0);		
		if ( status != pdPASS)	USART1_puts("queue error");			
 	}

       if ((t == '\r')) {
       		while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
      		USART_SendData(USART1, t);
		arrowKey=t;
		if(arrowKey=='p')  {			
			status = xQueueSendToBack(t_queue, &states[GAME_PAUSE],0);	
			if ( status != pdPASS)	USART1_puts("queue error");				
		}

		if(arrowKey=='r')  {		
		status = xQueueSendToBack(t_queue, &states[GAME_RESUME],0);	
		if ( status != pdPASS)	USART1_puts("queue error");
 		}

		if(arrowKey==ESC)  {			
			status = xQueueSendToBack(t_queue, &states[GAME_STOP],0);	
			if ( status != pdPASS)	USART1_puts("queue error");		
 		}
 		
     }
	t = '\n';
}
コード例 #8
0
ファイル: main.c プロジェクト: numkang/STM32F429_Training
void USART1_Configuration(void)
{
    USART_InitTypeDef USART_InitStructure;

    /* USARTx configuration ------------------------------------------------------*/
    /* USARTx configured as follow:
     *  - BaudRate = 57600 baud
     *  - Word Length = 8 Bits
     *  - One Stop Bit
     *  - No parity
     *  - Hardware flow control disabled (RTS and CTS signals)
     *  - Receive and transmit enabled
     */
    USART_InitStructure.USART_BaudRate = 57600;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    //START USART
    USART_Init(USART1, &USART_InitStructure);
    USART_Cmd(USART1, ENABLE);

    //Using Interrupt
    USART_ClearFlag(USART1, USART_FLAG_TC);

    //Enable RX interrupt & Disable TX interrupt
    USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

    /* NVIC Initialization */
    NVIC_InitTypeDef NVIC_InitStruct = {
      .NVIC_IRQChannel = USART1_IRQn,
      .NVIC_IRQChannelPreemptionPriority = 0,
      .NVIC_IRQChannelSubPriority = 0,
      .NVIC_IRQChannelCmd = ENABLE
    };
    NVIC_Init(&NVIC_InitStruct);

}

void USART1_puts(char* s)
{
    while(*s) {
        while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
        USART_SendData(USART1, *s);
        s++;
    }
}

/**************************************************************************************/
int main(void)
{
    RCC_Configuration();
    GPIO_Configuration();
    USART1_Configuration();
    LED_Initialization();
    USART1_puts("Hello World!\r\n");
    USART1_puts("Just for STM32F429I Discovery verify USART1 with USB TTL Cable\r\n");

    while(1)
    {
        //LED4_Toggle();

        //Delay_1us(10000);
        //USART1_puts(i);        
    }
}
コード例 #9
0
void MPU6050Task(void) {
	char uart_out[32];
	uint8_t controller_command = 0;
	uint8_t pre_command = 0;
	uint8_t count = 0;

	MPU6050_Task_Suspend();
	vTaskDelayUntil(&xLastWakeTime, xFrequency); // wait while sensor is ready

	initKalman(&kalmanX);
	initKalman(&kalmanY);
	MPU6050_ReadAccelerometer();

	accX = MPU6050_Data.Accelerometer_X;
	accY = MPU6050_Data.Accelerometer_Y;
	accZ = MPU6050_Data.Accelerometer_Z;

	float roll = atan2(-accY, -accZ) * RAD_TO_DEG;
	float pitch = atan(-accX / sqrt1(Square(accY) + Square(accZ))) * RAD_TO_DEG;

	setAngle(&kalmanX, roll); // Set starting angle
	setAngle(&kalmanY, pitch);

	while (1) {

		/* Read all data from sensor */
		MPU6050_ReadAccGyo();

		accX = MPU6050_Data.Accelerometer_X;
		accY = MPU6050_Data.Accelerometer_Y;
		accZ = MPU6050_Data.Accelerometer_Z;
		gyroX = MPU6050_Data.Gyroscope_X;
		gyroY = MPU6050_Data.Gyroscope_Y;
		gyroZ = MPU6050_Data.Gyroscope_Z;

		float roll = atan2(-accY, -accZ) * RAD_TO_DEG;
		float pitch = atan(-accX / sqrt1(Square(accY) + Square(accZ))) * RAD_TO_DEG;

		float gyroXrate = gyroX * MPU6050_Data.Gyro_Mult; // Convert to deg/s
		float gyroYrate = gyroY * MPU6050_Data.Gyro_Mult; // Convert to deg/s

		// This fixes the transition problem when the accelerometer angle jumps between -180 and 180 degrees
		if ((roll < -90 && kalAngleX > 90) || (roll > 90 && kalAngleX < -90)) {
			setAngle(&kalmanX, roll);
		} else {
			kalAngleX = getAngle(&kalmanX, roll, gyroXrate, dt); // Calculate the angle using a Kalman filter
		}

		if (Abs(kalAngleX) > 90)
			gyroYrate = -gyroYrate; // Invert rate, so it fits the restriced accelerometer reading
		kalAngleY = getAngle(&kalmanY, pitch, gyroYrate, dt);

#ifdef DEBUG
		USART1_puts("\r\n");
		shell_float2str(accZ, uart_out);
		USART1_puts(uart_out);
#else
		if (accY < -6300) {
			controller_command = 1;
		} else if (accY > 6300) {
			controller_command = 2;
		} else if (accZ < 0 && kalAngleY > 47) {
			controller_command = 3;
		} else if (accZ < 0 && kalAngleY < -30) {
			controller_command = 4;
		} else if (accZ < 0 && kalAngleY > 25 && kalAngleY < 47) {
			controller_command = 5;
		} else {
			controller_command = 6;
		}

		// check hand gesture for a while
		if (count == 5 && pre_command == controller_command) {
			switch (controller_command) {
			case 1:
				USART1_puts("\r\nmove right");
				break;
			case 2:
				USART1_puts("\r\nmove left");
				break;
			case 3:
				USART1_puts("\r\nforward");
				break;
			case 4:
				USART1_puts("\r\nDOWN");
				break;
			case 5:
				USART1_puts("\r\nUP");
				break;
			case 6:
				USART1_puts("\r\nsuspend");
				break;
			}
		} else if (pre_command == controller_command) {
			count++;
		} else {
			pre_command = controller_command;
			count = 0;
		}
#endif

		vTaskDelayUntil(&xLastWakeTime, xFrequency);
	}
}