Monday, September 28, 2015

Getting data from ADXL345 From USART - ATmega32




A small update on Interfacing ADXL345 with ATmega32, this program will get raw data from ADXL345 working on I2C mode and send the data over USART.

To recieve data to PC over USART see this.



  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include<avr/io.h>
#include<inttypes.h>
#include<util/delay.h>
#include<math.h>

#define F_CPU 8000000UL
#define error 0
#define success 1

//USART
void USART_Init( unsigned int baud )
{
    /* Set baud rate */
    UBRRH = (unsigned char)(baud>>8);
    UBRRL = (unsigned char)baud;
    /* Enable transmitter and receiver */
    UCSRB = (1<<TXEN)|(1<<RXEN);
    /* Set frame format: 8data, 2stop bit */
    UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
}

void USART_Transmit_char(char txdata )
{
    /* Wait for empty transmit buffer */
    while ( !( UCSRA & (1<<UDRE)) )
    ;
    /* Put data into buffer, sends the data */
    UDR = txdata;
}

unsigned char USART_Receive( void )
{
 /* Wait for data to be received */
 while ( !(UCSRA & (1<<RXC)) );
 /* Get and return received data from buffer */
 return UDR;
}

//This function can only send three digit integers.
void USART_Transmit_int(unsigned int i)
{
        unsigned char h,t,o;
        o= (i%10) | 0x30;//ones
        i/=10;
        t= (i%10) | 0x30;//tens
        i=i%100;
        h=(i/10) | 0x30;//hundreds
        USART_Transmit_char(h);
        USART_Transmit_char(t);
        USART_Transmit_char(o);
}
//Can only send xxx.xx type number
void USART_Transmit_float(const float f)
{
        unsigned int v,p;
        long int num;
        num=f*1000;
        p=num%1000;
        num=num/1000;
        v=num;

        USART_Transmit_int(v);
        USART_Transmit_char('.');
        USART_Transmit_int(p);

}

void USART_Transmit(char* txdata)
{
    int i = 0;
    while(txdata[i] != 0x00)
    {
        USART_Transmit_char(txdata[i]);
        i++;
    }

}

//TWI Initialization
void TWI_Init(void)
{
    //set SCL to 400kHz
    TWSR = 0x00;
    TWBR = 0x02;
    //enable TWI
    TWCR = (1<<TWEN);
}

//TWI Start Singnal
void TWI_Start(void)
{
    TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
    while ((TWCR & (1<<TWINT)) == 0);
}

//TWI fuction tio retrieve the status
uint8_t TWI_GetStatus(void)
{
    uint8_t status;
    //mask status
    status = TWSR & 0xF8;
    return status;
}

//Write command for TWI
void TWI_Write(uint8_t u8data)
{
    TWDR = u8data;
    TWCR = (1<<TWINT)|(1<<TWEN);
    while ((TWCR & (1<<TWINT)) == 0);
}


//Read NACK for TWI
//Return read data from TWCR
uint8_t TWIReadNACK(void)
{
    TWCR = (1<<TWINT)|(1<<TWEN);
    while ((TWCR & (1<<TWINT)) == 0);
    return TWDR;
}

uint8_t TWIReadACK(void)
{
 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
 while((TWCR & (1<<TWINT)) == 0);

    return TWDR;
}


//Terminate TWI Interface
void TWI_Stop(void)
{
 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
 while(TWCR & (1<<TWSTO));
}


//Function to write to ADXL345-Accelerometer
//CS has to be high for ADXL345 to work in I2C mode
//Supports standard 100KHz and fast 400KHz Mode
//ALT address pin grounded implies 0xA6 for write and 0xA7 for read
uint8_t ADXL_write(uint8_t reg, uint8_t data)
{
    TWI_Start();
    if(TWI_GetStatus() != 0x08)
    {
        return error;
    }
    TWI_Write(0b10100110);
    if(TWI_GetStatus() != 0x18)
    {
        return error;
    }
    TWI_Write(reg);
    if(TWI_GetStatus() != 0x28)
    {
        return error;
    }
    TWI_Write(data);
    if(TWI_GetStatus() != 0x28)
    {
        return error;
    }
    TWI_Stop();
    return success;
}

//Write multiple bits to ADXL345
uint8_t ADXL_Multi_write(uint8_t reg, uint8_t data1, uint8_t data2)
{
 TWI_Start();
 if(TWI_GetStatus() != 0x08)
 {
  return error;
 }
 TWI_Write(0b10100110);
 if(TWI_GetStatus() != 0x18)
 {
  return error;
 }
 TWI_Write(data1);
 if(TWI_GetStatus() != 0x28)
 {
  return error;
 }
 TWI_Write(data2);
 if(TWI_GetStatus() != 0x28)
    {
        return error;
    }
    TWI_Stop();
    return success;
}


//Fuction to read data sent from ADXL Accelerometer
uint8_t ADXL_read(uint8_t reg)
{
    uint8_t data;
    TWI_Start();
    if(TWI_GetStatus() != 0x08)
    {
        return error;
    }
    TWI_Write(0b10100110);
    if(TWI_GetStatus() != 0x18)
    {
        return error;
    }
    TWI_Write(reg);
    if(TWI_GetStatus() != 0x28)
    {
        return error;
    }
    TWI_Start();
    if(TWI_GetStatus() != 0x10)
    {
        return error;
    }
    TWI_Write(0b10100111);
    if(TWI_GetStatus() != 0x40)
    {
        return error;
    }
    data = TWIReadNACK();
    if(TWI_GetStatus() != 0x58)
    {
        return error;
    }
    TWI_Stop();
    return data;
}

int main (void)
{
 //initialize USART
 USART_Init(51);
 //USART_Transmit_char('H');
 //initialize TWI
 TWI_Init();
 //Initailze ADXL345
 uint8_t x1,x2,y1,y2,z1,z2;
 //set ADXL to 400 Hz
 ADXL_write(0x2c,0b00001100);
 _delay_ms(100);
 ADXL_write(0x31,0b01000010);
 _delay_ms(100);
 ADXL_write(0x2D,0x00);
 _delay_ms(100);
 ADXL_write(0x2d,0x16);
 _delay_ms(100);
 ADXL_write(0x2d,0x08);
 _delay_ms(100);
 while(1)
 {
  x1 = ADXL_read(0x32);
  x2 = ADXL_read(0x33);
  y1 = ADXL_read(0x34);
  y2 = ADXL_read(0x35);
  z1 = ADXL_read(0x36);
  z2 = ADXL_read(0x37);
  x1 += (x2<<8);
  z1 += (z2<<8);
  y1 += (y2<<8);
  USART_Transmit("x1 =");
  USART_Transmit_int(x1);
  USART_Transmit("y1 =");
  USART_Transmit_int(y1);
  USART_Transmit("z1 =");
  USART_Transmit_int(z1);
  USART_Transmit("\n");
  _delay_ms(100);
 }
}



No comments :

Post a Comment