⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 key.c

📁 cygnal上的数据采集的程序(带实时时钟)
💻 C
字号:
#include<intrins.h>
#include "c8051f.h"
#include "key.h"

unsigned char KeyBuf[24];
unsigned char bdata tempp_p;

sbit DDD_7 = tempp_p^7;
sbit DDD_0 = tempp_p^0;
void Delay1ms(unsigned char T);
////////////////////////////////////////////////////////////////////////////
void delay50us()
{	
	unsigned char temp;
	for(temp = 0; temp<5; temp++)
	{
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		_nop_();
	}	
}


/***************************************************************************/
void K_Start() 
{
	delay50us();
	K_SDA = 1;
	delay50us();
	K_SCL = 1;
	delay50us();
	K_SDA = 0;
	delay50us();	
	K_SCL = 0;
	delay50us();
}

/***************************************************************************/
void K_Stop() 
{
	K_SCL = 0;
	K_SDA = 0;
	delay50us();
	K_SCL = 1;
	delay50us();
	K_SDA = 1;
	delay50us();
}

/***************************************************************************/
/*
void Ack() {
	SDA=0;
	SCL=1;
	delay5us();
	SCL=0;
	SDA=1;
	delay5us();
}
*/
/***************************************************************************/

void K_NoAck()
{
	K_SDA = 1;
	delay50us();
	K_SCL = 1;
	delay50us();
	K_SCL = 0;
	delay50us();
}

/***************************************************************************/
bit K_TestAck() 
{
	bit ErrorBit;
	K_SDA = 1;
	delay50us();
	K_SCL = 1;
	delay50us();
	ErrorBit = K_SDA;
	K_SCL = 0;
	delay50us();
	return(ErrorBit);
}

/***************************************************************************/
void K_Write8Bit(unsigned char input)
{
	unsigned char temp_2;

	tempp_p = input;
	for(temp_2 = 8; temp_2 != 0; temp_2--) 
	{
		K_SDA = DDD_7;
		_nop_();
		_nop_();
		_nop_();
		K_SCL = 1;
		delay50us();
		K_SCL = 0;
		tempp_p = tempp_p<<1;
		delay50us();
	}
}

/***************************************************************************/
void K_Write(unsigned char RomAddress,unsigned char Wdata)
{
	EA = 0;
	K_Start();
	K_Write8Bit(0x70);
	K_TestAck();
	K_Write8Bit(RomAddress);
	K_TestAck();
	K_Write8Bit(Wdata);
	K_TestAck();
	K_Stop();
	EA = 1;
	Delay1ms(15);
}

/***************************************************************************/
unsigned char K_Read8Bit()
{
	unsigned char temp_2,rbyte = 0;
	K_SDA = 1;
	for(temp_2=8;temp_2!=0;temp_2--)
	{
		K_SCL=1;
		delay50us();
		tempp_p=tempp_p<<1;
		DDD_0=K_SDA;
		K_SCL=0;
		delay50us();
	}
	return(tempp_p);
}

/***************************************************************************/
unsigned char K_Read(unsigned char RomAddress) 
{
	unsigned char temp_3;
	EA=0;
	K_Start();
	K_Write8Bit(0x70);
	K_TestAck();
	K_Write8Bit(RomAddress);
	K_TestAck();
	K_SDA=1;
	K_SCL=0;
	delay50us();
	K_Start();
	K_Write8Bit(0x71);
	K_TestAck();
	temp_3=K_Read8Bit();
	K_NoAck();
	K_Stop();
	EA=1;
	return(temp_3);
}

void KeyInit(void)
{	
	unsigned char temp;
	K_SCL = 1;
	K_SDA = 1;
	for(temp = 0; temp < 0x18; temp++)
	{
		KeyBuf[temp] = 0x00;
	}
	KeyBuf[0] = 0xf0;
	KeyBuf[1] = 0x00;
	KeyBuf[2] = 0x00;
	KeyBuf[3] = 0xff;
	KeyBuf[7] = 0x00;
	KeyBuf[8] = 0x00;
	KeyBuf[0x0c] = 0x77;
	KeyBuf[0x0d] = 0x07;
	for(temp = 0x10; temp< 0x18;temp++)
	{
		KeyBuf[temp]=0xff;
	}
	
	Key_WriteBuf();
}

void Key_WriteBuf(void)
{	
	unsigned char temp;
	for(temp = 0; temp < 0x18; temp++)
	{
		K_Write(temp,KeyBuf[temp]);
	}

}

void Key_ReadBuf(void)
{	
	unsigned char temp;
	for(temp = 0; temp < 0x18; temp++)
	{
			KeyBuf[temp] = K_Read(temp);
	}

}

unsigned char KeyReadSYS()
{
	return(K_Read(0));
	
}

unsigned char KeyRead()
{
	return(K_Read(1));
}

void KeyClear()
{
	unsigned char temp;

	K_Write(0x00,0xf0);
	K_Write(0x02,0x00);
	temp = KeyRead();
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -