사극 관: IIC 의 AVR 24c 64 읽 기와 쓰기

주 함수:
4 극관: main. c
#include<avr/io.h>
#include<util/delay.h>
#include<1602driver.c>
#include<24c64.c>


int  main(void)
{
	uchar const data[]="0123456789";
	uchar data_from_24c64=0;
    	Init_1602();	
	Write_24c64(0x0000,000);
	_delay_ms(10);
	Displaypstr(0,1,"oh h!");
	DDRA=0Xff;
	PORTA=0X0f;
	data_from_24c64=Read_24c64(0x0000);
	while(1)
	{			
		Displaychar(0,0,data[data_from_24c64/100]);
		Displaychar(1,0,data[(data_from_24c64%100)/10]);
		Displaychar(2,0,data[data_from_24c64%10]);
        		_delay_ms(100);
	}
    	return  0;
}

4 극관: 24c 64 구동 (24C64. c)
/***********************************
    :24c64    
          
    :      : MCU: ATmega32L 
                      F_CPU = 16000000
                        :16000000 
    :A0/A1/A2  ;WP  ;scl/sda  10k          IO 
          			  
    :WinAVR-20080610	
    :  24c64   64kbit          ,     0000H-FFFFFH
     16        
             24c64  : 64kbit  2048 ,  32  ,     (   ):0--31(0x00--0x1f)
			  ( 11 ):  :0-2047(0x20---0xffff)
            :
		     1、   :(0x00--0x1f)
			 2、    :(0x20---0xffff)	  
            :
		     1、   :(0x00--0x1f)
			 2、    :(0x20---0xffff)	
  :    		  
  :2009 7 26 
   : 			   	(  :  )
 *******************************************************************/
//       
#define  SDA_1() {DDRB|=(1<<0);PORTB|=(1<<0);}		
#define  SDA_0() {DDRB|=(1<<0);PORTB&=~(1<<0);}
#define  SCL_1() {DDRB|=(1<<1);PORTB|=(1<<1);}		
#define  SCL_0() {DDRB|=(1<<1);PORTB&=~(1<<1);}

#define uint  unsigned int
#define uchar unsigned char
/*---------------IIC      -----------------------------------------
*   、      :
*        →     →     →   →. . .→    
*          :    (  )+      +1     (1:  ,0: )
*   、        :
*        →     →     →    →          →
*       →. . .→    
*   、        
*        →          →   →. . .→    
------------------------------------------------------------------------*/


/*---------------IIC      -----------------------------------------
*  IIC      :1010XXXX
*      :
*        BIT7-BIT4:IIC     4    1010, 10  A
*        BIT3-BIT1:           , PCF8563 001,24C   000
*        BIT0     :     ,0:   ;1:             
------------------------------------------------------------------------*/

/****************************************
**    :                        *
**     :                         *
**    :                            *
**     :uint time                    *
**    :                            *
****************************************/
void DelayMS(uint time) 
{ 
  do 
  { 
    time--;  
  } 
  while(time); 
} 

/****************************************
**    :                      *
**     :I2C                      *
**    :DelayMs();                   *
**     :                            *
**    :                            *
****************************************/
void  Start_24c64(void)
{
	SDA_1();
	DelayMS(2); 
	SCL_1();
	DelayMS(2);
	SDA_0();
	DelayMS(8);
	SCL_0(); 
	DelayMS(2); 
}	
/****************************************
**    :                      *
**     :I2C                      *
**    :DelayMs();                   *
**     :                            *
**    :                            *
****************************************/
void Stop_24c64(void)
{
    SDA_0();
	DelayMS(2);
	SCL_0();
	DelayMS(2);
	SCL_1();
	DelayMS(2);
	SDA_1();
	DelayMS(8);
	SCL_0();
	DelayMS(2);
}

