三. 软件设计
1. 摇棒的启动
通过P3.6和P3.7输出驱动音圈电机的信号, 由于没有设置检测摇棒的传感器,时间间隔只能跟据实验确定。在调试中发现,当驱动摇棒的信号转换加速到一定的时候,从静止状态转到正常摆动状态发生困难,因此需要由慢到快的驱动过程。由下面的程序段实现。
do {
mm--;
DelayMs(120+mm*5); //逐渐缩短延时间
put1=~put1; put2=~put2; //转换输出口电平状态
}while(mm>0);
2. 秒闪现的实现
数字和字符的闪现是通过变量Ms和数组w[]实现的,如需要七位字符中的间的第4位闪现,则w[3]=1。这是通过全局变量Ms在void timer0(void)函数中每隔一秒改变一次状态(0或1),在void timer1(void)函数中的下列语句中实现。
if((put1==1)&&(ii<7)&&(Ms*w[ii]==0)) P2=~ASCIIDOC[v[ii]*6+jj]; else P2=0xff;
3. 校正显示不均匀
调试中发现,显示屏上的字符并不是一样宽的,仔细分析是由于摇棒在运动中一直受力,使摇棒产生加速运动,这样就需要在程序中加一校正因子,逐渐缩短显示的时间段。具体是通过变量tt在函数void timer1(void)中实现的。
4. 按键去抖程序
按键的去抖采用软件编程实现,其方法是当检测到按键的接口出现低电平后,隔一段时间再检测,如还是低电平,则确认为有效,否则无效。具体的程序是通过函数unsigned char ChKey(bit Key)实现。
5. 显示同步
调试时要根据实际情况确定Ti的值,保证显示正常。
6.预留的部分功能
提供的程序除提供了一个完整的时钟程序外,还预留了显示七个字符功能(见图12),只是主程序要作一些修改。
图12
7.完整C51程序代码
/*
POV摇摆显示LED钟C51程序
*/
#include
/*硬件端口定义*/
sbit set0=P0^1;
sbit set1=P0^0;
sbit set2=P0^2;
sbit put1=P3^6;
sbit put2=P3^7;
/*时钟用数组*/
unsigned char BUFFER[]={0,0,0,0};
unsigned char maxnum[]={59,23};
/*显示用数组*/
unsigned int v[20];
unsigned int w[7];
/*字符字模*/
unsigned char code ASCIIDOC[] = // ASCII
{
0x7C,0x8A,0x92,0xA2,0x7C,0x00, // -0-00
0x00,0x42,0xFE,0x02,0x00,0x00, // -1-01
0x46,0x8A,0x92,0x92,0x62,0x00, // -2-02
0x84,0x82,0x92,0xB2,0xCC,0x00, // -3-03
0x18,0x28,0x48,0xFE,0x08,0x00, // -4-04
0xE4,0xA2,0xA2,0xA2,0x9C,0x00, // -5-05
0x3C,0x52,0x92,0x92,0x8C,0x00, // -6-06
0x80,0x8E,0x90,0xA0,0xC0,0x00, // -7-07
0x6C,0x92,0x92,0x92,0x6C,0x00, // -8-08
0x62,0x92,0x92,0x94,0x78,0x00, // -9-09
0x00,0x00,0x00,0x00,0x00,0x00, // - -10
0x00,0x00,0xFA,0x00,0x00,0x00, // -!-11
0x04,0x08,0x10,0x20,0x40,0x00, // -/-12
0x00,0x6C,0x6C,0x00,0x00,0x00, // -:-13
0x3E,0x48,0x88,0x48,0x3E,0x00, // -A-14
0xFE,0x92,0x92,0x92,0x6C,0x00, // -B-15
0x7C,0x82,0x82,0x82,0x44,0x00, // -C-16
0xFE,0x82,0x82,0x82,0x7C,0x00, // -D-17
0xFE,0x92,0x92,0x92,0x82,0x00, // -E-18
0xFE,0x90,0x90,0x90,0x80,0x00, // -F-19
0x7C,0x82,0x8A,0x8A,0x4E,0x00, // -G-20
0xFE,0x10,0x10,0x10,0xFE,0x00, // -H-21
0x00,0x82,0xFE,0x82,0x00,0x00, // -I-22
0x04,0x02,0x82,0xFC,0x80,0x00, // -J-23
0xFE,0x10,0x28,0x44,0x82,0x00, // -K-24
0xFE,0x02,0x02,0x02,0x02,0x00, // -L-25
0xFE,0x40,0x30,0x40,0xFE,0x00, // -M-26
0xFE,0x20,0x10,0x08,0xFE,0x00, // -N-27
0x7C,0x82,0x82,0x82,0x7C,0x00, // -O-28
0xFE,0x90,0x90,0x90,0x60,0x00, // -P-29
0x7C,0x82,0x8A,0x84,0x7A,0x00, // -Q-30
0xFE,0x90,0x98,0x94,0x62,0x00, // -R-31
0x64,0x92,0x92,0x92,0x4C,0x00, // -S-32
0x80,0x80,0xFE,0x80,0x80,0x00, // -T-33
0xFC,0x02,0x02,0x02,0xFC,0x00, // -U-34
0xF8,0x04,0x02,0x04,0xF8,0x00, // -V-35
0xFE,0x04,0x18,0x04,0xFE,0x00, // -W-36
0xC6,0x28,0x10,0x28,0xC6,0x00, // -X-37
0xC0,0x20,0x1E,0x20,0xC0,0x00, // -Y-38
0x86,0x8A,0x92,0xA2,0xC2,0x00, // -Z-39
0x24,0x2A,0x2A,0x1C,0x02,0x00, // -a-40
0xFE,0x14,0x22,0x22,0x1C,0x00, // -b-41
0x1C,0x22,0x22,0x22,0x10,0x00, // -c-42
0x1C,0x22,0x22,0x14,0xFE,0x00, // -d-43
0x1C,0x2A,0x2A,0x2A,0x10,0x00, // -e-44
0x10,0x7E,0x90,0x90,0x40,0x00, // -f-45
0x19,0x25,0x25,0x25,0x1E,0x00, // -g-46
0xFE,0x10,0x20,0x20,0x1E,0x00, // -h-47
0x00,0x00,0x9E,0x00,0x00,0x00, // -i-48
0x00,0x01,0x11,0x9E,0x00,0x00, // -j-49
0xFE,0x08,0x14,0x22,0x02,0x00, // -k-50
0x00,0x82,0xFE,0x02,0x00,0x00, // -l-51
0x1E,0x20,0x1E,0x20,0x1E,0x00, // -m-52
0x20,0x1E,0x20,0x20,0x1E,0x00, // -n-53
0x1C,0x22,0x22,0x22,0x1C,0x00, // -o-54
0x3F,0x24,0x24,0x24,0x18,0x00, // -p-55
0x18,0x24,0x24,0x24,0x3F,0x00, // -q-56
0x20,0x1E,0x20,0x20,0x10,0x00, // -r-57
0x12,0x2A,0x2A,0x2A,0x24,0x00, // -s-58
0x20,0xFC,0x22,0x22,0x24,0x00, // -t-59
0x3C,0x02,0x02,0x3C,0x02,0x00, // -u-60
0x38,0x04,0x02,0x04,0x38,0x00, // -v-61
0x3C,0x02,0x3C,0x02,0x3C,0x00, // -w-62
0x22,0x14,0x08,0x14,0x22,0x00, // -x-63
0x39,0x05,0x05,0x09,0x3E,0x00, // -y-64
0x22,0x26,0x2A,0x32,0x22,0x00, // -z-65
};
unsigned int TZ,Ti;
unsigned char ii,jj,nn,mm,tt, Ms;