金牌会员
 
- 积分
- 1181
- 金钱
- 1181
- 注册时间
- 2015-12-28
- 在线时间
- 132 小时
|
5金钱
我想用串口通信,如果从串口助手上发送00,就拉低GPIOA.1,如果串口助手上发送01 就拉高GPIOA.1;
如果串口助手上发送 10,就拉低GPIOC.1,如果串口助手上发送11 就拉高GOIOC.1。。
我的程序如下,(估计是我的逻辑错误,完全不按照想法执行啊,,)
main.c 如下:
int main(void)
{
GPIO_Init();
delay_init();
USART3_Init(9600);
while(1)
{
if(flag)
{
flag = 0;
if(buf[count = 0] == '1')
{
if(buf[count = 1] == '0')
{
GPIO_ResetBits(GPIOC,GPIO_Pin_1);
}
if(buf[count = 1] == '1')
{
GPIO_SetBits(GPIOC,GPIO_Pin_1);
}
}
if(buf[count = 0] == '1')
{
if(buf[count = 1] == '0')
{
GPIO_ResetBits(GPIOC,GPIO_Pin_1);
}
if(buf[count = 1] == '1')
{
GPIO_SetBits(GPIOC,GPIO_Pin_1);
}
}
}
}
}
串口中断如下:
u8 i;
u8 buf[20]; //存数用的数组
u8 count = 0; //计一共发送和接收多少数据长度的
u8 flag = 0 ;
void USART3_IRQHandler(void)
{
if(USART_GetITStatus(USART3,USART_IT_RXNE) != RESET) //如果进入中断了
{
flag = 1;
buf[count++] = USART_ReceiveData(USART3); //接数,存到buf里
for(i = 0;i < count;i++)
{
USART_SendData(USART3,buf[i]);
}
count = 0;
}
else
{
count = 0; //没进中断的话,清一下count,清一下中断标志位。
USART_ClearITPendingBit(USART3,USART_IT_RXNE);
}
}
GPIO.c 如下:
GPIO_InitTypeDef GPIO_InitStructure;
//RCC时钟使能 GPIOB.5 GPIOE.5
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
//GPIOA.1初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//GPIOC.1初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_1);
GPIO_SetBits(GPIOC,GPIO_Pin_1);
|
最佳答案
查看完整内容[请看2#楼]
这是我昨天自己鼓秋出来的最终版,只能发0123来控制GPIOA.1,GPIOC.1的高低电平。 有待参考。
|