getbyte.c

来自「AT90S8515读写AT17C010源程序」· C语言 代码 · 共 42 行

C
42
字号
// GetByte.c
// reads a byte from the slave, lastbyte is used to tell slave that the read is over

#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 GetByte(unsigned char lastbyte) {

// lastbyte == 1 for last byte
 
unsigned char i, bit;
unsigned char result = 0;
    
    DDRB &= 0xfe; // SDA to input

    for (i = 0;i < 8;i++) { // each bit at a time, LSB first
        SetSCLHigh();
        BitDelay();
        bit = (PINB & 0x01);
        result = (bit << (i)) | result;
        WriteSCL(0);
        BitDelay();
    }
                       
    // send ACK
    WriteSDA(lastbyte); // no ack on last byte ... lastbyte = 1 for the last byte
    BitDelay();
    SetSCLHigh();
    BitDelay();
    WriteSCL(0);
    BitDelay();
    WriteSDA(1);
    BitDelay();
    
    return(result);

}

⌨️ 快捷键说明

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