Accelerometer H48C with ATmega16
fredrik
Posts: 4
Hi!
I've been trying to communicate with my accelerometer for some time now. It is parallax's accelerometer module h48c with built-in ad-converter (MCP3204).
the module has a single data-in,data-out pin.
for basic stamp users the data is easily read with the
commands SHIFTIN and SHIFTOUT.
example basic stamp code:
so ive tried to make a similar code for the avr with help from bobgardner who supplied me with a example code.
but i still cant get it to work.
im wondering if the clockpulses must be more accurate delays or something.
i was hoping anyone of you guys could help me find whats wrong.
I've been trying to communicate with my accelerometer for some time now. It is parallax's accelerometer module h48c with built-in ad-converter (MCP3204).
the module has a single data-in,data-out pin.
for basic stamp users the data is easily read with the
commands SHIFTIN and SHIFTOUT.
example basic stamp code:
LOW CS SHIFTOUT Dio, Clk, MSBFIRST, [noparse][[/noparse]%11\2, axis\3] ' select axis SHIFTIN Dio, Clk, MSBPOST, [noparse][[/noparse]axCount\13] ' read axis voltage counts HIGH CS
so ive tried to make a similar code for the avr with help from bobgardner who supplied me with a example code.
but i still cant get it to work.
im wondering if the clockpulses must be more accurate delays or something.
i was hoping anyone of you guys could help me find whats wrong.
#include <avr/io.h> #define DIOOUT() DDRC |= 0x40 //PC6 #define DIOIN() DDRC &= ~0x40 #define CSHI() PORTC |= 0x10 //PC4 #define CSLO() PORTC &= ~0x10 #define DIOHI() PORTC |= 0x40 //PC6 #define DIOLO() PORTC &= ~0x40 #define CLKHI() PORTC |= 0x80 //PC7 #define CLKLO() PORTC &= ~0x80 #define NOP() asm("nop") void clock(){ //Clockpulse CLKLO(); NOP(); CLKHI(); NOP(); } int main() { int ch;int bit;int msk;int dat; DDRC=0xFF; PORTC=0xFF; DIOOUT(); //Set DIO pin input CSHI(); //Deactivate AD ch=0b11000; //Channel0 (x-axis) while(1){ CSLO(); //Activate AD //---ShiftOut--- msk=0b10000; for(int i=0; i<5; i++){ bit=(ch & msk) != 0; if(bit){DIOHI();} else {DIOLO();}; msk>>=1; clock(); } //---ShiftIn--- DIOIN(); //Set DIO pin Input clock(); //Sampling time clock(); //Null bit dat=0; for(int i=0; i<12; i++){ clock(); dat<<=1; bit=(PINC & 0x40) != 0; } CSHI(); //Deactivate AD
Comments