stm32F103 最小系统,modbus slave RTU 示例代码,采用freemodbus
单片机源程序如下:
- #include <rtthread.h>
- #include <rtdevice.h>
- #include <board.h>
- #include "wdt.h"
- //#include "uart.h"
- //#include "mpu6050.h"
- #include "mb_slave.h"
- #include "bsp_flash.h"
- /* 私用宏定义----------------------------------------------------------------*/
- #define THREAD_PRIORITY 25
- #define THREAD_STACK_SIZE 512
- #define THREAD_TIMESLICE 5
- #define EVENT_BIT_0 (1 << 0)
- #define EVENT_BIT_1 (1 << 1)
- //#define EVENT_BIT_2 (1 << 2)
- //#define EVENT_BIT_3 (1 << 3)
- //#define EVENT_BIT_4 (1 << 4)
- //#define EVENT_BIT_5 (1 << 5)
- /* 私用变量----------------------------------------------------------------*/
- /**************************** 任务句柄 ********************************/
- static rt_thread_t th_wdg = RT_NULL; //看门狗
- static rt_thread_t th_Gyro = RT_NULL; //陀螺仪
- /*事件控制块*/
- static struct rt_event event;
- /*wdg 入口函数*/
- static void th_wdg_entry(void *parameter)
- {
- rt_uint32_t count = 0;
- while (1)
- {
-
- // rt_kprintf("wdg thread count: %d\n", count ++);
- rt_thread_mdelay(50);
- /* 收到所有事件,复位看门狗*/
- rt_uint32_t e;
- if (rt_event_recv(&event, (EVENT_BIT_0 ),
- RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
- RT_WAITING_FOREVER, &e) == RT_EOK)
- {
- // rt_kprintf("wdg: AND recv event 0x%x\n", e);
- //喂狗
- iwdg_feed();
- }
-
- }
- }
- /* gyro 入口函数*/
- static void th_Gyro_entry(void *parameter)
- {
- // float angle_Y = 0; //初始状态值
- // float angle_X = 0;
- // float angle_Z = 0;
- //
- // rt_int16_t temp; //温度
- // rt_int16_t gx,gy,gz; //三轴加速度
- // rt_int16_t ax,ay,az; //三轴角速度
- //
- // rt_err_t ret;
- //
- while(1)
- {
- // ret = mpu6050_temperature_get(&temp);
- // if (ret != RT_EOK)
- // {
- // rt_kprintf("mpu6050 : get temperature error\r\n");
- // }
- // ret = mpu6050_accelerometer_get(&ax, &ay, &az);
- // if (ret != RT_EOK)
- // {
- // rt_kprintf("mpu6050 : get acc error\r\n");
- // }
- // ret = mpu6050_gyroscope_get(&gx, &gy, &gz);
- // if (ret != RT_EOK)
- // {
- // rt_kprintf("mpu6050 : get gyro error\r\n");
- // }
- // if (ret == RT_EOK)
- // {
- // rt_kprintf("mpu6050: temperature=%-6d gx=%-6d gy=%-6d gz=%-6d ax=%-6d ay=%-6d az=%-6d\r\n",temp/100,gx,gy,gz,ax,ay,az);
- // angle_X=Kalman_X(ax/100,ay/100,az/100,gx,gy,gz);
- // angle_Y=Kalman_Y(ax/100,ay/100,az/100,gx,gy,gz);
- // angle_Z=Kalman_Z(ax/100,ay/100,az/100,gx,gy,gz);
- // rt_kprintf("x=%-6d y=%-6d z=%-6d \r\n ",angle_X,angle_Y,angle_Z);
- // }
- // rt_thread_delay(rt_tick_from_millisecond(1000));
- rt_event_send(&event, EVENT_BIT_0);
- rt_thread_delay(200);
- }
- }
- //动态创建线程
- void th_create(){
- rt_err_t result;
- /* 初始化事件对象 */
- result = rt_event_init(&event, "event", RT_IPC_FLAG_FIFO);
- if (result != RT_EOK)
- {
- rt_kprintf("init event failed.\n");
- return ;
- }
-
-
- /*动态创建WDG线程*/
- th_wdg = rt_thread_create("wdg",
- th_wdg_entry, RT_NULL,
- THREAD_STACK_SIZE-256,
- THREAD_PRIORITY, THREAD_TIMESLICE);
-
- /* 如果获取线程控制块,启动这个线程 */
- if (th_wdg != RT_NULL)
- {
- rt_thread_startup(th_wdg);
- }
-
- /*动态创建姿态获取线程*/
- th_Gyro = rt_thread_create("Gyro",
- th_Gyro_entry, RT_NULL,
- THREAD_STACK_SIZE,
- THREAD_PRIORITY-1, THREAD_TIMESLICE);
-
- /* 如果获取线程控制块,启动这个线程 */
- if (th_Gyro != RT_NULL)
- rt_thread_startup(th_Gyro);
-
-
-
-
- }
- int main(void)
- {
-
- // //485 初始化
- // uart5_init();
-
- //创建线程
- th_create();
-
- //modbus 初始化
- mb_slave_init();
-
- //看门狗初始化
- if(iwdg_init()==RT_EOK){
- rt_kprintf("iwdg init success.\n");
- };
-
- // rt_pin_mode(MODBUS_MASTER_RT_CONTROL_PIN_INDEX, PIN_MODE_OUTPUT);
- // rt_pin_write(MODBUS_MASTER_RT_CONTROL_PIN_INDEX, PIN_HIGH);
- // #define RT_MODBUS_MASTER_USE_CONTROL_PIN 1
- // #define MODBUS_MASTER_RT_CONTROL_PIN_INDEX 19
-
- // uint16_t Data [6] = {7,1,2,3,4,5};
- // uint32_t Address = 0x0800FC00 ;
- // FLASH_WriteData(Address,Data,1);
- //
- // uint16_t Text [6] = {0};
- // FLASH_ReadMoreData(Address,Text,6);
- // for(int i=0;i<6;i++){
- // rt_kprintf("%d \r\n",Text[i]);
- // }
-
- return RT_EOK;
- }
复制代码
所有资料51hei提供下载:
stm32f103_modbus.rar
(111.17 KB, 下载次数: 74)
|