当前位置: 首页 > >

蓝桥杯单片机笔记?各个驱动使用

发布时间:

/*


//向E2prom写入


void AT24c02wirte(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();
}

//向E2prom读出


unsigned char AT24c02read(unsigned char addr)
{
unsigned char num;
IIC_Start();
IIC_SendByte(0xa0); //发送器件地址
IIC_WaitAck();
IIC_SendByte(addr); //发送存储地址
IIC_WaitAck();
IIC_Stop();

IIC_Start();
IIC_SendByte(0xa1); //发送读操作
IIC_WaitAck();
num=IIC_RecByte(); //读一字节
IIC_Ack(0);
IIC_Stop();

return num;
}

//电位器使用驱动


unsigned char Read_AIN()
{
unsigned char temp;

IIC_Start();
IIC_SendByte(0x90); //地址+写
IIC_WaitAck();
IIC_SendByte(0x03); //选择电位器RB2
IIC_WaitAck();
IIC_Stop();

IIC_Start();
IIC_SendByte(0x91); //地址+读
IIC_WaitAck();
temp = IIC_RecByte();
IIC_Ack(1); //高电*回复
IIC_Stop();

return temp;
}

//光敏电阻


unsigned char read_AIN()
{
unsigned char temp;


IIC_Start();
IIC_SendByte(0x90); //地址+写
IIC_WaitAck();
IIC_SendByte(0x01); //选择光敏电阻
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91); //地址+读
IIC_WaitAck();
temp = IIC_RecByte();
IIC_Ack(0); //低电*回复
IIC_Stop();

return temp;
}

*/


//蜂鸣器buzz是 P06控制
//继电器relay是 P04控制
//I/O代码
/


/P2=((P2&0x1f)|0xA0)
//P06=1; //打开蜂鸣器
//P2 &= 0x1f;

//P2=((P2&0x1f)|0xA0)
//P04=1; //打开继电器
//P2 &= 0x1f;

//DS18B20温度采集程序:整数
unsigned char rd_temperature(void)


{
unsigned char low,high;
char 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;
temp |= (low>>4);

return temp;
}

//ds1302初始化


void init_time()
{
unsigned char i;
Write_Ds1302(0x8E,1); //关闭写保护
for(i=0;i<7;i++)
{
Write_Ds1302(write_add[i],time[i]); //地址, 初始化时间
}
Write_Ds1302(0x8E,0); //打开写保护

Hour=Read_Ds1302(0x85);
Minute=Read_Ds1302(0x83);
Second=Read_Ds1302(0x81);
}

//BCD码->十进制


unsigned char BCD_Ten(uchar dat)
{
return ((dat>>4)*10 + (dat&0x0f));
}

//十进制->BCD码


unsigned char Ten_BCD(uchar dat)
{
return (((dat/10)<<4) + (dat%10));
}



友情链接: