中文字幕无码不卡一区二区三区_少妇被又大又粗又爽毛片久久黑人_91精品国产在热久久无毒不卡_久久久久久亚洲综合网站

技術(shù)熱線: 4007-888-234

專注差異化嵌入式產(chǎn)品解決方案 給智能產(chǎn)品定制注入靈魂給予生命

提供開發(fā)工具、應(yīng)用測(cè)試 完善的開發(fā)代碼案例庫(kù)分享

從全面的產(chǎn)品導(dǎo)入到強(qiáng)大技術(shù)支援服務(wù) 全程貼心伴隨服務(wù),創(chuàng)造無(wú)限潛能!

技術(shù)支持

PIC單片機(jī)軟件模擬i2c讀2402cC語(yǔ)言

更新時(shí)間: 2019-03-22

十年單片機(jī)開發(fā)方案公司深圳英銳恩分享PIC單片機(jī)軟件模擬i2c讀2402cC語(yǔ)言。

/*******************************************************************
一、程序說(shuō)明:
1, 24LC02器件地址是1010000R/W.
2, 數(shù)組寫入24LC02采取頁(yè)寫方式.
3, 數(shù)組code從24LC02讀出時(shí)采取自由讀方式.
4, 采用4.00M晶體。
5,采用軟件I2C。

二、硬件連接:
1, SDA------->23 pin.(當(dāng)然你可以任意選擇腳位)
2,  SCL------->18 Pin.(當(dāng)然你可以任意選擇腳位)
3, PORTD----->外接8個(gè)LED,顯示讀出的數(shù)據(jù),在這里,讀出的剛好是一個(gè)閃動(dòng)的流水燈狀態(tài)。

*******************************************************************/
#include "pic.h"

#define uchar   unsigned char
#define nop()  asm("nop"
#define SCL  TRISC3
#define SDA  TRISC4

void start_i2c();
void stop_i2c();
void send_byte(uchar c);
uchar receive_byte();
void I_send_str(uchar sla,uchar suba,uchar *s,uchar no);
void delay_250ms();
void i2c_error ();

uchar code[]={0x00,0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff};
uchar no,ack,c,data; 

void main(void)
{
 uchar i;
 TRISC=0Xff;     //C口設(shè)為輸入 RC3為SCL線,RC4為SDA線。
 PORTC=0X00;    
 TRISD=0X00;     //D口為輸出,顯示IC24LC02中讀出的內(nèi)容
 PORTD=0X00;     //初始顯示全亮

 I_send_str(0xa0,0x00,code,9); //頁(yè)寫入code數(shù)組到24LC02,器件地址為0Xa0,子地址為0X00,共9個(gè)數(shù)。
 
 delay_250ms();
 

 ///////////開始讀出到D口進(jìn)行顯示,根據(jù)Random read時(shí)序圖。
 while (1) 

 {
    for (i=0x00;i<0x09;i++)
     {
      start_i2c();
      send_byte(0xa0);      //發(fā)送器件地址,即DEVICE ADDRESS。
        if (ack==0) i2c_error(); //如果24LC02無(wú)應(yīng)答。則進(jìn)入I2C ERROR錯(cuò)誤指示。
      send_byte(i);         //發(fā)送字地址,即WORD ADDRESS。D口顯示數(shù)組。
        if (ack==0) i2c_error();
      start_i2c();          //重新啟動(dòng)總線。
      send_byte(0xa1);      //發(fā)送讀命令和器件地址DEVICE ADDRESS。
        if (ack==0) i2c_error();
      data=receive_byte();
      stop_i2c();
      PORTD=data;
      delay_250ms();
     }
  }
}


/*******************************************************************
起動(dòng)總線函數(shù)
函數(shù)原型: void start_i2c();
Function: start on the I2C bus
*******************************************************************/
void start_i2c()
{
 SDA=1;       //發(fā)送啟始條件的數(shù)據(jù)信號(hào)
 nop();
 SCL=1; 

 nop();nop();nop();nop();nop(); //24LC02要求建立時(shí)間大于4,7S
 SDA=0;                      //發(fā)送起始信號(hào)
 nop();nop();nop();nop();nop();
 SCL=0;                      //鉗住I2C總線,準(zhǔn)備發(fā)送數(shù)據(jù)或接收數(shù)據(jù)
 nop();nop();
}


/*******************************************************************
停止總線函數(shù)
函數(shù)原型: void stop_i2c();
Function: stop the I2C bus
*******************************************************************/
void stop_i2c()

