最近開始學
STM32 想實驗STM32串口發送到PC機
源碼如下 會循環發送 12 34 到PC機(PC機也是9600)
實驗的結果是PC機一直顯示 60 F8
不知為何會如此? 我的程序有缺少什麼嗎?
謝謝
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOAtiNG;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- USART_InitStructure.USART_BaudRate = 9600;
- 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;
- USART_Init(USART1, &USART_InitStructure);
- USART_Cmd(USART1, ENABLE);
-
- USART_ClearFlag(USART1,USART_FLAG_TC);
-
- while(1)
- {
- USART1->DR = 0x1234;
- while( USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET );
- Delay(0xfffff);
- Delay(0xfffff);
- }
复制代码
上網查了一下
- #define HSE_VALUE ((uint32_t)8000000)
复制代码這部份是對的
- float temp;
- u16 mantissa;
- u16 fraction;
- temp=(float)(pclk2*1000000)/(bound*16);//得到USARTDIV
- mantissa=temp; //得到整数部分
- fraction=(temp-mantissa)*16; //得到小数部分
- mantissa<<=4;
- mantissa+=fraction;
- USART1->BRR=mantissa; // 波特率设置
复制代码上面的部份還看得懂
所以再檢查官方的原碼內容 stm32f10x_usart.c是否一樣
- /* Determine the integer part */
- if ((USARTx->CR1 & CR1_OVER8_Set) != 0)
- {
- /* Integer part computing in case Oversampling mode is 8 Samples */
- integerdivider = ((25 * apbclock) / (2 * (USART_InitStruct->USART_BaudRate)));
- }
- else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
- {
- /* Integer part computing in case Oversampling mode is 16 Samples */
- integerdivider = ((25 * apbclock) / (4 * (USART_InitStruct->USART_BaudRate)));
- }
- tmpreg = (integerdivider / 100) << 4;
- /* Determine the fractional part */
- fractionaldivider = integerdivider - (100 * (tmpreg >> 4));
- /* Implement the fractional part in the register */
- if ((USARTx->CR1 & CR1_OVER8_Set) != 0)
- {
- tmpreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07);
- }
- else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
- {
- tmpreg |= ((((fractionaldivider * 16) + 50) / 100)) & ((uint8_t)0x0F);
- }
-
- /* Write to USART BRR */
- USARTx->BRR = (uint16_t)tmpreg;
复制代码看來並不一樣
這個就看不太懂了
用apbclock乘上25再除以兩倍的USART_BaudRate........
它跟上面的程式會得到相同的USARTx->BRR ?
今天晚上回家再試試看
STM32F10X_HD_VL
改成
STM32F10X_HD
就行了
會從24M改回72M
之前不知為何設成value line
一周热门 更多>