【#文档大全网# 导语】以下是®文档大全网的小编为您整理的《蓝桥杯C代码》,欢迎阅读!
//数码管扫描程序 void SegScan() { static unsigned char com = 0; P0 = 0xff;
P2 = ((P2 & 0x1f) | 0xe0); P2 &= 0x1f; P0 = (0x80 >> com); P2 = ((P2 & 0x1f) | 0xc0); P2 &= 0x1f;
P0 = Ledbuff[com++]; P2 = ((P2 & 0x1f) | 0xe0); P2 &= 0x1f; com &= 0x07;
}
//24C02读程序
unsigned char Read_24C02(unsigned char addr) { unsigned char temp; IIC_Start();
IIC_SendByte(0xa0); IIC_WaitAck(); IIC_SendByte(addr); IIC_WaitAck(); IIC_Start();
IIC_SendByte(0xa1); IIC_WaitAck(); temp=IIC_RecByte(); IIC_Ack(0); IIC_Stop();
return temp;
}
//24C02写程序
void Write_24C02(unsigned char addr,unsigned char dat) { IIC_Start();
IIC_SendByte(0xa0); IIC_WaitAck(); IIC_SendByte(addr); IIC_WaitAck();
IIC_SendByte(dat); IIC_WaitAck(); IIC_Stop();
}
void Read_ADCPCF8591() { //初始化PCF8591 IIC_Start();
IIC_SendByte(0x90); IIC_WaitAck(); IIC_Start();
IIC_SendByte(0x03); IIC_WaitAck(); IIC_Stop();
//从PCF8591读数据 IIC_Start();
IIC_SendByte(0x91); IIC_WaitAck(); dat=IIC_RecByte(); IIC_Ack(0); IIC_Stop();
}
//ADCPCF8591输出数据
void OUT_DAC(unsigned char dat) { IIC_Start();
IIC_SendByte(0x90); IIC_WaitAck(); IIC_SendByte(0x40); IIC_WaitAck(); IIC_SendByte(dat); IIC_WaitAck(); IIC_Stop(); }
//从DS18B20读温度 unsigned char Read_Temp() { unsigned char low,high,temp; 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<<4)|(low>>4); return temp;
}
//DS1302设置时间函数
void set_time(uchar shi,uchar fen,uchar miao) { Write_Ds1302(0x8e,0); Write_Ds1302(0x80,miao); Write_Ds1302(0x82,fen); Write_Ds1302(0x84,shi); Write_Ds1302(0x8e,0x80);
}
给出的程序,需要改下
void Write_Ds1302( unsigned char address,unsigned char dat ) { RST=0; _nop_(); SCK=0; _nop_(); RST=1; _nop_();
Write_Ds1302_Byte(address);
Write_Ds1302_Byte((dat/10)*16+(dat%10)); //十进制转化成BCD码
RST=0;
}
//读时间,读函数给出 shi=Read_Ds1302(0x85); fen=Read_Ds1302(0x83); miao=Read_Ds1302(0x81); 读出数据处理
shi/16得到十位 shi%16得到个位
//读DS1302函数
unsigned char Read_Ds1302 ( unsigned char address ) { unsigned char i,temp=0x00; RST=0; _nop_(); SCK=0; _nop_();
RST=1; _nop_(); Write_Ds1302_Byte(address); for (i=0;i<8;i++) {
SCK=0; temp>>=1; if(SDA) temp|=0x80; SCK=1;
}
RST=0; _nop_();
RST=0; SCK=0; _nop_(); SCK=1; _nop_(); SDA=0; _nop_(); SDA=1; _nop_(); return (temp);
}
//矩阵按键扫描 #include
#define uchar unsigned char #define uint unsigned int #define temp P3 sfr P4 = 0xc0; sbit P44 = P4^4; sbit P42 = P4^2; uchar key_value = 0;
void keyscan() //按键扫描函数 {
temp = 0x0f; P42 = 0; P44 = 0; if(temp!=0x0f) //扫描行 {
delay_ms(10); //延时消抖 switch(temp) {
case 0x0e: key_value = 0; break; case 0x0d: key_value = 4; break; case 0x0b: key_value = 8; break; case 0x07: key_value = 12; break; }
temp = 0xf0; P42 = 1; P44 = 1; //扫描列 if(P44 == 0) key_value + = 0;
while(P44 == 0); //松手检测
if(P42 == 0) key_value + = 1;
while(P42 == 0); //松手检测
if(temp == 0xd0) key_value + = 2;
while(temp == 0x0d); //松手检测
if(temp == 0xe0) key_value + = 3; while(temp == 0x0e); //松手检测 } }
//超声波发送波
sbit TX = P1^0; //发射引脚 sbit RX = P1^1; //接收引脚 void Send_Wave() { }
//超声波得到距离函数 while(1) {
/** 200毫秒更新一次数据 */
if(s_flag) {
s_flag = 0;
/** 关闭定时器0中断:计算超声波发送到返回的时间 */ //
ET0 = 0;
send_wave(); //发送方波信号 TR1 = 1; //启动计时 冲
TR1 = 0; //关闭计时 //发生溢出 if(TF1 == 1) { } else {
/** 计算时间 */ t = TH1; t <<= 8; t |= TL1;
TF1 = 0; distance = 9999; //无返回 while((RX == 1) && (TF1 == 0)); //等待收到脉unsigned char i = 0; for(i = 0; i < 8; i++) { }
Trig = 1; somenop; Trig = 0; somenop;
} }
TH1 = 0; TL1 = 0; /** 数据处理*/
dspbuf[5] = distance/100; dspbuf[6] = distance%100/10; dspbuf[7] = distance%10;
}
}
Somenop包含50个_nop_已宏定义
distance = (unsigned int)(t*0.017); //计算距离
本文来源:https://www.wddqxz.cn/5a30368cbb1aa8114431b90d6c85ec3a86c28b34.html