eg1:数码管如何显示出字符
eg2:数码管的静态显示
在单片机系统中,常用的显示器有以下几种:数码管的显示通常是由锁存器来进行控制的
#include
#include
//控制断点
sbit dula = P2^6;
sbit wela = P2^7;
//控制数码管的每一个位置的数字
sbit mun1 = P0^0;
sbit mun2 = P0^1;
sbit mun3 = P0^2;
sbit mun4 = P0^3;
/*
共阳数码管码表
0xc0,//0
0xf9,//1
0xa4,//2
0xb0,//3
0x99,//4
0x92,//5
0x82,//6
0xf8,//7
0x80,//8
0x90,//9
0x88,//A
0x83,//B
0xc6,//C
0xa1,//D
0x86,//E
0x8e, //F
0x8c, //P
0xc1,//U
0x91,//Y
0x7c,//L
0x00,//全亮
0xff //熄灭
数码管码表
0x3F, //"0"
0x06, //"1"
0x5B, //"2"
0x4F, //"3"
0x66, //"4"
0x6D, //"5"
0x7D, //"6"
0x07, //"7"
0x7F, //"8"
0x6F, //"9"
0x77, //"A"
0x7C, //"B"
0x39, //"C"
0x5E, //"D"
0x79, //"E"
0x71, //"F"
0x76, //"H"
0x38, //"L"
0x37, //"n"
0x3E, //"u"
0x73, //"P"
0x5C, //"o"
0x40, //"-"
0x00 //熄灭
*/
void main(){
//位锁存器开启
wela = 1;
// 给p0口送数0011 1111---> 1111 1100 == fc
P0 = 0x00;
// 锁存器关闭
wela = 0;
// 断选锁存器
dula = 1;
P0 = 0x06;
dula = 0;
while(1);
}
#include
#include
//控制断点
sbit dula = P2^6;
sbit wela = P2^7;
// 宏定义
#define uint unsigned int;
#define uchar unsigned char;
uint i,ret;
uchar num;
// 创建一个数组,编写code的目的就是将数据存放在程序存储器中,不写code会放在随机存储器当中
uchar code table[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,0x76,0x38,0x37,0x3E};
// 延时函数
void Delay1000ms(void) //@11.0592MHz
{
unsigned char data i, j, k;
_nop_();
_nop_();
i = 43;
j = 6;
k = 203;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main(){
// 操作所有的数码管让数码管亮起
wela = 1;
P0 = 0xc0;
wela = 0;
while(1){
ret = sizeof(table)/sizeof(table[0]);
for(num = 0;num <= ret; num ++){
// 操作单个数码管让它分别显示数据
dula = 1;
P0 = table[num];
dula = 0;
Delay1000ms();
}
}
}
80C51是一种经典的单片机系列,其中断系统结构如下:
80C51的中断系统结构提供了一种有效的处理异步事件的机制。通过合理配置中断源的优先级和中断处理程序的设计,可以实现对实时性要求较高的应用的响应和处理。
#include
#include
//控制断点
sbit dula = P2^6;
sbit wela = P2^7;
sbit L1= P1^0;
// 宏定义
#define uint unsigned int;
#define uchar unsigned char;
uint i,ret;
uchar num;
// 创建一个数组,编写code的目的就是将数据存放在程序存储器中,不写code会放在随机存储器当中
uchar code table[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,0x76,0x38,0x37,0x3E};
// 延时函数
void Delay1000ms(void) //@11.0592MHz
{
unsigned char data i, j, k;
_nop_();
_nop_();
i = 43;
j = 6;
k = 203;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
// 中断服务程序,编写中断函数时无返回值函数名,interrupt 加上中断序号
void exter0() interrupt 0{
L1 = 0;
}
void main(){
// 开总中断
EA = 1;
// 开外部中断0
EX0 = 1;
// 跳变触发中断的方式 TCON =0X01;
IT0 = 1;
// 操作所有的数码管让数码管亮起
wela = 1;
P0 = 0xc0;
wela = 0;
while(1){
ret = sizeof(table)/sizeof(table[0]);
for(num = 0;num <= ret; num ++){
// 操作单个数码管让它分别显示数据
L1 = 1;
dula = 1;
P0 = table[num];
dula = 0;
Delay1000ms();
}
}
}
eg4:单片机定时的应用**
80C51微控制器中的定时器和计数器是其内部的重要功能模块,用于计时、计数和生成定时中断等操作。以下是关于80C51定时器和计数器的基本介绍:
定时器和计数器的工作原理如下:
…
#include
#include
#define uint unsigned int
#define uchar unsigned char
sbit dula = P2^6;
sbit wela = P2^7;
uchar temp,aa,numwe,numdu,lenght1,lenght2;
uchar code table[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,0x76,0x38,0x37,0x3E};
uchar code tablewe[] = {0xfe,0xfd,0xf7,0xef,0xdf};
// 编写中断函数using 后面跟机,表示的是使用第几组的工作寄存器
void timer0() interrupt 1{
TH0 = (65536-50000)/256;// 高8位
TL0 = (65536-50000)/256; // 低8位
aa++;
}
void main(){
numdu = 0;
numwe = 0;
aa = 0;
// 选择寄存器的工作方式
TMOD = 0x01;
// 计算初值以2秒的时间变化
TH0 = (65536-50000)/2556;// 高8位
TL0 = (65536-50000)%256; // 低8位
// 中断方式,开启中断EA = 1
EA = 1;
// 开启定时器中断
ET0 = 1;
//TR0 = 1 启动定时器
TR0 = 1;
// 计算数组长度
lenght1 = sizeof(table)/sizeof(table[0]);
lenght2 = sizeof(tablewe)/sizeof(tablewe[0]);
while(1){
if(aa == 4){
aa = 0;
numdu++;
if(numdu == 10){
numdu = 1;
}
dula = 1;
P0= table[numdu];
dula = 0;
wela = 1;
P0 = tablewe[numwe];
wela = 0;
numwe++;
if(numwe == 6){
numwe = 0;
}
}
}
}
动态显示灯管
数码管的动态显示原理如下:
通过快速切换数码管的段选引脚,控制器可以实现数码管的动态显示。通过控制显示数据的存储和转换,可以在数码管上显示各种数字、字符和符号。
#include
#include
#define uint unsigned int
#define uchar unsigned char
sbit dula = P2^6;
sbit wela = P2^7;
uchar temp,aa,numwe,numdu,lenght1,lenght2;
uchar code table[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,0x76,0x38,0x37,0x3E};
uchar code tablewe[] = {0xfe,0xfd,0xf7,0xef,0xdf};
// 编写中断函数using 后面跟机,表示的是使用第几组的工作寄存器
void timer0() interrupt 1{
TH0 = (65536-50000)/256;// 高8位
TL0 = (65536-50000)/256; // 低8位
aa++;
}
void main(){
numdu = 0;
numwe = 0;
aa = 0;
// 选择寄存器的工作方式
TMOD = 0x01;
// 计算初值以2秒的时间变化
TH0 = (65536-50000)/2556;// 高8位
TL0 = (65536-50000)%256; // 低8位
// 中断方式,开启中断EA = 1
EA = 1;
// 开启定时器中断
ET0 = 1;
//TR0 = 1 启动定时器
TR0 = 1;
// 计算数组长度
lenght1 = sizeof(table)/sizeof(table[0]);
lenght2 = sizeof(tablewe)/sizeof(tablewe[0]);
while(1){
if(aa == 4){
aa = 0;
numdu++;
if(numdu == 10){
numdu = 1;
}
dula = 1;
P0= table[numdu];
dula = 0;
wela = 1;
P0 = tablewe[numwe];
wela = 0;
numwe++;
if(numwe == 6){
numwe = 0;
}
}
}
}
#include
#include
#define uint unsigned int
#define uchar unsigned char
sbit dula = P2^6;
sbit wela = P2^7;
uchar temp,aa,numwe,numdu,lenght1,lenght2,shi,ge;
uchar code table[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
//延迟5000ms
void Delay5000ms(void) //@11.0592MHz
{
unsigned char data i, j, k;
_nop_();
_nop_();
i = 211;
j = 30;
k = 11;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main(){
temp = 12;
numdu = 0;
while(1){
shi = temp/10;
ge = temp%10;
dula = 1;
P0 = table[shi];
dula = 0;
wela = 1;
P0 = 0xfe;
wela = 0;
Delay5000ms();
dula = 1;
P0 = table[ge];
dula = 0;
wela = 1;
P0 = 0xfd;
wela = 0;
Delay5000ms();
}
}
以上代码的显示结果为三个数轮流在不同位数显示
一下代码的结果为三个数同时显示在数码管上
#include
#include
#define uint unsigned int
#define uchar unsigned char
sbit dula = P2^6;
sbit wela = P2^7;
uchar temp,aa,numwe,numdu,bai,shi,ge;
uchar code table[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
//延迟5000ms
void delay(uint z){
uint x,y;
for(x = z; x > 0; x --){
for(y = 110; y > 0; y--){
}
}
}
void main(){
temp = 152;
numdu = 0;
while(1){
bai = temp/100;
shi = temp%100/10;
ge = temp%10;
dula = 1;
P0 = table[bai];
dula = 0;
wela = 1;
P0 = 0xfe;
wela = 0;
delay(5);
dula = 1;
P0 = table[shi];
dula = 0;
wela = 1;
P0 = 0xfd;
wela = 0;
delay(5);
dula = 1;
P0 = table[ge];
dula = 0;
wela = 1;
P0 = 0xfb;
wela = 0;
delay(5);
}
}
编写子程序灵活调用
#include
#include
#define uint unsigned int
#define uchar unsigned char
sbit dula = P2^6;
sbit wela = P2^7;
uchar temp,aa,numwe,numdu,bai,shi,ge;
uchar code table[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
//延迟5000ms
void delay(uint z){
uint x,y;
for(x = z; x > 0; x --){
for(y = 110; y > 0; y--){
}
}
}
void display(uchar bai,uchar shi,uchar ge)
{
dula = 1;
P0 = table[bai];
dula = 0;
wela = 1;
P0 = 0xfe;
wela = 0;
delay(5);
dula = 1;
P0 = table[shi];
dula = 0;
wela = 1;
P0 = 0xfd;
wela = 0;
delay(5);
dula = 1;
P0 = table[ge];
dula = 0;
wela = 1;
P0 = 0xfb;
wela = 0;
delay(5);
}
void main(){
temp = 152;
numdu = 0;
bai = temp/100;
shi = temp%100/10;
ge = temp%10;
while(1){
display(bai,shi,ge);
}
}
定时器中断三位数准备增加
#include
#include
#define uint unsigned int
#define uchar unsigned char
sbit dula = P2^6;
sbit wela = P2^7;
uchar temp,aa,bai,shi,ge;
uchar code table[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
// 初始化函数
void init(){
wela = 0;
dula = 0;
temp = 0;
// 初始化定时器
TMOD = 0x01;
// 定时器模式设置定时器初始值
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
//开始中断
EA = 1;
ET0 = 1;
TR0 = 1;
}
//延迟5000ms
void delay(uint z){
uint x,y;
for(x = z; x > 0; x --){
for(y = 110; y > 0; y--){
}
}
}
// 编写中断函数using 后面跟机,表示的是使用第几组的工作寄存器
void timer0() interrupt 1{
TH0 = (65536-50000)/256;// 高8位
TL0 = (65536-50000)/256; // 低8位
aa++;
}
void display(uchar bai,uchar shi,uchar ge)
{
dula = 1;
P0 = table[bai]; // 送百位数
dula = 0; // 消隐
P0 = 0xff;
wela = 1;
P0 = 0xfe; // 位选通百位数
wela = 0;
delay(5);
dula = 1;
P0 = table[shi];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfd;
wela = 0;
delay(5);
dula = 1;
P0 = table[ge];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfb;
wela = 0;
delay(5);
}
void main(){
// 调用初始化函数
init();
while(1){
if(aa == 20){
aa = 0;
temp ++;
if(temp == 100){
temp = 0;
}else{
bai = temp /100;
shi = temp %100/10;
ge = temp%10;
}
}
display(bai,shi,ge);
}
}
实现的效果为从后往前数字自增1
eg:51单片机较为重要的部分为,中断系统,定时器和串口需要重点掌握,important