完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
文章最后面附有:数码管一些特殊运用。
数码管电路图 数码管原理和结构图 真值表,编程时用的到,这边先列出来。 下面就是点亮大数码管(共阳级)的代码: //七段数码管显示实验 //2018.7.22 #include #include #define DataPort P1 // 换接口,只需修改这里 ***it Seg_Latch = P2^2; //段锁存 ***it Bit_Latch = P2^3; //位锁存 unsigned char code Seg_Code[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; unsigned char code Bit_Code[] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; //段码,位码 void main() { int i; while(1) { for(i = 0;i < 16;i++) { P1 = ~Seg_Code; DelayMs(255); } } } 点亮大数码管(共阴级)的代码: //七段数码管显示实验 //2018.7.22 #include #include #define DataPort P1 // 换接口,只需修改这里 ***it Seg_Latch = P2^2; //段锁存 ***it Bit_Latch = P2^3; //位锁存 unsigned char code Seg_Code[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; unsigned char code Bit_Code[] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; //段码,位码 void main() { unsigned char i; unsigned char j; while(1) { DataPort = 0; //送段码, Seg_Latch = 1; //开门 Seg_Latch = 0; //关门 for(j = 0;j < 8;j++) { DataPort = Bit_Code[j]; //送位码,选择哪个灯亮 Bit_Latch = 1; //开门 Bit_Latch = 0; //关门 //DelayMs(255); DataPort = Seg_Code[j]; //送段码, Seg_Latch = 1; //开门 Seg_Latch = 0; DelayMs(255); } for(j = 0;j < 8;j++) { DataPort = 0; //送段码, Seg_Latch = 1; //开门 Seg_Latch = 0; //关门 DataPort = Bit_Code[j]; //送位码,选择哪个灯亮 Bit_Latch = 1; //开门 Bit_Latch = 0; //关门 DataPort = Seg_Code[j + 8]; //送段码, Seg_Latch = 1; //开门 Seg_Latch = 0; //关门 DelayMs(255); } } } //七段数码管显示实验 //2018.7.22 #include #include #define DataPort P1 // 换接口,只需修改这里 ***it Seg_Latch = P2^2; //段锁存 ***it Bit_Latch = P2^3; //位锁存 unsigned char code Seg_Code[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x40}; //unsigned char code Seg_Code[] = {0x30,0x38,0x3f,0x3e,0x79,0x6e,0x3f,0x3e}; unsigned char code Bit_Code[] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; //段码,位码 void display1() //0-F,使用共阳极数码管显示 { int i; while(1) { for(i = 0;i < 16;i++) { P1 = ~Seg_Code; DelayMs(255); } } } void display2() //0-7(跑马灯),使用共阴极数码管显示 { unsigned char i; while(1) { for(i = 0;i < 8;i++) { DataPort = 0; //送段码, Seg_Latch = 1; //开门 Seg_Latch = 0; //关门 DataPort = Bit_Code; //送位码,选择哪个灯亮 Bit_Latch = 1; //开门 Bit_Latch = 0; //关门 //DelayMs(255); DataPort = Seg_Code; //送段码, Seg_Latch = 1; //开门 Seg_Latch = 0; //关门 DelayMs(255); } } } void display3() //0-F(跑马灯形式) { unsigned char i; unsigned char j; while(1) { DataPort = 0; //送段码, Seg_Latch = 1; //开门 Seg_Latch = 0; //关门 for(j = 0;j < 8;j++) { DataPort = Bit_Code[j]; //送位码,选择哪个灯亮 Bit_Latch = 1; //开门 Bit_Latch = 0; //关门 //DelayMs(255); DataPort = Seg_Code[j]; //送段码, Seg_Latch = 1; //开门 Seg_Latch = 0; DelayMs(255); } for(j = 0;j < 8;j++) { DataPort = 0; //送段码, Seg_Latch = 1; //开门 Seg_Latch = 0; //关门 DataPort = Bit_Code[j]; //送位码,选择哪个灯亮 Bit_Latch = 1; //开门 Bit_Latch = 0; //关门 DataPort = Seg_Code[j + 8]; //送段码, Seg_Latch = 1; //开门 Seg_Latch = 0; //关门 DelayMs(255); } } } void display4(unsigned char x,unsigned char num) //0—99//参数选两个数码管(0-8随机),参数X控制速度//精确版 { unsigned char i,j,Data[8]; Data[6] = num / 10; Data[7] = num % 10; for(i = 0;i < x;i++) { for(j = 6;j < 8;j++) { DataPort = 0; //送段码, Seg_Latch = 1; //开门 Seg_Latch = 0; //关门 DataPort = Bit_Code[j]; //送位码,选择哪个灯亮 Bit_Latch = 1; //开门 Bit_Latch = 0; //关门 DataPort = Seg_Code[Data[j]]; //送段码, Seg_Latch = 1; //开门 Seg_Latch = 0; DelayMs(3); } } } void display5(unsigned char x,unsigned char y,unsigned char z) //0—999//参数选两个数码管(0-8随机),简陋版(个位过快) { unsigned char i,j,k; while(1) { for(i = 0;i < 10;i++) { for(j = 0;j < 10;j++) { for(k = 0;k < 10;k++) { DataPort = 0; //送段码, Seg_Latch = 1; //开门 Seg_Latch = 0; //关门 DataPort = Bit_Code[x]; //送位码,选择哪个灯亮 Bit_Latch = 1; //开门 Bit_Latch = 0; //关门 DataPort = Seg_Code; //送段码, Seg_Latch = 1; //开门 Seg_Latch = 0; DelayMs(5); DataPort = Bit_Code[y]; //送位码,选择哪个灯亮 Bit_Latch = 1; //开门 Bit_Latch = 0; //关门 DataPort = Seg_Code[j]; //送段码 Seg_Latch = 1; //开门 Seg_Latch = 0; DelayMs(5); DataPort = Bit_Code[z]; //送位码,选择哪个灯亮 Bit_Latch = 1; //开门 Bit_Latch = 0; //关门 DataPort = Seg_Code[k]; //送段码 Seg_Latch = 1; //开门 Seg_Latch = 0; DelayMs(5); } } } } } void display6(unsigned char x,unsigned int num) //0—999//参数选两个数码管(0-8随机),精确版 { unsigned int i,j,Data[8]; Data[5] = num / 100; Data[6] = num / 10 % 10; Data[7] = num % 10; for(i = 0;i < x;i++) { for(j = 5;j < 8;j++) { DataPort = 0; //送段码, Seg_Latch = 1; //开门 Seg_Latch = 0; //关门 DataPort = Bit_Code[j]; //送位码,选择哪个灯亮 Bit_Latch = 1; //开门 Bit_Latch = 0; //关门 DataPort = Seg_Code[Data[j]]; //送段码, Seg_Latch = 1; //开门 Seg_Latch = 0; DelayMs(3); } } } void display7() // I LOVE YOU 显示 { unsigned char i,j; while(1) { DataPort = 0; //送段码, Seg_Latch = 1; //开门 Seg_Latch = 0; //关门 for(j = 0;j < 8;j++) { DataPort = Bit_Code[j]; //送位码,选择哪个灯亮 Bit_Latch = 1; //开门 Bit_Latch = 0; //关门 DataPort = Seg_Code[j]; //送段码, Seg_Latch = 1; //开门 Seg_Latch = 0; DelayMs(3); } } } void display8(unsigned char x,unsigned int hour,unsigned int minutes,unsigned int second) //时刻表(时 :分 :秒)显示 { unsigned int i,j,Data[8]; Data[0] = hour / 10; Data[1] = hour % 10; Data[2] = 16; Data[3] = minutes / 10; Data[4] = minutes % 10; Data[5] = 16; Data[6] = second / 10; Data[7] = second % 10; for(i = 0;i < x;i++) { for(j = 0;j < 8;j++) { DataPort = 0; //送段码, Seg_Latch = 1; //开门 Seg_Latch = 0; //关门 DataPort = Bit_Code[j]; //送位码,选择哪个灯亮 Bit_Latch = 1; //开门 Bit_Latch = 0; //关门 DataPort = Seg_Code[Data[j]]; //送段码, Seg_Latch = 1; //开门 Seg_Latch = 0; DelayMs(3); } } } void main() { int i,j,k; //display1(); //display2(); //display3(); /* for(i = 1000;i > 0;i--) //自增 { display6(150,i); } for(i = 0;i < 1000;i++) //自减 { display6(150,i); } */ //display5(1,2,3); //display7(); for(i = 0;i <= 12;i++) { for(j = 0;j < 60;j++) { for(k = 0;k < 60;k++) { display8(50,i,j,k); } } } } |
|
|
|
只有小组成员才能发言,加入小组>>
2553 浏览 0 评论
1152浏览 2评论
750浏览 1评论
503浏览 0评论
269浏览 0评论
433浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-23 12:25 , Processed in 1.288929 second(s), Total 69, Slave 61 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号