|
stm32-mini版S485通信
單片機(jī)源程序如下:
- /***************************************
- * 文件名 :main.c
- * 描述 :給相同兩個(gè)MINI板下載此程序后,按下一個(gè)板子上的按鍵,可以點(diǎn)亮另一個(gè)板子
- * 上對(duì)應(yīng)的LED。兩個(gè)板子不分主從。
- * 實(shí)驗(yàn)平臺(tái):MINI STM32開(kāi)發(fā)板 基于STM32F103RBT6
- * 庫(kù)版本 :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);//使能外設(shè)時(shí)鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//使能外設(shè)時(shí)鐘
- 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);
- }
- /*檢測(cè)是否有按鍵按下*/
- void GetKey(void)
- {
- if(Bit_RESET == GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8))
- {
- Delay(1000000);//去抖動(dòng)
- 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);//去抖動(dòng)//去抖動(dòng)
- 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);//去抖動(dòng)//去抖動(dòng)
- 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();//配置系統(tǒng)時(shí)鐘為 72M
- KeyInit(); //按鍵管腳初始化
- LED_GPIO_Config(); //LED管腳初始化
- NVIC_Configuration();//USART3 接收中斷配置
- USART3_int(); //USART1 配置
- while (1)
- {
- GetKey(); //檢測(cè)是否有按鍵按下
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
高級(jí)例程-基于兩個(gè)MINI板的RS485通訊.7z
(139.37 KB, 下載次數(shù): 42)
2020-3-4 18:47 上傳
點(diǎn)擊文件名下載附件
stm32-485通信 下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|