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

📄 ledsubfuc.c

📁 在51上控制LED屏的显示和输出
💻 C
字号:
/*******************************************************************************/
/******  RTC&TEMPERATURE SUB SYSTEM ******/

/******************DESIGNED BY LINGFENG*************2004/9/2*********************/
/******************CHANGE BY CAOCONG****************2004/10/10*******************/


/*******************************************************************************/


/********include files**********************/

//#include<w77e58.h>
#include<absacc.h>
#include <setjmp.h>
#include<intrins.h>
#include<string.h>
#include"ledsubfuc.h"



/*********************************program******************************/



/***********DS1820 sub_function****************************************/
void delay100m()
 {
  	uchar i,j,k;
    	for(i=0;i<8;i++)
    		{
			for(j=0;j<25;j++)
	      			{
	      				for(k=0;k<250;k++);
				}
    		}
 }

void delay15()                        //delay time is  15us
{
	uchar i;
	for(i=0;i<5;i++);
}

void delay60()                       //delay time is 60us     
{
	uchar i;
  	for(i=0;i<20;i++);
}

void write_0_IO()
{
 	DS1820=1;
    	NOP;
  	DS1820=0;
    delay15();	  					//delay 60us
	delay15();
	delay15();
	delay15();
	DS1820=1;            				 //over 
	NOP;
	NOP;

}

void write_1_IO()
{
	DS1820=1;
	NOP;
	DS1820=0;
	NOP;
	NOP;
	NOP;
	NOP;
	NOP;
	NOP;
	NOP;
	DS1820=1;
	NOP;
	NOP;
	NOP;
	NOP;
	NOP;
	NOP;
	NOP;
	delay15();
	delay15();
	delay15();
	DS1820=1;     // over
	NOP;

}


bit read_IO()
{
	bit b;
    	DS1820 =1;
	DS1820=0;
	NOP;
    	NOP;
	DS1820=1;
	NOP;
    	NOP;
    	NOP;
    	NOP;
    	NOP;
	NOP;
	NOP;
	NOP;
	NOP;
	b=DS1820;
	delay15();
	delay15();	
	delay15();
	NOP;
	NOP;
	DS1820=1;                //over
	NOP;
	return b;
	
}

void reset_IO()
{
   	uchar i;
   	DS1820=1;
   	DS1820=0;
     	for(i=0;i<9;i++)
     		{
	    		delay60();                   //----540us----
     		}
   	DS1820=1;
       delay15();
	delay15();                  //----delay 30us-----
      	while(DS1820);
	for(i=0;i<8;i++)
		{
		 	delay60();
		}
	DS1820=1;             //over
	
}

void write_b_IO(uchar byte)            //write byte data to ds1820
{
   	uchar i;
     	for(i=0;i<8;i++)
		{
	     		if(byte&0x01)
	     			{
		    			write_1_IO();
	     			}
		 	else
		 		{
					write_0_IO();
		 		}
	   		byte=byte>>1;
	 	}
}

uchar read_b_IO()                     //read byte data from ds1820
{
  	uchar i,j;
  	bit  b;
	j=0;
    	for(i=0;i<8;i++)
		{
	    		b=read_IO();
		  	if(b){
		   		j+=1;
		  		}
			j=_cror_(j,1);   //sycle right shift a bit
	 	}
	return j;
	
}



void ds1820_ini()
{		

	reset_IO();		        //reset the io
	
}


 

void  get_temp(uchar time[])
{
    	uchar i;
	bit b;
	b=0;
      	reset_IO();
      	write_b_IO(0xcc);   //skip the ROM
      	write_b_IO(0x44);                       //convert T
      	for(i=1;i<20;i++)  
       	{                                     //delay 2s
       		delay100m();      
			watchdog();
      		}
      	while(!b){
		b=read_IO();
      		}
     	reset_IO();
	write_b_IO(0xcc);   //skip the ROM
       write_b_IO(0xbe);                       //read T
       temp_x=read_b_IO();                      // T lsb
       temp_z=read_b_IO();                      // T  msb
       reset_IO();
    

	  //temp_z store sign of temperature data  
	  // if temp_z is 0xff the temperature is minus 
 	  //temp_x store temperature data
	  // bit7-bit4 is temperature`s HSB   bit3-bit0 is temperature`s LSB
   	if (temp_z)                               
      		{                                       
       		temp_x=temp_x^0xff;                    
       		temp_x=temp_x+0x01;                            
	  	}
       temp_x=temp_x>>1;       //int the temperature data
	i=temp_x/10;            //temperature`s HSB                        
	i=i<<4;
	i|=temp_x%10;           //add temperature`s LSB
	time[7]=temp_z;
       time[8]=temp_x;	     

}
        
       
                                    
 



          


	 




 
  


/*****************PCF8563 sub function********************************/


void start_i2c(void)
{
  	SDA=1;                    //ready for send  signal of begin                     
    NOP;
		
  	SCL=1;
  	NOP;                    
  	NOP;
  	NOP;
  	NOP;
  	NOP; 
	
  	SDA=0;                   //send  signal of beign  
  	NOP;     
  	NOP;
  	NOP;
  	NOP;
  	NOP;
	
  	SCL=0;                   //control i2c
  	NOP;
  	NOP;
	
}


