⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sendbyte.c

📁 一个AVR单片机at17c85的系统程序
💻 C
字号:
// SendByte.c
// send a byte of address or data to the slave
// parameter order used to select between sending LSB or MSB first
// returns a 1 if the slave didn't ack and a 0 if the slave did
 
#include "at17c.h"

extern void WriteSCL(unsigned char state);
extern void WriteSDA(unsigned char state);
extern void BitDelay(void);
extern unsigned char SetSCLHigh(void);

unsigned char SendByte(unsigned char byte, unsigned char order) {

unsigned char i;
unsigned char error;

    for (i = 0; i < 8; i++) {
        if (order) {
            WriteSDA(byte & 0x80); // if > 0 SDA will be a 1
            byte = byte << 1; // send each bit, MSB first for address
        }
        else {
            WriteSDA(byte & 0x01); // if > 0 SDA will be a 1
            byte = byte >> 1; // send each bit, LSB first for data
        }

        BitDelay();
        SetSCLHigh();
        BitDelay();
        WriteSCL(0);
        BitDelay();
    }
    
    // now for an ack
    
    // master generates clock pulse for ACK
    WriteSDA(1); // release SDA ... listen for ACK
    BitDelay();
    SetSCLHigh(); // ACK should be stable ... data not allowed to change when SCL is high
 
    // SDA at 0 ?
    error = (PINB & 0x01); // ack didn't happen if bit 0 = 1

    WriteSCL(0);
    BitDelay();
 
    return(error);

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -