明天就是最后冲刺了 祝各位小伙伴都取的不错的成绩
关于13届最新驱动代码需要注意的点希望对大家有所帮助,个人看法,有误还望大佬指正
下面就将三个驱动代码都测试一遍 下面就使用S4 按键进行三个显示界面的切换 温度、电压、时间
不管是做真题还是自己平时练习,一定要注意的是每写一个模块就测试一下功能,千万不要一下子写很多然后再来测试,不出错还好,如果出错了真的不好检查的
注意:
1、当我们建好工程后,根据编程大题的结构框图看用到了那些驱动代码
我们最好的方法是把相应的.c 和.h文件移到我们的工程下面
2、将驱动代码文件添加到工程中
3、一定要检查驱动代码文件是否完整、相关的引脚是否有误、头文件是否包含了相关的函数声明
一、温度驱动代码测试
当我们打开温度驱动代码文件,很明显我们需要进行相应的修改
//温度获取
void Read_Temperature(void)
{
unsigned char LSB = 0,MSB = 0;
unsigned int temp = 0;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
delay(500);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
LSB = Read_DS18B20();
MSB = Read_DS18B20();
init_ds18b20();
temp = (MSB << 8) | LSB ;
if((temp & 0xf800) == 0x0000)
{
T_temp = temp *0.0625;
}
T_value = T_temp * 10; //保留一位小数乘10 两位的话乘100
}
//1.温度显示
void SMG_Display_Temperature(void)
{
//C -> 0110 0011 0xC6
SMG_DisplayBit(0,0xc6);
delay(200);
SMG_DisplayBit(1,0xff);
delay(200);
SMG_DisplayBit(2,0xff);
delay(200);
SMG_DisplayBit(3,0xff);
delay(200);
SMG_DisplayBit(4,0xff);
delay(200);
SMG_DisplayBit(5,smg_data[T_value/100]);
delay(200);
SMG_DisplayBit(6,smg_data_dot[T_value/10%10]);
delay(200);
SMG_DisplayBit(7,smg_data[T_value%10]);
delay(200);
SMG_All(0xff);
delay(200);
}
第一次测试 温度没有值 经过检查是因为延时的问题 ,修改后就可以正常显示了
二、IIC驱动代码测试
iic驱动代码没有什么问题 就是头文件条件定义中多了##删除就好了
//读取电压
void Read_Voltage(void)
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x03);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
adc_volt = IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
//数据转换处理
smg_volt = adc_volt *(5.0/255)*100;
}
//电压显示
void SMG_Display_V(void)
{
//E -> 0110 0001 0X86
SMG_DisplayBit(0,0x86);
delay(200);
SMG_DisplayBit(5,smg_data_dot[smg_volt/100]);
delay(200);
SMG_DisplayBit(6,smg_data[smg_volt/10%10]);
delay(200);
SMG_DisplayBit(7,smg_data[smg_volt%10]);
delay(200);
SMG_All(0xff);
delay(200);
}
三、ds1320驱动代码测试
注意:当我把ds1302加进去后就报了这个错误SDA重定义了
这是因为Iic.h 头文件中也有该定义,最好的解决方式是,把相对于.h文件中定义的变量和声明放到相对应的.c文件中
同理ds1302中我们也可以这样放 ,然后错误就解决了
unsigned char code write_ds1302_addr[] = {0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
unsigned char code Read_ds1302_addr[] = {0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
unsigned char Init_Timer[] = {0x50,0x59,0x16};//默认时间16时59分50秒
//写入初始化时间
void Write_Timer(void)
{
unsigned char i = 0;
Write_Ds1302_Byte(0x8e,0x00); //关闭写保护
for(i = 0; i<7;i++)
{
Write_Ds1302_Byte(write_ds1302_addr[i],Init_Timer[i]);
}
Write_Ds1302_Byte(0x8e,0x80); //打开写保护
}
//读取时间
void Read_Timer(void)
{
unsigned char i = 0;
for(i =0;i< 7;i++)
{
Init_Timer[i] = Read_Ds1302_Byte(Read_ds1302_addr[i]);
}
}
void SMG_Display_Time(void)
{
SMG_DisplayBit(0,smg_data[Init_Timer[2]/16]);
delay(200);
SMG_DisplayBit(1,smg_data[Init_Timer[2]%16]);
delay(200);
SMG_DisplayBit(2,0xbf);
delay(200);
SMG_DisplayBit(3,smg_data[Init_Timer[1]/16]);
delay(200);
SMG_DisplayBit(4,smg_data[Init_Timer[1]%16]);
delay(200);
SMG_DisplayBit(5,0xbf);
delay(200);
SMG_DisplayBit(6,smg_data[Init_Timer[0]/16]);
delay(200);
SMG_DisplayBit(7,smg_data[Init_Timer[0]%16]);
delay(200);
SMG_All(0xff);
delay(200);
}
main.c
#include "reg52.h"
#include "onewire.h"
#include "iic.h"
#include "ds1302.h"
sbit S4 = P3^3;
unsigned char interface = 0;//界面
unsigned int T_temp = 0,T_value = 0;//温度
unsigned int smg_volt= 0,adc_volt = 0; //电压
unsigned char code write_ds1302_addr[] = {0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
unsigned char code Read_ds1302_addr[] = {0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
unsigned char Init_Timer[] = {0x50,0x59,0x16};//默认时间16时59分50秒
unsigned char code smg_data[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
unsigned char code smg_data_dot[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
//简单延时
void delay(unsigned int t)
{
while(t--);
}
//初始化锁存器
void Init_74HC138(unsigned char channel)
{
switch(channel)
{
case 4:P2 = (P2 & 0x1f) | 0x80;break;
case 5:P2 = (P2 & 0x1f) | 0xa0;break;
case 6:P2 = (P2 & 0x1f) | 0xc0;break;
case 7:P2 = (P2 & 0x1f) | 0xe0;break;
case 0:P2 = (P2 & 0x1f) | 0x00;break;
}
P2 = (P2 & 0x1f) | 0x00;
}
//操作所有数码管
void SMG_All(unsigned char dat)
{
P0 = 0xff;
Init_74HC138(6);
P0 = dat;
Init_74HC138(7);
}
//安位操作数码管
void SMG_DisplayBit(unsigned char pos,unsigned char dat)
{
P0 = (0x01 << pos);
Init_74HC138(6);
P0 = dat;
Init_74HC138(7);
}
//温度获取
void Read_Temperature(void)
{
unsigned char LSB = 0,MSB = 0;
unsigned int temp = 0;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
delay(500);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
LSB = Read_DS18B20();
MSB = Read_DS18B20();
init_ds18b20();
temp = (MSB << 8) | LSB ;
if((temp & 0xf800) == 0x0000)
{
T_temp = temp *0.0625;
}
T_value = T_temp * 10;
}
//读取电压
void Read_Voltage(void)
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x03);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
adc_volt = IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
//数据转换处理
smg_volt = adc_volt *(5.0/255)*100;
}
//写入初始化时间
void Write_Timer(void)
{
unsigned char i = 0;
Write_Ds1302_Byte(0x8e,0x00); //关闭写保护
for(i = 0; i<7;i++)
{
Write_Ds1302_Byte(write_ds1302_addr[i],Init_Timer[i]);
}
Write_Ds1302_Byte(0x8e,0x80); //打开写保护
}
//读取时间
void Read_Timer(void)
{
unsigned char i = 0;
for(i =0;i< 7;i++)
{
Init_Timer[i] = Read_Ds1302_Byte(Read_ds1302_addr[i]);
}
}
//1.温度显示
void SMG_Display_Temperature(void)
{
//C -> 0110 0011 0xC6
SMG_DisplayBit(0,0xc6);
delay(200);
SMG_DisplayBit(1,0xff);
delay(200);
SMG_DisplayBit(2,0xff);
delay(200);
SMG_DisplayBit(3,0xff);
delay(200);
SMG_DisplayBit(4,0xff);
delay(200);
SMG_DisplayBit(5,smg_data[T_value/100]);
delay(200);
SMG_DisplayBit(6,smg_data_dot[T_value/10%10]);
delay(200);
SMG_DisplayBit(7,smg_data[T_value%10]);
delay(200);
SMG_All(0xff);
delay(200);
}
//3.电压显示
void SMG_Display_V(void)
{
//E -> 0110 0001 0X86
SMG_DisplayBit(0,0x86);
delay(200);
SMG_DisplayBit(1,0xff);
delay(200);
SMG_DisplayBit(2,0xff);
delay(200);
SMG_DisplayBit(3,0xff);
delay(200);
SMG_DisplayBit(4,0xff);
delay(200);
SMG_DisplayBit(5,smg_data_dot[smg_volt/100]);
delay(200);
SMG_DisplayBit(6,smg_data[smg_volt/10%10]);
delay(200);
SMG_DisplayBit(7,smg_data[smg_volt%10]);
delay(200);
SMG_All(0xff);
delay(200);
}
void SMG_Display_Time(void)
{
//C -> 0110 0011 0xC6
SMG_DisplayBit(0,smg_data[Init_Timer[2]/16]);
delay(200);
SMG_DisplayBit(1,smg_data[Init_Timer[2]%16]);
delay(200);
SMG_DisplayBit(2,0xbf);
delay(200);
SMG_DisplayBit(3,smg_data[Init_Timer[1]/16]);
delay(200);
SMG_DisplayBit(4,smg_data[Init_Timer[1]%16]);
delay(200);
SMG_DisplayBit(5,0xbf);
delay(200);
SMG_DisplayBit(6,smg_data[Init_Timer[0]/16]);
delay(200);
SMG_DisplayBit(7,smg_data[Init_Timer[0]%16]);
delay(200);
SMG_All(0xff);
delay(200);
}
void Key_Scans(void)
{
if(S4 == 0)
{
delay(20);
if(S4 == 0)
{
while(S4 == 0)
{
if(interface == 0)
{
interface = 1;
SMG_Display_Temperature();
}
else if(interface == 1)
{
interface = 2;
SMG_Display_V();
}
else if(interface == 2)
{
interface = 0;
SMG_Display_Time();
}
}
}
}
}
//系统初始化
void Init_System(void)
{
P0 = 0xff;
Init_74HC138(4);
P0 = 0x00;
Init_74HC138(5);
SMG_All(0xff);
}
void main(void)
{
Init_System();
Write_Timer();
while(1)
{
Read_Temperature();
Read_Voltage();
Read_Timer();
Key_Scans();
switch(interface)
{
case 0:SMG_Display_Temperature();break;
case 1: SMG_Display_V();break;
case 2:SMG_Display_Time();
}
}
}
冲鸭 兄弟们