/****************main.C文件*************************************/ #include #include "board.h" /*函数声明*///////////////////////// extern int Proportional_valve(); extern int adc_r(); /////////////////////////////////// int main(void) {rt_kprintf("Hello, world\n"); uint8_t pwm= rt_pin_get("PE.4"); rt_pin_mode(pwm, PIN_MODE_OUTPUT); rt_pin_write(pwm, PIN_HIGH); uint8_t pwm_b= rt_pin_get("PE.1"); rt_pin_mode(pwm_b, PIN_MODE_OUTPUT); rt_pin_write(pwm_b, PIN_HIGH); Proportional_valve();//启动比main while (1) {adc_r();//启动模数转换,采集LED灯电压值 rt_thread_mdelay(500); } } /****************PWM.C文件*************************************/ /* 占空比=pulse/period */ /* 脉冲宽度pulse=占空比*period */ /*为简化程序,将输入串口值定义为 串口值 占空比% pwm_pulse值 ASCII 0 0 0 48 1 10 50000 49 2 20 100000 50 3 30 150000 51 4 40 200000 52 5 50 250000 53 6 60 300000 54 7 70 350000 55 8 80 400000 56 9 90 450000 57 */ //头文件 #include #include //PWM常量 #define THREAD_PRIORITY 7 #define THREAD_STACK_SIZE 512 #define THREAD_TIMESLICE 10 #define PWM_DEV_NAME "lpwm0" /* PWM设备名称 */ #define PWM_DEV_CHANNEL 1 /* PWM通道 */ struct rt_device_pwm *pwm_dev; /* PWM设备句柄 */ #define PWM_DEV_NAME_B "t5pwm" /* PWM设备名称*/ #define PWM_DEV_CHANNEL_B 1 /* PWM通道 */ struct rt_device_pwm *pwm_dev_b; /* PWM设备句柄 */ static rt_thread_t pwm_tid = RT_NULL; //ADC常量 #define ADC_DEV_NAME "adc0" /* ADC 设备名称 */ #define ADC_DEV_CHANNEL 9 /* ADC 通道 */ #define REFER_VOLTAGE 330 /* 参考电压 3.3V,数据精度乘以 100 保留 2 位小数*/ #define CONVERT_BITS (1 << 10) /* 转换位数为 10 位 */ /* 串口名字 */ #define SAMPLE_UART_NAME "uart1" /* 用于接收消息的信号量 */ static struct rt_semaphore rx_sem; static rt_device_t serial; rt_uint32_t period=500000,pulse_pwm=500000; /* period周期为0.5ms,单位为纳秒ns PWM频率2000Hz*/ /*pulse_pwm PWM脉冲宽度值,单位为纳秒ns */ /* 线程 pwm_entry 的入口函数 */ /*将接收到的串口值转为脉冲宽度*/ static void pwm_entry(void *parameter) {char ch; /* 查找设备 */ pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME); if (pwm_dev == RT_NULL) {rt_kprintf("pwm run failed! can't find %s device!\n", PWM_DEV_NAME); } pwm_dev_b = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME_B); if (pwm_dev_b == RT_NULL) {rt_kprintf("pwm led b run failed! can't find %s device!\n", PWM_DEV_NAME_B); } /* 设置PWM周期和脉冲宽度默认值 */ rt_pwm_set(pwm_dev_b, PWM_DEV_CHANNEL_B, period, pulse_pwm); rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse_pwm); /* 使能设备 */ rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL); rt_pwm_enable(pwm_dev_b, PWM_DEV_CHANNEL_B); while (1) { /* 从串口读取一个字节的数据,没有读取到则等待接收信号量 */ while (rt_device_read(serial, -1, &ch, 1) != 1) {/* 阻塞等待接收信号量,等到信号量后再次读取数据 */ rt_sem_take(&rx_sem, RT_WAITING_FOREVER); } /* 读取到的数据转为PWM的pulse值*/ if(ch>=48||ch<=57)//数字0-9 { pulse_pwm=period/10*(57-ch); /*设置PWM周期和脉冲宽度 */ rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse_pwm); rt_pwm_set(pwm_dev_b, PWM_DEV_CHANNEL_B, period, pulse_pwm); } /*读取到的数据通过串口输出*/ rt_device_write(serial, 0, &ch, 1); } } /* 接收数据回调函数 */ static rt_err_t uart_input(rt_device_t dev, rt_size_t size) {/* 串口接收到数据后产生中断,调用此回调函数,然后发送接收信号量 */ rt_sem_release(&rx_sem); return RT_EOK; } /*比例阀串口控制程序初始化 */ int Proportional_valve() { rt_err_t ret = RT_EOK; char uart_name[RT_NAME_MAX]; const char str[] = "hello RT-Thread!\r\n"; rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX); /* 查找系统中的串口设备 */ serial = rt_device_find(uart_name); if (!serial) { rt_kprintf("find %s failed!\n", uart_name); return RT_ERROR; } /* 初始化信号量 */ rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO); /* 以中断接收及轮询发送模式打开串口设备 */ rt_device_open(serial, RT_DEVICE_FLAG_INT_RX); /* 设置接收回调函数 */ rt_device_set_rx_indicate(serial, uart_input); /* 发送字符串 */ rt_device_write(serial, 0, str, (sizeof(str) - 1)); /* 创建线程,名称是 pwm_thread,入口是 pwm_thread*/ pwm_tid = rt_thread_create("pwm_thread",pwm_entry, RT_NULL,THREAD_STACK_SIZE, THREAD_PRIORITY,THREAD_TIMESLICE); /* 如果获得线程控制块,启动这个线程 */ if (pwm_tid != RT_NULL)rt_thread_startup(pwm_tid); return ret; } /*ADC采集PWM电压 */ int adc_r() { rt_adc_device_t adc_dev; rt_uint32_t value, vol; rt_err_t ret = RT_EOK; /* 查找设备 */ adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME); if (adc_dev == RT_NULL) { rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME); return RT_ERROR; } /* 使能设备 */ ret = rt_adc_enable(adc_dev, ADC_DEV_CHANNEL); /* 读取采样值 */ value = rt_adc_read(adc_dev, ADC_DEV_CHANNEL); // rt_kprintf("the value is :%d \n", value); /* 转换为对应电压值 */ vol = value * REFER_VOLTAGE / CONVERT_BITS; rt_kprintf("the voltage is :%d.%02d \n", vol / 100, vol % 100); /* 关闭通道 */ ret = rt_adc_disable(adc_dev, ADC_DEV_CHANNEL); return ret; }