Difference between revisions of "Timemachine"
(New page: ==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...) |
m (adding sourcecode preview with syntaxhighlight) |
||
(4 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
==Introduction== | ==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 over | + | 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. |
+ | <br/> 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. <br/> | ||
− | This is how does the output from /dev/ttyUSBx looks like ( after watching episode of Southpark ^_^ ) | + | This is how does the output from /dev/ttyUSBx looks like ( after testing, watching episode of Southpark ^_^ ) |
<enscript> | <enscript> | ||
000:022:013: | 000:022:013: | ||
</enscript> | </enscript> | ||
− | + | == Timemachine setup == | |
− | + | [[Image:Tmsetup.jpg | 480px | Timemachine setup ]] | |
− | |||
+ | == Source Code == | ||
+ | <syntaxhighlight lang="c"> | ||
#define F_CPU 4000000UL | #define F_CPU 4000000UL | ||
#define BAUD_RATE 19200 | #define BAUD_RATE 19200 | ||
Line 32: | Line 35: | ||
//unsigned char check=0; | //unsigned char check=0; | ||
//unsigned char data=0; | //unsigned char data=0; | ||
− | + | while(! (UCSRA & ( 1 << RXC )) ); | |
− | |||
− | while(! (UCSRA & ( 1 << RXC )) ) | ||
− | ; | ||
− | |||
/* | /* | ||
check |= UCSRA; | check |= UCSRA; | ||
check &= 0b00011100; | check &= 0b00011100; | ||
− | |||
if(!check) | if(!check) | ||
return 1; // zavolat nejaku fce ktora "oznami" koniec/chybu | return 1; // zavolat nejaku fce ktora "oznami" koniec/chybu | ||
*/ | */ | ||
− | |||
return UDR; | return UDR; | ||
− | |||
} | } | ||
− | |||
− | |||
void transmit( unsigned char data ) | void transmit( unsigned char data ) | ||
{ | { | ||
− | + | while( !(UCSRA & ( 1 << UDRE ) ) ); | |
− | while( !(UCSRA & ( 1 << UDRE ) ) ) | ||
− | |||
− | |||
UDR = data; | UDR = data; | ||
− | |||
while (!(UCSRA & (1 << TXC ) ) ) // TXC == 0; | while (!(UCSRA & (1 << TXC ) ) ) // TXC == 0; | ||
− | + | ; | |
− | |||
if((UCSRA & (1<<TXC))) // TXC != 0; | if((UCSRA & (1<<TXC))) // TXC != 0; | ||
UCSRA = (1 << TXC); | UCSRA = (1 << TXC); | ||
− | |||
} | } | ||
Line 71: | Line 59: | ||
unsigned char pom_hodnota; | unsigned char pom_hodnota; | ||
unsigned int delitel = 100; | unsigned int delitel = 100; | ||
− | |||
while(delitel>=1) | while(delitel>=1) | ||
{ | { | ||
Line 81: | Line 68: | ||
} | } | ||
} | } | ||
− | |||
void eeprom_write(unsigned int uiAddress, unsigned char ucData) { | void eeprom_write(unsigned int uiAddress, unsigned char ucData) { | ||
− | while(EECR & (1<<EEWE)) | + | while(EECR & (1<<EEWE)); |
− | |||
− | |||
EEAR = uiAddress; | EEAR = uiAddress; | ||
EEDR = ucData; | EEDR = ucData; | ||
− | |||
EECR = (1<<EEMWE); | EECR = (1<<EEMWE); | ||
EECR = (1<<EEWE); | EECR = (1<<EEWE); | ||
Line 98: | Line 81: | ||
unsigned char eeprom_read(unsigned int uiAddress) | unsigned char eeprom_read(unsigned int uiAddress) | ||
{ | { | ||
− | while(EECR & (1<<EEWE)) | + | while(EECR & (1<<EEWE)); |
− | |||
EEAR = uiAddress; | EEAR = uiAddress; | ||
− | |||
EECR |= (1<<EERE); | EECR |= (1<<EERE); | ||
− | |||
return EEDR; | return EEDR; | ||
} | } | ||
int main(void) { | int main(void) { | ||
− | |||
− | |||
unsigned char i,data,hh=0,mm=0,ss=0; | unsigned char i,data,hh=0,mm=0,ss=0; | ||
unsigned int address,value; | unsigned int address,value; | ||
− | |||
DDRB = 0x00; | DDRB = 0x00; | ||
− | |||
init_uart(); | init_uart(); | ||
− | |||
data = PINB; | data = PINB; | ||
data &= 0b10000000; | data &= 0b10000000; | ||
− | |||
while(data) { | while(data) { | ||
++ss; | ++ss; | ||
Line 129: | Line 103: | ||
mm -= 60; | mm -= 60; | ||
} | } | ||
− | |||
data = PINB; | data = PINB; | ||
data &= 0b10000000; | data &= 0b10000000; | ||
− | |||
_delay_ms(1000); | _delay_ms(1000); | ||
} | } | ||
− | |||
− | |||
eeprom_write(0,hh); | eeprom_write(0,hh); | ||
_delay_ms(100); | _delay_ms(100); | ||
Line 142: | Line 112: | ||
_delay_ms(100); | _delay_ms(100); | ||
eeprom_write(2,ss); | eeprom_write(2,ss); | ||
− | |||
− | |||
for(i=0,address=0; i<3; i++,address++ ) { | for(i=0,address=0; i<3; i++,address++ ) { | ||
data=eeprom_read(address); | data=eeprom_read(address); | ||
Line 151: | Line 119: | ||
} | } | ||
transmit('\n'); | transmit('\n'); | ||
− | |||
− | |||
return 0; | return 0; | ||
− | |||
} | } | ||
− | + | </syntaxhighlight> | |
− | |||
− | </ | ||
==See Also== | ==See Also== |
Latest revision as of 15:20, 5 September 2011
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 ^_^ ) <enscript> 000:022:013: </enscript>
Timemachine setup
Source Code
<syntaxhighlight lang="c">
- 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; } </syntaxhighlight>