我用的是stm32f103rc,我用我的代码调试了俩三天都不行求各位大哥给我一个列子好不好
通信协议如下,我用的是串口四。
通信协议
你们看一看我的单片机程序吧!
u16 k=0;
u8 gg[1];
- void Tongxin(void)//串口初始化
- {
- GPIO_InitTypeDef GPIO_tongxin;
- USART_InitTypeDef USART_tongxin;
- NVIC_InitTypeDef NVIC_tongxin;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC , ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
- USART_DeInit(UART4);
-
- GPIO_tongxin.GPIO_Mode=GPIO_Mode_AF_PP ;
- GPIO_tongxin.GPIO_Pin=GPIO_Pin_10;
- GPIO_tongxin.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOC,&GPIO_tongxin);
-
- GPIO_tongxin.GPIO_Mode=GPIO_Mode_IN_FLOATING;
- GPIO_tongxin.GPIO_Pin=GPIO_Pin_11;
- GPIO_tongxin.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOC,&GPIO_tongxin);
-
- USART_tongxin.USART_BaudRate=115200;
- USART_tongxin.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
- USART_tongxin.USART_Mode=USART_Mode_Rx |USART_Mode_Tx ;
- USART_tongxin.USART_Parity=USART_Parity_No;
- USART_tongxin.USART_StopBits=USART_StopBits_1;
- USART_tongxin.USART_WordLength=USART_WordLength_8b;
- USART_Init(UART4,&USART_tongxin);
-
- USART_Cmd(UART4,ENABLE);
- USART_ITConfig(UART4,USART_IT_RXNE,ENABLE);
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
-
- NVIC_tongxin.NVIC_IRQChannel=UART4_IRQn;
- NVIC_tongxin.NVIC_IRQChannelCmd=ENABLE;
- NVIC_tongxin.NVIC_IRQChannelPreemptionPriority=3;
- NVIC_tongxin.NVIC_IRQChannelSubPriority=3;
- NVIC_Init(&NVIC_tongxin);
- }
-
- void UART4_IRQHandler(void)//串口4中断服务函数
- {
- u8 a,b=0,c=0;
- if((USART_GetITStatus(UART4,USART_IT_RXNE))!=RESET)//判断中断
- {
-
- do{
- if(a==0x0d) b=1; //判断数据是否以0x0d结束
- a=USART_ReceiveData(UART4);//接收数据
- gg[k]=a;
- k++;
- }while(b==0);
- for(b=0;b<k;b++)
- {
- USART_SendData(UART4,gg[b]);//发送数据
- }
- k=0;
- }
- }
复制代码
我对这个简单协议深感无力,改了好几天了,一直在调,求各位大佬给我一个列子研究一下。
|