#include"reg52.h"
unsigned char code table[]={0xfe,0xfc,0xf0,0xe0,0xc0,0x80,0x00};
void main()
{
unsigned char i;
while(1)
{
i++;
i&=0x07;
P1=table;
}
}
低电平点亮LED,i=0到7;
while循环周期*8=PWM的周期
当i=0时,D0亮
当i=1时,D0,D1亮
当i=2时,D0,D1,D2亮
。。。。。。
当i=7时,D0,D1,D2,D3,D4,D5,D6,D7全亮
也就是说,在while的8个循环中,D0一直亮,D1则只有7次亮。。。。。。D7则只有1次亮
按照这个算法,只要定义一个一维表格,就可以静态现实LED的8个不同亮度,且亮度次序任意
如果定义一个二维表格,则可以动态现实LED亮度,几乎可以任意定义次序花样
下面来看一个完整的二维数组渐变程序
完整代码下载地址:http://www.51hei.com/f/jianb.rar
#include "reg52.h" unsigned char code table[37][8]= //定义一个2维数组 {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,//off 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,//D0亮度等级1/8 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfc,//D1亮度等级1/8 0xff,0xff,0xff,0xff,0xff,0xff,0xfd,0xfc,//D1亮度等级2/8 0xff,0xff,0xff,0xff,0xff,0xff,0xfd,0xf8,//D2亮度等级1/8 0xff,0xff,0xff,0xff,0xff,0xff,0xf9,0xf8,//D2亮度等级2/8 0xff,0xff,0xff,0xff,0xff,0xfb,0xf9,0xf8,//D2亮度等级3/8 0xff,0xff,0xff,0xff,0xff,0xfb,0xf9,0xf0,//D3 0xff,0xff,0xff,0xff,0xff,0xfb,0xf1,0xf0, 0xff,0xff,0xff,0xff,0xff,0xf3,0xf1,0xf0, 0xff,0xff,0xff,0xff,0xf7,0xf3,0xf1,0xf0, 0xff,0xff,0xff,0xff,0xf7,0xf3,0xf1,0xe0,//D4 0xff,0xff,0xff,0xff,0xf7,0xf3,0xe1,0xe0, 0xff,0xff,0xff,0xff,0xf7,0xe3,0xe1,0xe0, 0xff,0xff,0xff,0xff,0xe7,0xe3,0xe1,0xe0, 0xff,0xff,0xff,0xef,0xe7,0xe3,0xe1,0xe0, 0xff,0xff,0xff,0xef,0xe7,0xe3,0xe1,0xc0,//D5 0xff,0xff,0xff,0xef,0xe7,0xe3,0xc1,0xc0, 0xff,0xff,0xff,0xef,0xe7,0xc3,0xc1,0xc0, 0xff,0xff,0xff,0xef,0xc7,0xc3,0xc1,0xc0, 0xff,0xff,0xff,0xcf,0xc7,0xc3,0xc1,0xc0, 0xff,0xff,0xdf,0xcf,0xc7,0xc3,0xc1,0xc0, 0xff,0xff,0xdf,0xcf,0xe7,0xe3,0xc1,0x80,//D6 0xff,0xff,0xdf,0xcf,0xe7,0xe3,0x81,0x80, 0xff,0xff,0xdf,0xcf,0xe7,0x83,0x81,0x80, 0xff,0xff,0xdf,0xcf,0x87,0x83,0x81,0x80, 0xff,0xff,0xdf,0x8f,0x87,0x83,0x81,0x80, 0xff,0xff,0x9f,0x8f,0x87,0x83,0x81,0x80, 0xff,0xbf,0x9f,0x8f,0x87,0x83,0x81,0x80, 0xff,0xbf,0x9f,0x8f,0x87,0x83,0x81,0x00,//D7 0xff,0xbf,0x9f,0x8f,0x87,0x83,0x01,0x00, 0xff,0xbf,0x9f,0x8f,0x87,0x03,0x01,0x00, 0xff,0xbf,0x9f,0x8f,0x07,0x03,0x01,0x00, 0xff,0xbf,0x9f,0x0f,0x07,0x03,0x01,0x00, 0xff,0xbf,0x1f,0x0f,0x07,0x03,0x01,0x00, 0xff,0x3f,0x1f,0x0f,0x07,0x03,0x01,0x00, 0x7f,0x3f,0x1f,0x0f,0x07,0x03,0x01,0x00, }; void main() { unsigned char i; unsigned int j; unsigned int counter; bit flag=0; while(1) { i++; i&=0x07; //i=0到7 counter++; if(counter==12000) //延时对j++ { if(flag==0) j++; else j--; counter=0; if(j==36 || j==0) { flag=!flag; //决定正向扫描还是逆向扫描 i=0; } } P1=table[j]; //扫描第i列,扫描第j行 } }
注意:用2003时,是高电平点亮LED。要对它取反。
上一篇:外部中断控制
下一篇:单片机驱动19264a液晶显示英文程序
推荐阅读最新更新时间:2024-03-16 13:06
设计资源 培训 开发板 精华推荐
- ASML在2024 年投资者日会议上就市场机遇提供最新看法
- AMD将裁员4%,以在人工智能芯片领域争取更强的市场地位
- Arm:以高效计算平台为核心,内外协力共筑可持续未来
- NEC收获新超算订单:英特尔CPU+AMD加速器+英伟达交换机
- 高通推出其首款 RISC-V 架构可编程连接模组 QCC74xM,支持 Wi-Fi 6 等协议
- 消息称内存原厂考虑 HBM4 采用无助焊剂键合,进一步降低层间间隙
- 随时随地享受大屏幕游戏:让便携式 4K 超高清 240Hz 游戏投影仪成为现实
- 高信噪比MEMS麦克风驱动人工智能交互
- 里程碑式进展!思特威CMOS图像传感器芯片单月出货超1亿颗!
- 下一代汽车微控制器:意法半导体技术解析