rt-thread学习(一) 创建led闪烁 加入串口设备 pwm配置 modbus主/从测试代码

rt-thread学习(一) 创建led闪烁 加入串口设备 pwm配置 modbus主/从测试代码

  • led灯闪烁
  • 加入串口设备
  • 使用cubemx 配置hse
  • 配置pwm
  • freemodbus 测试代码
  • freemodbus 从机测试代码

led灯闪烁

	#include 
	#include 
#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include 
#include "drv_common.h"
#define LED0_PIN    GET_PIN(C, 13)

int main(void)
{
    int count = 1;
    /* set LED0 pin mode to output */
    rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);

    while (count++)
    {
        /* set LED0 pin level to high or low */
        rt_pin_write(LED0_PIN, count % 2);
        LOG_D("Hello RT-Thread!");
        rt_thread_mdelay(1000);
    }

    return RT_EOK;
}

加入串口设备

直接在board.h里 添加宏定义

#define BSP_USING_UART6
#define BSP_UART2_TX_PIN       "PA2"
#define BSP_UART2_RX_PIN       "PA3"

使用cubemx 配置hse

直接配时钟即可
不要勾选 分成.c/.h
在rtt studio中会自动调用生成的 系统时钟配置
rt-thread学习(一) 创建led闪烁 加入串口设备 pwm配置 modbus主/从测试代码_第1张图片
还要修改 HSE_VALUE

 #define HSE_VALUE    ((uint32_t)8000000U) /*!< Value of the External oscillator in Hz */

构建排除

配置pwm

参考文章
如果要用pwm1的话,要在pwm_config.h里自己加

#ifdef BSP_USING_PWM1
#ifndef PWM1_CONFIG
#define PWM1_CONFIG                             \
    {                                           \
       .tim_handle.Instance     = TIM1,         \
       .name                    = "pwm1",       \
       .channel                 = 0             \
    }
#endif /* PWM1_CONFIG */

board.h

#define BSP_USING_PWM1
#define BSP_USING_PWM1_CH1

stm32f4xx_hal_conf.h
开启宏定义

#define HAL_TIM_MODULE_ENABLED

测试代码

/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-10-02     RT-Thread    first version
 */

#include 
#include 
#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include 
#include 
#define PWM_DEV_NAME        "pwm1"
#define PWM_DEV_CHANNEL     1 << 0

struct rt_device_pwm *pwm_dev;

static void pwm_thread_entry(void *parameter)
{
    rt_uint32_t period, pulse,dir=1;
    period = 20000000;  //20ms
    pulse = 0;


    pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME);
    if (pwm_dev == RT_NULL)
    {
        rt_kprintf("pwm sample run failed! can't find %s device!\n", PWM_DEV_NAME);
        //return RT_ERROR;
    }
    rt_kprintf("pwm sample run ! find %s device!\n", PWM_DEV_NAME);

    rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);

    rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
    while (1)
    {
        rt_thread_mdelay(50);
        if (dir)
               {
                   pulse += period*0.01; 
               }
               else
               {
                   pulse -= period*0.01; 
               }
               if (pulse <= period*0.1)
               {
                   dir = 1;
               }
               if ( pulse>=period*0.9)
               {
                   dir = 0;
               }

        rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
    }
}

static int pwm_led_init(void)
{
    static rt_thread_t tid_pwm = RT_NULL;

    rt_err_t ret = RT_EOK;


    tid_pwm = rt_thread_create("t_pwm",pwm_thread_entry, RT_NULL, 512, 24, 10);

   if (tid_pwm != RT_NULL)
   {
       rt_thread_startup(tid_pwm);
   }
   else
   {
       ret = RT_ERROR;
   }

   return ret;

}

INIT_APP_EXPORT(pwm_led_init);

int main(void)
{
    pwm_led_init();
    return RT_EOK;
}

freemodbus 测试代码



#include 

#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include 


#include "mb.h"
#include "user_mb_app.h"



#define SLAVE_ADDR      0x01
#define PORT_NUM        2
#define PORT_BAUDRATE   115200

#define PORT_PARITY     MB_PAR_EVEN

#define MB_POLL_THREAD_PRIORITY  10
#define MB_SEND_THREAD_PRIORITY  RT_THREAD_PRIORITY_MAX - 1

#define MB_POLL_CYCLE_MS 200

extern USHORT usSRegHoldBuf[S_REG_HOLDING_NREGS];

static void send_thread_entry(void *parameter)
{
    USHORT         *usRegHoldingBuf;
    usRegHoldingBuf = usSRegHoldBuf;
    rt_base_t level;

    while (1)
    {
        /* Test Modbus Master */
        level = rt_hw_interrupt_disable();

        usRegHoldingBuf[0] = (USHORT)(18);
        usRegHoldingBuf[1] = (USHORT)(96);

        rt_hw_interrupt_enable(level);

        rt_thread_mdelay(1000);
    }
}

static void mb_slave_poll(void *parameter)
{
    eMBInit(MB_RTU, SLAVE_ADDR, PORT_NUM, PORT_BAUDRATE, PORT_PARITY);
    eMBEnable();
    while (1)
    {
        eMBPoll();
        rt_thread_mdelay(50);
    }
}

