新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > AVR MEGA8 通過 SPI 總線讀寫 93C46

        AVR MEGA8 通過 SPI 總線讀寫 93C46

        作者: 時間:2016-11-10 來源:網絡 收藏
        AVR MEGA8 通過 SPI 總線讀寫 93C46,主要是練習SPI總線的使用而已,93C46的驅動相當的簡單,這個代碼也很好寫,調試也不難,冷藏了一段時間的代碼了,也懶得整理,copy出來就算了,隨便看看就算了。



        /*
        * 項目:SPI總線驅動93C46
        * 平臺:AVR MEGA8 + WinAVR
        * 作者:Etual /Etual@163.com
        * 日期:2008-8-18
        */

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

        SPI訪問部分代碼:

        /////////////////////////////////////////////////

        // pin assignment

        #define SS PB2 //chip select

        #define SCK PB5 //clock

        #define MOSI PB3 //input

        #define MISO PB4 //output


        #define SS_SET (PORTB|=(1<
        #define SCK_SET (PORTB|=(1<
        #define MOSI_SET (PORTB|=(1<< P>

        #define SS_CLR (PORTB &= ~(1<
        #define SCK_CLR (PORTB &= ~(1<
        #define MOSI_CLR (PORTB &= ~(1<< P>


        void spi_init(void)
        {
        DDRB |= (1<< SSBR >< MOSI > DDRB &=~(1<

        SPCR = 0x53;
        SPSR = 0x00;
        }


        void SendByte(u8 sData)
        {
        SPDR = sData;
        while(!(SPSR & (1<
        }

        u8 spi_read(void)
        {
        SPDR = 0x00;
        while(!(SPSR & (1<
        return SPDR;
        }

        //////////////////////////////////////////////////

        //start and stop condition of 93C46


        void Start(void)
        {
        u8 temp;

        temp = SPCR;
        SPCR = 0; // 禁止SPI功能

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

        SCK_CLR; // 手工產生一個起始位,93C46特殊的地方

        MOSI_SET; // 所以要特殊處理

        SS_SET;
        SCK_SET;
        //-----------------------------------------------

        SPCR = temp; // 使能SPI功能

        }

        void Stop(void)
        {
        SS_CLR;
        }

        //////////////////////////////////////////////////

        // write enable / diable

        void EWEN(void)
        {
        Start();
        SendByte(0x30); // EWEN command

        Stop();
        }

        void EWDS(void)
        {
        Start();
        SendByte(0x00); // EWDS command

        Stop();
        }


        //////////////////////////////////////////////////

        // read word

        u16 ReadWord(u8 addr)
        {
        u16 temp=0;
        u8 hig,low;

        Start();
        SendByte(addr | 0x80); // read command

        //------------------------ 切換到SPI模式1

        SPCR = 0x5b;
        hig = spi_read();
        low = spi_read();
        //------------------------ 切換回SPI模式0

        SPCR = 0x53;
        Stop();
        temp = (hig<<8) + low ;
        return temp;
        }

        //////////////////////////////////////////////////

        // write a word

        void WriteWord(u16 data,u8 addr)
        {
        EWEN();
        Start();
        SendByte(addr | 0x40); // write command

        SendByte((u8)(data>>8)); // send hig byte

        SendByte((u8)data); // send low byte

        Stop(); // wait at lease 2ms

        }

        void WriteAll(u16 data)
        {
        EWEN(); // write enable

        Start();
        SendByte(0x10); // write command

        SendByte((u8)(data>>8)); // send hig byte

        SendByte((u8)data); // send low byte

        Stop(); // wait at lease 10MS

        }



        評論


        技術專區

        關閉
        主站蜘蛛池模板: 长春市| 吉木乃县| 武川县| 尚志市| 赤城县| 万盛区| 灵石县| 星子县| 凭祥市| 左云县| 通许县| 饶平县| 安顺市| 麦盖提县| 台安县| 邳州市| 集贤县| 阿克| 新疆| 手游| 安宁市| 南召县| 东城区| 襄樊市| 安泽县| 察雅县| 渑池县| 中宁县| 奉化市| 类乌齐县| 茂名市| 台中县| 通化市| 分宜县| 荔浦县| 普宁市| 旬阳县| 达孜县| 彭山县| 正安县| 潜江市|