色婷婷AⅤ一区二区三区|亚洲精品第一国产综合亚AV|久久精品官方网视频|日本28视频香蕉

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > AVR128的程序——24C02記錄復(fù)位次數(shù)

          AVR128的程序——24C02記錄復(fù)位次數(shù)

          作者: 時(shí)間:2016-11-22 來源:網(wǎng)絡(luò) 收藏
          #include
          #include
          #define uchar unsigned char
          #define uint unsigned int
          #define rd_device_add 0xA1
          #define wr_device_add 0xA0
          #define START 0x08
          #define RE_START 0x10
          #define MT_SLA_ACK 0x18
          #define MT_SLA_NOACK 0x20
          #define MT_DATA_ACK 0x28
          #define MT_DATA_NOACK 0x30
          #define MR_SLA_ACK 0x40
          #define MR_SLA_NOACK 0x48
          #define MR_DATA_ACK 0x50
          #define MR_DATA_NOACK 0x58
          #define Start() (TWCR=(1<
          #define Stop() (TWCR=(1<
          #define Wait() {while(!(TWCR&(1<
          #define TestAck() (TWSR&0xf8)
          #define SetAck() (TWCR|=(1<
          #define SetNoAck() (TWCR&=~(1<
          #define Twi() (TWCR=(1<
          #define Write8Bit(x) {TWDR=(x);TWCR=(1<
          void Delayms(uint MS);
          void display(uchar FirstBit,uchar Num);
          void i2cini(void) ;
          void delay_nus(unsigned int n);
          void delay_nms(unsigned int n);
          unsigned char i2c_Write(unsigned char Wdata,unsigned char RomAddress) ;
          unsigned char i2c_Read(unsigned char RomAddress) ;
          uchar TempData[8];
          uchar WeiMa[8] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
          uchar led_discode[11]={0xc0,0xf9,0xa4,0xb0,0x99,
          0x92,0x82,0xf8,0x80,0x90,0xff};
          void main(void)
          {
          uchar i;
          DDRA=0XFF;
          PORTA=0XFF;
          DDRC=0XFF;
          PORTC=0XFF;
          DDRF|=0X0E;
          PORTF|=0X0E;
          PORTF&=0xF7;
          DDRB |=0X10;
          PORTB|=0X10;
          i2cini();
          i=i2c_Read(0x03);
          i++;
          i=i;
          i2c_Write(i,0x03);
          TempData[0] = led_discode[i];
          while(1)
          {
          display(0,1);
          }
          }
          void i2cini(void)
          {
          TWBR=0x20;
          TWCR=0x04;
          TWSR=0;
          }
          unsigned char i2c_Write(unsigned char Wdata,unsigned char RomAddress)
          {
          unsigned char i;
          Start();//I2C啟動
          Wait();
          if(TestAck()!=START) return 1;//ACK
          Write8Bit(wr_device_add);//寫I2C從器件地址和寫方式
          Wait();
          if(TestAck()!=MT_SLA_ACK) return 1;//ACK
          Write8Bit(RomAddress);//寫24C02的ROM地址
          Wait();
          if(TestAck()!=MT_DATA_ACK) return 1;//ACK
          Write8Bit(Wdata);//寫數(shù)據(jù)到24C02的ROM
          Wait();
          if(TestAck()!=MT_DATA_ACK) return 1;//ACK
          Stop();//I2C停止
          for(i=0;i<250;i++);
          return 0;
          }
          unsigned char i2c_Read(unsigned char RomAddress)
          {
          unsigned char temp;
          Start();//I2C啟動
          Wait();
          if (TestAck()!=START) return 1;//ACK
          Write8Bit(wr_device_add);//寫I2C從器件地址和寫方式
          Wait();
          if (TestAck()!=MT_SLA_ACK) return 1;//ACK //
          //
          Write8Bit(RomAddress);//寫24C02的ROM地址
          Wait();
          if (TestAck()!=MT_DATA_ACK) return 1; //?
          //data1=TestAck();
          Start();//I2C重新啟動
          Wait();
          if (TestAck()!=RE_START) return 1;
          Write8Bit(rd_device_add);//寫I2C從器件地址和讀方式
          Wait();
          if(TestAck()!=MR_SLA_ACK) return 1;//ACK
          Twi();//啟動主I2C讀方式
          Wait();
          if(TestAck()!=MR_DATA_NOACK) return 1;//ACK
          temp=TWDR;//讀取I2C接收數(shù)據(jù)
          Stop();//I2C停止
          return temp;
          }
          void delay_nus(unsigned int n) //N us延時(shí)函數(shù)
          {
          unsigned int i=0;
          for (i=0;i
          asm("nop");
          }
          void delay_nms(unsigned int n) //N ms延時(shí)函數(shù)
          {
          unsigned int i=0,j=0;
          for (i=0;i
          for (j=0;j<1140;j++);
          }
          void Delayms(uint MS)
          {
          uint i,j;
          for( i=0;i
          for(j=0;j<1141;j++);
          }
          void display(uchar FirstBit,uchar Num)
          {
          unsigned char i;
          for(i=0;i
          {
          PORTC=led_discode[10];
          PORTA=WeiMa[i+FirstBit];
          PORTC=TempData[i];
          Delayms(5);
          }
          }


          關(guān)鍵詞: AVR12824C02復(fù)位次

          評論


          技術(shù)專區(qū)

          關(guān)閉