基于stc15f2k60s2芯片单片机编程(EEPROM24C02实验)

主函数

#include 
#include 
#include <12.h>
void shumaguan();
sbit K1=P3^0;
unsigned char a;

void Timer0Init();

unsigned char abile[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF};
unsigned char saomiao[]={0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF};
void Delay100us()		//@11.0592MHz
{
	unsigned char i, j;

	_nop_();
	_nop_();
	i = 2;
	j = 15;
	do
	{
		while (--j);
	} while (--i);
}

void main()
{
  P2=0XA0;P0=0X00;
	a=At24c02Read(1);
	Timer0Init();
	while(1)
	{
		if(K1==0)
		{
    Delay100us();
			if(K1==0)
			{
				a++;
			}
			while(!K1);
		}
       shumaguan();
     	At24c02Write(1,a);
	}
}
void Timer0Init(void)		//[email protected]
{
	AUXR |= 0x80;		//?????1T??
	TMOD &= 0xF0;		//???????
	TL0 = 0xAE;		//??????
	TH0 = 0xFB;		//??????
	TF0 = 0;		//??TF0??
	TR0 = 1;		//???0????
	ET0=1;
	EA=1;
}
void Timer0() interrupt 1
{
	int i;
  P2=P2&0x1f|0xe0;
	P0=0xff;
	P2&=0x1f;
	
	P2=P2&0x1f|0xc0;
	P0=1<<i;
	P2&=0x1f;
	
	P2=P2&0x1f|0xe0;
	P0=saomiao[i];
	P2&=0x1f;
	i++;
	if(i>7)
		i=0;
}

void shumaguan()
{
  saomiao[0]=abile[a%1000/100];
  saomiao[1]=abile[a%100/10];
	saomiao[2]=abile[a%10];
	saomiao[3]=0xff;
	saomiao[4]=0xff;
	saomiao[5]=0xff;
	saomiao[6]=0xff;
	saomiao[7]=0xff;
}

12.c


#include 
#include <12.h>

/*******************************************************************************
* º¯ÊýÃû         : Delay10us()
* º¯Êý¹¦ÄÜ		   : ÑÓʱ
* ÊäÈë           : ÎÞ
* Êä³ö         	 : ÎÞ
*******************************************************************************/

