本帖最后由 1475963 于 2019-12-29 12:37 编辑
原帖:https://www.cnblogs.com/garden6zg/p/8469705.html
1.数码管 uchar code display[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0xbf, 0xff};
P2 = (P2 & 0x1f) | 0xc0;
P0 = order[cnt];
P2 &= 0x1f;
P2 = (P2 & 0x1f) | 0xe0;
P0 = display[disbuf[cnt]];
P2 &= 0x1f;
2.LED显示 P2 = (P2 & 0x1f) | 0x80;
P0 = 0xfe;
P2 &= 0x1f;
3.系统初始化 P2 = ((P2 & 0x1f) | 0xa0); //关闭全部外设
P0 = 0x00;
P2 &= 0x1f;
P2 = (P2 & 0x1f) | 0xe0; //数码管都不显示
P0 = 0xff;
P2 &= 0x1f;
TMOD = 0x01; //计时器0,采用方式1
TH0 = (65536 - 2000) / 256; //定时2ms
TL0 = (65536 - 2000) % 256;
EA = 1; //打开总中断
ET0 = 1; //定时器中断0打开
TR0 = 1; //开始计数
SCON = 0x50; //工作方式1
AUXR = 0x40; //模式为1T模式
TMOD = 0x20; //定时器模式为2,自动重装
TH1 = 256 - (11059200 / 2400 / 32); //波特率为2400
TL1 = 256 - (11059200 / 2400 / 32);
EA = 1; //打开总中断
ES = 1; //打开串口中断
TR1 = 1; //打开计时器1
4.IIC
#define SlaveAddrW 0x90
#define SlaveAddrR 0x91
#define EEPROMW 0xa0
#define EEPROMR 0xa1
5.DS1302 #define AM(X) X
#define PM(X) (X + 12)
#define DS1302_SECOND 0x80
#define DS1302_MINUTE 0x82
#define DS1302_HOUR 0x84
#define DS1302_DAY 0x86
#define DS1302_MONTH 0x88
#define DS1302_WEEK 0x8A
#define DS1302_YEAR 0x8C
#define DS1302_RAM(X) (0xC0 + (X) * 2)
void InitDS1302(void)
{
Write_Ds1302(0x8E, 0x00);//关掉写保护
Write_Ds1302(0x80, 0x50);//秒
Write_Ds1302(0x82, 0x59);//分
Write_Ds1302(0x84, 0x23);//时
Write_Ds1302(0x8E, 0x80);//打开写保护
}
//转化时间格式
void DS1302_GetTime(SYSTEMTIME *Time)
{
unsigned char ReadValue; /* 将BCD码转换成十进制数 */
ReadValue = Read_Ds1302(DS1302_MINUTE | 0x01);
Time->minute = ((ReadValue & 0x70)>>4) * 10 + (ReadValue & 0x0f);
ReadValue = Read_Ds1302(DS1302_HOUR | 0x01);
Time->hour = ((ReadValue & 0x70)>>4) * 10 + (ReadValue & 0x0f);
}
6.A/D
unsigned char Rb2_Read()
{
unsigned char dat;
IIC_Start();
IIC_SendByte(0x90); //写入地址
IIC_WaitAck();
IIC_SendByte(0x43); //0x43是Rb2, 0x41是光敏
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0x91); //读命令
IIC_WaitAck();
dat = IIC_RecByte(); //读取一个字节
IIC_Ack(0);
IIC_Stop();
return dat;
}
7.EEPROM(读取和写入数据都不可中断)
unsigned char Read_EEPROM(unsigned char con)
{
unsigned char dat;
IIC_Start();
IIC_SendByte(0xa0); //写入地址
IIC_WaitAck();
IIC_SendByte(con); //从起始位置开始读取
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0xa1); //读命令
IIC_WaitAck();
dat = IIC_RecByte(); //读取一个字节
IIC_Ack(0);
IIC_Stop();
return dat;
}
void Write_EEPROM(unsigned char date, unsigned char con)
{
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(con);
IIC_WaitAck();
IIC_SendByte(date);
IIC_WaitAck();
IIC_Stop();
Delay(3); //写入后要注意延时一段时间
}
8.DS18B20(读取温度不可中断)
//单总线延时函数
void Delay_OneWire(uint t)
{
char z;
while(t--)
for(z = 0; z < 12; z++);
}
- 读取温度 /* 读取数据不可中断,可以采用关闭终端来实现 */
unsigned int Read_Temperature()
{
unsigned char low,high;
unsigned int temp;
float t;
init_ds18b20(); //器件初始化
Write_DS18B20(0xcc); //跳过读序列
Write_DS18B20(0x44); //启动温度转换
Delay_OneWire(200); //延迟一会
init_ds18b20();
Write_DS18B20(0xcc); //同上
Write_DS18B20(0xbe); //读取温度
low = Read_DS18B20(); //读取低位
high = Read_DS18B20(); //读取高位
temp = (high << 8) | low;
t = temp * 0.0625;
temp = t + 0.5;
return temp;
}
9.矩阵键盘
sbit r1=P3^0; //4行
sbit r2=P3^1;
sbit r3=P3^2;
sbit r4=P3^3;
sbit c1=P4^4; //4列
sbit c2=P4^2;
sbit c3=P3^5;
sbit c4=P3^4;
unsigned char key_scan() //读取矩阵键盘键值
{
unsigned char key_value;
r1=0;
r2=r3=r4=1;
c1=c2=c3=c4=1;
else if(!c1) key_value=0;
else if(!c2) key_value=1;
else if(!c3) key_value=2;
else if(!c4) key_value=3;
r2=0;
r1=r3=r4=1;
c1=c2=c3=c4=1;
if(!c1) key_value=4;
else if(!c2) key_value=5;
else if(!c3) key_value=6;
else if(!c4) key_value=7;
r3=0;
r2=r1=r4=1;
c1=c2=c3=c4=1;
if(!c1) key_value=8;
else if(!c2) key_value=9;
else if(!c3) key_value=10;
else if(!c4) key_value=11;
r4=0;
r2=r3=r1=1;
c1=c2=c3=c4=1;
if(!c1) key_value=12;
else if(!c2) key_value=13;
else if(!c3) key_value=14;
else if(!c4) key_value=15;
return key_value;
}
10.超声波测距
#define somenops {_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();}
sbit TX = P1^0; //发射引脚
sbit RX = P1^1; //接收引脚
//TX 引脚发送 40KHz 方波信号驱动超声波发送探头
void send_wave(void)
{
unsigned char i = 8; //发送 8 个脉冲
do
{
TX = 1;
somenops;somenops;somenops;somenops;somenops;
somenops;somenops;somenops;somenops;somenops;
TX = 0;
somenops;somenops;somenops;somenops;somenops;
somenops;somenops;somenops;somenops;somenops;
}
while(i--);
}
void Check_Distance()
{
uint t;
/* 关闭定时器 0 中断:计算超声波发送到返回的时间 */
ET0 = 0;
send_wave(); //发送方波信号
ET0 = 1;
TR1 = 1; //启动计时
while((RX == 1) && (TF1 == 0)); //等待收到脉冲
TR1 = 0; //关闭计时
//发生溢出
if(TF1 == 1)
{
TF1 = 0;
distance = 99; //无返回
}
else
{
/** 计算时间 */
t = TH1;
t <<= 8;
t |= TL1;
distance = (unsigned int)(t * 12 * 0.017 / 12); //计算距离
}
TH1 = 0;
TL1 = 0;
}
|