/*********************************************
**    :           24c64    *
**     : 24c64     ,          *
**    :DelayMs();                        *
**     :uchar bytedata                    *
**    :uchar                             *
**********************************************/
uchar SendByte_24c64(uchar bytedata)
{
    uchar i;
	uchar flag=0;
	SCL_0();
	DelayMS(4);
	for(i=0;i<8;i++)
	{
		if(bytedata&0x80) 
		{
		    SDA_1();
		}	
		else              
		{
		    SDA_0();
		}	
		DelayMS(2);
		SCL_1();
		DelayMS(2);
		bytedata<<=1;
		SCL_0();
		DelayMS(2);
	}
	SDA_1();
	DelayMS(2);
	DDRB&=~(1<<0);
	SCL_1();	
	DelayMS(1);
	if(!(PINB&(1<<0)))
	{
		flag=1;
	}
	DDRB|=(1<<0);	
	SCL_0();
	DelayMS(2);
	return(flag);
	
}
/*********************************************
**    :                 *
**     :  24c64    ,          *
**    :DelayMs();                        *
**     :uchar ack                         *
**    :uchar                             *
**********************************************/
uchar ReceiveByte_24c64(uchar ack)
{
    uchar i,a=0;
	SCL_0();
	for(i=0;i<8;i++)
	{
		SDA_1();
		DelayMS(2);
	    a<<=1;
		DDRB&=~(1<<0);
		SCL_1();
		DelayMS(2);
		if(PINB&(1<<0)) 
		{
		    a++;
		}
		SCL_0();
		DelayMS(2);
	}	
	DDRB|=(1<<0);
	if(!ack)
	{
		SDA_1();
	}
	else
	{
		SDA_0();
	}
	SCL_1();
	DelayMS(2);
	SCL_0();
	DelayMS(2);
	return(a);	 
		 
} 

/*********************************************************
**    : 24c64                                   *
**     : 24c64                               *
**    :Start_24c64();SendByte_24c64();Stop_24c64();  *
**     :uint Address, uchar Data                      *
**    :                                             *
*********************************************************/

void Write_24c64(uint Address, uchar Data)
{ 
  uchar flag1=0,flag2=0,flag3=0,flag4=0;  
  Start_24c64();   //    
  flag1=SendByte_24c64(0xa0);  //           (w/R)  0
  DelayMS(5);
  if(flag1==1)
  {                                                                                                          
    flag2=SendByte_24c64((Address&0xff00)>>8);//       
	DelayMS(5);
	if(flag2==1)
	{
	   flag3=SendByte_24c64(Address&0xff);//       
	   DelayMS(5);
		if(flag3==1)
		{
			flag4=SendByte_24c64(Data); //   
			DelayMS(5);
		}
	}   
  }
    Stop_24c64();    //    
}
/**************************************************************************
**    : 24c64                                                    *
**     : 24c64                                                *
**    :Start_24c64();SendByte_24c64();ReceiveByte_24c64Stop_24c64();  *
**     :uint Address                                                   *
**    :uchar                                                          *
***************************************************************************/
 uchar Read_24c64(uint Address) //         
 {   

   uchar flag1=0,flag2=0,flag3=0,flag4=0;
	uchar readdata=0;
	Start_24c64();     //     
	flag1=SendByte_24c64(0xa0);  //     (   )
	DelayMS(5);
	if(flag1==1)
	{
		flag2=SendByte_24c64((Address&0xff00)>>8);//       
		DelayMS(5);
		if(flag2==1)
		{
			flag3=SendByte_24c64(Address&0xff);//       
			DelayMS(5);
			if(flag3==1)
			{
				Start_24c64();    //    
				flag4=SendByte_24c64(0xa1);   //     (   )
				DelayMS(5);
				if(flag4==1)
				{  
				   
					readdata=ReceiveByte_24c64(1);    //   
					DelayMS(5);
				}
			}
		}
	}
	Stop_24c64();
	return(readdata);	
}


4 극관: 액정 디 스 플레이 구동 (1602 driver. c)
/***********************************
     :1602            
     :    ,     :DATA_PORT
          
     :      : MCU: ATmega32L 
                      F_CPU = 16000000
                        :16000000 
     :3    :PC1-RS   PC2-RW  PC3-E
           4    :PC				  
     :WinAVR-20080610	
     :  
               :
             Displaychar(unsigned char x,unsigned char y, unsigned char wdata)
            (x       y    wdata        )
		        :
		     Displaypstr(unsigned char x,unsigned char y,unsigned char *str)
            (x       y     wdata        )
		  
       :X:0-15
                Y:0-1	
      :MyBit.h 	
   :  			  
   :2009 7 13 
    : 			   	(  :  )				   
***********************************/		  
//    		  
#define DATA_PORT PORTC
#define RS_0() {DDRC |=(1<<1);PORTC &=~(1<<1);}       
#define RS_1() {DDRC |=(1<<1);PORTC |= (1<<1);}
#define RW_0() {DDRC |=(1<<2);PORTC &=~(1<<2);}
#define RW_1() {DDRC |=(1<<2);PORTC |= (1<<2);}
#define E_0()  {DDRC |=(1<<3);PORTC &=~(1<<3);} 
#define E_1()  {DDRC |=(1<<3);PORTC |= (1<<3);} 


