00001
00033 #include <iotiny24.h>
00034
00035
00036
00037
00038
00039
00040 #define SYS_CLK 1000.0 // [kHz]
00041
00042 #ifdef TWI_FAST_MODE // TWI FAST mode timing limits. SCL = 100-400kHz
00043 #define T2_TWI ((SYS_CLK *1300) /1000000) +1 // >1,3us
00044 #define T4_TWI ((SYS_CLK * 600) /1000000) +1 // >0,6us
00045
00046 #else // TWI STANDARD mode timing limits. SCL <= 100kHz
00047 #define T2_TWI ((SYS_CLK *4700) /1000000) +1 // >4,7us
00048 #define T4_TWI ((SYS_CLK *4000) /1000000) +1 // >4,0us
00049 #endif
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 #define W 0 //Data transfer direction WRITE
00064 #define R 1 //Data transfer direction READ
00065 #define OWN_ADR 60 //Own slave address
00066 #define SUCCESS 0xFF
00067
00068 #define TWI_READ_BIT 0 // Bit position for R/W bit in "address byte".
00069 #define TWI_ADR_BITS 1 // Bit position for LSB of the slave address bits in the init byte.
00070 #define TWI_NACK_BIT 0 // Bit position for (N)ACK bit.
00071
00072 #define USI_TWI_NO_DATA 0x00 // Transmission buffer is empty
00073 #define USI_TWI_DATA_OUT_OF_BOUND 0x01 // Transmission buffer is outside SRAM space
00074 #define USI_TWI_UE_START_CON 0x02 // Unexpected Start Condition
00075 #define USI_TWI_UE_STOP_CON 0x03 // Unexpected Stop Condition
00076 #define USI_TWI_UE_DATA_COL 0x04 // Unexpected Data Collision (arbitration)
00077 #define USI_TWI_NO_ACK_ON_DATA 0x05 // The slave did not acknowledge all data
00078 #define USI_TWI_NO_ACK_ON_ADDRESS 0x06 // The slave did not acknowledge the address
00079 #define USI_TWI_MISSING_START_CON 0x07 // Generated Start Condition not detected on bus
00080 #define USI_TWI_MISSING_STOP_CON 0x08 // Generated Stop Condition not detected on bus
00081
00082
00083 #define DDR_USI DDRA
00084 #define PORT_USI PORTA
00085 #define PIN_USI PINA
00086 #define PORT_USI_SDA PORTA6
00087 #define PORT_USI_SCL PORTA4
00088 #define PIN_USI_SDA PINA6
00089 #define PIN_USI_SCL PINA4
00090
00091 #define TRUE 1
00092 #define FALSE 0
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 typedef struct
00105 {
00106 unsigned char slave_adr;
00107 unsigned char size;
00108 unsigned char *data_ptr;
00109 } tx_type;
00110
00111
00112
00113 void USI_TWI_Master_Initialise( void );
00114 unsigned char USI_TWI_Start_Transceiver_With_Data(unsigned char *, unsigned char);
00115
00116
00117 unsigned char USI_TWI_LCD_Test(void);
00118 unsigned char USI_TWI_Get_State_Info( void );
00119
00120 #pragma vector = USI_START_vect
00121 __interrupt void USI_START_ISR(void);