|
本系列文章主要介绍如何使用EFM8控制器制作一款智能环境光监测仪,共包含5个部分: ● Part 1:在LCD上显示测量值 ● Part 2:理解和实现ADC ● Part 3:测量和解析环境光照度 ● Part 4:过零检测 ● Part 5:使用双向可控硅调节灯亮度
所需的硬件/软件 ● SLSTK2000A EFM8评估板 ● Simplicity Studio集成开发环境 ● SCILAB
简介 该系列文章的总体目标是设计一款智能环境光监测仪,用于分析室内光照强度,并据此执行对应的响应动作,如控制灯调光器。在开发该项目的过程中,我们需要一种简便的方法来报告表示电流和电压幅度的模数转换值。因此,我们将先编写固件,该固件可以将存储在变量中的普通数值转换为一系列单独的数字,然后这些数字将决定我们传输哪个像素数据数组到LCD模块中。
该固件设计为显示三位数的测量值,单位为毫伏、伏、微安或毫安。如果显示的单位为伏或毫安,则自动启用第一个数字后面的小数点。这意味着显示接口可以处理0μA至9.99 mA的电流幅度和0 mV至9.99 V的电压幅度。但是,量程范围内的大部分值永远不会被使用 - 光传感器的最大输出电流为5 mA,并且ADC无法测量高于其参考电压的电压,在此设计中为2.4 V.
端口I / O
如上图所示,SPI信号被使能并映射到连接到相应LCD信号的引脚。 SPI芯片选择信号由固件和P0.1输出引脚控制,因为内置的SPI芯片选择信号与LCD接口不兼容。
外设和中断 在本篇文章中,我们只需要使用两个外设:SPI和Timer4。 SPI配置为3线主机模式,时钟分频器设置为产生875 kHz的SPI时钟频率。
SPI中断使能,因为SPI传输由SPI中断服务程序中的状态机控制。发送每个字节后,将触发中断。 Timer4用于短延时,例如LCD模块数据表中指定的建立和保持延迟时间。一个Timer4计数约为490 ns,因此如果我们需要6μs的延迟,我们将Timer4寄存器设置为零并等待计数达到12。
固件 该项目的固件可分为三个主要部分:LCD通信接口、将存储在变量中的数字转换为一系列单个数字的函数,以及用于更新LCD像素数据阵列的程序。
LCD接口 我们使用多行更新模式与LCD通信。当微控制器启动时,它会将LCD的所有像素点清除为白色。随后通过将128位像素数据写入一个或多个行地址来更新LCD。所有LCD更新都由“LCDControl.c”源文件中的UpdateLCD()函数启动,数据传输过程在SPI中断服务程序中继续。该项目中的LCD通信接口包括对我们在前几篇文章中使用的改进:每次调用UpdateLCD()都可以通过将适当的第一行和最后一行地址放入LCDLineStart和LCDLineStop变量来指定要更新的显示部分。
将数值转换为单个数字 重要的是,要理解存储在变量中的数值与我们看到的数值的一系列数字基本上不同。变量只是一个1和0的序列;此序列可以以多种方式解释 - 例如,无符号整数、有符号整数或浮点值。然后需要进一步转换以视觉形式表达该解释值。将变量值转换为一系列数字或字符的标准C语言方式是printf()函数,它包含在库中。但是,尽可能避免使用库例程是明智的,主要是因为设计自己的代码更有趣、更有价值、更有启发性。但是,也有一些实际的好处,因为您定制设计的代码可以提供所有所需的功能,同时还可以提高执行速度或降低内存需求。
数值转换过程的关键是取余运算符,由“%”符号表示: - /*the modulus operator is used to obtain the first digit, which
- corresponds to the remainder that would result from dividing
- by 10; we then twice divide the measured value by 10 and repeat
- the modulus operation to obtain the remainders corresponding
- to the next two digits*/
- remainder = MeasuredValue % 10;
- SetLCDDigit(DIGIT_POS_3, MatchDigittoArray(remainder));
- MeasuredValue = MeasuredValue/10;
- remainder = MeasuredValue % 10;
- SetLCDDigit(DIGIT_POS_2, MatchDigittoArray(remainder));
- MeasuredValue = MeasuredValue/10;
- remainder = MeasuredValue % 10;
- SetLCDDigit(DIGIT_POS_1, MatchDigittoArray(remainder));
复制代码
如果将变量的整数值除以“%”符号右侧的数字,取余运算符返回产生的余数。如代码摘录所示,我们使用“MeasuredValue%10”来提取对应于最右边数字的数字。您可以将其视为将所有数字向右移动一位,然后在越过小数点时将最右边的数字切掉。
但请注意,模数运算符实际上并未改变原始值。因此,在提取第一个数字后,我们将原始值除以10并重复模数运算以提取下一个数字。 MatchDigittoArray()函数包含一个简单的switch语句,用于确定哪个LCD像素数据数组与其余变量中包含的数字相对应。
从数字到像素数据 二维数组用于保持LCD像素数据。在该项目中,像素数据阵列具有30行,因为数字显示在LCD屏幕的中间30行中。以下功能将数字的像素值复制到LCD像素数据阵列中;传递给该函数的参数是数字位置(第一、第二或第三)和指向数字像素数据数组的指针。类似的代码用于显示适当的单位缩写(μA,mA,mV或V)。 - void SetLCDDigit(unsigned char DigitPosition, unsigned char *LCD_Digit)
- {
- unsigned char row;
- unsigned char column_byte;
- unsigned char column_byte_begin, column_byte_end;
- /*this switch statement determines which column bytes to
- modify based on the chosen digit position (first, second, or third)*/
- switch(DigitPosition)
- {
- case DIGIT_POS_1: column_byte_begin = 0;
- break;
- case DIGIT_POS_2: column_byte_begin = DIGIT_WIDTH_BYTE;
- break;
- case DIGIT_POS_3: column_byte_begin = DIGIT_WIDTH_BYTE*2;
- break;
- }
- column_byte_end = column_byte_begin + DIGIT_WIDTH_BYTE;
- /*here the LCD display data array is loaded with the bytes
- from the appropriate pixel data array generated by Scilab*/
- for(row = 0; row < DIGIT_HEIGHT_PIX; row++)
- {
- for(column_byte = column_byte_begin; column_byte < column_byte_end; column_byte++)
- {
- LCDDisplayData[row][column_byte] = *LCD_Digit;
- LCD_Digit++;
- }
- }
- //wait until the SPI state variable indicates that the bus is available for a new transfer
- while(LCDTxState != IDLE);
- //the SPI state machine needs to know the first and last lines to be updated
- LCDLineStart = DIGIT_BEGIN_LINE;
- LCDLineStop = DIGIT_BEGIN_LINE + DIGIT_HEIGHT_PIX;
- UpdateLCD();
- }
复制代码
如果单位为毫安或伏特,则小数点自动显示,如果单位为微安或毫伏,则自动关闭。 像素数据阵列被更新以显示或不显示小数点,如下所示: - void SetDecimalPoint(unsigned char DecimalPointStatus)
- {
- unsigned char row;
- unsigned char column_byte;
- //the decimal point can only be located after the first digit
- column_byte = DIGIT_WIDTH_BYTE - 1;
- /*the decimal point requires an area of 4 pixels by 4 pixels,
- but the displayed shape is rounded because the top and bottom
- lines have 2 horizontal black pixels and the 2 middle lines have
- 4 horizontal black pixels*/
- /*note the use of bitwise AND and OR operations here: bitwise
- operations are needed because the decimal point does not cover
- 8 horizontal pixels (i.e., one horizontal byte), and AND and OR are used
- to ensure that other pixels in the byte are not altered*/
- if(DecimalPointStatus == DEC_POINT_OFF)
- {
- row = (DIGIT_HEIGHT_PIX - DEC_POINT_HEIGHT);
- LCDDisplayData[row][column_byte] |= BIT0;
- LCDDisplayData[row][column_byte + 1] |= BIT7;
- row++;
- LCDDisplayData[row][column_byte] |= (BIT1|BIT0);
- LCDDisplayData[row][column_byte + 1] |= (BIT7|BIT6);
- row++;
- LCDDisplayData[row][column_byte] |= (BIT1|BIT0);
- LCDDisplayData[row][column_byte + 1] |= (BIT7|BIT6);
- row++;
- LCDDisplayData[row][column_byte] |= BIT0;
- LCDDisplayData[row][column_byte + 1] |= BIT7;
- }
- if(DecimalPointStatus == DEC_POINT_ON)
- {
- row = (DIGIT_HEIGHT_PIX - DEC_POINT_HEIGHT);
- LCDDisplayData[row][column_byte] &= ~BIT0;
- LCDDisplayData[row][column_byte + 1] &= ~BIT7;
- row++;
- LCDDisplayData[row][column_byte] &= ~(BIT1|BIT0);
- LCDDisplayData[row][column_byte + 1] &= ~(BIT7|BIT6);
- row++;
- LCDDisplayData[row][column_byte] &= ~(BIT1|BIT0);
- LCDDisplayData[row][column_byte + 1] &= ~(BIT7|BIT6);
- row++;
- LCDDisplayData[row][column_byte] &= ~BIT0;
- LCDDisplayData[row][column_byte + 1] &= ~BIT7;
- }
- //wait until the SPI state variable indicates that the bus is available for a new transfer
- while(LCDTxState != IDLE);
- //the SPI state machine needs to know the first and last lines to be updated
- LCDLineStart = DEC_PNT_LINE_BEGIN;
- LCDLineStop = DEC_PNT_LINE_END;
- UpdateLCD();
- }
复制代码
“AmbientLightMonitor_main.c”中的while循环如下所示: - number = 800;
- while (1)
- {
- ConvertMeasurementandDisplay(CURRENT, number);
- //these instructions provide a delay, so that the displayed number increments more slowly
- while(LCDTxState != IDLE);
- for(n = 0; n < 0xFFFF; n++)
- SFRPAGE = TIMER4_PAGE; TMR4 = 0; while(TMR4 < 0xFF00);
- number++;
- if(number == 10000) //ConvertMeasurementandDisplay() only accepts numbers up to 9999
- number = 0;
- }
复制代码
SCILAB 数字和单位缩写的像素数据数组是使用Scilab脚本生成的。在本篇文章中,数字(以及使用此脚本处理的digit.bmp图像)的尺寸是30个垂直像素乘24个水平像素。 它们比之前使用的10×8像素字符更大、更具视觉吸引力。 请注意,水平像素尺寸再次选为8的倍数,以确保在更新LCD像素数据数组时不需要使用笨拙的按位操作。 |