#define budy_in()   {DDRC&=~(1<<7);PORTC|=(1<<7);}
#define budy_high    PINC&(1<<7)
#define busy_out()   {DDRC|=(1<<7);}
// :                   
/****************************************
**    :readbusy
**     :    
**    : 
**     : 
----------------------------------------
****************************************/
void readbusy(void)
{
	 //        
	budy_in();//  
    RW_1();
	RS_0();
	E_1();
	while(budy_high);//         ????????@@@@@@
	E_0();
	busy_out(); 
}
/****************************************
**    :sentbyte
**     :     
**    :readbusy()
**     :      tybe=1   0  
----------------------------------------
****************************************/
void sentbyte(unsigned char i,unsigned char tybe)
{
    unsigned char temp;
	readbusy();
	
    if(tybe == 1)
	{
	    RW_0();
        RS_1();	
    }
	if(tybe == 0)
	{
	    RW_0();
		RS_0();
	}
	
	temp = i&0xf0;
	DATA_PORT &=0x0f;
    DATA_PORT |=temp;
    E_1();
    E_0(); 
	
	temp = (i<<4)&0xf0;
	DATA_PORT &=0X0f;
	DATA_PORT |=temp;
	E_1();
    E_0(); 
}
/****************************************
**    :Init_1602()
**     :   1602    
**     : 
----------------------------------------
****************************************/
void Init_1602(void)
{
    DDRC = 0xff;
	
    sentbyte(0x30,0);
    sentbyte(0x30,0);
	sentbyte(0x30,0);
	sentbyte(0x02,0);

    sentbyte(0x28,0);   //      :4       0x28,8       0x38
	sentbyte(0x01,0);  //  
	sentbyte(0x02,0);   //      
	sentbyte(0x0c,0);   //   ,     
	sentbyte(0x06,0);   //        
	sentbyte(0x01,0);   //  
}
/****************************************
**    :Locate_xy()
**     :   
**     :x,y
----------------------------------------
****************************************/
void Locate_xy(unsigned char x,unsigned char y )
{
    unsigned char addr=0;
	if(y == 0)addr=0x80+x;
	if(y == 1)addr=0xc0+x;
    sentbyte(addr,0);
}

/****************************************
**    :Displaychar()
**     :1602        
**     :x,y,wdata
----------------------------------------
****************************************/
void Displaychar(unsigned char x,unsigned char y,
    unsigned char wdata)
{
    Locate_xy(x,y);   
    sentbyte(wdata,1);
}
/****************************************
**    :Displaypstr()
**     :1602         
**     :x,y,*str
----------------------------------------
****************************************/
void Displaypstr(unsigned char x,unsigned char y,unsigned char *str)
{
    unsigned int i=0;
	Locate_xy(x,y);   
	while(str[i]!='\0')
	{
	    sentbyte(str[i++],1);
	    x++;
	    if(x>15)
	    {
		   x=0;
		   y++;
		   if(y==2)y=0;
		   Locate_xy(x,y);   
		}
	}    
}

     (MyBit.h)

/***********************************************************   :  File :MyBit.h   : ,   ,   :    A 0 : OA0=1;     A 1 : while(IA1);     A 2 : RA2=1;   :2007-2-14  Author : ***********************************************************/

typedef struct { unsigned  bit0: 1 ; unsigned  bit1: 1 ; unsigned  bit2: 1 ; unsigned  bit3: 1 ; unsigned  bit4: 1 ; unsigned  bit5: 1 ; unsigned  bit6: 1 ; unsigned  bit7: 1 ; }PORTtype,Byte;

typedef struct { unsigned  bit0: 1 ; unsigned  bit1: 1 ; unsigned  bit2: 1 ; unsigned  bit3: 1 ; unsigned  bit4: 1 ; unsigned  bit5: 1 ; unsigned  bit6: 1 ; unsigned  bit7: 1 ; unsigned  bit8: 1 ; unsigned  bit9: 1 ; unsigned  bit10: 1 ; unsigned  bit11: 1 ; unsigned  bit12: 1 ; unsigned  bit13: 1 ; unsigned  bit14: 1 ; unsigned  bit15: 1 ; }Word; /************************************************* IO *************************************************/

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

// A , PORTA #define A_OUT (*(volatile PORTtype *)(0x1B+0x20)) // A , DDRA #define A_DIR (*(volatile PORTtype *)(0x1A+0x20)) // A , PINA #define A_DIN (*(volatile PORTtype *)(0x19+0x20))

// B , PORTB #define B_OUT (*(volatile PORTtype *)(0x18+0x20)) // B , DDRB #define B_DIR (*(volatile PORTtype *)(0x17+0x20)) // B , PINB #define B_DIN (*(volatile PORTtype *)(0x16+0x20))

// C , PORTC #define C_OUT (*(volatile PORTtype *)(0x15+0x20)) // C , DDRC #define C_DIR (*(volatile PORTtype *)(0x14+0x20)) // C , PINC #define C_DIN (*(volatile PORTtype *)(0x13+0x20))

