//用的是串口3,目的是想从电脑发过来7字节数据,完成后再返回
//端口配置部分,
void PBioConfig(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //éùÃ÷ò»¸ö½á11ìå±äá¿£¬óÃà′3õê¼»ˉGPIO
//SystemInit(); //ê±Öó3õê¼»ˉ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
/* ÅäÖÃGPIOμÄÄ£ê½oíIO¿ú */
GPIO_InitStructure.GPIO_Pin=BUZ; //Ñ¡ÔñÄãòaéèÖÃμÄIO¿ú
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; //éèÖÃíÆíìêä3öÄ£ê½
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //éèÖÃ′«êäËùÂê
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//TX //′®¿úêä3öPB10
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_Init(GPIOB,&GPIO_InitStructure); /* 3õê¼»ˉ′®¿úêäèëIO */
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;//RX //′®¿úêäèëPB11
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //Ä£Äaêäèë
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
void Usart1_Config() //′®¿ú1ÅäÖÃ
{
USART_InitTypeDef USART_InitStructure; //′®¿ú½á11ì嶨òå
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
USART_InitStructure.USART_BaudRate=9600; //2¨ìØÂêéèÖÃÎa9600 //2¨ìØÂê
USART_InitStructure.USART_WordLength=USART_WordLength_8b; //êy¾Y3¤8λ
USART_InitStructure.USART_StopBits=USART_StopBits_1; //1λí£Ö1λ
USART_InitStructure.USART_Parity=USART_Parity_Even; //oD§Ñé
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //ê§Äüó2¼tá÷
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx; //¿aÆô·¢Ëíoí½óêüÄ£ê½
USART_Init(USART3,&USART_InitStructure); /* 3õê¼»ˉUSART1 */
USART_Cmd(USART3, ENABLE); /* ê1ÄüUSART1 */
USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);//ê1Äü»òÕßê§ÄüÖ¸¶¨μÄUSARTÖD¶Ï ½óêÕÖD¶Ï
USART_ClearFlag(USART3,USART_FLAG_TC);//Çå3yUSARTxμÄ′y′|àí±ê־λ
}
void Nvic_Init()
{
NVIC_InitTypeDef NVIC_InitStructure;//ÖD¶Ï½á11ì嶨òå
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //′ò¿aê±Öó
/* éèÖÃNVIC2Îêy */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; //′ò¿aUSART1μÄ諾ÖÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //ÇàÕ¼óÅÏ輶Îa0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //Ïìó|óÅÏ輶Îa0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ê1Äü
NVIC_Init(&NVIC_InitStructure);
}
//中段函数,摘抄的网上的
void USART3_IRQHandler(void) //c′®¿ú3ÖD¶Ï
{
u8 dat;
//USART_ClearFlag(USART3,USART_FLAG_TC);
if (USART_GetITStatus(USART3, USART_IT_ORE) == SET)
{
USART_ClearITPendingBit(USART3,USART_IT_ORE);
USART_ReceiveData( USART3 );
}
if( USART_GetITStatus(USART3,USART_IT_RXNE) != RESET )
{
USART_ClearITPendingBit(USART3,USART_IT_RXNE);
dat = USART_ReceiveData( USART3 );
ReceiveData[RecStrlen++]=dat;
if( RecStrlen >= RECEIVEDATALEN )
{
RecStrlen=0;
ReceiveCompleteFlag=1;
}
}
if( USART_GetITStatus(USART3,USART_IT_TXE) != RESET )
{
USART_ClearITPendingBit(USART3,USART_IT_TXE);
if( SendStrlen >0 )
USART_SendData(USART3,SendBuffer[--SendStrlen]);
}
}
main()
{
SystemInit(); //ê±Öó3õê¼»ˉ
GpioConfig();
InitState();
while(1)
{
if( ReceiveCompleteFlag )
{
ReceiveCompleteFlag=0;
InitCom();
}
//BuzOut();
}
}
void InitCom(void)
{
u8 i;
SendStrlen = RECEIVEDATALEN;
for( i=0;i<7;i++ )
SendBuffer[i] = ReceiveData[i];
UartSend(USART3,0xfc);
}
实际操作时,只返回一个FC
此帖出自
小平头技术问答
使能之后,还是不行的
一周热门 更多>