新手入门
- 积分
- 25
- 金钱
- 25
- 注册时间
- 2014-9-15
- 在线时间
- 0 小时
|
5金钱
为什么我的串口给PC发送0x01,不成功,接收都是00.
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void Delay(__IO uint32_t nCount);
void USART_Config(USART_TypeDef* USARTx);
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStruct;
USART_ClockInitTypeDef USART_ClockInitStruct;
/****************************************************************************
* ?? ????
* ?? ??
* ??????????
* ????????????
* ?? ?÷??
****************************************************************************/
void USART_Config(USART_TypeDef* USARTx){
USART_InitStructure.USART_BaudRate = 115200; //????115200bps
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //??????8??
USART_InitStructure.USART_StopBits = USART_StopBits_1; //??????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; //??·?????
/* Configure USART1 */
USART_Init(USARTx, &USART_InitStructure); //?????®??????????
USART1->SR&=~(1<<7);
/* Enable USART1 Receive and Transmit interrupts */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //????????????
USART_ITConfig(USART1, USART_IT_TXE, ENABLE); //????·?????????????
/* Enable the USART1 */
USART_Cmd(USART1, ENABLE);
}
/****************************************************************************
* ?? ????int main(void)
* ?? ?????÷????
* ????????????
* ????????????
* ?? ?÷??
* ?÷??·?·¨????
****************************************************************************/
int main(void)
{
RCC_Configuration(); //?????±???è??
NVIC_Configuration(); //??????????
GPIO_Configuration(); //??????????
USART_Config(USART1); //?®??1??????
USART_SendData(USART1,0x01);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
while (1);
}
/****************************************************************************
* ?? ????void Delay(__IO uint32_t nCount)
* ?? ???????±????
* ????????????
* ????????????
* ?? ?÷??
* ?÷??·?·¨????
****************************************************************************/
void Delay(__IO uint32_t nCount)
{
for(; nCount != 0; nCount--);
}
/****************************************************************************
* ?? ????void RCC_Configuration(void)
* ?? ?????????±????????72MHZ?? ???è?±??????
* ????????????
* ????????????
* ?? ?÷??
* ?÷??·?·¨????
****************************************************************************/
void RCC_Configuration(void)
{
SystemInit();
RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOC |RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_AFIO , ENABLE);
}
/****************************************************************************
* ?? ????void GPIO_Configuration(void)
* ?? ?????¨??IO??????
* ????????????
* ????????????
* ?? ?÷??
* ?÷??·?·¨??
****************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //LED3????--PB2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //???ì????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1 TX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //???????ì????
GPIO_Init(GPIOA, &GPIO_InitStructure); //A????
}
/****************************************************************************
* ?? ????void NVIC_Configuration(void)
* ?? ??????????????
* ????????????
* ????????????
* ?? ?÷??
* ?÷??·?·¨????
****************************************************************************/
void NVIC_Configuration(void)
{
/* ?á???ù?÷*/
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
/* Configure one bit for preemption priority */
/* ??????×é ???÷?????????????ù????????????×????????ù???????? ????????1?? 7 */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //?è???®??1????
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //?????????? 0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //×?????????0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //????
NVIC_Init(&NVIC_InitStructure);
}
|
最佳答案
查看完整内容[请看2#楼]
你的GPIO初始化一塌糊涂。。。
仔细看看去。。。
完全是胡乱写的。。。
void GPIO_Configuration(void)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //LED3????--PB2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //???ì????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(G ...
|