1、基本思路
三个线程,一个消息队列(大小是1,长度是12) 线程1:LED 500ms闪烁一次; 线程2:adc采样; 线程3:oled显示; 在线程2中,采样到数据后,通过消息队列发送到线程3,线程3收到消息后,更新oled显示。
2、RT-Thread配置
默认是没有使能消息队列的,需要使能消息队列;
或
3、代码实现
(1)Led初始化及led线程
代码语言:javascript复制static rt_thread_t led_thread = RT_NULL;
void LedInit(void)
{
/* enable the LED GPIO clock */
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_GPIOC);
/* configure LED GPIO pin */
gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_7 | GPIO_PIN_8);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7 | GPIO_PIN_8);
/* reset LED GPIO pin */
gpio_bit_reset(GPIOA, GPIO_PIN_7 | GPIO_PIN_8);
}
static void led_thread_entry(void *parameter)
{
while (1)
{
gpio_bit_toggle(GPIOA, GPIO_PIN_7|GPIO_PIN_8);
rt_thread_mdelay(500);
rt_kprintf("led blinkrn");
}
}
void LedThreadStart(void)
{
led_thread = rt_thread_create( "led", /*线程名字*/
led_thread_entry,/*线程入口函数*/
RT_NULL,/*线程入口函数参数*/
256, /*线程栈大小*/
4 , /*线程优先级*/
20); /*线程时间片*/
rt_thread_startup (led_thread);
}
(2)adc初始化以及adc线程
代码语言:javascript复制static rt_thread_t adc_thread = RT_NULL;
static uint8_t adc_data[12];
void AdcInit(void)
{
AdcGpioInit();
AdcConfig();
}
static void adc_thread_entry(void *parameter)
{
uint16_t ref=0;
float ch1=0;
float vdd=0;
float temp=0;
uint16_t part1=0,part2=0;
int result;
uint8_t *p=(uint8_t *)parameter;
while(1)
{
ref=AdcSample(ADC_CHANNEL_17);
vdd=1.2/(float)ref*4095;
part1=(uint32_t)vdd;
part2=vdd*1000-part1*1000;
rt_kprintf("rnVdd=%d.%d Vrn",part1,part2);
rt_memcpy(&p[0],(uint8_t *)&part1,2);
rt_memcpy(&p[2],(uint8_t *)&part2,2);
ch1=AdcSample(ADC_CHANNEL_1)*vdd / 4095;
part1=(uint32_t)ch1;
part2=ch1*1000-part1*1000;
rt_kprintf("Channel 1=%d.%d Vrn",part1,part2);
rt_memcpy(&p[4],(uint8_t *)&part1,2);
rt_memcpy(&p[6],(uint8_t *)&part2,2);
temp = ((float)((int16_t)AdcSample(ADC_CHANNEL_16) - (*(int16_t *)(0x1FFFF7F8)))* vdd / 4095 * 1000 / vdd) 30;
part1=(uint32_t)temp;
part2=temp*100-part1*100;
rt_kprintf("Temp=%d.%d rnrn",part1,part2);
rt_memcpy(&p[8],(uint8_t *)&part1,2);
rt_memcpy(&p[10],(uint8_t *)&part2,2);
extern rt_mq_t adc_to_oled_mq;
result = rt_mq_send(adc_to_oled_mq, p,12); //发送消息
if (result != RT_EOK)
{
rt_kprintf("%s,send mq errrn",adc_thread->name);
}
else
{
rt_kprintf("%s,send mq successrn",adc_thread->name);
}
rt_memset(p,'