STM32學習資料,基于485總線例程,便于參考學習
本實驗將實現如下功能:通過連接兩個戰艦STM32開發板的RS485接口,然后由KEY0控制發送,當按下一個開發板的KEY0的時候,就發送5個數據給另外一個開發板,并在兩個開發板上分別顯示發送的值和接收到的值。
主程序:
- #include "sys.h"
- #include "usart.h"
- #include "delay.h"
- #include "led.h"
- #include "beep.h"
- #include "key.h"
- #include "exti.h"
- #include "wdg.h"
- #include "timer.h"
- #include "tpad.h"
- #include "oled.h"
- #include "lcd.h"
- #include "usmart.h"
- #include "rtc.h"
- #include "wkup.h"
- #include "adc.h"
- #include "dac.h"
- #include "dma.h"
- #include "24cxx.h"
- #include "flash.h"
- #include "rs485.h"
- //ALIENTEK戰艦STM32開發板實驗24
- //485 實驗
- //技術支持:www.openedv.com
- //廣州市星翼電子科技有限公司
-
- int main(void)
- {
- u8 key;
- u8 i=0,t=0;
- u8 cnt=0;
- u8 rs485buf[5];
- Stm32_Clock_Init(9); //系統時鐘設置
- uart_init(72,9600); //串口初始化為9600
- delay_init(72); //延時初始化
- LED_Init(); //初始化與LED連接的硬件接口
- LCD_Init(); //初始化LCD
- usmart_dev.init(72); //初始化USMART
- KEY_Init(); //按鍵初始化
- RS485_Init(36,9600); //初始化RS485
- POINT_COLOR=RED;//設置字體為紅色
- LCD_ShowString(60,50,200,16,16,"WarShip STM32");
- LCD_ShowString(60,70,200,16,16,"RS485 TEST");
- LCD_ShowString(60,90,200,16,16,"ATOM@ALIENTEK");
- LCD_ShowString(60,110,200,16,16,"2012/9/9");
- LCD_ShowString(60,130,200,16,16,"KEY0:Send"); //顯示提示信息
-
- POINT_COLOR=BLUE;//設置字體為藍色
- LCD_ShowString(60,150,200,16,16,"Count:"); //顯示當前計數值
- LCD_ShowString(60,170,200,16,16,"Send Data:"); //提示發送的數據
- LCD_ShowString(60,210,200,16,16,"Receive Data:"); //提示接收到的數據
-
- while(1)
- {
- key=KEY_Scan(0);
- if(key==KEY_RIGHT)//KEY0按下,發送一次數據
- {
- for(i=0;i<5;i++)
- {
- rs485buf[i]=cnt+i;//填充發送緩沖區
- LCD_ShowxNum(60+i*32,190,rs485buf[i],3,16,0X80); //顯示數據
- }
- RS485_Send_Data(rs485buf,5);//發送5個字節
- }
- RS485_Receive_Data(rs485buf,&key);
- if(key)//接收到有數據
- {
- if(key>5)key=5;//最大是5個數據.
- for(i=0;i<key;i++)LCD_ShowxNum(60+i*32,230,rs485buf[i],3,16,0X80); //顯示數據
- }
- t++;
- delay_ms(10);
- if(t==20)
- {
- LED0=!LED0;//提示系統正在運行
- t=0;
- cnt++;
- LCD_ShowxNum(60+48,150,cnt,3,16,0X80); //顯示數據
- }
- }
- }
復制代碼
完整的源碼下載:
實驗24 485實驗.rar
(126.04 KB, 下載次數: 38)
2017-4-16 22:58 上傳
點擊文件名下載附件
|