蓝桥杯知识汇总
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;
TMOD = 0x20;
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 DS1302_SetProtect(bit flag){
if (flag)
Write_Ds1302(0x8e, 0x80);
else
Write_Ds1302(0x8e, 0x00);
}
//设置时间
void DS1302_SetTime(unsigned char Address, unsigned char Value){
DS1302_SetProtect(0);
Write_Ds1302(Address, ((Value / 10) << 4) | (Value % 10)); //将十进制数转换成BCD码
}
//转化时间格式
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);
}
//时间初始化
void Time_Initial(){
unsigned char Second;
DS1302_SetTime(DS1302_HOUR, 8);
DS1302_SetTime(DS1302_MINUTE, 30);
DS1302_SetTime(DS1302_SECOND, 0);
Second = Read_Ds1302(DS1302_SECOND | 0x01);
if (Second & 0x80){
DS1302_SetTime(DS1302_SECOND, 0); //启动时钟
}
}
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;
}