void lab08_USARTsimpleReceive(void) { SystemInit(); MF_USART_init(); for(;;) { usartPutChar('\n'); usartPutChar('p'); usartPutChar('r'); usartPutChar('e'); usartPutChar('s'); usartPutChar('s'); usartPutChar(' '); usartPutChar('a'); usartPutChar('n'); usartPutChar('y'); usartPutChar(' '); usartPutChar('k'); usartPutChar('e'); usartPutChar('y'); usartPutChar(' '); uint8_t dataFromUser = usartGetChar(); usartPutChar(dataFromUser); } }
/***************************************************************************//** * @brief * Flush uart buffer, Always use this when switching between tx/rx operation. * ******************************************************************************/ void usartFlushBuffer(void) { uint8_t byte; /* Flush buffer first to make sure we don't have stale data when switching between RX/TX. */ while(usartGetChar(&byte) != UART_STATUS_NOT_ENOUGH_BYTES_RECEIVED); }
int main (void) { char textBuffer[50]; uint8_t batReqs=0; uint8_t heartbeats=0; uint8_t statusFlag=0; uint8_t gpsFlag=0; uint8_t batFlag=0; setCurBuf(0); setPwrBuf(0,0); setAltBuf(0); setAirBuf(0); setGPSBuf(0,0,0,0,0,0); TWISlaveInit(); #ifdef TIMEIT TimerInit(); #endif mavlink_system.sysid = 1; mavlink_system.compid = 50; USART_Init(); mavlink_message_t msg; mavlink_status_t status; while(1) { uint8_t byte=usartGetChar(); if(mavlink_parse_char(MAVLINK_COMM_0, byte,&msg,&status)) { switch(msg.msgid) { case MAVLINK_MSG_ID_HEARTBEAT: heartbeats++; for (int n=1;n<10;n++) PORTB ^= (1<<PB5); // After a couple of heartbeats, try up to 3 times to get the battery capacity if((heartbeats>2)&&(!batFlag)&&(batReqs<3)) { mavlink_msg_param_request_read_send(MAVLINK_COMM_0,0x01,0x00,capName,-1); batReqs++; } // if you go 2 heartbeats without getting the status or GPS message, re-request the telemetry stream if(((!statusFlag)||(!gpsFlag))&&(heartbeats>2)) { mavlink_msg_request_data_stream_send(MAVLINK_COMM_0, 0x01, 0x00, 0x01, 0x200, 0x01); heartbeats=0; } else { statusFlag=0; gpsFlag=0; } break; case MAVLINK_MSG_ID_PARAM_VALUE: mavlink_msg_param_value_get_param_id(&msg,textBuffer); float value=mavlink_msg_param_value_get_param_value(&msg); if(strncmp(textBuffer,capName,16)==0) { batteryCapacity=(uint16_t)value; batFlag=1; } break; case MAVLINK_MSG_ID_SYS_STATUS: setPwrBuf(mavlink_msg_sys_status_get_voltage_battery(&msg),mavlink_msg_sys_status_get_battery_remaining(&msg)); setCurBuf(mavlink_msg_sys_status_get_current_battery(&msg)); statusFlag=1; break; case MAVLINK_MSG_ID_GPS_RAW_INT: setAltBuf(mavlink_msg_gps_raw_int_get_alt(&msg)); #ifdef TIMEIT TCNT1=0; #endif setGPSBuf(mavlink_msg_gps_raw_int_get_lat(&msg),mavlink_msg_gps_raw_int_get_lon(&msg),mavlink_msg_gps_raw_int_get_alt(&msg), mavlink_msg_gps_raw_int_get_vel(&msg),mavlink_msg_gps_raw_int_get_cog(&msg), mavlink_msg_gps_raw_int_get_satellites_visible(&msg)); #ifdef TIMEIT timeVal=TCNT1; #endif setAirBuf(mavlink_msg_gps_raw_int_get_vel(&msg)); gpsFlag=1; break; case MAVLINK_MSG_ID_STATUSTEXT: mavlink_msg_statustext_get_text(&msg,textBuffer); // Set status for Powerbox when the low battery message is received if(strncmp(textBuffer,lowBattery,strlen(lowBattery))==0) alarm |= (1<<LOW_BATTERY); break; default: break; } } } }
int main(void) { inir(); intic(); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15 | GPIO_Pin_14 | GPIO_Pin_13 | GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOD, &GPIO_InitStructure); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 /*| GPIO_Pin_2 | GPIO_Pin_3*/; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); stop(); int a=0; char b=0; while (1) { static int count = 0; static int i; // TIM2->CCR4 = 4200; // TIM2->CCR3 = 4200; //for (i = 0; i < 10000000; ++i); //printf("%d\r\n", ++count); do{ b=usartGetChar(); printf("%d\n", b); if(b==1){ przod(); GPIO_SetBits(GPIOD, GPIO_Pin_15); GPIO_ResetBits(GPIOD, GPIO_Pin_14|GPIO_Pin_13|GPIO_Pin_12); } else if(b==2){ prawo(); GPIO_SetBits(GPIOD, GPIO_Pin_12); GPIO_ResetBits(GPIOD, GPIO_Pin_14|GPIO_Pin_13|GPIO_Pin_15); } else if(b==3){ tyl(); GPIO_SetBits(GPIOD, GPIO_Pin_13); GPIO_ResetBits(GPIOD, GPIO_Pin_14|GPIO_Pin_12|GPIO_Pin_15); } else if(b==4){ lewo(); GPIO_SetBits(GPIOD, GPIO_Pin_14); GPIO_ResetBits(GPIOD, GPIO_Pin_13|GPIO_Pin_12|GPIO_Pin_15); } else if(b==5){ stop(); GPIO_ResetBits(GPIOD, GPIO_Pin_14|GPIO_Pin_13|GPIO_Pin_12|GPIO_Pin_15); } else if(b>10&&b<22){ f_Z(b); } else{ f_V(b); } }while(USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET); } }