<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > AVR單片機(jī)硬件I2C接口驅(qū)動(dòng)程序

          AVR單片機(jī)硬件I2C接口驅(qū)動(dòng)程序

          作者: 時(shí)間:2016-11-17 來(lái)源:網(wǎng)絡(luò) 收藏
          今天上午寫(xiě)了atmega16的I2C硬件接口程序,程序不難,只是給初學(xué)的朋友一個(gè)思路,不過(guò)通過(guò)和51的軟件模擬I2C程序比較發(fā)現(xiàn),AVR的I2C硬件接口使程序更簡(jiǎn)單了,
          程序功能:先由atmega16向EEPROM中某地址寫(xiě)數(shù)字71,然后再?gòu)腅EPROM里讀出來(lái)顯示到數(shù)碼管上;
          #include
          #include <avrdef.h>
          unsigned charconstduacode[]={0x3f,0x06,0x5b,0x4f,
          0x66,0x6d,0x7d,0x07,
          0x7f,0x6f,0x77,0x7c,
          0x39,0x5e,0x79,0x71}; //0-f 數(shù)字模*/
          void delay(unsigned int);
          void display(unsigned int);
          void send1byte(unsigned char address,unsigned char data);
          unsigned char read1byte(unsigned char address);
          #define START TWCR=BIT(7)|BIT(5)|BIT(2)
          #define STOP TWCR=BIT(7)|BIT(4)|BIT(2)
          #define WRITE(x) {TWDR=x; TWCR=BIT(7)|BIT(2);}
          void main()
          {unsigned char i=0;
          DDRC=0xff;
          while(1)
          {
          send1byte(4,71);
          i=read1byte(4);
          display(i);
          }
          }
          void send1byte(unsigned char address,unsigned char data)
          {
          START;
          while(!(TWCR&0x80));
          WRITE(0xa0);
          while(!(TWCR&0x80));
          WRITE(address);
          while(!(TWCR&0x80));
          WRITE(data);
          while(!(TWCR&0x80));
          STOP;
          delay(2);
          }
          unsigned char read1byte(unsigned char address)
          {
          unsigned char temp;
          START;
          while(!(TWCR&0x80));
          WRITE(0xa0);
          while(!(TWCR&0x80));
          WRITE(address);
          while(!(TWCR&0x80));
          START;
          while(!(TWCR&0x80));
          WRITE(0xa1);
          while(!(TWCR&0x80));
          TWCR=BIT(7)|BIT(2);
          while(!(TWCR&0x80));
          temp=TWDR;
          STOP;
          return temp;
          }
          void display(unsigned int dat)
          {
          unsigned char a[4],i;
          for(i=0;i<4;i++)
          {
          a[3-i]=dat%10;
          dat/=10;
          }
          DDRB=0xff; DDRD=0xff;
          for(i=0;i<4;i++)
          {
          PORTB=duacode[a[i]];
          PORTD&=~BIT(i);
          delay(10);
          PORTD|=BIT(i);
          }
          }
          void delay(unsigned int x)
          {
          unsigned int a; unsigned char b;
          for(a=x;a>0;a--)
          for(b=100;b>0;b--);
          }



          評(píng)論


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

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();