小蜜蜂代码风格的,模块都在,希望能够帮助你吧。
目录
AT24C02存储芯片
DS18B20温度传感器
DS1302 时钟
NE555频率测量
PCF8591 AD
PWM波
超声波测距
串口UART
4X4矩阵按键
AT24C02存储芯片
void Write_AT24C02(unsigned char addr,unsigned char dat)
{I2CStart();I2CSendByte(0xa0);I2CWaitAck();I2CSendByte(addr);I2CWaitAck();I2CSendByte(dat);I2CWaitAck();I2CStop();
}void Read_AT24C02(unsigned char addr)
{I2CStart();I2CSendByte(0xa0);I2CWaitAck();I2CSendByte(addr);I2CWaitAck();I2CStart();I2CSendByte(0xa1);I2CWaitAck();tmp = I2CReceiveByte();I2CSendAck(1); I2CStop();
}
DS18B20温度传感器
void Read_ds18b20_adc()
{unsigned char LSB, MSB;unsigned int tmep = 0;init_ds18b20();Write_DS18B20(0xcc);Write_DS18B20(0x44);init_ds18b20();Write_DS18B20(0xcc);Write_DS18B20(0x44);LSB = Read_DS18B20();MSB = Read_DS18B20();tmep = (LSB << 8) | MSB;if ((temp & 0xf800) == 0x0000){temp_ds18b2 = temp * 0.0625;}
}
// 如果上电就是85 记得Delay一下
DS1302 时钟
unsigned char code READ_RTC_ADDR[7] = {0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d};unsigned char code WRITE_RTC_ADDR[7] = {0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c};unsigned char TIME[7] = {0x24, 0x58, 0x23, 0x19, 0x04, 0x06, 0x20};void Init_ds1302()
{unsigned char i;Write_Ds1302_Byte(0x8E, 0x00);for (i = 0; i < 3; i++){Write_Ds1302_Byte(WRITE_RTC_ADDR[i], TIME[i]);}Write_Ds1302_Byte(0x8E, 0x80);
}unsigned char hour, min, sec;
void Read_ds1302()
{sec = Read_Ds1302_Byte(0x81);min = Read_Ds1302_Byte(0x83);hour = Read_Ds1302_Byte(0x85);
}// 考点显示
void DisplaySMG_ds1302()
{DisplaySMG_Bit(0, SMG_NoDot[hour >> 4]);DisplaySMG_Bit(1, SMG_NoDot[hour & 0x0f]);DisplaySMG_Bit(2, 0xbf);DisplaySMG_Bit(3, SMG_NoDot[min >> 4]);DisplaySMG_Bit(4, SMG_NoDot[min & 0x0f]);DisplaySMG_Bit(5, 0xbf);DisplaySMG_Bit(6, SMG_NoDot[sec >> 4]);DisplaySMG_Bit(7, SMG_NoDot[sec & 0x0f]);
}
NE555频率测量
// 方式1
void Init_timer()
{TH0 = 0xff;TL0 = 0xff; // 定时器 0 采用8位重装 0110TH1 = (65536 - 50000) / 256;TL1 = (65536 - 50000) % 256;TMOD = 0x16;ET0 = 1;ET1 = 1;EA = 1;TR0 = 1;TR1 = 1;
}void Service_T0() interrupt 1
{count_f++; // 每一次中断都加一
}void Service_T1() interrupt 3
{TH1 = (65536 - 50000) / 256;TL1 = (65536 - 50000) % 256;count_t++;if (count_t == 20){dat_f = count_f;count_f = 0;count_t = 0;}
}// 方式二
void Init_Timer0_Timer1()
{TH1 = (65536 - 10000) / 256;TL1 = (65536 - 10000) % 256;TH0 = 0;TL0 = 0;TMOD = 0x15;ET0 = 1;ET1 = 1;EA = 1;TR0 = 1;TR1 = 1;
}void Service_Timer0() interrupt 3
{TH1 = (65536 - 10000) / 256;TL1 = (65536 - 10000) % 256;count_t++;if (count_t == 100){count_t = 0;freq = (TH0 << 8) | TL0;TH0 = 0;TL0 = 0;}
}
PCF8591 AD
void Write_DA(unsigned char dat)
{I2CStart();I2CSendByte(0x90);I2CWaitAck();I2CSendByte(0x41);I2CWaitAck();I2CSendByte(dat);I2CWaitAck();IIC_Stop();
}unsigned char Read_AD()
{unsigned char temp;I2CStart();I2CSendByte(0x90);I2CWaitAck();I2CSendByte(0x41);I2CWaitAck();I2CStart();I2CSendByte(0x91);I2CWaitAck();temp = I2CReceiveByte();I2CSendAck(1);I2CStop();return temp;
}
// 用的最多的就是0x41 0x43
PWM波
// 10000 分成 100份
void Init_Timer0() interrupt 1
{TH0 = (65536 - 100) / 256;TL0 = (65536 - 100) % 256;conut++;if (count <= duty){L1 = 0;}else if (count < 100){L1 = 1;}else if (count == 100){L1 = 0;count = 0;}
}
// 实现了呼吸灯
超声波测距
// 最好用T1实现 因为T0用来实现NE555
sbit TX = P1 ^ 0;
sbit RX = P1 ^ 1;void Delay_12us()
{unsigned char i;_nop_();_nop_(); // 空操作i = 33;while (--i);
}
// STC 生成出来的 带上头文件
void Send_Wave()
{unsigned char i;for(i = 0;i < 8;i++){TX = 1;Delay_12us();TX = 0;Delay_12us();}
}void Measure_Distance()
{unsigned int time = 0;TMOD &= 0x0f;TH1 = 0x00;TH1 = 0x00;Send_Wave();TR1 = 1;while((RX == 1)&&(TF1 ==0))TR1 = 0;if (TF1 == 0){time = TH1;time = (time << 8) | TL1;distance = ((time / 10) * 17) / 100 + 3;}else{TF1 = 0;distance = 999;}
}
串口UART
sfr AUXR = 0x8e;void SendByte(unsigned char dat)
{SBUF = dat;while(TI == 0);TI = 0;
}void SendString(unsigned char *str)
{while(*str != '\0'){SendByte(*str++);}
}
void Init_Uart()
{TMOD = 0x20;TH1 = 0xfd;TL1 = 0xfd;TR1 = 1;SCON = 0x50;AUXR = 0x00;ES = 1;EA = 1;
}unsigned char command = 0x00;
void Service_Uart() interrupt 4
{if(RI == 1){RI = 0;command = SBUF;}
}
4X4矩阵按键
sbit R3 = P3^2;
sbit R4 = P3^3;
sbit C1 = P4^4;
sbit C2 = P4^2;
void Scan_Keys()
{R3 = 0;R4 = 1;C1 = C2 = 1;if(C1 == 0) //S5{DelaySMG(200);if(C1 == 0){if(F_smg == 21 || F_smg == 22 || F_smg == 23){switch(F_smg){case 21: F_smg = 22;break;case 22: F_smg = 23;break;case 23: F_smg = 21;break;}}while(C1 == 0){Display_Info();}}}else if(C2 == 0) //S9{DelaySMG(200);if(C2 == 0){if(F_smg == 3){Param_temprature = Param_temprature - 1;}//if(F_smg == 21 || F_smg == 22 || F_smg == 23){F_time_begin = 1;}if(F_time_2s == 1){hour = 0x00;min = 0x00;sec = 0x00;F_smg = 1;Max_temprature = 0;Ave_temprature = 0;Max_shidu = 0;Ave_shidu = 0;num = 0;Param_temprature = 30;F_time = 0;F_time_2s = 0;}while(C2 == 0){Display_Info();}F_time_begin = 0;}}R3 = 1;R4 = 0;C1 = C2 = 1;if(C1 == 0) //S4{DelaySMG(200);if(C1 == 0){switch(F_smg){case 1: F_smg = 21;break;case 21: F_smg = 3;break;case 22: F_smg = 3;break;case 23: F_smg = 3;break;case 3: F_smg = 1;break;}while(C1 == 0){Display_Info();}}}else if(C2 == 0) //S8{DelaySMG(200);if(C2 == 0){if(F_smg == 3){Param_temprature = Param_temprature + 1;}while(C2 == 0){Display_Info();}}}
}
、