|
发表于 2011-5-10 08:34:54
|
显示全部楼层
#include "L3G4200D.h"
#define ClrGyCS() GPIOC->BSRR = 1 << (16 + 3)
#define SetGyCS() GPIOC->BSRR = 1 << 3
#define ClrGyCK() GPIOA->BSRR = 1 << (16 + 1)
#define SetGyCK() GPIOA->BSRR = 1 << 1
#define ClrGyIO() GPIOA->BSRR = 1 << (16 + 0)
#define SetGyIO() GPIOA->BSRR = 1 << 0
#define StateGyIO() (GPIOA->IDR & 1)
extern void Delay_us(INT32U cnt);
void L3G4200DInit(void)
{
SetGyCS();
SetGyCK();
Delay_us(100000);
ClrGyCS();
L3G4200DWriteByte(0x60);
L3G4200DWriteByte(0xEF);
L3G4200DWriteByte(0x00);
L3G4200DWriteByte(0x00);
L3G4200DWriteByte(0xD1);
L3G4200DWriteByte(0x00);
SetGyCS();
}
void L3G4200DReadData(INT16S *px, INT16S *py, INT16S *pz)
{
ClrGyCS();
L3G4200DWriteByte(0xE8);
*px = (INT16U)L3G4200DReadByte() << 8;
*px |= (INT16U)L3G4200DReadByte();
*py = (INT16U)L3G4200DReadByte() << 8;
*py |= (INT16U)L3G4200DReadByte();
*pz = (INT16U)L3G4200DReadByte() << 8;
*pz |= (INT16U)L3G4200DReadByte();
SetGyCS();
}
void L3G4200DWriteByte(INT8U dat)
{
INT8U i;
Delay_us(1);
for(i = 0; i < 8; i++) {
if(dat & 0x80) SetGyIO();
else ClrGyIO();
dat <<= 1;
Delay_us(1);
ClrGyCK();
Delay_us(1);
SetGyCK();
}
Delay_us(1);
}
INT8U L3G4200DReadByte(void)
{
INT8U i,dat = 0;
SetGyIO();
for(i = 0; i < 8; i++) {
Delay_us(1);
ClrGyCK();
Delay_us(1);
dat <<= 1;
if(StateGyIO()) dat++;
SetGyCK();
}
Delay_us(1);
return dat;
} |
|