twi.h(自己参考,删了许多,否则暴露我军目标~~~)
#include "main.h"
#ifdef __IAR_SYSTEMS_ICC__ #ifndef _SYSTEM_BUILD #pragma system_include #endif #endif
#ifndef __IARAVR_TWI_H #define __IARAVR_TWI_H
#ifdef __cplusplus extern "C" { #endif
enum enum_TWIState { /* Master */ TW_START = 0x08, TW_REP_START= 0x10, /* Master Transmitter */ TW_MT_SLA_ACK= 0x18, TW_MT_SLA_NACK= 0x20, TW_MT_DATA_ACK= 0x28, TW_MT_DATA_NACK= 0x30, TW_MT_ARB_LOST= 0x38, /* Master Receiver */ TW_MR_ARB_LOST= 0x38, TW_MR_SLA_ACK= 0x40, TW_MR_SLA_NACK= 0x48, TW_MR_DATA_ACK= 0x50, TW_MR_DATA_NACK= 0x58, /* Slave Transmitter */ TW_ST_SLA_ACK= 0xA8, TW_ST_ARB_LOST_SLA_ACK= 0xB0, TW_ST_DATA_ACK= 0xB8, TW_ST_DATA_NACK= 0xC0, TW_ST_LAST_DATA= 0xC8, /* Slave Receiver */ TW_SR_SLA_ACK= 0x60, TW_SR_ARB_LOST_SLA_ACK= 0x68, TW_SR_GCALL_ACK= 0x70, TW_SR_ARB_LOST_GCALL_ACK= 0x78, TW_SR_DATA_ACK= 0x80, TW_SR_DATA_NACK= 0x88, TW_SR_GCALL_DATA_ACK= 0x90, TW_SR_GCALL_DATA_NACK= 0x98, TW_SR_STOP= 0xA0, /* Misc */ TW_NO_INFO= 0xF8, TW_BUS_ERROR= 0x00, TW_READ= 1, TW_WRITE= 0 };
#define TW_STATUS_MASK ((1 << TWS7) | (1 << TWS6) | (1 << TWS5) | (1 << TWS4) | (1 << TWS3)) #define TW_STATUS (TWSR & TW_STATUS_MASK)
#define DDR_SDA DDRC_Bit0 #define DDR_SCL DDRC_Bit1
#define SDA PORTC_Bit0 #define SCL PORTC_Bit1
#define UsiSlaveAddrWr 0xa0 #define UsiSlaveAddrRd 0xa1
class TwiObj; class TwiObj {//系统通讯类 public: TwiObj(void); void Start(void); void RepStart(void); void Stop(void); void Exec(void); void WorkExec(void); private: void Init(void); void Exit(void); void SetDataBuff(void); void SendCommand(void); public: bool Succeed; volatile unsigned char MainCount, SubCount; unsigned char SubAddr, SubComm; unsigned int DataBuffer[8]; private: volatile bool Busy; volatile unsigned char Status; unsigned char Count; unsigned char TxBuffer[4], RxBuffer[4]; };
#ifdef __cplusplus } #endif
#endif//__IARAVR_TWI_H |
|