Você está na página 1de 8

/*******************************************************************************

****
* Mangalyaan.c
*
* Created: 17-11-2013 AM 01:43:58
* Author: Sid
!!!!!!!!!!!!!!!!!!!!!!!!!!IMPORTANT INSTRUCTIONS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!
All the LCD functions are included in LCD.h
The pin connections for the LCD remain the same.
LCD Pin 1 is the pin closest to the corner of the LCD board
The connections are:
1. GND
2. +5V
3. VEE(output of the middle pin of the 5k potentiometer)
LCD
4. RS
5. RW
6. E
7. DB0
8. DB1
9. DB2
10.DB3
11.DB4
12.DB5
13.DB6
14.DB7
15.NC
16.NC

Atmega32 board
PB0
PB1
PB2
PD0
PD1
PD2
PD3
PD4
PD5
PD6
PD7

You need to use a 5k potentiometer with one side pin at +5V, middle pin connect
ed to pin 3 of LCD and the other side pin connected to GND
Adjust the potentiometer to vary the contrast.
If you want to turn the backlight on, connect Pin 15 to +5V through a 270E resi
stor, Pin 16 to GND.
Attention: The resistor is a must! or you could end up burning the backlight LE
D.
///////////////////////////////////////////////////////////////////////////////
/////////
The ADXL345 accelerometer board connections are:
ADXL345board
ATmega board
5V---------------VCC
3V3--------------NC(No Connection)
GND--------------GND
VS---------------VCC
CS---------------VCC
INT1-------------NC
INT2-------------NC
SDO--------------NC
SDA--------------PC1(SDA)
SCL--------------PC0(SCL)

///////////////////////////////////////////////////////////////////////////////
//////////
The header file LCD.h and the i2chw folder must be copied to the folder where t
he C file is.
*******************************************************************************
***********/
#define F_CPU 11095200UL
#include
#include
#include
#include
#include

<avr/io.h>
<util/delay.h>
"i2chw/twimaster.c"
"LCD.h"
<math.h>

#define ADXL345_ADDR (0x53<<1)


unsigned char TWI_read_one_byte(unsigned char device_address, unsigned char regi
ster_address);
void TWI_write_one_byte(unsigned char device_address, unsigned char register_add
ress, unsigned char data);
void morse_convert(int x);
int main(void)
{
LCD_initialize();
DDRC|=(1<<0)|(1<<1); // initialize PC0 and PC1 as input pins
PORTC&=~((1<<0)|(1<<1)); // Internally pull PC0 and PC1 high
DDRB|=(1<<3); //for LED
i2c_init(); // initialize i2c
TWI_write_one_byte(ADXL345_ADDR, 0x2D, 0b00001000); // Remove ADXL345 fr
om standby mode, read datasheet for more details
TWI_write_one_byte(ADXL345_ADDR, 0x31, 0x00); // select range as =-2g,
read datasheet for more details
int read;
while(1)
{
read=TWI_read_one_byte(ADXL345_ADDR, 0x33); // get higher byte
read=(read<<8)+TWI_read_one_byte(ADXL345_ADDR, 0x32); // left sh
ift the higher byte and add the lower byte
writeno(0x80, read, 5); //write it to LCD starting from position
0x80, read variable and show 5 digits
read = fabs(read);
int read3=read;
int x=0,read1=1;
for(int j=0; j<3; j++)
{
x = read%10;
read1 += x*pow(10,2-j);
read = read/10;
}
int read2 = read1;

int digit;
for(int i=0; i<3; i++)
{
digit = read2%10;
morse_convert(digit);
read2 = read2/10;
_delay_ms(600);
}
_delay_ms(800);
}
}
void morse_convert(int data)
{
switch (data)
{
case 0:
PORTB|=(1<<3);
_delay_ms(600);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(600);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(600);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(600);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(600);
PORTB&=~(1<<3);
break;
case 1:
PORTB|=(1<<3);
_delay_ms(200);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(600);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(600);
PORTB&=~(1<<3);

_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(600);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(600);
PORTB&=~(1<<3);
break;
case 2:
PORTB|=(1<<3);
_delay_ms(200);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(200);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(600);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(600);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(600);
PORTB&=~(1<<3);
break;
case 3:
PORTB|=(1<<3);
_delay_ms(200);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(200);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(200);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(600);
PORTB&=~(1<<3);
_delay_ms(200);

PORTB|=(1<<3);
_delay_ms(600);
PORTB&=~(1<<3);
break;
case 4:
PORTB|=(1<<3);
_delay_ms(200);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(200);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(200);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(200);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(600);
PORTB&=~(1<<3);
break;
case 5:
PORTB|=(1<<3);
_delay_ms(200);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(200);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(200);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(200);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(200);
PORTB&=~(1<<3);
break;

case 6:
PORTB|=(1<<3);
_delay_ms(600);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(200);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(200);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(200);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(200);
PORTB&=~(1<<3);
break;
case 7:
PORTB|=(1<<3);
_delay_ms(600);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(600);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(200);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(200);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(200);
PORTB&=~(1<<3);
break;
case 8:
PORTB|=(1<<3);
_delay_ms(600);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);

_delay_ms(600);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(600);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(200);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(200);
PORTB&=~(1<<3);
break;
case 9:
PORTB|=(1<<3);
_delay_ms(600);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(600);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(600);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(600);
PORTB&=~(1<<3);
_delay_ms(200);
PORTB|=(1<<3);
_delay_ms(200);
PORTB&=~(1<<3);
break;
}
}
unsigned char TWI_read_one_byte(unsigned char device_address, unsigned char regi
ster_address) // i2c function to read a byte
{
unsigned char status;
i2c_start_wait(device_address);
i2c_write(register_address);
i2c_rep_start(device_address+1);
status=i2c_readNak();
i2c_stop();
return status;
}
void TWI_write_one_byte(unsigned char device_address, unsigned char register_add

ress, unsigned char data) // i2c function to write a byte


{
i2c_start_wait(device_address);
i2c_write(register_address);
i2c_write(data);
i2c_stop();
}

Você também pode gostar