新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > armega16與24c16的操作

        armega16與24c16的操作

        作者: 時間:2016-11-26 來源:網絡 收藏
        atmega16有硬件IIC(TWI)接口,不需模擬,直接操作寄存器即可

        本文引用地址:http://www.104case.com/article/201611/321657.htm

        #include
        #include
        #define uchar unsigned char
        #define uint unsigned int

        #define Start() TWCR|=(1<#define Stop() TWCR|=(1<//終止信號
        #define Wait() while(!(TWCR&(1<//等待數據發送完
        #define Test() (TWSR&0XF8)//檢查twsr的狀態碼
        #define Write(X) {TWDR=X;TWCR=(1<//寫字節
        #define Read() TWCR|=(1<//讀字節

        extern const uchar table[]={
        ~0x3f,~0x06,~0x5b,~0x4f,
        ~0x66,~0x6d,~0x7d,~0x07,
        ~0x7f,~0x6f,~0x77,~0x7c,
        ~0x39,~0x5e,~0x79,~0x71};
        void delay(uint t)
        {
        uint i,j;
        for(i=0;i<10;i++)
        for(j=0;j}

        void init()
        {
        TWSR=0;
        TWBR=0X23;
        TWCR=0X44;
        }
        //可以通過讀出Test()來看TWSR處于什么狀態,也可以不用使用Test()

        uchar write()
        {
        Start();
        Wait();
        //if(Test()!=0x08)
        //return 0;
        Write(0xa0);
        Wait();

        if(Test()!=0x18)
        return 0;
        Write(3);

        Wait();

        //if(Test()!=0x28)
        //return 0;
        Write(56);
        Wait();

        // if(Test()!=0x28)
        // return 0;

        Stop();
        delay(1000);
        }
        uchar read()
        {


        uchar dat;
        TWSR=0;
        Start();
        Wait();
        //if(Test()!=0x08)
        // return 0;

        Write(0xa0);

        Wait();


        //if(Test()!=0X18)
        //return 0;
        Write(3);
        Wait();

        // if(Test()!=0x28)
        // return 0;


        Start();
        Wait();
        // if(Test()!=0x10)//這里必須寫10,因為是第二次開始
        // return 0;
        Write(0xa1);
        Wait();

        if(Test()!=0x40)
        return 0;
        Read();
        Wait();

        // if(Test()!=0x58)//讀完無應答
        // return 0;
        dat=TWDR;

        Stop();


        }
        void main()
        {
        uchar z,shi,ge;
        init();
        write();

        z=read();
        DDRA=0XFF;

        shi=z/10;
        ge=z;


        while(1) {


        PORTB=0xfe;
        PORTA=table[shi];
        delay(10);
        PORTB=0xfd;
        PORTA=table[ge];
        delay(10);

        }
        }



        關鍵詞: armega1624c1

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 甘孜县| 罗江县| 崇文区| 巩义市| 昌平区| 许昌县| 山阳县| 晋中市| 聊城市| 渝北区| 景东| 通州区| 宜阳县| 邵阳市| 淄博市| 灵璧县| 湖口县| 东明县| 盱眙县| 大港区| 桃园县| 浪卡子县| 敦煌市| 澄江县| 宜丰县| 哈尔滨市| 东乌珠穆沁旗| 微山县| 金山区| 伊吾县| 颍上县| 雷山县| 乐亭县| 牡丹江市| 普安县| 河西区| 盐城市| 南安市| 桂平市| 临湘市| 隆昌县|