概述
一般仪器仪表的信号电流都为4-20mA,指最小电流为4mA,最大电流为20mA。传输信号时候,要考虑到导线上也有电阻,如果用电压传输则会在导线的产生一定的压降,那接收端的信号就会产生一定的误差,所以使用电流信号作为变送器的标准传输。为什么选择4-20mA而不是0-20mA呢?4ma而不是0ma是用来检测线路开路的,如果0是最小,那么开路故障就检测不到了。为了解决上述问题和避开相关噪声的影响,我们用电流来传输信号,因为电流对噪声并不敏感。4~20mA的电流环便是用4mA表示零信号,用20mA表示信号的满刻度,而低于4mA高于20mA的信号用于各种故障的报警。
很多控制器接受来自各种检测仪表的0~20mA或4~20mA电流,制作一个0-20mA的的信号发生器,可以给很多仪器仪表做校准或测试。
2. 总体方案设计
2.1. 设计目标
单片机控制4路PWM,输出的PWM信号控制输出电压在0-3.0V之间,经过电流电压转电流电路变成电流信号,输出电流在0-20mA之间。按键调节输出电流,oled实时显示4路电流值。
2.2. 设计思路
芯片选择STM32F030C8T6,带4路PWM输出,性价比高,同时支持rt-thread操作系统,这里我们选用2.1的版本。OLED选用中景园电子0-96寸OLED。
2.2.1.硬件电路原理
图1是一个电压转电流的典型电路。单片机输出PWM,控制Vi电压电平在0-3V之间,RL流过的电流为0-20ma。
图1 电压转电流电路
图2 OLED显示电路
由于MCU内部可配置上拉电阻,所以可以直接将按键接到MCU上。
图3 按键电路
2.2.2.软件设计流图
2.2.3.关键代码
按键部分代码。
/* key thread entry */
staticvoid key_thread_entry(void*parameter)
{
KEY_e i;
uint8_t key_state1[KEY_NUM];
uint8_t key_state2[KEY_NUM];
uint8_t key_counter[KEY_NUM];
rt_base_t level;
memset(key_counter, 0, sizeof(key_counter));
while(1)
{
for (i=KEY1; i
key_state1[i] = rt_hw_key(i);
}
rt_thread_delay(RT_TICK_PER_SECOND / 20);
for (i=KEY1; i
key_state2[i] = rt_hw_key(i);
}
for (i=KEY1; i
if (key_state1[i] == key_state2[i] &&
key_state1[i] == 0)
{
level =rt_hw_interrupt_disable();
if (key_counter[i] == 0)
{
switch(i)
{
case KEY2:
if (pwm_channel< 3)
{
pwm_channel++;
}
break;
case KEY1:
if (pwm_channel> 0)
pwm_channel--;
break;
case KEY3:
if(pwm_value[pwm_channel] < 20000)
pwm_value[pwm_channel]++;
break;
case KEY4:
if(pwm_value[pwm_channel] > 0)
pwm_value[pwm_channel]--;
break;
case KEY5:
if(pwm_value[pwm_channel] < 16000)
pwm_value[pwm_channel] += 4000;
else
pwm_value[pwm_channel] = 20000;
break;
case KEY6:
if(pwm_value[pwm_channel] >= 4000)
pwm_value[pwm_channel] -= 4000;
else
pwm_value[pwm_channel] = 0;
break;
}
rt_kprintf("key %dclicked ", i);
}
if (key_counter[i] >= 5)
{
switch(i)
{
case KEY2:
if (pwm_channel< 3)
{
pwm_channel++;
}
break;
case KEY1:
if (pwm_channel> 0)
pwm_channel--;
break;
case KEY3:
if(pwm_value[pwm_channel] < 20000)
pwm_value[pwm_channel]++;
break;
case KEY4:
if(pwm_value[pwm_channel] > 0)
pwm_value[pwm_channel]--;
break;
case KEY5:
if(pwm_value[pwm_channel] < 16000)
pwm_value[pwm_channel]+= 4000;
else
pwm_value[pwm_channel] = 20000;
break;
case KEY6:
if(pwm_value[pwm_channel] >= 4000)
pwm_value[pwm_channel] -= 4000;
else
pwm_value[pwm_channel] = 0;
break;
}
rt_kprintf("key%d pressed ", i);
}
if (key_counter[i] < 5)
{
key_counter[i]++;
}
rt_hw_interrupt_enable(level);
}
else
{
key_counter[i] = 0;
}
}
rt_thread_delay(RT_TICK_PER_SECOND / 100);
}
}
oled显示部分代码。
/* oled thread entry */
staticvoid oled_thread_entry(void*parameter)
{
uint8_t i;
rt_base_tlevel;
char str_pwm[64];
OLED_Init();
OLED_Clear();
PWM_TIM1(999, 1); //48MHZ/(999+1)/(1+1) = 24KHZ
while(1)
{
//OLED_ShowString(0, 3,"1.3' OLED TEST");
if ((memcmp(pwm_value_temp, pwm_value, sizeof(pwm_value)) != 0) ||
(pwm_channel_temp != pwm_channel))
{
level = rt_hw_interrupt_disable();
memcpy((char *)pwm_value_temp, (char *)pwm_value, sizeof(pwm_value));
pwm_channel_temp = pwm_channel;
rt_hw_interrupt_enable(level);
for (i=0; i<4; i++)
{
if (pwm_channel == i)
{
snprintf(str_pwm, 64,"* %2d.%03d ma", pwm_value[i]/1000, pwm_value[i]%1000);
}
else
{
snprintf(str_pwm, 64," %2d.%03d ma",pwm_value[i]/1000, pwm_value[i]%1000);
}
OLED_ShowString(0, i*2,(uint8_t *)str_pwm);
}
TIM_SetCompare1(TIM1, 0.915 *(pwm_value_temp[3] * 999) / 20000);
TIM_SetCompare2(TIM1, 0.915 *(pwm_value_temp[2] * 999) / 20000);
TIM_SetCompare3(TIM1, 0.915 *(pwm_value_temp[1] * 999) / 20000);
TIM_SetCompare4(TIM1, 0.915 *(pwm_value_temp[0] * 999) / 20000);
rt_thread_delay(RT_TICK_PER_SECOND / 10);
}
else
{
//OLED_ShowString(63,6,"CODE:");
rt_thread_delay(RT_TICK_PER_SECOND / 10);
}
}
}
2.2.4.RTT使用情况
按键的初始化函数可以不用再main函数中添加,而是在key.c中调用下面的函数就可以了INITDEVICEEXPORT(rthwkey_init),代码看起来干净整洁。通过scons来裁剪配置系统,不需要人为删减代码,非常好用。Finsh组件,方便调试,方便自定义添加串口命令。代码类linux编程风格。提供POSIX 标准接口,上层代码移植方便。