Shop OBEX P1 Docs P2 Docs Learn Events
Accelerometer H48C with ATmega16 — Parallax Forums

Accelerometer H48C with ATmega16

fredrikfredrik Posts: 4
edited 2010-03-03 22:52 in Accessories
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.
schemag.jpg
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

  • Mike GreenMike Green Posts: 23,101
    edited 2010-03-03 22:52
    We can't help you with the AVR code. Download the "BASIC Stamp Syntax and Reference Manual" from Parallax (www.parallax.com/tabid/440/Default.aspx) and read the chapters on the SHIFTIN and SHIFTOUT statements, particularly the description of the clock pulse and its relationship to the data, particularly the MSBPOST option. Also read the datasheet for the MCP3204 ADC and look at the charts that show the timing.
Sign In or Register to comment.