51 단편기 PCF8591-AD-DA 변환

3498 단어 51 노트
51 단편기 PCF8591-AD-DA 변환, 중단 전환 실현.

#include 
#define uchar unsigned char
#define uint unsigned int

sbit SCL=P2^1; 	//       
sbit SDA=P2^0; 	//       
sbit HC595_RCLK  = P2^5;
sbit HC595_SRCLK = P2^6;
sbit HC595_SER   = P2^7;

uchar flag,n,a,b,c,yi,er,san,si,wu,liu,qi,ba;
uchar code shuma_duan[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x40,0x00};
void display();
void delay()	//  4-5   
{;;}
//
void Delay_Ms(uint z)
{
	uint x,y;
	for(x=z;x>0;x--)
		for(y=110;y>0;y--);
}
//
void Send_595(uchar dat)
{		
	uchar	i;
	for(i=0; i<8; i++)
	{
		if(dat & 0x80)
			HC595_SER = 1;
		else			
			HC595_SER = 0;
		
		HC595_SRCLK = 0;
		HC595_SRCLK = 1;
 
		dat = dat << 1;
	}
}
//
void start()//    
{
	SDA=1;
	delay();
	SCL=1;
	delay();
	SDA=0;
	delay();
}
//
void stop()	//    
{
	SDA=0;
	delay();
	SCL=1;
	delay();
	SDA=1;
	delay();
}
//
void respons()//  	              
{
	uchar i;
	SCL=1;
	delay();
	while((SDA==1)&&(i<250))i++;
	SCL=0;
	delay();
}
//
void init()	//   
{
	SDA=1;
	delay();
	SCL=1;
	delay();
}

uchar read_byte()
{
	uchar i,k;
	SCL=0;
	delay();
	SDA=1;
	delay();
	for(i=0;i<8;i++)
	{
		SCL=1;
		delay();
		k=(k<<1)|SDA;//     ,          
		SCL=0;
		delay();
	}
	return k;
}
//
void write_byte(uchar date)	//      
{
	uchar i,temp;
	temp=date;
	for(i=0;i<8;i++)
	{
		temp=temp<<1;	//           CY 
		SCL=0;			//   scl=0 sda    
		delay();
		SDA=CY;
		delay();
		SCL=1;
		delay();
		SCL=0;
	 delay();		
	}	
	SDA=1;
	delay();
}
//
void write_process(uchar kongzhizi,uchar date)
{
	start();
	write_byte(0x90);	//1010 0000                          0              
	respons();
	write_byte(kongzhizi);
	respons();
	write_byte(date);
	respons();
	stop();
}
//
uchar read_process(uchar kongzhizi)
{
	uchar date;
	
	start();
	write_byte(0x90);
  respons();
	write_byte(kongzhizi);
	respons();
	
	start();
	write_byte(0x91);	 //       1, 
	respons();
	date = read_byte();
	stop();
	
	return date;
}
//
void main()
{
	flag = 0;
	
	EA = 1;
	EX0 = 1;
	IT0 = 1;
	while(1)
	{
		//A/D  
    //  AD  ,     。
		if(flag == 0)
		{
			init();
			n = read_process(0x00);      //rb2     
//			n = read_process(0x01);      //               
//			n = read_process(0x02);      //               
//			n = read_process(0x03);      //   0
			
			yi=n/100;er=n%100/10;san=n%10;
			si=11;wu=11;liu=11;qi=11;ba=11;
			display();
		}
		//D/A  
                //          ,    。
		if(flag == 1)
		{
			if(a <= 250)
			{
				a++;
				c = a;
				if(a == 251)
				{
					b = 250;
				}
			}
			
			if(b >= 2)
			{
				b--;
				c = b;
				if(b == 1)
				{
					a = 0;
				}
			}
			Delay_Ms(5);
			write_process(0x40,c);
		}
	}
}
//
void exter() interrupt 0  //  D/A    A/D  。
{
	flag++;
	if(flag == 2)
	{
		flag = 0;
	}
}
//
void display()
{
	Send_595(0xfe); 
	Send_595(shuma_duan[yi]);
	HC595_RCLK = 0;
	HC595_RCLK = 1;

	Send_595(0xfd); 
	Send_595(shuma_duan[er]);
	HC595_RCLK = 0;
	HC595_RCLK = 1;
	
	Send_595(0xfb); 
	Send_595(shuma_duan[san]);
	HC595_RCLK = 0;
	HC595_RCLK = 1;

	Send_595(0xf7);
	Send_595(shuma_duan[si]);
	HC595_RCLK = 0;
	HC595_RCLK = 1;
	
	Send_595(0xef); 
	Send_595(shuma_duan[wu]);
	HC595_RCLK = 0;
	HC595_RCLK = 1;

	Send_595(0xdf); 
	Send_595(shuma_duan[liu]);
	HC595_RCLK = 0;
	HC595_RCLK = 1;
	
	Send_595(0xbf); 
	Send_595(shuma_duan[qi]);
	HC595_RCLK = 0;
	HC595_RCLK = 1;

	Send_595(0x7f); 
	Send_595(shuma_duan[ba]);
	HC595_RCLK = 0;
	HC595_RCLK = 1;
}

개인적인 견해, 읽어주셔서 감사합니다.

좋은 웹페이지 즐겨찾기