void stop_i2c(void)
{

  	SDA=0;                   //ready send  signal of end
  	NOP;
	
  	SCL=1;   
  	NOP;
  	NOP;
  	NOP;
  	NOP;
  	NOP;
	
  	SDA=1;                  //send   signal  of end  
  	NOP;
  	NOP;
  	NOP;
  	NOP;
	
}


  
bit  sendbyte(uchar c)          //if  ACK=1 the data of sended  is ok  
{                               //if  ACK-0 the data of sended  is not received
 	uchar BitCnt;
	bit ACK;

	for(BitCnt=0;BitCnt<8;BitCnt++)     // data`length is eight
    		{
     			if((c<<BitCnt)&0x80)
				{
					SDA=1;
     				}
       		else  
				{
				SDA=0;
       			}
     	NOP;
		
     	SCL=1;                //begin receive data
     	NOP;               //delay
     	NOP;                
    	NOP;
     	NOP;
     	NOP;
     	SCL=0;
		
               }

    	NOP;
    	NOP;
		
    	SDA=1;                //begin receive ACK
    	NOP;
    	NOP;
    	SCL=1;
    	NOP;
    	NOP;
    	NOP;
		
    	if(SDA==1)
		{
			ACK=0;        //Is receive ACK?
    		}
       else 
	   	{
	   		ACK=1; 
       	}
    	SCL=0;
    	NOP;
    	NOP;
		
    	return(ACK);
		
}


uchar  rcvbyte()
{
  	uchar retc;
  	uchar BitCnt;

  	retc=0;
  	SDA=1;              //set receive`state 
  	for(BitCnt=0;BitCnt<8;BitCnt++)
      		{
        		NOP;
			
        		SCL=0;          //begin receive data
        		NOP;           //delay
        		NOP;          
        		NOP;
        		NOP;
        		NOP;
        		SCL=1;          //make data valid
        		NOP;
        		NOP;
			
        		retc=retc<<1;
        		if(SDA==1)retc=retc+1;           
        		NOP;
        		NOP;
      		}
  	SCL=0;
  	NOP;
  	NOP;
	
  	return(retc);
}

  

void ack_i2c(bit a)
{

  	if(a==0)
		{
			SDA=0;        //is not last byte
  		}
        else
		{
			SDA=1;     //is last byte
        	}
  	NOP;
  	NOP;
  	NOP;
  	SCL=1;
    	NOP;
    	NOP;               
    	NOP;
    	NOP;
    	NOP;
		
 	SCL=0;                 //begin receive data  again
    	NOP;
    	NOP;
		
}

 

void  pcf8563_init(void)
{
    	uchar i;
	

       for (i=0;i<50;i++)  
	   	{
	   		NOP;
       	}
       start_i2c();              //write control word
       while(~sendbyte(0xa2));                //send  slave address      1010001|r/w
       while(~sendbyte(0x00));                //send  word address 
       while(~sendbyte(0x00));                //send  data
       while(~sendbyte(0x01));                //enable the interrupt of timer  (TIE=1)
       start_i2c();
	while(~sendbyte(0xa2));                //send  slave address      1010001|r/w
       while(~sendbyte(0x0e));                //send  word address
       while(~sendbyte(0x83));                //enable the timer,  the frequency  is 1/60Hz  
       while(~sendbyte(0x05));                //the time of  timer's interrupet is 5min    
	stop_i2c();
	get_time(time);
	if (time[6]==0x00)
		{                          // set default time
		   	time[0]=0x00;            //sec
		   	time[1]=0x30;            //min
		   	time[2]=0x05;            //hour
		   	time[3]=0x19;            //date
		   	time[4]=0x04;            //week
		   	time[5]=0x05;            //month
		    time[6]=0x05;            //year
		   	
			set_time(time);   
         	}
	
}




 
void set_time(uchar time[])
{
    	uchar i;
	start_i2c();
      	while(~sendbyte(0xa2));
      	while(~sendbyte(0x02));
    	for(i=0;i<7;i++)
    		{
      			while(~sendbyte(time[i]));
    		}
      stop_i2c();
}
 

void get_time(uchar time[])
{
    	uchar i;
    	bit a;
    	a=0;

       start_i2c();
      	while(~sendbyte(0xa2));               //send  slave  address
      	while(~sendbyte(0x02));               //send word address  
      	start_i2c();
      	while(~sendbyte(0xa3));                    
       for (i=0; i<7; i++)
         	{
           		time[i]=rcvbyte();
           		if (i==6){
				a=1;
           			}
           		ack_i2c(a);
         	}
       stop_i2c();
    
     
}



/************************24C01*****************************************/

void  get_c_code(uchar *ptr, uchar cnt)
{     
       uchar i;
	 bit a;
	 a=0;
       start_i2c();
	while(~sendbyte(0xa0));              //write address 
	while(~sendbyte(0x00));
       start_i2c();
	 while(~sendbyte(0xa1));            //begin read data
	for (i=0; i<cnt; i++)
		{
			 *(ptr+i) = rcvbyte();
			 if (i==(cnt-1))
			 	{
			 		a=1;
			 	}
			 ack_i2c(a);
		}
	stop_i2c();
}
	


void store_c_code (uchar *ptr, uchar  cnt)
{      
	uchar i, j, res;

	i =(uchar) cnt/16;
	res = (uchar)cnt%16;
	while (i>0)
		{                                                                      //send  16 byte data
			start_i2c();
			while (~sendbyte(0xa0))
				{
				     delay15();
				}
			while (~sendbyte(0x00))

		 	for (j=0; j<16; j++)
		 		{
		        		while (~sendbyte(*(ptr+j)));
		 		}
			ptr+=16;
			stop_i2c();
			i--;
		}
        if (res>0) {                                                      //send  reserved data
			
		start_i2c();
		while (~sendbyte(0xa0))
				{
				     delay15();
				}
		for (j=0; j<res; j++ )
			{
				while(~sendbyte(*(ptr+j)));
			}
		stop_i2c();
		
        	}
}


















⌨️ 快捷键说明

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