SDA=0; //發(fā)送結(jié)束條件的數(shù)據(jù)信號(hào)
 nop();
 SCL=1;
 nop();nop();nop();nop();nop();
 SDA=1;
 nop();nop();nop();nop();
}

/*=================================================================
字節(jié)數(shù)據(jù)傳送函數(shù)
函數(shù)原型: void send_byte(uchar c);
Function: 將數(shù)據(jù)C發(fā)送出去,可以是地址,也可以是數(shù)據(jù),發(fā)完后等待回應(yīng),并對(duì)此狀態(tài)
         位進(jìn)行操作(不應(yīng)答或非應(yīng)答都使ack=0 ),發(fā)送數(shù)據(jù)正常,ack=1;ack=0
         表示被控器無(wú)應(yīng)答或損壞。
==================================================================*/
void send_byte(uchar c)
{
 uchar bit_count;
 for (bit_count=0;bit_count<8;bit_count++)
  {
   if ((c<<bit_count)&0x80) {sda="1;}">   else {SDA=0;}
   nop();
   SCL=1;
   nop();nop();nop();nop();nop();
   SCL=0;
  }
  nop();nop();
  SDA=1; 

 nop();nop();
  SCL=1;
  nop();nop();nop();
  if (RC4==1) ack=0;
  else ack=1; //用ASK=1為有應(yīng)答信號(hào)
  SCL=0;
  nop();nop();
}

/*==================================================================
字節(jié)數(shù)據(jù)接收函數(shù)
函數(shù)原型:uchar receive_byte();
FUNCTION: 用來(lái)接收從器件傳來(lái)的數(shù)據(jù),并判斷總線錯(cuò)誤(不發(fā)應(yīng)答信號(hào)),
         發(fā)完后請(qǐng)用應(yīng)答函數(shù)。
===================================================================*/
uchar receive_byte()
{
  uchar retc,bit_count; 

retc=0;
  SDA=1;
  for (bit_count=0;bit_count<8;bit_count++)
  {
     nop();
     SCL=0;
     nop();nop();nop();nop();nop();
     SCL=1;
     nop();nop();
     retc=retc<<1;
     if (RC4==1)  retc=retc+1;
     nop();nop();
   }
   SCL=0;
   nop();nop();
   return (retc);
}


/*================================================================
向有子地址器件發(fā)送多字節(jié)數(shù)據(jù)函數(shù)
函數(shù)原型: bit I_send_str(uchar sla,uchar suba,uchar *s,uchar no);
Function: 從啟動(dòng)總線到發(fā)送地址,數(shù)據(jù),結(jié)束總線的全過(guò)程,從器件地址sla。如果
         返回1表示操作成功,否則操作有誤。 

=================================================================*/
void I_send_str(uchar sla,uchar suba,uchar *s,uchar no)
{
  uchar i;
  start_i2c();
  send_byte(sla);
  if (ack==0)  i2c_error();
  send_byte(suba);
  if (ack==0)  i2c_error();
  for (i=0;i<no;i++)>   {
     send_byte(*s);
     if (ack==0)  i2c_error();
     s++;
   }
  stop_i2c();
 // return(1);

/*****************************************************************
延時(shí)函數(shù)
函數(shù)原型: void delay_250ms();
FUNCTION: 延明250ms
*****************************************************************/
void delay_250ms()
{
 unsigned int d=24999;
 while (--d);
}

/*****************************************************************
總線錯(cuò)誤函數(shù)
函數(shù)原型: void i2c_error();
Function: 通過(guò)RD7閃動(dòng)8次表示總線操作失敗一次報(bào)警。
*****************************************************************/
void i2c_error ()
{
 uchar i;
 for (i=0;i<8;i++)
 {
   RD7=0;
   delay_250ms();
   RD7=1;
   delay_250ms();
 }
 
}
/**********END**************/

404
返回首頁(yè) |  返回上一頁(yè)
区。| 沧州市| 海门市| 海阳市| 陇南市| 资源县| 高安市| 扎囊县| 巴彦县| 永城市| 庆云县| 洛南县| 玉田县| 丰原市| 江西省| 宜丰县| 广元市| 遵义市| 炉霍县| 保山市| 枞阳县| 凤庆县| 五家渠市| 阳谷县| 宝应县| 宜宾市| 枣强县| 改则县| 郸城县| 隆尧县| 沾益县| 宝应县| 濮阳县| 门头沟区| 呼图壁县| 仁寿县| 咸阳市| 越西县| 西藏| 海原县| 万年县|