找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1857|回复: 0
收起左侧

基于两个STM32 MINI板的RS485通讯程序 不分主从

[复制链接]
ID:700421 发表于 2020-3-1 11:12 | 显示全部楼层 |阅读模式
stm32-mini版S485通信

单片机源程序如下:
  1. /***************************************
  2. * 文件名  :main.c
  3. * 描述    :给相同两个MINI板下载此程序后,按下一个板子上的按键,可以点亮另一个板子
  4. *           上对应的LED。两个板子不分主从。         
  5. * 实验平台:MINI STM32开发板 基于STM32F103RBT6
  6. * 库版本  :ST3.0.0                                                                                                                                                                                                                     
  7. *********************************************************/

  8. #include "stm32f10x.h"
  9. #include "usart1.h"
  10. #include "led.h"


  11. void Delay(vu32 nCount)
  12. {
  13.   for(; nCount != 0; nCount--);
  14. }
  15. /*按键管脚初始化*/
  16. void KeyInit(void)
  17. {
  18.         GPIO_InitTypeDef GPIO_InitStructure;

  19.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//使能外设时钟
  20.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//使能外设时钟

  21.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_15 ;
  22.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; //最高输出速率10MHz
  23.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
  24.         GPIO_Init(GPIOB, &GPIO_InitStructure);

  25.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  26.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; //最高输出速率10MHz
  27.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输入
  28.         GPIO_Init(GPIOA, &GPIO_InitStructure);
  29. }
  30. /*检测是否有按键按下*/
  31. void  GetKey(void)
  32. {
  33.         if(Bit_RESET == GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8))
  34.         {
  35.                 Delay(1000000);//去抖动
  36.                 if(Bit_RESET == GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8))
  37.                 {
  38.                         while(Bit_RESET == GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8)){ ; }//等待按键释放
  39.                        RS485_SendByte(0X03);
  40.                                            LED1(1);LED2(1);LED3(1);
  41.                 }
  42.         }

  43.         if(Bit_RESET == GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15))
  44.         {
  45.                 Delay(1000000);//去抖动//去抖动
  46.                 if(Bit_RESET == GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15))
  47.                 {
  48.                         while(Bit_RESET == GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15)){ ; }//等待按键释放                        
  49.                         RS485_SendByte(0X02);
  50.                                                 LED1(1);LED2(1);LED3(1);
  51.                 }
  52.         }

  53.         if(Bit_RESET == GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1))
  54.         {
  55.                  Delay(1000000);//去抖动//去抖动
  56.                 if(Bit_RESET == GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1))
  57.                 {
  58.                         while(Bit_RESET == GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1)){ ; }//等待按键释放                        
  59.                         RS485_SendByte(0X01);       
  60.                                                 LED1(1);LED2(1);LED3(1);
  61.                 }
  62.         }   

  63.       
  64. }
  65. /*USART3 接收中断配置 */
  66. void NVIC_Configuration(void)
  67. {   
  68.   NVIC_InitTypeDef NVIC_InitStructure;

  69.   /* Configure the NVIC Preemption Priority Bits */  
  70.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

  71.   
  72.   /* Enable the USART1 Interrupt */
  73.   NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
  74.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  75.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  76.   NVIC_Init(&NVIC_InitStructure);

  77.   
  78. }


  79. int main(void)
  80. {         
  81.         SystemInit();//配置系统时钟为 72M
  82.         KeyInit();        //按键管脚初始化
  83.         LED_GPIO_Config(); //LED管脚初始化
  84.         NVIC_Configuration();//USART3 接收中断配置
  85.         USART3_int(); //USART1 配置               

  86.   while (1)
  87.   {         
  88.     GetKey(); //检测是否有按键按下
  89.   }
  90. }
复制代码

所有资料51hei提供下载:
高级例程-基于两个MINI板的RS485通讯.7z (139.37 KB, 下载次数: 38)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表