注:前两课太过简单,不做赘述。
Lesson3
3.1、利用定时计数器 T0 从 P1.0 输出周期为 1s 的方波,让发光二极管以 1HZ 闪烁,设晶振频率为12MHz
#include <REGX52.H>
#include "intrins.h"
void Init(void);
unsigned char m_t0;
void main(void)
{
Init();
while(1);
}
void Init(void)
{
TMOD = 0x01;
TH0 = (65536 - 50000) / 256;
TL0 = (65536 - 50000) % 256;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void Timer0()interrupt 1
{
TH0 = (65536 - 50000) / 256;
TL0 = (65536 - 50000) % 256;
m_t0++;
if(m_t0 >= 10)
{
m_t0 = 0;
P1_0 = ~P1_0;
}
}
3.2、利用定时计数器 T1 产生定时时钟,由 P1 口控制 8 个发光二极管,使 8 个指示灯依次一个一个闪动, 闪动频率为10次/秒(8 个灯依次亮一遍为一个周期) 循环,设晶振频率为12MHz
#include <REGX52.H>
#include "intrins.h"
void Init(void);
unsigned char m_t1, m_temp = 0xfe;
void main(void)
{
Init();
while(1)
{
P1 = m_temp;
if(m_t1 >= 2)
{
m_t1 = 0;
m_temp = _crol_(m_temp, 1);
}
}
}
void Init(void)
{
TMOD = 0x10;
TH1 = (65536 - 50000) / 256;
TL1 = (65536 - 50000) % 256;
EA = 1;
ET1 = 1;
TR1 = 1;
}
void Timer1()interrupt 3
{
TH1 = (65536 - 50000) / 256;
TL1 = (65536 - 50000) % 256;
m_t1++;
}
3.3、同时用两个定时器控制蜂鸣器发声,定时器 0 控制频率,定时器 1 控制同个频率持续的时间,间隔 2s 依次输出 1,10,50,100,200,400,800,1k Hz的方波,设晶振频率为 12MHz
#include <REGX52.H>
#include "intrins.h"
#include "delay.h"
void Init(void);
sbit beep = P2^3;
unsigned char m_t0, m_t1, m_count;
unsigned int m_freq;
void main(void)
{
Init();
while(1);
}
void Timer0()interrupt 1
{
TR0 = 0;
TH0 = (65536 - m_freq) / 256;
TL0 = (65536 - m_freq) % 256;
m_t0++;
if(m_t1 <= 40)
{
if(m_t0 >= 10)
{
m_t0 = 0;
m_freq = 50000;
beep = ~beep;
}
}
if(m_t1 > 40 && m_t1 <= 80)
{
m_t0 = 0;
m_freq = 50000;
beep = ~beep;
}
if(m_t1 > 80 && m_t1 <= 120)
{
m_t0 = 0;
m_freq = 10000;
beep = ~beep;
}
if(m_t1 > 120 && m_t1 <= 160)
{
m_t0 = 0;
m_freq = 5000;
beep = ~beep;
}
if(m_t1 > 160 && m_t1 <= 200)
{
m_t0 = 0;
m_freq = 2500;
beep = ~beep;
}
if(m_t1 > 200 && m_t1 <= 240)
{
m_t0 = 0;
m_freq = 1250;
beep = ~beep;
}
if(m_t1 > 240 && m_t1 <= 280)
{
m_t0 = 0;
m_freq = 625;
beep = ~beep;
}
if(m_t1 > 280 && m_t1 <= 320)
{
m_t0 = 0;
m_freq = 312;
beep = ~beep;
}
TR0 = 1;
}
void Timer1()interrupt 3
{
TH1 = (65536 - 50000) / 256;
TL1 = (65536 - 50000) % 256;
m_t1++;
if(m_t1 >= 320)
{
m_t1 = 0;
m_freq = 50000;
}
}
void Init(void)
{
m_freq = 50000;
beep = 0;
TMOD = 0x11;
TH0 = (65536 - 50000) / 256;
TL0 = (65536 - 50000) % 256;
TH1 = (65536 - 50000) / 256;
TL1 = (65536 - 50000) % 256;
EA = 1;
ET0 = 1;
TR0 = 1;
ET1 = 1;
TR1 = 1;
}
在定时器0 的中断处理函数中关闭定时器0 的原因是因为在定时器0 的中断处理函数中程序运行的时间比定时器0定时的时间 50ms 要长,会影响定时器0 的运行。
3.4、用定时器以间隔 500ms 在 6 位数码管上依次显示0、1、2、3…C、D、E、F,重复。设时钟频率为 12M
#include <REGX52.H>
#include "intrins.h"
#include "delay.h"
void Init(void);
void Nixie_Display(unsigned char m_num);
sbit wela = P2^7;
sbit dula = P2^6;
unsigned char Nixie_Array[] = {0x3f, 0x06, 0x5b, 0x4f,
0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c,
0x39, 0x5e, 0x79, 0x71};
unsigned char m_t1, m_count;
void main(void)
{
Init();
while(1)
{
if(m_t1 >= 10)
{
m_t1 = 0;
if(m_count >= 16)
{
m_count = 0;
}
Nixie_Display(m_count++);
}
}
}
void Timer1()interrupt 3
{
TH1 = (65536 - 50000) / 256;
TL1 = (65536 - 50000) % 256;
m_t1++;
}
void Init(void)
{
TMOD = 0x10;
TH1 = (65536 - 50000) / 256;
TL1 = (65536 - 50000) % 256;
EA = 1;
ET1 = 1;
TR1 = 1;
}
void Nixie_Display(unsigned char m_num)
{
wela = 1;
P0 = 0xc0;
wela = 0;
dula = 1;
P0 = Nixie_Array[m_num];
dula = 0;
delay_ms(1);
}
Lesson4
4.1、利用动态扫描方法在六位数码管上显示出稳定的 654321,时钟频率为11.0592M
#include <REGX52.H>
#include "intrins.h"
#include "delay.h"
void Init(void);
void Nixie_Display(unsigned char m_num);
sbit wela = P2^7;
sbit dula = P2^6;
unsigned char Num_Array[] = {0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf};
unsigned char Nixie_Array[] = {0x3f, 0x06, 0x5b, 0x4f,
0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c,
0x39, 0x5e, 0x79, 0x71};
unsigned char New_Array[] = {0x7d, 0x6d, 0x66, 0x4f, 0x5b, 0x06};
unsigned char m_count;
void main(void)
{
while(1)
{
Nixie_Display(m_count++);
if(m_count >= 6)
{
m_count = 0;
}
}
}
void Nixie_Display(unsigned char m_num)
{
wela = 1;
P0 = Num_Array[m_num];
wela = 0;
dula = 1;
P0 = New_Array[m_num];
dula = 0;
delay_ms(1);
}
4.2、用动态扫描方法和定时器1 在数码管的前三位显示出秒表,精确到 1%秒,即最后一位显示 1%秒,一直循环下去,设时钟频率为 12M
#include <REGX52.H>
#include "intrins.h"
#include "delay.h"
void Init(void);
void Nixie_Display(void);
sbit wela = P2^7;
sbit dula = P2^6;
unsigned char Nixie_Array[] = {0x3f, 0x06, 0x5b, 0x4f,
0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c,
0x39, 0x5e, 0x79, 0x71};
unsigned char New_Array[] = {0xbf, 0x86, 0xdb, 0xcf,
0xe6, 0xed, 0xfd, 0x87,
0xff, 0xef};
unsigned char bai, shi, ge;
unsigned int m_t1;
void main(void)
{
Init();
while(1)
{
bai = m_t1 / 100;
shi = m_t1 / 10 % 10;
ge = m_t1 % 10;
Nixie_Display();
}
}
void Init(void)
{
TMOD = 0x10;
TH1 = (65536 - 10000) / 256;
TL1 = (65536 - 10000) % 256;
EA = 1;
ET1 = 1;
TR1 = 1;
}
void Timer1(void)interrupt 3
{
TH1 = (65536 - 10000) / 256;
TL1 = (65536 - 10000) % 256;
m_t1++;
if(m_t1 >= 1000)
{
m_t1 = 0;
}
}
void Nixie_Display(void)
{
wela = 1;
P0 = 0xfe;
wela = 0;
dula = 1;
P0 = New_Array[bai];
dula = 0;
delay_ms(1);
P0 = 0xff;
wela = 1;
P0 = 0xfd;
wela = 0;
dula = 1;
P0 = Nixie_Array[shi];
dula = 0;
delay_ms(1);
P0 = 0xff;
wela = 1;
P0 = 0xfb;
wela = 0;
dula = 1;
P0 = Nixie_Array[ge];
dula = 0;
delay_ms(1);
P0 = 0xff;
}
4.3、利用动态扫描和定时器 1 在数码管上显示出从 765432 开始以 1/10 秒的速度往下递减直至 765398 并保持显示此数,与此同时利用定时器 0 以 500ms 速度进行流水灯从上至下移动,当数码管上数减到停止时,实验板上流水灯也停止然后全部开始闪烁,3秒后(用 T0 定时)流水灯全部关闭、数码管上显示出 “HELLO”。到此保持住。设晶振频率为 12MHz
#include <REGX52.H>
#include "intrins.h"
#include "delay.h"
#define uint unsigned int
#define uchar unsigned char
void Init(void);
void Nixie_Display(uchar a, uchar b, uchar c, uchar bai, uchar shi, uchar ge);
sbit wela = P2^7;
sbit dula = P2^6;
unsigned char Nixie_Array[] = {0x3f, 0x06, 0x5b, 0x4f,
0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c,
0x39, 0x5e, 0x79, 0x71,
0x76, 0x79, 0x38, 0x3f, 0x80};
unsigned char m_t0, m_count, m_flag, m_t1, bai, shi, ge;
uchar m_flow = 0xfe;
uchar m_flash = 0xff;
uint m_temp = 432;
void main(void)
{
Init();
while(1)
{
if(m_flag)
{
Nixie_Display(16, 17, 18, 18, 19, 20);
}
else
{
Nixie_Display(7, 6, 5, bai, shi, ge);
}
}
}
void Init(void)
{
P1 = m_flow;
TMOD = 0x11;
TH0 = (65536 - 50000) / 256;
TL0 = (65536 - 50000) % 256;
TH1 = (65536 - 10000) / 256;
TL1 = (65536 - 10000) % 256;
EA = 1;
ET0 = 1;
TR0 = 1;
ET1 = 1;
TR1 = 1;
}
void Timer0(void)interrupt 1
{
TH0 = (65536 - 50000) / 256;
TL0 = (65536 - 50000) % 256;
m_t0++;
if(m_t0 >= 10)
{
m_t0 = 0;
if(m_temp > 398)
{
m_flow = _crol_(m_flow, 1);
P1 = m_flow;
}
else
{
if(m_count < 6)
{
m_count++;
m_flash = ~m_flash;
P1 = m_flash;
}
else
{
P1 = 0xff;
m_count = 6;
m_flag = 1;
TR0 = 0;
}
}
}
}
void Timer1(void)interrupt 3
{
TH1 = (65536 - 10000) / 256;
TL1 = (65536 - 10000) % 256;
m_t1++;
bai = m_temp / 100;
shi = m_temp / 10 % 10;
ge = m_temp % 10;
if(m_t1 >= 10)
{
m_t1 = 0;
if(m_temp > 398)
{
m_temp--;
}
else
{
TR1 = 0;
}
}
}
void Nixie_Display(uchar a, uchar b, uchar c, uchar bai, uchar shi, uchar ge)
{
wela = 1;
P0 = 0xfe;
wela = 0;
dula = 1;
P0 = Nixie_Array[a];
dula = 0;
delay_ms(1);
P0 = 0xff;
wela = 1;
P0 = 0xfd;
wela = 0;
dula = 1;
P0 = Nixie_Array[b];
dula = 0;
delay_ms(1);
P0 = 0xff;
wela = 1;
P0 = 0xfb;
wela = 0;
dula = 1;
P0 = Nixie_Array[c];
dula = 0;
delay_ms(1);
P0 = 0xff;
wela = 1;
P0 = 0xf7;
wela = 0;
dula = 1;
P0 = Nixie_Array[bai];
dula = 0;
delay_ms(1);
P0 = 0xff;
wela = 1;
P0 = 0xef;
wela = 0;
dula = 1;
P0 = Nixie_Array[shi];
dula = 0;
delay_ms(1);
P0 = 0xff;
wela = 1;
P0 = 0xdf;
wela = 0;
dula = 1;
P0 = Nixie_Array[ge];
dula = 0;
delay_ms(1);
P0 = 0xff;
}
Lesson5
5.1、数码管前三位显示一个跑表,从 000 到 999 之间以 1% 秒速度运行,当按下一个独立键盘时跑表停止,松开手后跑表继续运行。(用定时器设计表)
#include <REGX52.H>
#include "intrins.h"
#include "delay.h"
sbit wela = P2^7;
sbit dula = P2^6;
unsigned char Num_Array[] = {0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf};
unsigned char Nixie_Array[] = {0x3f, 0x06, 0x5b, 0x4f,
0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c,
0x39, 0x5e, 0x79, 0x71,
0x76, 0x79, 0x38, 0x3f, 0x80};
unsigned char m_key, bai, shi, ge;
unsigned int m_t0;
void Init(void);
void Nixie_Display(unsigned char bai, unsigned char shi, unsigned char ge);
unsigned char MatrixKey(void);
void main(void)
{
Init();
while(1)
{
bai = m_t0 / 100;;
shi = m_t0 / 10 % 10;
ge = m_t0 % 10;
m_key = MatrixKey();
Nixie_Display(bai, shi, ge);
}
}
void Init(void)
{
TMOD = 0x01;
TH0 = (65536 - 10000) / 256;
TL0 = (65536 - 10000) % 256;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void Timer0() interrupt 1
{
TH0 = (65536 - 10000) / 256;
TL0 = (65536 - 10000) % 256;
if(m_key == 0)
{
m_t0++;
}
if(m_t0 > 999)
{
m_t0 = 0;
}
}
void Nixie_Display(unsigned char bai, unsigned char shi, unsigned char ge)
{
wela = 1;
P0 = Num_Array[0];
wela = 0;
dula = 1;
P0 = Nixie_Array[bai];
dula = 0;
delay_ms(1);
P0 = 0xff;
wela = 1;
P0 = Num_Array[1];
wela = 0;
dula = 1;
P0 = Nixie_Array[shi];
dula = 0;
delay_ms(1);
P0 = 0xff;
wela = 1;
P0 = Num_Array[2];
wela = 0;
dula = 1;
P0 = Nixie_Array[ge];
dula = 0;
delay_ms(1);
P0 = 0xff;
}
unsigned char MatrixKey(void)
{
unsigned char KeyNumber = 0;
P3 = 0xff;
P3_0 = 0;
if(P3_4 == 0)
{
delay_ms(20);
while(P3_4 == 0)
Nixie_Display(bai, shi, ge);
delay_ms(20);
KeyNumber = 1;
}
return KeyNumber;
}
5.2、在上题的基础上,用另外三个独立键盘实现按下第一个时计时停止,按下第二个时计时开始,按下第三个是计数值清零从头开始 按键分别为:s6、s7、s8、s9
#include <REGX52.H>
#include "intrins.h"
#include "delay.h"
sbit wela = P2^7;
sbit dula = P2^6;
unsigned char Num_Array[] = {0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf};
unsigned char Nixie_Array[] = {0x3f, 0x06, 0x5b, 0x4f,
0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c,
0x39, 0x5e, 0x79, 0x71,
0x76, 0x79, 0x38, 0x3f, 0x80};
unsigned char m_key, m_flag, bai, shi, ge;
unsigned int m_t0;
void Init(void);
void Nixie_Display(unsigned char bai, unsigned char shi, unsigned char ge);
unsigned char MatrixKey(void);
void main(void)
{
Init();
while(1)
{
bai = m_t0 / 100;;
shi = m_t0 / 10 % 10;
ge = m_t0 % 10;
m_key = MatrixKey();
Nixie_Display(bai, shi, ge);
}
}
void Init(void)
{
TMOD = 0x01;
TH0 = (65536 - 10000) / 256;
TL0 = (65536 - 10000) % 256;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void Timer0() interrupt 1
{
TH0 = (65536 - 10000) / 256;
TL0 = (65536 - 10000) % 256;
if(m_key == 0 && m_flag == 0)
{
m_t0++;
}
if(m_t0 > 999)
{
m_t0 = 0;
}
}
void Nixie_Display(unsigned char bai, unsigned char shi, unsigned char ge)
{
wela = 1;
P0 = Num_Array[0];
wela = 0;
dula = 1;
P0 = Nixie_Array[bai];
dula = 0;
delay_ms(1);
P0 = 0xff;
wela = 1;
P0 = Num_Array[1];
wela = 0;
dula = 1;
P0 = Nixie_Array[shi];
dula = 0;
delay_ms(1);
P0 = 0xff;
wela = 1;
P0 = Num_Array[2];
wela = 0;
dula = 1;
P0 = Nixie_Array[ge];
dula = 0;
delay_ms(1);
P0 = 0xff;
}
unsigned char MatrixKey(void)
{
unsigned char KeyNumber = 0;
P3 = 0xff;
P3_0 = 0;
if(P3_4 == 0)
{
delay_ms(20);
while(P3_4 == 0)
Nixie_Display(bai, shi, ge);
delay_ms(20);
KeyNumber = 1;
}
if(P3_5 == 0)
{
m_flag = 1;
delay_ms(20);
while(P3_5 == 0)
Nixie_Display(bai, shi, ge);
delay_ms(20);
KeyNumber = 2;
}
if(P3_6 == 0)
{
m_flag = 0;
delay_ms(20);
while(P3_6 == 0)
Nixie_Display(bai, shi, ge);
delay_ms(20);
KeyNumber = 3;
}
if(P3_7 == 0)
{
delay_ms(20);
while(P3_7 == 0)
{
m_t0 = 0;
bai = m_t0 / 100;;
shi = m_t0 / 10 % 10;
ge = m_t0 % 10;
Nixie_Display(bai, shi, ge);
}
delay_ms(20);
KeyNumber = 4;
}
return KeyNumber;
}
5.3、按下16个矩阵键盘依次在数码管上显示 1-16 的平方。如按下第一个显示1,第二个显示4…
#include <REGX52.H>
#include "MatrixKey.h"
#include "intrins.h"
#include "delay.h"
sbit wela = P2^7;
sbit dula = P2^6;
unsigned char Num_Array[] = {0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf};
unsigned char Nixie_Array[] = {0x3f, 0x06, 0x5b, 0x4f,
0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c,
0x39, 0x5e, 0x79, 0x71,
0x76, 0x79, 0x38, 0x3f, 0x80};
unsigned char keyvalue, bai, shi, ge, value;
unsigned int m_temp;
void Nixie_Display(unsigned char temp);
void main(void)
{
while(1)
{
keyvalue = MatrixKey();
if(keyvalue > 0)
{
value = keyvalue;
}
Nixie_Display(value);
}
}
void Nixie_Display(unsigned char temp)
{
m_temp = temp * temp;
if(m_temp > 0 && m_temp < 10)
{
wela = 1;
P0 = Num_Array[2];
wela = 0;
dula = 1;
P0 = Nixie_Array[m_temp];
dula = 0;
delay_ms(1);
P0 = 0xff;
}
else if(m_temp >= 10 && m_temp < 100)
{
shi = m_temp / 10;
ge = m_temp % 10;
wela = 1;
P0 = Num_Array[1];
wela = 0;
dula = 1;
P0 = Nixie_Array[shi];
dula = 0;
delay_ms(1);
P0 = 0xff;
wela = 1;
P0 = Num_Array[2];
wela = 0;
dula = 1;
P0 = Nixie_Array[ge];
dula = 0;
delay_ms(1);
P0 = 0xff;
}
else if(m_temp >= 100 && m_temp < 1000)
{
bai = m_temp / 100;
shi = m_temp / 10 % 10;
ge = m_temp % 10;
wela = 1;
P0 = Num_Array[0];
wela = 0;
dula = 1;
P0 = Nixie_Array[bai];
dula = 0;
delay_ms(1);
P0 = 0xff;
wela = 1;
P0 = Num_Array[1];
wela = 0;
dula = 1;
P0 = Nixie_Array[shi];
dula = 0;
delay_ms(1);
P0 = 0xff;
wela = 1;
P0 = Num_Array[2];
wela = 0;
dula = 1;
P0 = Nixie_Array[ge];
dula = 0;
delay_ms(1);
P0 = 0xff;
}
}
|