int mb_slave_samlpe()
{
    static rt_uint8_t is_init = 0;
    rt_thread_t tid1 = RT_NULL, tid2 = RT_NULL;

    if (is_init > 0)
    {
        rt_kprintf("sample is running\n");
        return -RT_ERROR;
    }
    tid1 = rt_thread_create("md_s_poll", mb_slave_poll, RT_NULL, 1024, MB_POLL_THREAD_PRIORITY, 10);
    if (tid1 != RT_NULL)
    {
        rt_thread_startup(tid1);
    }
    else
    {
        goto __exit;
    }

    tid2 = rt_thread_create("md_s_send", send_thread_entry, RT_NULL, 512, MB_SEND_THREAD_PRIORITY, 10);
    if (tid2 != RT_NULL)
    {
        rt_thread_startup(tid2);
    }
    else
    {
        goto __exit;
    }

    is_init = 1;
    return RT_EOK;

__exit:
    if (tid1)
        rt_thread_delete(tid1);
    if (tid2)
        rt_thread_delete(tid2);

    return -RT_ERROR;
}
MSH_CMD_EXPORT(mb_slave_samlpe, run a modbus slave sample);


int main(void)
{
    mb_slave_samlpe();
    return RT_EOK;
}

freemodbus 从机测试代码

#include 
#include 
#include 

#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include 

#include "mb.h"
#include "mb_m.h"


#include "mbconfig.h"
#include "user_mb_app.h"
extern USHORT   usMRegInBuf[MB_MASTER_TOTAL_SLAVE_NUM][M_REG_INPUT_NREGS];
extern USHORT   usMRegHoldBuf[MB_MASTER_TOTAL_SLAVE_NUM][M_REG_HOLDING_NREGS];

#define SLAVE_ADDR      0x01
#define PORT_NUM        2
#define PORT_BAUDRATE   115200

#define PORT_PARITY     MB_PAR_NONE

#define MB_POLL_THREAD_PRIORITY  10
#define MB_SEND_THREAD_PRIORITY  RT_THREAD_PRIORITY_MAX - 1

#define MB_SEND_REG_START  2
#define MB_SEND_REG_NUM    2

#define MB_POLL_CYCLE_MS   800
#define MB_READ_REG_NUM 2
#define MB_READ_REG_ADDR 0x000f
static void send_thread_entry(void *parameter)
{
    eMBMasterReqErrCode error_code2 = MB_MRE_NO_ERR;
    static rt_uint16_t error_count2 = 0;

    while (1)
    {
        /* 03 功能码 连续读取保持寄存器多个地址的数据 */
        error_code2 =eMBMasterReqReadHoldingRegister( SLAVE_ADDR, MB_READ_REG_ADDR, MB_READ_REG_NUM, RT_WAITING_FOREVER );
        /* Record the number of errors */
        if (error_code2 != MB_MRE_NO_ERR)
        {
            error_count2++;

            rt_kprintf("\r\n \r\n error_code2 = %d, error_count2 = %d \r\n",error_code2,error_count2);

            error_code2 = MB_MRE_NO_ERR;
        } else
        {
      uint32_t distance=0;
       rt_kprintf("HoldBuf[0] = %d, HoldBuf[1] = %d\r\n",usMRegHoldBuf[SLAVE_ADDR-1][MB_READ_REG_ADDR],usMRegHoldBuf[SLAVE_ADDR-1][MB_READ_REG_ADDR+1]);
       /*distance=usMRegHoldBuf[SLAVE_ADDR-1][MB_READ_REG_ADDR];
       distance<<=16;
       distance|=usMRegHoldBuf[SLAVE_ADDR-1][MB_READ_REG_ADDR+1];
       rt_kprintf("test_distance:%d mm",distance);

            rt_kprintf("\r\n");*/
        }
        rt_thread_mdelay(1000);
    }
}

static void mb_master_poll(void *parameter)
{
    eMBMasterInit(MB_RTU, PORT_NUM, PORT_BAUDRATE, PORT_PARITY);
    eMBMasterEnable();

    while (1)
    {
        eMBMasterPoll();
        rt_thread_mdelay(50);
    }
}

int mb_master_samlpe()
{
    static rt_uint8_t is_init = 0;
    rt_thread_t tid1 = RT_NULL, tid2 = RT_NULL;

    if (is_init > 0)
    {
        rt_kprintf("sample is running\n");
        return -RT_ERROR;
    }
    tid1 = rt_thread_create("md_m_poll", mb_master_poll, RT_NULL, 512, MB_POLL_THREAD_PRIORITY, 10);
    if (tid1 != RT_NULL)
    {
        rt_thread_startup(tid1);
    }
    else
    {
        goto __exit;
    }

    tid2 = rt_thread_create("md_m_send", send_thread_entry, RT_NULL, 512, MB_SEND_THREAD_PRIORITY, 10);
    if (tid2 != RT_NULL)
    {
        rt_thread_startup(tid2);
    }
    else
    {
        goto __exit;
    }

    is_init = 1;
    return RT_EOK;

__exit:
    if (tid1)
        rt_thread_delete(tid1);
    if (tid2)
        rt_thread_delete(tid2);

    return -RT_ERROR;
}
MSH_CMD_EXPORT(mb_master_samlpe, run a modbus master sample);


int main(void)
{
    mb_master_samlpe();
    return RT_EOK;
}

你可能感兴趣的:(rtt,操作系统,stm32学习,stm32,物联网,rt-thread,modbus,rtt,studio)