Timemachine

From Electriki
Jump to: navigation, search

Introduction

So, here is the thing. I wanted to measure, how long will my batteries in accelerometer over Bluetooth last. I got an idea to write a simple program for my Atmega8. It was also a great way how to learn something new. And i did! :) Now i can r/w from/into internal EEPROM and i learned from michai that i can't read from ports using PORTx and that i need to use PINx.
Anyway, power source of accelerometer data over bluetooth uses LP2951CM IO, which has /ERROR output. It goes low, when output voltage drops 5% below configured voltage. It's an indication of low voltage on batteries ( aka. battery depleted ). And for the PRO folks, i know that precision of this setup isn't much, but it should do the trick.

This is how does the output from /dev/ttyUSBx looks like ( after testing, watching episode of Southpark ^_^ ) UNIQ2d5359f43c0d0853-enscript-00000000-QINU

Timemachine setup

Timemachine setup

Source Code

#define F_CPU 4000000UL
#define BAUD_RATE 19200
 
#include <util/delay.h>
#include <avr/io.h>
 
void init_uart(void)
{
UCSRA = (0<<RXC) | (0<<TXC) | (0<<UDRE) | (0<<FE) | (0<<DOR) | (0<<PE) | (0<<U2X) | (0<<MPCM);
UCSRB = (0<<RXCIE) | (0<<TXCIE) | (0<<UDRIE) | (1<<RXEN) | (1<<TXEN) | (0<<UCSZ2) | (0<<RXB8) | (0<<TXB8);
UCSRC = (1<<URSEL) | (0<<UMSEL) | (0<<UPM1) | (0<<UPM0) | (0<<USBS) | (1<<UCSZ1) | (1<<UCSZ0) | (0<<UCPOL);
//UBRRH = 0b00000000;
//UBRRL = 0x02;
UBRRH = (((F_CPU/BAUD_RATE)/16)-1)>>8;          // set baud rate
UBRRL = (((F_CPU/BAUD_RATE)/16)-1);
}
 
unsigned char receive (void)
{
//unsigned char check=0;
//unsigned char data=0;
while(! (UCSRA & ( 1 << RXC )) );
/*
check |= UCSRA;
check &= 0b00011100;
if(!check)
return 1;       // zavolat nejaku fce ktora "oznami" koniec/chybu
*/
return UDR;
}
 
void transmit( unsigned char data )
{
while( !(UCSRA & ( 1 << UDRE ) ) );
UDR = data;
while (!(UCSRA & (1 << TXC ) ) )  // TXC == 0;
;
if((UCSRA & (1<<TXC)))  // TXC != 0;
        UCSRA = (1 << TXC);
}
 
void decompose( unsigned char data )
{
unsigned char pom_hodnota;
unsigned int delitel = 100;
while(delitel>=1)
        {
        pom_hodnota=data/delitel;
        data -= delitel*pom_hodnota;
        pom_hodnota += 48;
        delitel /= 10;
        transmit(pom_hodnota);
        }
}
 
void eeprom_write(unsigned int uiAddress, unsigned char ucData) {
	while(EECR & (1<<EEWE));
	EEAR = uiAddress;
	EEDR = ucData;
	EECR = (1<<EEMWE);
	EECR = (1<<EEWE);
//	EECR |= (1<<EEMWE);
//	EECR |= (1<<EEWE);
}
 
unsigned char eeprom_read(unsigned int uiAddress)
{
	while(EECR & (1<<EEWE));
	EEAR = uiAddress;
	EECR |= (1<<EERE);
	return EEDR;
}
 
int main(void) {
unsigned char i,data,hh=0,mm=0,ss=0;
unsigned int address,value;
	DDRB = 0x00;
	init_uart();
	data = PINB;
	data &= 0b10000000;
	while(data) {
	++ss;
	if(ss == 60 ) {
		++mm;
		ss -= 60;
	} else if( mm == 60 ) {
		++hh;
		mm -= 60;
	}
	data = PINB;
	data &= 0b10000000;
	_delay_ms(1000);
	}
	eeprom_write(0,hh);
	_delay_ms(100);
	eeprom_write(1,mm);
	_delay_ms(100);
	eeprom_write(2,ss);
	for(i=0,address=0; i<3; i++,address++ ) {
		data=eeprom_read(address);
		decompose(data);
		transmit(':');
		_delay_ms(100);
	}
	transmit('\n');
return 0;
}

See Also

Btm-112
Accelerometer data over bluetooth