📄 at24c02.c
字号:
/*
*****************************************************************************
* Mountain View Silicon Tech. Inc.
* Copyright 2007, Mountain View Silicon Tech. Inc., ShangHai, China
* All rights reserved.
*
* Filename: at24c02.c
* Description: at24c02 eeprom control c file
*
* Change History:
* Jack - 12/xx/2007 - V0.1
* - created
******************************************************************************
*/
/*
**********************************************************
* INCLUDE
**********************************************************
*/
#include <reg51.h>
#include "type.h"
#include "utility.h"
#include "AT24C02.h"
#include ".\addon\addon_cfg.h"
#include ".\config\syscfg.h"
#include "sysctrl.h"
/*
**********************************************************
* LOCAL MACRO
**********************************************************
*/
#if 0
/*define pin operate macro*/
#define SET_PIN(reg, mask) ((reg) |= (mask)) //pin = 1
#define CLR_PIN(reg, mask) ((reg) &= (~(mask))) //pin = 0
#define GET_PIN(reg, mask) ((reg) & (mask)) //get pin status
#endif
sbit SCL = P0^1;
sbit SDA = P0^0;
/* declare ack type */
#define NO_ACK 1
#define ACK 0
/*set SCL rate: 100Kbps~400Kbps*/
#define I2C_TIME 5 //EX51 -> 100 us/bit
//I51 -> 10 us/bit
/*
**********************************************************
* LOCAL FUNCTION
**********************************************************
*/
//
// I2C start condition: SCL = 1, SDA 1->0.
//
extern void
I2CStart(void)
{
SDA = 1;
WaitUs(I2C_TIME);
SCL = 1;
WaitUs(I2C_TIME); //约等于48uS / 12 = 4uS
SDA = 0;
WaitUs(I2C_TIME);
SCL = 0;
WaitUs(0);
}
//
// I2C stop condition: SCL = 1, SDA 0->1.
//
extern void
I2CStop(void)
{
SDA = 0;
//SCL = 0;
WaitUs(I2C_TIME);
SCL = 1;
WaitUs(I2C_TIME);
SDA = 1;
//WaitUs(I2C_TIME);
}
//
// I2C ack
//
extern void
I2CAckType(
bit ackType //if == 1, then SDA = 1, means I2C send NO ACK.
//if == 0, then SDA = 0, means I2C send ACK.
)
{
if (ackType)
{
SDA = 1; //send NO ACK
}
else
{
SDA = 0; //send ACK
}
WaitUs(I2C_TIME);
SCL = 1;
WaitUs(I2C_TIME);
SCL = 0;
WaitUs(0);
//SDA = 1;
}
//
// I2C check ack from master
//
#if 1
extern bit //if error return 1, else return 0
I2CChkAck(void)
{
unsigned int errtime = 1000; //超时值,因故障接收方无ACK
WaitUs(I2C_TIME);
SCL = 1;
SDA = 1;
if(SDA){};
// while(SDA)
// {
// if (--errtime == 0)
// {
// I2CStop();
// return 1;
// }
// }
WaitUs(I2C_TIME);
WaitUs(I2C_TIME);
SCL = 0;
WaitUs(0);
return 0;
}
#endif
//
// I2C write 8 bits to device
//
extern bit //if error return 1, else return 0
I2CWrite8Bit(
unsigned char b //data to write into device
)
{
unsigned char temp;
for(temp=8; temp!=0; temp--)
{
if(b & 0x80) //MSB output first
{
SDA = 1;
}
else
{
SDA = 0;
}
WaitUs(I2C_TIME);
SCL = 1;
WaitUs(I2C_TIME);
b = b << 1;
SCL = 0;
WaitUs(0);
}
return I2CChkAck();
// WaitUs(I2C_TIME);
// SCL = 1;
// SDA = 1;
// WaitUs(I2C_TIME);
// SCL = 0;
// WaitUs(I2C_TIME);
}
//
// I2C read 8 bits from device
//
extern unsigned char //return data, read from device
I2CRead8Bit(void)
{
unsigned char temp, rbyte = 0;
for(temp=8; temp!=0; temp--)
{
WaitUs(I2C_TIME);
SDA = 1;
SCL = 1;
rbyte <<= 1;
if(SDA)
{
rbyte |= 0x01;
}
WaitUs(I2C_TIME);
SCL = 0;
}
WaitUs(0);
return(rbyte);
}
extern void
I2CSendByte(unsigned char dev, unsigned char thedat)
{
I2CStart();
I2CWrite8Bit(dev);
I2CWrite8Bit(thedat);
I2CStop();
// WaitUs(100);
}
extern void
I2cSend2Byte(unsigned char thedat1, unsigned char thedat2) // 1,10
{
I2CStart();
I2CWrite8Bit(0x88);
I2CWrite8Bit(thedat2);
I2CWrite8Bit(thedat1);
I2CStop();
// WaitUs(100);
}
extern void
I2cSend2ByteSub(unsigned char thedat1, unsigned char thedat2) //10,1
{
I2CStart();
I2CWrite8Bit(0x88);
I2CWrite8Bit(thedat1);
I2CWrite8Bit(thedat2);
I2CStop();
// WaitUs(100);
}
extern void PT2323Sel(char sel)//SEL 0=DVD ;1=CD;2 = USB
{
if(sel == 0) //dvd 5.1
{
I2CSendByte(0x94,0xc7); //sel 6 input
}
else if(sel == 1) //cd
{
I2CSendByte(0x94,0xca); //input2
}
else if(sel == 2) //usb
{
I2CSendByte(0x94,0xc8); //input4
}
}
extern void PT2323Change5(char on) // 1=5.1;0=2.1
{
if(on)
{
I2CSendByte(0x94,0xf0); //fl
I2CSendByte(0x94,0xf2); //fr
I2CSendByte(0x94,0xf4); //ct
I2CSendByte(0x94,0xf6); //sub
I2CSendByte(0x94,0xf8); //sl
I2CSendByte(0x94,0xfa); //sr
I2CSendByte(0x94,0xfe); //all
}
else
{
I2CSendByte(0x94,0xf0); //fl
I2CSendByte(0x94,0xf2); //fr
I2CSendByte(0x94,0xf5); //ct
I2CSendByte(0x94,0xf6); //sub
I2CSendByte(0x94,0xf9); //sl
I2CSendByte(0x94,0xfb); //sr
// I2CSendByte(0x94,0xfe); //all
}
}
extern void PT2323Mute(char on) // 1=mute
{
if(on)I2CSendByte(0x94,0xff); //all
else
{
if(VolMain != 0)
I2CSendByte(0x94,0xfe); //all
}
}
extern void VolRearSet(BYTE dir)// 0 -10
{
unsigned char temp1,temp2,out;
// out = 79-(VolMain+VolRear); //(0-60)+(0-10)-5
// out = 50-VolRear*3; //(0-60)+(0-10)-5
out = 70-(VolMain+VolRear); //(0-60)+(0-10)-5
temp1 = 0x40|(out/10);
temp2 = 0x50|(out%10);
if(dir)
I2cSend2Byte(temp1,temp2); //sl
else
I2cSend2ByteSub(temp1,temp2);
temp1 = 0x00|(out/10);
temp2 = 0x10|(out%10);
if(dir)
I2cSend2Byte(temp1,temp2); //sr
else
I2cSend2ByteSub(temp1,temp2);
}
extern void VolRearSub(void)
{
unsigned char temp1,temp2,out;
if(VolRear >0)
{
VolRear--;
// out = 50-VolRear*3; //(0-60)+(0-10)-5
out = 70-(VolMain+VolRear); //(0-60)+(0-10)-5
temp1 = 0x40|(out/10);
temp2 = 0x50|(out%10);
I2cSend2ByteSub(temp1,temp2); //sl
temp1 = 0x00|(out/10);
temp2 = 0x10|(out%10);
I2cSend2ByteSub(temp1,temp2); //sr
}
}
extern void VolRearAdd(void)
{
unsigned char temp1,temp2,out;
if(VolRear <10)
{
VolRear++;
// out = 50-VolRear*3; //(0-60)+(0-10)-5
out = 70-(VolMain+VolRear); //(0-60)+(0-10)-5
temp1 = 0x40|(out/10);
temp2 = 0x50|(out%10);
I2cSend2Byte(temp1,temp2); //sl
temp1 = 0x00|(out/10);
temp2 = 0x10|(out%10);
I2cSend2Byte(temp1,temp2); //sr
}
}
extern void VolCenSet(BYTE dir) // 0-10
{
unsigned char temp1,temp2,out;
// out = 50-VolCen*3; //(0-60)+(0-10)-5
out = 70-(VolMain+VolCen); //(0-60)+(0-10)-5
temp1 = 0x20|(out/10);
temp2 = 0x30|(out%10);
if(dir)
I2cSend2Byte(temp1,temp2); //cen
else
I2cSend2ByteSub(temp1,temp2);
}
extern void VolCenSub(void)
{
unsigned char temp1,temp2,out;
if(VolCen >0)
{
VolCen--;
// out = 50-VolCen*3; //(0-60)+(0-10)-5
out = 70-(VolMain+VolCen); //(0-60)+(0-10)-5
temp1 = 0x20|(out/10);
temp2 = 0x30|(out%10);
I2cSend2ByteSub(temp1,temp2); //cen
}
}
extern void VolCenAdd(void)
{
unsigned char temp1,temp2,out;
if(VolCen < 10)
{
VolCen++;
// out = 50-VolCen*3; //(0-60)+(0-10)-5
out = 70-(VolMain+VolCen); //(0-60)+(0-10)-5
temp1 = 0x20|(out/10);
temp2 = 0x30|(out%10);
I2cSend2Byte(temp1,temp2); //cen
}
}
extern void VolFrontSet(BYTE dir) // 0-10
{
unsigned char temp1,temp2,out;
// out = 50-VolFront*3; //(0-60)+(0-10)-5
out = 70-(VolMain+VolFront); //(0-60)+(0-10)-5
temp1 = 0x60|(out/10);
temp2 = 0x70|(out%10);
if(dir)
I2cSend2Byte(temp1,temp2); //fl
else
I2cSend2ByteSub(temp1,temp2);
temp1 = 0xa0|(out/10);
temp2 = 0xb0|(out%10);
if(dir)
I2cSend2Byte(temp1,temp2); //fr
else
I2cSend2ByteSub(temp1,temp2);
}
extern void VolFrontSub(void)
{
unsigned char temp1,temp2,out;
if(VolFront > 0)
{
VolFront--;
// out = 50-VolFront*3; //(0-60)+(0-10)-5
out = 70-(VolMain+VolFront); //(0-60)+(0-10)-5
temp1 = 0x60|(out/10);
temp2 = 0x70|(out%10);
I2cSend2ByteSub(temp1,temp2); //fl
temp1 = 0xa0|(out/10);
temp2 = 0xb0|(out%10);
I2cSend2ByteSub(temp1,temp2); //fr
}
}
extern void VolFrontAdd(void)
{
unsigned char temp1,temp2,out;
if(VolFront < 10)
{
VolFront++;
// out = 50-VolFront*3; //(0-60)+(0-10)-5
out = 70-(VolMain+VolFront); //(0-60)+(0-10)-5
temp1 = 0x60|(out/10);
temp2 = 0x70|(out%10);
I2cSend2Byte(temp1,temp2); //fl
temp1 = 0xa0|(out/10);
temp2 = 0xb0|(out%10);
I2cSend2Byte(temp1,temp2); //fr
}
}
extern void VolSubSet(BYTE dir) // 0-10
{
unsigned char temp1,temp2,out;
// out = 50-VolSub*3; //(0-60)+(0-10)-5
out = 70-(VolMain+VolSub); //(0-60)+(0-10)-5
temp1 = 0x80|(out/10);
temp2 = 0x90|(out%10);
if(dir)
I2cSend2Byte(temp1,temp2); //cen
else
I2cSend2ByteSub(temp1,temp2);
}
extern void VolSubSub(void)
{
unsigned char temp1,temp2,out;
if(VolSub > 0)
{
VolSub--;
// out = 50-VolSub*3; //(0-60)+(0-10)-5
out = 70-(VolMain+VolSub); //(0-60)+(0-10)-5
temp1 = 0x80|(out/10);
temp2 = 0x90|(out%10);
I2cSend2ByteSub(temp1,temp2); //cen
}
}
extern void VolSubAdd(void)
{
unsigned char temp1,temp2,out;
if(VolSub < 10)
{
VolSub++;
// out = 50-VolSub*3; //(0-60)+(0-10)-5
out = 70-(VolMain+VolSub); //(0-60)+(0-10)-5
temp1 = 0x80|(out/10);
temp2 = 0x90|(out%10);
I2cSend2Byte(temp1,temp2); //cen
}
}
extern void VolMainSet(void) //0-60
{
unsigned char temp1,temp2,out;
// VolRearSet();
// VolSubSet();
// VolFrontSet();
// VolCenSet();
out = 65-VolMain; //(0-60)+(0-10)-5
temp1 = 0xd0+(out/10);
temp2 = 0xe0+(out%10);
I2cSend2Byte(temp1,temp2); //fl
}
extern void VolMainSub(void)
{
unsigned char temp1,temp2,out;
if(VolMain > 1)
{
VolMain--;
out = 65-VolMain; //(0-60)+(0-10)-5
temp1 = 0xd0+(out/10);
temp2 = 0xe0+(out%10);
I2cSend2ByteSub(temp1,temp2); //fl
}
else if(VolMain == 1)
{
VolMain--;
PT2323Mute(1); //pt2323 mute off
VolSetMute(1); //pt2258 mute off
}
if(VolFront > 5) VolFrontSet(1);
else if(VolFront < 5) VolFrontSet(0);
if(VolRear > 5) VolRearSet(1);
else if(VolRear < 5) VolRearSet(0);
if(VolCen > 5) VolCenSet(1);
else if(VolCen < 5) VolCenSet(0);
if(VolSub > 5) VolSubSet(1);
else if(VolSub < 5) VolSubSet(0);
}
extern void VolMainAdd(void)
{
unsigned char temp1,temp2,out;
if(VolMain == 0)
{
VolMain++;
PT2323Mute(0); //pt2323 mute off
VolSetMute(0); //pt2258 mute off
}
else if(VolMain < 60)
{
VolMain++;
out = 65-VolMain; //(0-60)+(0-10)-5
temp1 = 0xd0+(out/10);
temp2 = 0xe0+(out%10);
I2cSend2Byte(temp1,temp2); //fl
}
if(VolFront > 5) VolFrontSet(1);
else if(VolFront < 5) VolFrontSet(0);
if(VolRear > 5) VolRearSet(1);
else if(VolRear < 5) VolRearSet(0);
if(VolCen > 5) VolCenSet(1);
else if(VolCen < 5) VolCenSet(0);
if(VolSub > 5) VolSubSet(1);
else if(VolSub < 5) VolSubSet(0);
}
extern void VolInit(void)
{
I2CSendByte(0x88,0xc0); //no mean
}
extern void VolSetMute(char on)
{
if(on)I2CSendByte(0x88,0xf9); //mute on
else
{
if(VolMain != 0)
I2CSendByte(0x88,0xf8);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -