| Wheelchair Control MCU firmware

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Enumerations Groups Pages
SRF08.c
Go to the documentation of this file.
1
11 //Includes
12 #include "SRF08.h"
13
14 //Functions
15 void I2C_Init(void) //initializes I2C
16 {
17 TWSR = 0x00; //prescaler set to 0;
18 TWBR = ((F_CPU/SCL_CLOCK)-16)/2; //F_CPU=16 MHZ, SCL_CLOCK=50 kHz
19 }
20
21 uint8_t I2C_Start(uint8_t address) //sends start sequence and addresses device
22 {
23 TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN); //send START
24
25 while(!(TWCR & (1<<TWINT))); //wait until START is sent
26
27 if (((TW_STATUS & 0xF8) != TW_START) && ((TW_STATUS & 0xF8) != TW_REP_START)) //check status register (must mask prescaler bits)
28 {
29 return TRUE;
30 }
31
32 TWDR = address; //load address
33
34 TWCR = (1<<TWINT) | (1<<TWEN); //transmit address
35
36 while(!(TWCR & (1<<TWINT))); //wait until address transmission is completed
37
38 if (((TW_STATUS & 0xF8) != TW_MT_SLA_ACK) && ((TW_STATUS & 0xF8) != TW_MR_SLA_ACK) ) //check status register (with masked prescaler bits)
39 {
40 return TRUE;
41 }
42
43 return FALSE;
44 }
45
46 void I2C_Stop(void) //sends stop sequence
47 {
48 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO); //send STOP
49
50 while(TWCR & (1<<TWSTO)); //wait until TWSTO (stop flag) is set
51 }
52
53 uint8_t I2C_Write( uint8_t data ) //writes data to the previously addressed device
54 {
55 TWDR = data; //load data
56
57 TWCR = (1<<TWINT) | (1<<TWEN); //start data transmission
58
59 while(!(TWCR & (1<<TWINT))); //wait until transmission is complete
60
61 if((TW_STATUS & 0xF8) != TW_MT_DATA_ACK) //check status register (mask prescaler bits)
62 {
63 return TRUE;
64 }
65
66 return FALSE;
67 }
68
69 uint8_t I2C_ReadAck(void) //reads data from the previously addressed device, generates ACK bit
70 {
71 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA); //start reading with ACK enabled (TWEA bit is set)
72
73 while(!(TWCR & (1<<TWINT))); //wait until transmission is complete
74
75 return TWDR;
76 }
77
78 uint8_t I2C_ReadNak(void) //reads data from the previously addressed device, generates NACK bit
79 {
80 TWCR = (1<<TWINT) | (1<<TWEN); //start reading with ACK enabled (TWEA bit is set to 0)
81
82 while(!(TWCR & (1<<TWINT))); //wait until transmission is complete
83
84 return TWDR;
85 }
86
87 uint8_t SRF08_Write(uint8_t address, uint8_t reg, uint8_t data) //writes data to the register of the assigned device; returns FALSE if write is successful / TRUE if write fails
88 {
89
90 #ifndef DEBUG
91
92 uint8_t status = I2C_Start(address); // initialize transmission
93
94 if (status) //status = TRUE -> writing error, wrong connection or unplugged device
95 {
96 I2C_Stop(); //end of transmission
97 }
98 else
99 {
100 I2C_Write(reg); //send address of register to be written to
101 I2C_Write(data); //data to be written
102 I2C_Stop(); //end of transmission
103 }
104
105 return status;
106
107 #else
108
109 return FALSE;
110
111 #endif
112
113 }
114
115 uint8_t SRF08_Read(uint8_t address, uint8_t reg) //reads data from the register of the assigned device; returns data / 0 on error
116 {
117
118 #ifndef DEBUG
119
120 uint8_t data;
121
122 data = I2C_Start(address); // initialize transmission
123
124 if (data) //data = 1 -> writing error, wrong connection or unplugged device
125 {
126 I2C_Stop(); //end of transmission
127
128 return FALSE;
129 }
130 else
131 {
132 I2C_Write(reg); // write address of the register to write to
133 I2C_Start(address + 1); // set device address and read bit
134 data = I2C_ReadNak(); // read one byte
135 I2C_Stop();
136 }
137
138 return data;
139
140 #else
141
142 return FALSE;
143
144 #endif
145
146 }