stm32-mini版S485通信
單片機源程序如下:
- /***************************************
- * 文件名 :main.c
- * 描述 :給相同兩個MINI板下載此程序后,按下一個板子上的按鍵,可以點亮另一個板子
- * 上對應的LED。兩個板子不分主從。
- * 實驗平臺:MINI STM32開發板 基于STM32F103RBT6
- * 庫版本 :ST3.0.0
- *********************************************************/
- #include "stm32f10x.h"
- #include "usart1.h"
- #include "led.h"
- void Delay(vu32 nCount)
- {
- for(; nCount != 0; nCount--);
- }
- /*按鍵管腳初始化*/
- void KeyInit(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//使能外設時鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//使能外設時鐘
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_15 ;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; //最高輸出速率10MHz
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉輸入
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; //最高輸出速率10MHz
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉輸入
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- }
- /*檢測是否有按鍵按下*/
- void GetKey(void)
- {
- if(Bit_RESET == GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8))
- {
- Delay(1000000);//去抖動
- if(Bit_RESET == GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8))
- {
- while(Bit_RESET == GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8)){ ; }//等待按鍵釋放
- RS485_SendByte(0X03);
- LED1(1);LED2(1);LED3(1);
- }
- }
- if(Bit_RESET == GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15))
- {
- Delay(1000000);//去抖動//去抖動
- if(Bit_RESET == GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15))
- {
- while(Bit_RESET == GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15)){ ; }//等待按鍵釋放
- RS485_SendByte(0X02);
- LED1(1);LED2(1);LED3(1);
- }
- }
- if(Bit_RESET == GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1))
- {
- Delay(1000000);//去抖動//去抖動
- if(Bit_RESET == GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1))
- {
- while(Bit_RESET == GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1)){ ; }//等待按鍵釋放
- RS485_SendByte(0X01);
- LED1(1);LED2(1);LED3(1);
- }
- }
-
- }
- /*USART3 接收中斷配置 */
- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- /* Configure the NVIC Preemption Priority Bits */
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
-
-
- /* Enable the USART1 Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- }
- int main(void)
- {
- SystemInit();//配置系統時鐘為 72M
- KeyInit(); //按鍵管腳初始化
- LED_GPIO_Config(); //LED管腳初始化
- NVIC_Configuration();//USART3 接收中斷配置
- USART3_int(); //USART1 配置
- while (1)
- {
- GetKey(); //檢測是否有按鍵按下
- }
- }
復制代碼
所有資料51hei提供下載:
高級例程-基于兩個MINI板的RS485通訊.7z
(139.37 KB, 下載次數: 42)
2020-3-4 18:47 上傳
點擊文件名下載附件
stm32-485通信
|