/** * @brief Configure UART1 for the communication with HyperTerminal * @param None * @retval None */ void UART1_Config(void) { /* EVAL COM (UART) configuration -----------------------------------------*/ /* USART configured as follow: - BaudRate = 115200 baud - Word Length = 8 Bits - One Stop Bit - Odd parity - Receive and transmit enabled - UART Clock disabled */ UART1_Init((uint32_t)115200, UART1_WORDLENGTH_8D,UART1_STOPBITS_1, UART1_PARITY_NO, UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE); /* Enable the UART Receive interrupt: this interrupt is generated when the UART receive data register is not empty */ //UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE); /* Enable the UART Transmit complete interrupt: this interrupt is generated when the UART transmit Shift Register is empty */ UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE); /* Enable UART */ UART1_Cmd(ENABLE); /* Enable general interrupts */ // enableInterrupts(); }
/********************************************** UART1 configured as follow: - BaudRate = 115200 baud - Word Length = 8 Bits - One Stop Bit - No parity - Receive and transmit enabled - Receive interrupt - UART1 Clock disabled *********************************************/ void Uart_Init(void) { UART1_DeInit(); UART1_Init((u32)9600, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, \ UART1_PARITY_NO , UART1_SYNCMODE_CLOCK_DISABLE , UART1_MODE_TXRX_ENABLE); UART1_ITConfig(UART1_IT_RXNE_OR,ENABLE ); UART1_Cmd(ENABLE ); }
/******************************************************************************* * 名称: Uart_Init * 功能: UART1初始化操作 * 形参: 无 * 返回: 无 * 说明: 无 ******************************************************************************/ void Uart1_Init(void) { UART1_DeInit(); /* 将寄存器的值复位 */ /* * 将UART1配置为: * 波特率 = 115200 * 数据位 = 8 * 1位停止位 * 无校验位 * 使能接收和发送 * 使能接收中断 */ UART1_Init((u32)115200, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, \ UART1_PARITY_NO , UART1_SYNCMODE_CLOCK_DISABLE , UART1_MODE_TXRX_ENABLE); UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE); UART1_Cmd(ENABLE); }
/** * @brief Main program. * @param None * @retval None */ void main(void) { /*High speed internal clock prescaler: 1*/ CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); /* UART1 configuration -------------------------------------------------------*/ /* UART1 configured as follow: - Word Length = 8 Bits - 1 Stop Bit - No parity - BaudRate = 9600 baud - UART1 Clock enabled - Polarity Low - Phase Middle - Last Bit enabled - Receive and transmit enabled */ UART1_DeInit(); UART1_Init((uint32_t)9600, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO, (UART1_SyncMode_TypeDef)(UART1_SYNCMODE_CLOCK_ENABLE | UART1_SYNCMODE_CPOL_LOW |UART1_SYNCMODE_CPHA_MIDDLE |UART1_SYNCMODE_LASTBIT_ENABLE), UART1_MODE_TXRX_ENABLE); UART1_Cmd(DISABLE); /* SPI configuration */ SPI_DeInit(); /* Initialize SPI in Slave mode */ SPI_Init(SPI_FIRSTBIT_LSB, SPI_BAUDRATEPRESCALER_2, SPI_MODE_SLAVE, SPI_CLOCKPOLARITY_LOW, SPI_CLOCKPHASE_1EDGE, SPI_DATADIRECTION_2LINES_FULLDUPLEX, SPI_NSS_SOFT,(uint8_t)0x07); /* Enable the UART1*/ UART1_Cmd(ENABLE); Delay(0xFFF); /* Enable the SPI*/ SPI_Cmd(ENABLE); while (NbrOfDataToRead--) { /* Wait until end of transmit */ while (SPI_GetFlagStatus(SPI_FLAG_TXE)== RESET) { } /* Write one byte in the SPI Transmit Data Register */ SPI_SendData(TxBuffer2[TxCounter]); /* Write one byte in the UART1 Transmit Data Register */ UART1_SendData8(TxBuffer1[TxCounter++]); /* Wait until end of transmit */ while (UART1_GetFlagStatus(UART1_FLAG_TXE) == RESET) { } /* Wait the byte is entirely received by UART1 */ while (UART1_GetFlagStatus(UART1_FLAG_RXNE) == RESET) { } /* Store the received byte in the RxBuffer1 */ RxBuffer1[RxCounter] = UART1_ReceiveData8(); /* Wait the byte is entirely received by SPI */ while (SPI_GetFlagStatus(SPI_FLAG_RXNE) == RESET) { } /* Store the received byte in the RxBuffer2 */ RxBuffer2[RxCounter++] = SPI_ReceiveData(); } /* Check the received data with the sent ones */ TransferStatus1 = Buffercmp(TxBuffer1, RxBuffer2, TxBufferSize1); /* TransferStatus = PASSED, if the data transmitted from UART1 and received by SPI are the same */ /* TransferStatus = FAILED, if the data transmitted from UART1 and received by SPI are different */ TransferStatus2 = Buffercmp(TxBuffer2, RxBuffer1, TxBufferSize2); /* TransferStatus = PASSED, if the data transmitted from SPI and received by UART1 are the same */ /* TransferStatus = FAILED, if the data transmitted from SPI and received by UART11 are different */ while (1); }
int main(void) { int i; RCC_Configuration(); //????????????? NVIC_Configuration(); LED_Config(); TIM7_Configuration(10) ; RCC_Config(); ADC_initial(); UART1_Init(); UART1_Config(9600); UART1_Cmd(ENABLE); UART1_Write("stm start",9); LCD_GLASS_Configure_GPIO(); LCD_GLASS_Init(); while (1) { int wdt=0; int adc_wdt=0; double adc=0; //// solution 2 working String i=0; memcpy(buff2,buff, strlen(buff)); // ? buff ??? buff2 memset(buff, 0, strlen(buff)); // ?? buff ??????? while(1) { if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE) != RESET) { char c = USART_ReceiveData(USART1); i=i+1; if(c == '\r') break; else if (c == '\n') break; else sprintf (buff, "%s%c", buff,c); }else { wdt++; adc_wdt++; if(adc_wdt%100==0) { adc=(adc*99.0+GetADC())/100.0; } if(adc_wdt>10000) { adc_wdt=0; LCDPrint(" %0.1f ",adc); } if(wdt==50) { wdt=0; for(i=0;i<4;i++) { if(flag[i]==0) { LED[i]++; if(LED[i]>300) { flag[i]=1; } }else { LED[i]--; if(LED[i]==0) { flag[i]=0; } } } } } } /* strcat(buff,"\n"); UART1_Write(buff, strlen(buff)); Lcd_print(buff); */ // UART1_Write(")",1); USART_ClearFlag(USART1, USART_FLAG_RXNE); } }