標(biāo)題: 基于兩個(gè)STM32 MINI板的RS485通訊程序 不分主從 [打印本頁]

作者: wsyhdc    時(shí)間: 2020-3-1 11:12
標(biāo)題: 基于兩個(gè)STM32 MINI板的RS485通訊程序 不分主從
stm32-mini版S485通信

單片機(jī)源程序如下:
  1. /***************************************
  2. * 文件名  :main.c
  3. * 描述    :給相同兩個(gè)MINI板下載此程序后,按下一個(gè)板子上的按鍵,可以點(diǎn)亮另一個(gè)板子
  4. *           上對應(yīng)的LED。兩個(gè)板子不分主從。         
  5. * 實(shí)驗(yàn)平臺:MINI STM32開發(fā)板 基于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);//使能外設(shè)時(shí)鐘
  20.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//使能外設(shè)時(shí)鐘

  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);//去抖動(dòng)
  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);//去抖動(dòng)//去抖動(dòng)
  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);//去抖動(dòng)//去抖動(dòng)
  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();//配置系統(tǒng)時(shí)鐘為 72M
  82.         KeyInit();        //按鍵管腳初始化
  83.         LED_GPIO_Config(); //LED管腳初始化
  84.         NVIC_Configuration();//USART3 接收中斷配置
  85.         USART3_int(); //USART1 配置               

  86.   while (1)
  87.   {         
  88.     GetKey(); //檢測是否有按鍵按下
  89.   }
  90. }
復(fù)制代碼

所有資料51hei提供下載:
高級例程-基于兩個(gè)MINI板的RS485通訊.7z (139.37 KB, 下載次數(shù): 42)







歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1