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

📄 iic.c

📁 contain many examples code for I2c,UART,string ,digital convert, read/write to EEprom in microchip P
💻 C
字号:


//1636 byte
#define	byte unsigned char

// routines used for 24LC256
void random_write(uint mem_adr, byte dat);
byte random_read(uint mem_adr);
// common i2c routines
byte i2c_in_byte(void);
void i2c_out_byte(byte o_byte);
void i2c_nack(void);
void i2c_ack(void);
void i2c_start(void);
void i2c_stop(void);
void i2c_high_sda(void);
void i2c_low_sda(void);
void i2c_high_scl(void);
void i2c_low_scl(void);

// LCD routines
void delay_ms(long t);
void delay_10us(int t);
#if 0
void lcd_init(void);
void out_RAM_str(int *s);
void lcd_hex_byte(int val);
void lcd_dec_byte(int val, int digits);
int num_to_char(int val);
void lcd_char(int ch);
void lcd_new_line(void);

#define TxData 0	// RA.0 for serial LCD
#endif
#define SDA_PIN RA1	// RB.2  RC4
#define SCL_PIN RA0	// RB.1

#define SDA_DIR TRISA1
#define SCL_DIR TRISA0

void set_action(uint	mem_adr)
{
	
 	i2c_start();
   	i2c_out_byte(0xa0);
   	i2c_nack();
   	i2c_out_byte((mem_adr >> 8) & 0xff);
   	i2c_nack();
   	i2c_out_byte(mem_adr & 0xff);
   	i2c_nack();

}


void random_write(uint mem_adr, byte dat)
{
	
  	set_action(mem_adr);
   	i2c_out_byte(dat);			// and finally the data
   	i2c_nack();
   	i2c_stop();
   	delay_ms(200); // allow for the programming of the eeprom
}
byte random_read(uint  mem_adr)
{
   	byte y;
   	set_action(mem_adr);
   	i2c_start();				// no intermediate stop
   	i2c_out_byte(0xa1);	// read operation
   	i2c_nack();
   	y=i2c_in_byte();
   	i2c_stop();
  	return(y);
}


// Common I2C Routines

byte i2c_in_byte(void)
{
   	byte i_byte, n;
   	i2c_high_sda();
   	for (n=0; n<8; n++)
   	{
      	i2c_high_scl();

      	if (SDA_PIN)
      	{
         i_byte = (i_byte << 1) | 0x01; // msbit first
      	}
      else
      {
         i_byte = i_byte << 1;
      }
      i2c_low_scl();
   }
   return(i_byte);
}

void i2c_out_byte(byte o_byte)
{
   byte n;
   for(n=0; n<8; n++)
   {
      if(o_byte&0x80)
      {
         i2c_high_sda();
      }
      else
      {
         i2c_low_sda();
      }
      i2c_high_scl();
      i2c_low_scl();
      o_byte = o_byte << 1;
   }
   i2c_high_sda();
}

void i2c_nack(void)
{
   i2c_high_sda();	// data at one
   i2c_high_scl();	// clock pulse
   i2c_low_scl();
}

void i2c_ack(void)
{
   i2c_low_sda();	// bring data low and clock
   i2c_high_scl();
   i2c_low_scl();
   i2c_high_sda();
}


void i2c_start(void)
{
   i2c_low_scl();
   i2c_high_sda();
   i2c_high_scl();	// bring SDA low while SCL is high
   i2c_low_sda();
   i2c_low_scl();
}

void i2c_stop(void)
{
   i2c_low_scl();
   i2c_low_sda();
   i2c_high_scl();
   i2c_high_sda();  // bring SDA high while SCL is high
   // idle is SDA high and SCL high
}

void i2c_high_sda(void)
{
   // bring SDA to high impedance
   SDA_DIR = 1;
   delay_10us(5);
}

void i2c_low_sda(void)
{
   SDA_PIN = 0;
   SDA_DIR = 0;  // output a hard logic zero
   delay_10us(5);
}

void i2c_high_scl(void)
{
   SCL_DIR = 1;   // high impedance
   delay_10us(5);
}

void i2c_low_scl(void)
{
   SCL_PIN = 0;
   SCL_DIR = 0;
   delay_10us(5);
}

// LCD routines

void delay_10us(int t)
{
 uchar f;
 for(f=0;f<1;f++)
	  asm("clrwdt");
}

void delay_ms(long t)	// delays t millisecs
{
   do
   {
     delay_10us(100);
     asm("clrwdt");
   } while(--t);
}



//--------------------------------------------------------------------------------

⌨️ 快捷键说明

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