// D , PORTD #define D_OUT (*(volatile PORTtype *)(0x12+0x20)) // D , DDRD #define D_DIR (*(volatile PORTtype *)(0x11+0x20)) // D , PIND #define D_DIN (*(volatile PORTtype *)(0x10+0x20))

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

// OAX---- O output, // PORTA , , // X X // OBX,OCX,ODX

// RAX---- R rudder( ), // DDRA , // , RBX,RCX,RDX

// IAX---- I input, // PINA , , // IBX,ICX,IDX

//-----------------------************************ //

#define OA0 A_OUT.bit0 #define OA1 A_OUT.bit1 #define OA2 A_OUT.bit2 #define OA3 A_OUT.bit3 #define OA4 A_OUT.bit4 #define OA5 A_OUT.bit5 #define OA6 A_OUT.bit6 #define OA7 A_OUT.bit7

#define OB0 B_OUT.bit0 #define OB1 B_OUT.bit1 #define OB2 B_OUT.bit2 #define OB3 B_OUT.bit3 #define OB4 B_OUT.bit4 #define OB5 B_OUT.bit5 #define OB6 B_OUT.bit6 #define OB7 B_OUT.bit7

#define OC0 C_OUT.bit0 #define OC1 C_OUT.bit1 #define OC2 C_OUT.bit2 #define OC3 C_OUT.bit3 #define OC4 C_OUT.bit4 #define OC5 C_OUT.bit5 #define OC6 C_OUT.bit6 #define OC7 C_OUT.bit7

#define OD0 D_OUT.bit0 #define OD1 D_OUT.bit1 #define OD2 D_OUT.bit2 #define OD3 D_OUT.bit3 #define OD4 D_OUT.bit4 #define OD5 D_OUT.bit5 #define OD6 D_OUT.bit6 #define OD7 D_OUT.bit7

//--------------------*************************** //

#define RA0 A_DIR.bit0 #define RA1 A_DIR.bit1 #define RA2 A_DIR.bit2 #define RA3 A_DIR.bit3 #define RA4 A_DIR.bit4 #define RA5 A_DIR.bit5 #define RA6 A_DIR.bit6 #define RA7 A_DIR.bit7

#define RB0 B_DIR.bit0 #define RB1 B_DIR.bit1 #define RB2 B_DIR.bit2 #define RB3 B_DIR.bit3 #define RB4 B_DIR.bit4 #define RB5 B_DIR.bit5 #define RB6 B_DIR.bit6 #define RB7 B_DIR.bit7

#define RC0 C_DIR.bit0 #define RC1 C_DIR.bit1 #define RC2 C_DIR.bit2 #define RC3 C_DIR.bit3 #define RC4 C_DIR.bit4 #define RC5 C_DIR.bit5 #define RC6 C_DIR.bit6 #define RC7 C_DIR.bit7

#define RD0 D_DIR.bit0 #define RD1 D_DIR.bit1 #define RD2 D_DIR.bit2 #define RD3 D_DIR.bit3 #define RD4 D_DIR.bit4 #define RD5 D_DIR.bit5 #define RD6 D_DIR.bit6 #define RD7 D_DIR.bit7

//----------------------************************** //

#define IA0 A_DIN.bit0 #define IA1 A_DIN.bit1 #define IA2 A_DIN.bit2 #define IA3 A_DIN.bit3 #define IA4 A_DIN.bit4 #define IA5 A_DIN.bit5 #define IA6 A_DIN.bit6 #define IA7 A_DIN.bit7

#define IB0 B_DIN.bit0 #define IB1 B_DIN.bit1 #define IB2 B_DIN.bit2 #define IB3 B_DIN.bit3 #define IB4 B_DIN.bit4 #define IB5 B_DIN.bit5 #define IB6 B_DIN.bit6 #define IB7 B_DIN.bit7

#define IC0 C_DIN.bit0 #define IC1 C_DIN.bit1 #define IC2 C_DIN.bit2 #define IC3 C_DIN.bit3 #define IC4 C_DIN.bit4 #define IC5 C_DIN.bit5 #define IC6 C_DIN.bit6 #define IC7 C_DIN.bit7

#define ID0 D_DIN.bit0 #define ID1 D_DIN.bit1 #define ID2 D_DIN.bit2 #define ID3 D_DIN.bit3 #define ID4 D_DIN.bit4 #define ID5 D_DIN.bit5 #define ID6 D_DIN.bit6 #define ID7 D_DIN.bit7

 

 

 

 

 

。 : 。 : [email protected]


좋은 웹페이지 즐겨찾기