int main(void) { UART_Init(); UART_SendStr("\nSTM32F103RET6 is online.\n"); UART_SendStr("I2C init ... "); if (!LM75_Init(100000)) UART_SendStr("ready.\n"); else { UART_SendStr("fail.\n"); UART_SendStr("MCU halted now.\n"); while(1); } uint16_t value; value = LM75_ReadReg(0x00); UART_SendHex16(value); UART_SendChar('\n'); value = LM75_ReadConf(); UART_SendHex8(value); UART_SendChar('\n'); value = LM75_ReadReg(0x02); UART_SendHex16(value); UART_SendChar('\n'); value = LM75_ReadReg(0x03); UART_SendHex16(value); UART_SendChar('\n'); LM75_Shutdown(DISABLE); int16_t temp = LM75_Temperature(); UART_SendInt(temp / 10); UART_SendChar('.'); temp %= 10; if (temp < 0) temp *= -1; UART_SendInt(temp % 10); UART_SendStr("C\n"); while(1); }
/* ************************************************** POST报文发送函数 当查找到有效协议帧时调用 注POST http://hailandev.sinaapp.com/gps/api.php?action=setlocation是地图的地址,另一个是之前post测试的,记得换回来 ************************************************** */ void Send_PostMessage(Datapack* pack,char* lat1,char* lon1) { int i = 0; char num[10]; Packlen_Tostring(pack,num); //latUnit_change(pack); //lonUnit_change(pack); sendChars("AT+CIPSTART=\"TCP\",\"hailandev.sinaapp.com\",80\r\n"); sendChars(Host); sendChars("\",80\r\n"); delay_ms(1000); sendChars("AT+CIPSEND\r\n"); delay_ms(1000); sendChars("POST "); sendChars(Postadd); sendChars("HTTP/1.1\r\n"); sendChars("Referer: http://hailandev.sinaapp.com\r\n"); sendChars(Host); sendChars("\r\n"); sendChars("Accept:*/*\r\n"); sendChars("Accept-Language: zh-cn,en-us;q=0.5\r\n"); sendChars("Content-Type:text/json\r\n"); sendChars("User-Agent:YunShen1.0\r\n"); sendChars("Host: hailandev.sinaapp.com\r\n"); sendChars(Host); sendChars("\r\n"); sendChars("Content-Length: "); while(num[i]!='\0') { UART_SendChar(num[i++]); } sendChars("\r\n\r\n"); SendCoordinate(pack); /*sendChars("{\"longitude\":\""); for(i=0;i<11;i++) { UART_SendChar(lon1[i]); } sendChars("\",\"latitude\":\""); for(i=0;i<10;i++) { UART_SendChar(lat1[i]); } sendChars("\"}");*/ sendChars("\r\n\r\n"); UART_SendChar(0x1a); pack->countlat = 0; pack->countlon = 0; pack->countUTC = 0; pack->status = 'V'; setCharsOLED("OK!",1,6); }
void UART_SendInt(int32_t num) { char str[10]; // 10 chars max for INT32_MAX int i = 0; if (num < 0) { UART_SendChar('-'); num *= -1; } do str[i++] = num % 10 + '0'; while ((num /= 10) > 0); for (i--; i >= 0; i--) UART_SendChar(str[i]); }
/** * This task will send any response data out of the UART. * * This should be called in the UART_OnTxChar event. Internally, the method checks to make sure the buffer * is empty and that the buffer is ready to send, so this method can also be called in the main application * loop. * * This method will automagically wiggle the TXEN line when necessary. */ void Command_ResponseTask() { if(ResponseReadyToSend == FALSE) return; // if(UART_PDD_GetTxCompleteStatus(UART1_BASE_PTR) != 0x40u) // we're waiting for a transmit to complete already // return; if(UART_GetCharsInTxBuf() > 0) return; // we're waiting for a transmit to complete already if(ResponseIndex == 0) { TXEN_SetVal(); for(int i=0;i<10000;i++); } // We have to wait a bit before we send our first byte. UART_SendChar(ResponseBuffer[ResponseIndex++]); if(ResponseIndex == COMMAND_RESPONSE_BUFFER_SIZE) // we've sent everything { // while(!(UART_PDD_ReadStatus1Flags(UART1_BASE_PTR) && UART_S1_TC_MASK) ) while(UART_PDD_GetTxCompleteStatus(UART1_BASE_PTR) == 0U) // while(UART_GetCharsInTxBuf() > 0 ); // for(int i=0;i<10000;i++); ResponseIndex = 0; ResponseReadyToSend = FALSE; TXEN_ClrVal(); LED_GREEN_SetVal(); } }
/** * puts() is used by printf() to display or send a string.. This function * determines where printf prints to. For this case it sends a string * out over UART, another option could be to display the string on an * LCD display. **/ void puts(uint8 * p_ucBuff) { char c; // Loops through each character in string 's' while (c = *p_ucBuff++) { UART_SendChar(c); } }
/** @brief wrapper function - if a different debug interface is used, just change it here. @todo where to select the hw transfer interface? *******************************************************************************/ void DEBUG_SendMsg(char *pcString) { int i = 0; // loop through until reach string's zero terminator while (pcString[i] != 0) { UART_SendChar(pcString[i]); // print each character i++; } }
void sendString(char *str) { size_t i, len; //size_t is an unsigned integer len = strlen(str); // count the number of characters in a string byte err; for (i = 0; i < len; i++) { do { err = UART_SendChar(str[i]); } while(err != ERR_OK); } }
/* ************************************************** 经纬度发送函数 ************************************************** */ static void SendCoordinate(Datapack* pack)//注这是个奇葩问题 在我的板子上这里一定要USART1,具体原因找不出 { int i; sendChars("{\"longitude\":\""); //sendChars("{\"longitude\":\""); for(i=0; i<pack->countlon; i++) { //UART_SendChar(uarttemp) UART_SendChar(pack->longitude[i]); //UART_SendChar(pack->longitude[i]); } sendChars("\",\"latitude\":\""); //sendChars("\",\"latitude\":\""); for(i=0; i<pack->countlat; i++) { //printf("%c",pack->longitude[i]); UART_SendChar(pack->latitude[i]); //UART_SendChar(pack->latitude[i]); } sendChars("\"}"); //sendChars("\"}"); }
void EXTI9_5_IRQHandler(void) { if (EXTI->PR & INT1_EXTI_LINE) { i8 = BMC050_ACC_GetTSIRQ(); // Get IRQ source BMC050_ACC_GetXYZ(&X,&Y,&Z); // Get last accelerometer readings BMC050_ACC_SetIRQMode(ACC_IM_RESET); // Reset all latched interrupts UART_SendStr(USART2,"Slope="); UART_SendHex8(USART2,i8); UART_SendChar(USART2,' '); if (i8 & ACC_TS_SLOPEZ) UART_SendStr(USART2,"SLOPEZ "); if (i8 & ACC_TS_SLOPEY) UART_SendStr(USART2,"SLOPEY "); if (i8 & ACC_TS_SLOPEX) UART_SendStr(USART2,"SLOPEX "); UART_SendStr(USART2," X="); UART_SendInt(USART2,X); UART_SendStr(USART2," Y="); UART_SendInt(USART2,Y); UART_SendStr(USART2," Z="); UART_SendInt(USART2,Z); UART_SendChar(USART2,'\n'); EXTI->PR = INT1_EXTI_LINE; // Clear IT bit for EXTI line } }
/* ************************************************** 发送数据包计算转换函数 计算一次http连接后发送所需数据长度并转化为字符串型 ************************************************** */ static void Packlen_Tostring(Datapack* pack,char* str) { uint8_t num = 0,i = 0,len; char temp; num = pack->countlat + pack->countlon + strlen("longitude:latitude:,")+10;//+10为8和'"',和'{','}' UART_SendChar(pack->countlat+'0'); Dis_fnum(pack->countlat,1,5); while(num!=0) { str[i++] = num%10+'0'; num = num/10; } str[i]='\0'; len = strlen(str); for(i=0; i<len/2; i++) { temp = str[i]; str[i] = str[len-i-1]; str[len-i-1] = temp; } }
void UART_SendInt(uint32_t num) { char str[10]; // 10 chars max for UINT32_MAX int i = 0; do str[i++] = num % 10 + '0'; while ((num /= 10) > 0); for (i--; i >= 0; i--) UART_SendChar(str[i]); }
/** * puts() is used by printf() to display or send a character. This function * determines where printf prints to. For this case it sends a character * out over UART. **/ void putc(uint8 ucChar) { UART_SendChar(ucChar); }
void UART_CONSOLE_Send_Char (char value) { UART_SendChar(UART_CONSOLE_NUMBER, value); }
void UART_SendChars(char ch, uint8_t count) { uint8_t i; for (i = 0; i < count; i++) UART_SendChar(ch); }
int main(void) { UART_Init(); UART_SendStr("\nSTM32F103RET6 is online.\n"); SD_Init(); uint8_t b = 0; UART_SendStr("SD_CardInit: "); b = SD_CardInit(); UART_SendHex8(b); UART_SendStr("\nCard version: "); UART_SendHex8(SD_CardType); uint8_t response = 0; UART_SendStr("\nCSD: "); response = SD_Read_CSD(); if (response != 0x00) { UART_SendStr("error = "); UART_SendHex8(response); } else UART_SendBufHex((char*)&SD_CSD[0],16); UART_SendStr("\nCID: "); response = SD_Read_CID(); if (response != 0x00) { UART_SendStr("error = "); UART_SendHex8(response); } else UART_SendBufHex((char*)&SD_CID[0],16); UART_SendStr("\nMax bus clk.: "); UART_SendHex32(SD_MaxBusClkFreq); UART_SendStr("\nDevice size: "); UART_SendInt(SD_CardCapacity >> 10); UART_SendStr("Mb\n"); response = SD_Read_Block(0); if (response != 0x00) { UART_SendStr("error = "); UART_SendHex8(response); } else { UART_SendBufHexFancy((char*)&SD_sector[0],512,32,'.'); UART_SendStr("CRC16: "); UART_SendHex16(SD_CRC16_rcv); UART_SendStr(" == "); UART_SendHex16(SD_CRC16_cmp); } UART_SendChar('\n'); response = SD_Read_Block(1); if (response != 0x00) { UART_SendStr("error = "); UART_SendHex8(response); } else { UART_SendBufHexFancy((char*)&SD_sector[0],512,32,'.'); UART_SendStr("CRC16: "); UART_SendHex16(SD_CRC16_rcv); UART_SendStr(" == "); UART_SendHex16(SD_CRC16_cmp); } UART_SendChar('\n'); while(1); }
int main(void) { Delay_Init((void *)0); UARTx_Init(USART2,1382400); UART_SendStr(USART2,"--------------------------------------\n"); if (I2Cx_Init(BMC050_I2C_PORT,400000) != I2C_SUCCESS) { UART_SendStr(USART2,"I2C2 init fail\n"); while(1); } UART_SendStr(USART2,"I2C2 init at 400kHz\n"); // Enable PORTA peripheral RCC->AHBENR |= RCC_AHBENR_GPIOAEN; // Configure PA6 as external interrupt GPIOA->MODER &= ~GPIO_MODER_MODER6; // Input mode (reset state) GPIOA->PUPDR &= ~GPIO_PUPDR_PUPDR6; // No pull-up, pull-down GPIOA->PUPDR |= GPIO_PUPDR_PUPDR6_1; // Pull-down // Configure priority group: 4 bits for preemption priority, 0 bits for subpriority. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); // Enable the SYSCFG module clock RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; // PA6 -> EXTI line 6 (INT1 from BMC050) EXTI->PR = INT1_EXTI_LINE; // Clear IT pending bit for EXTI line EXTI->IMR |= INT1_EXTI_LINE; // Enable interrupt request from EXTI line EXTI->EMR &= ~INT1_EXTI_LINE; // Disable event on EXTI line EXTI->RTSR |= INT1_EXTI_LINE; // Trigger rising edge enabled EXTI->FTSR &= ~INT1_EXTI_LINE; // Trigger falling edge disabled // Enable the USB interrupt NVICInit.NVIC_IRQChannel = EXTI9_5_IRQn; NVICInit.NVIC_IRQChannelPreemptionPriority = 2; NVICInit.NVIC_IRQChannelSubPriority = 0; NVICInit.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVICInit); ///* uint8_t buf[2]; uint8_t reg; uint8_t val; uint32_t i; //*/ ///* reg = 0x00; val = 0; UART_SendStr(USART2,"\nAccelerometer\n"); for (reg = 0; reg <= 0x3f; reg++) { I2Cx_Write(BMC050_I2C_PORT,®,1,0x18 << 1,I2C_NOSTOP); I2Cx_Read(BMC050_I2C_PORT,&val,1,0x18 << 1); UART_SendStr(USART2,"R"); UART_SendHex8(USART2,reg); UART_SendStr(USART2," = "); UART_SendHex8(USART2,val); UART_SendStr(USART2,"\t\t"); } UART_SendChar(USART2,'\n'); //*/ /* UART_SendStr(USART2,"\nMagnetometer\n"); // Magnetometer power enable buf[0] = 0x4b; buf[1] = 0x01; // Set power control bit I2Cx_Write(BMC050_I2C_PORT,&buf[0],2,0x10 << 1,I2C_STOP); for (reg = 0x40; reg <= 0x52; reg++) { I2Cx_Write(BMC050_I2C_PORT,®,1,0x10 << 1,I2C_NOSTOP); I2Cx_Read(BMC050_I2C_PORT,&val,1,0x10 << 1); UART_SendStr(USART2,"R"); UART_SendHex8(USART2,reg); UART_SendStr(USART2," = "); UART_SendHex8(USART2,val); UART_SendStr(USART2,"\t\t"); } UART_SendStr(USART2,"\n========================================\n"); */ BMC050_ACC_SoftReset(); Delay_ms(5); // must wait for start-up time of accelerometer (2ms) BMC050_Init(); // Enable I2C watchdog timer with 50ms BMC050_ACC_InterfaceConfig(ACC_IF_WDT_50ms); UART_SendStr(USART2,"BMC050 ACC device ID: "); UART_SendHex8(USART2,BMC050_ACC_GetDeviceID()); UART_SendChar(USART2,'\n'); UART_SendStr(USART2,"BMC050 MAG device ID: "); UART_SendHex8(USART2,BMC050_MAG_GetDeviceID()); UART_SendChar(USART2,'\n'); UART_SendStr(USART2,"BMC050 temperature: "); temp = BMC050_ReadTemp(); UART_SendInt(USART2,temp / 10); UART_SendChar(USART2,'.'); UART_SendInt(USART2,temp % 10); UART_SendStr(USART2,"C\n"); BMC050_ACC_SetBandwidth(ACC_BW8); // Accelerometer readings filtering (lower or higher better?) BMC050_ACC_SetIRQMode(ACC_IM_NOLATCH); // No IRQ latching BMC050_ACC_ConfigSlopeIRQ(0,16); // Motion detection sensitivity BMC050_ACC_IntPinMap(ACC_IM1_SLOPE); // Map slope interrupt to INT1 pin BMC050_ACC_SetIRQ(ACC_IE_SLOPEX | ACC_IE_SLOPEY | ACC_IE_SLOPEZ); // Detect motion by all axes BMC050_ACC_LowPower(ACC_SLEEP_100); // Low power with sleep duration 0.1s // BMC050_ACC_Suspend(); while(1); /* while(1) { while (!BMC050_ACC_GetIRQStatus()); // Wait for new data from accelerometer i8 = BMC050_ACC_GetTSIRQ(); BMC050_ACC_GetXYZ(&X,&Y,&Z); UART_SendStr(USART2,"Slope="); UART_SendHex8(USART2,i8); UART_SendChar(USART2,' '); if (i8 & ACC_TS_SLOPEZ) UART_SendStr(USART2,"SLOPEZ "); if (i8 & ACC_TS_SLOPEY) UART_SendStr(USART2,"SLOPEY "); if (i8 & ACC_TS_SLOPEX) UART_SendStr(USART2,"SLOPEX "); UART_SendStr(USART2," X="); UART_SendInt(USART2,X); UART_SendStr(USART2," Y="); UART_SendInt(USART2,Y); UART_SendStr(USART2," Z="); UART_SendInt(USART2,Z); UART_SendChar(USART2,'\n'); BMC050_ACC_SetIRQMode(ACC_IM_RESET); Delay_ms(100); } */ }