qq_34177569 2021-11-10 15:42 采纳率: 33.3%
浏览 111
已结题

STM32发送某种协议数据,实现双机通信(库函数版)

任务名称:STM32双机通信
任务描述:MCU1发送某种协议数据,MCU2接收数据并通过LED1、LED2显示数据状态。建议发送与接收端均设置数组缓存数据。
MCU1发送:0xFE 0x01 0xEF MCU2接收数据,同时LED1亮
MCU1发送:0xFE 0x02 0xEF MCU2接收数据,同时LED1灭
MCU1发送:0xFE 0x03 0x01 0xEF MCU2接收数据,同时LED2亮
MCU1发送:0xFE 0x03 0x02 0xEF MCU2接收数据,同时LED2灭

MCU1发送:0xFE 0x04 0x03 0x01 0xEF MCU2接收数据,同时LED1、LED2同时亮
MCU1发送:0xFE 0x04 0x03 0x02 0xEF MCU2接收数据,同时LED1、LED2同时灭

  • 写回答

3条回答 默认 最新

  • soar3033 2021-11-11 08:32
    关注

    写好了 请采纳

    mcu1

    #include"stm32f10x.h"
    
    
    void my_USART_Init()
    {
        GPIO_InitTypeDef GPIO_InitStruct;
        USART_InitTypeDef USART_InitStruct;
        NVIC_InitTypeDef  NVIC_InitStruct;
        //1.时钟使能
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//ENABLE THE GPIOA
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//ENABLE THE USART1
    
        //2.GPIOA9 init
    
        GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;//复位推挽输出
        GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
        GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
        GPIO_Init(GPIOA, &GPIO_InitStruct);
        //2.GPIOA10 init
        GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
        GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
        GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
        GPIO_Init(GPIOA, &GPIO_InitStruct);
    
        //3.usart init
    
        USART_InitStruct.USART_BaudRate = 115200;//设置波特率
        USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//设置硬件流设置
        USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//设置模式
        USART_InitStruct.USART_Parity = USART_Parity_No;//不采用奇偶校验
        USART_InitStruct.USART_StopBits = USART_StopBits_1;//1位停止位
        USART_InitStruct.USART_WordLength = USART_WordLength_8b;//8位数据位
    
        USART_Init(USART1, &USART_InitStruct);
    
        //初始化某串口
        USART_Cmd(USART1, ENABLE);//´串口使能
    
    }
    
    void send(int len,u8* p) {//串口发送函数
        int n = 0;
        while (n<len)
        {
            USART_SendData(USART1, p[n]);
            n++;
            while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
        }
    
    }
    
    int main()
    {
        u8 array1[] = { 0xfe,0x01,0xef};
        u8 array2[] = { 0xfe,0x02,0xef };
        u8 array3[] = { 0xfe,0x03,0x01,0xef };
        u8 array4[] = { 0xfe,0x03,0x02,0xef };
        u8 array5[] = { 0xfe,0x04,0x03,0x01,0xef };
        u8 array6[] = { 0xfe,0x04,0x03,0x02,0xef };
        my_USART_Init();
        while (1) {
            send(3, array1);//发送第1组命令
            for (int i = 0; i < 60000; i++) {//延时
                for (int j = 0; j < 300; j++);
            }
            
            send(3, array21);//发送第2组命令
            for (int i = 0; i < 60000; i++) {//延时
                for (int j = 0; j < 300; j++);
            }
            send(4, array3);//发送第3组命令
            for (int i = 0; i < 60000; i++) {//延时
                for (int j = 0; j < 300; j++);
            }
            send(4, array4);//发送第4组命令
            for (int i = 0; i < 60000; i++) {//延时
                for (int j = 0; j < 300; j++);
            }
            send(5, array5);//发送第5组命令
            for (int i = 0; i < 60000; i++) {//延时
                for (int j = 0; j < 300; j++);
            }
            send(5, array6);//发送第6组命令
            for (int i = 0; i < 60000; i++) {//延时
                for (int j = 0; j < 300; j++);
            }
        }
    
    }
    
    

    mcu2

    
    #include"stm32f10x.h"
    
    int n=0;
    u8 readbuf[5];
    
    void my_USART_Init()
    {
        GPIO_InitTypeDef GPIO_InitStruct;
        USART_InitTypeDef USART_InitStruct;
        NVIC_InitTypeDef  NVIC_InitStruct;
        //1.时钟使能
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//ENABLE THE GPIOA
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//ENABLE THE USART1
    
        //2.GPIOA9 init
    
        GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;//复位推挽输出
        GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
        GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
        GPIO_Init(GPIOA, &GPIO_InitStruct);
        //2.GPIOA10 init
        GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
        GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
        GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
        GPIO_Init(GPIOA, &GPIO_InitStruct);
    
        //2.GPIOA6 init
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
        GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
        GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
        GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
        GPIO_Init(GPIOA, &GPIO_InitStruct);
    
        //2.GPIOA7 init
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
        GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
        GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
        GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
        GPIO_Init(GPIOA, &GPIO_InitStruct);
    
        //3.usart init
    
        USART_InitStruct.USART_BaudRate = 115200;//设置波特率
        USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//设置硬件流设置
        USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//设置模式
        USART_InitStruct.USART_Parity = USART_Parity_No;//不采用奇偶校验
        USART_InitStruct.USART_StopBits = USART_StopBits_1;//1位停止位
        USART_InitStruct.USART_WordLength = USART_WordLength_8b;//8位数据位
    
        USART_Init(USART1, &USART_InitStruct);
    
        //初始化某串口
        USART_Cmd(USART1, ENABLE);//´串口使能
    
        USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//实现中断
        //中断优先级
    
        NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
        NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
        NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
        NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
        NVIC_Init(&NVIC_InitStruct);
    
    }
    //中断处理函数
    void USART1_IRQHandler(void)
    {
        u8 res;
        if (USART_GetITStatus(USART1, USART_IT_RXNE))
        {
            res = USART_ReceiveData(USART1);//接收数据
            if (res==0xfe)//如果是起始符号
            {
                n = 0;//buf位置为0
            }
            readbuf[n] = res;//向buf存读入的数据
            if (n<4)//n<4的时候n才自加,防止buf溢出
            {
                n++;
            }
            if (res==0xef)//如果是结束符号
            {
                if (n==3)//判断接收数据位数为3
                {
                    if (readbuf[1]==0x01)//命令为0x01
                    {
                        GPIO_SetBits(GPIOA, GPIO_Pin_6);//led1亮
                    }
                    if (readbuf[1] == 0x02) //命令为0x02
                    {
                        GPIO_ResetBits(GPIOA, GPIO_Pin_6);//led1灭
                    }
                }
                else if (n==4)//判断接收数据位数为4
                {
                    if (readbuf[1]==0x03 && readbuf[2]==0x01)//命令为0x01
                    {
                        GPIO_SetBits(GPIOA, GPIO_Pin_7);//led2亮
                    }
                    else if (readbuf[1]==0x03 && readbuf[2]==0x02)//命令为0x02
                    {
                        GPIO_ResetBits(GPIOA, GPIO_Pin_7);//led2灭
                    }
                }
                else if(n==5)//判断接收数据位数为5
                {
                    if (readbuf[1]==0x04 && readbuf[2]==0x03 && readbuf[3]==0x01)//命令为0x01
                    {
                        GPIO_SetBits(GPIOA, GPIO_Pin_6);//led1亮
                        GPIO_SetBits(GPIOA, GPIO_Pin_7);//led2亮
                    }
                    else if (readbuf[1] == 0x04 && readbuf[2] == 0x03 && readbuf[3] == 0x01)//命令为0x02
                    {
                        GPIO_ResetBits(GPIOA, GPIO_Pin_6);//led1灭
                        GPIO_ResetBits(GPIOA, GPIO_Pin_7);//led2灭
                    }
                }
            }
        }
    
    }
    
    int main()
    {
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级
        my_USART_Init();
        while (1);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 11月11日
  • 已采纳回答 11月11日
  • 创建了问题 11月10日

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