void Delay10us()
{
	unsigned char a,b;
	for(b=1;b>0;b--)
		for(a=2;a>0;a--);

}
/********************************************************************
*º¯ÊýÃû   £ºI2cStart()
*º¯Êý¹¦ÄÜ £ºÆðʼÐźţ¬ÔÚSCLʱÖÓÐźÅÔڸߵçƽÆÚ¼äSDAÐźŲúÉúÒ»¸öϽµÑØ
*ÊäÈë     £ºÎÞ
*Êä³ö     £ºÎÞ
*±¸×¢     £ºÆðʼ֮ºóSDAºÍSCL¶¼Îª0
********************************************************************/
void I2cStart()
{
	SDA=1;       //³õʼ»¯
	Delay10us();
	SCL=1;
	Delay10us(); //½¨Á¢Ê±¼äÊÇSDA±£³Öʱ¼ä>4.7us
	SDA=0;       //²úÉúϽµÑØ
	Delay10us(); //±£³Öʱ¼äÊÇ>4us
	SCL=0;       //Éè¼ÆΪµÍµçƽ
	Delay10us();
}
/********************************************************************
*º¯ÊýÃû   £ºI2cStop()
*º¯Êý¹¦ÄÜ £ºÖÕÖ¹Ðźţ¬ÔÚSCLʱÖÓÐźŸߵçƽÆÚ¼äSDAÐźŲúÉúÒ»¸öÉÏÉýÑØ
*ÊäÈë     £ºÎÞ
*Êä³ö     £ºÎÞ
*±¸×¢     £º½áÊøÖ®ºóSDAºÍSCL¶¼Îª1£¬±íʾ×ÜÏß¿ÕÏÐ
********************************************************************/
void I2cStop()
{
	SDA=0;
	Delay10us();
	SCL=1;
	Delay10us(); //½¨Á¢Ê±¼ä´óÓÚ4us
	SDA=1;
	Delay10us(); //SDA³ÖÐøʱ¼ä´óÓÚ4.7us
}
/*********************************************************************************
*º¯ÊýÃû   £ºI2cSendByte(unsigned char num)
*º¯Êý¹¦ÄÜ £ºÍ¨¹ýI2C·¢ËÍÒ»¸ö×Ö·û¡£ÔÚSCLʱÖÓÐźŸߵçƽÆڼ䣬±£³Ö·¢ËÍÐźÅSDC±£³ÖÎȶ¨
*ÊäÈë     £ºnum
*Êä³ö     £º0»ò1£¬·¢Ëͳɹ¦·µ»Ø1£¬·¢ËÍʧ°Ü·µ»Ø0
*±¸×¢     £º·¢ËÍÍêÒ»¸ö×Ö·ûSCL=0£¬SDA=1
*********************************************************************************/
unsigned char I2cSendByte(unsigned char dat)
{
	unsigned char a=0,b=0; //×î´ó255£¬Ò»¸ö»úÆ÷ÖÜÆÚΪ1us,×î´óÑÓ³Ù255us¡£
	for(a=0;a<8;a++)       //Òª·¢ËÍ8룬´Ó×î¸ßλ¿ªÊ¼
	{
		SDA=dat>>7;          //ÆðʼÐźÅÖ®ºóSCL=0£¬ËùÒÔ¿ÉÒÔÖ±½Ó¸Ä±äSDAÐźÅ
		dat=dat<<1;
		Delay10us();
		SCL=1;
		Delay10us();         //½¨Á¢Ê±¼ä>4.7us
		SCL=0;
		Delay10us();         //ʱ¼ä´óÓÚ4us
	}
	SDA=1;
	Delay10us();
	SCL=1;
	while(SDA)             //µÈ´ýÓ¦´ð£¬Ò²¾ÍÊǵȴý´ÓÉ豸°ÑSDAÀ­µÍ
	{
		b++;
		if(b>200)            //Èç¹û³¬¹ý2000usûÓÐÓ¦´ð·¢ËÍʧ°Ü£¬»òÕßΪ·Ç´ðÓ¦£¬±íʾ½ÓÊÜʧ°Ü
		{
			SCL=0; 
			Delay10us();
			return 0;
		}
	}
	SCL=0;
	Delay10us();
	return 1;
}
/****************************************************
*º¯ÊýÃû   £ºI2cReadByte()
*º¯Êý¹¦ÄÜ £ºÊ¹ÓÃI2C¶ÁÈ¡Ò»¸ö×Ö½Ú
*ÊäÈë     £ºÎÞ
*Êä³ö     £ºdat
*±¸×¢     £º½ÓÊÜÍêÒ»¸ö×Ö½ÚSCL=0,SDA=1.
****************************************************/
unsigned char I2cReadByte()
{
	unsigned char a=0,dat=0;
	SDA=1;       //ÆðʼºÍ·¢ËÍÒ»¸ö×Ö½ÚÖ®ºóSCL¶¼µÃ0
	Delay10us();
	for(a=0;a<8;a++)
	{
		SCL=1;
		Delay10us();
		dat<<=1;
		dat|=SDA;
		Delay10us();
		SCL=0;
		Delay10us();
	}
	return dat;
}
/*******************************************************************
*º¯ÊýÃû   £ºvoid At24c02Write(unsigned char addr,unsigned char dat)
*º¯Êý¹¦ÄÜ £ºÍù24c02µÄÒ»¸öµØַдÈëÒ»¸öÊý¾Ý
*ÊäÈë     £ºÎÞ
*Êä³ö     £ºÎÞ
*******************************************************************/
void At24c02Write(unsigned char addr,unsigned char dat)
{
	I2cStart();
	I2cSendByte(0xa0); //·¢ËÍдÆ÷²ÄµØÖ·
	I2cSendByte(addr); //·¢ËÍҪдÈëÄÚ´æµØÖ·
	I2cSendByte(dat);  //·¢ËÍÊý¾Ý
	I2cStop();
}
/*******************************************************************
*º¯ÊýÃû   £ºunsigned char At24c02Read(unsigned char addr)
*º¯Êý¹¦ÄÜ £º¶ÁÈ¡24c02µÄÒ»¸öµØÖ·µÄÒ»¸öÊý¾Ý
*ÊäÈë     £ºÎÞ
*Êä³ö     £ºÎÞ
*******************************************************************/
unsigned char At24c02Read(unsigned char addr)
{
	unsigned char num;
	I2cStart();
	I2cSendByte(0xa0); //·¢ËÍдÆ÷²ÄµØÖ·
	I2cSendByte(addr); //·¢ËÍÒª¶ÁÈ¡µÄµØÖ·
	I2cStart();
	I2cSendByte(0xa1); //·¢ËͶÁÆ÷¼þµØÖ·
	num=I2cReadByte(); //¶ÁÈ¡Êý¾Ý
	I2cStop();
	return num;
	}

12.h

#ifndef __12_H__
#define __12_H__



sbit SCL=P2^0;
sbit SDA=P2^1;

void I2cStart();
void I2cstop();
unsigned char I2SendByte(unsigned char dat);
unsigned char I2cReadByte();
void At24c02Write(unsigned char addr,unsigned char dat);
unsigned char At24c02Read(unsigned char addr);

#endif

你可能感兴趣的:(基于stc15f2k60s2芯片单片机编程(EEPROM24C02实验))