hi
i have connected an adxl213 accelerometer to a pic16f877. The output from the adxl213 is a square waveform, the width of the wave form corresponds to the angle at the accelerometer is at. I'm using CCP modules to detect when the measure the width.
The accelerometers work fine as they give a nice square waveform which i have verified using a oscilloscope.
My goal is to have certain LEDs to light up when at a certain angle.
When i connect the outputs to the LEDs nothing happens when i move the accelerometer.
If anyone can have a look at my code and maybe give me a insight to why it doesn't work.
any help would be great
Thank you for your time.
PS i have attached the datasheets to the microcontroller and accelerometer
i have connected an adxl213 accelerometer to a pic16f877. The output from the adxl213 is a square waveform, the width of the wave form corresponds to the angle at the accelerometer is at. I'm using CCP modules to detect when the measure the width.
The accelerometers work fine as they give a nice square waveform which i have verified using a oscilloscope.
My goal is to have certain LEDs to light up when at a certain angle.
When i connect the outputs to the LEDs nothing happens when i move the accelerometer.
If anyone can have a look at my code and maybe give me a insight to why it doesn't work.
any help would be great
Thank you for your time.
Rich (BB code):
/*#include <Test program.h>*/
#include <16F877.h>
#include <MATH.h>
/*#device adc=8*/
#FUSES NOWDT,XT //No Watch Dog Timer
#use delay(clock=4000000)
//#define forever =1
unsigned char PeriodStartX;
unsigned char PeriodEndX;
int16 StartXCal;
int16 EndXCal;
int16 XSync;
int16 XCalperiod;
unsigned char PeriodStartY;
unsigned char PeriodEndY;
int16 StartYCal;
int16 EndYCal;
int16 YSync;
int16 YCalPeriod ;
int16 T2CalPeriod = 0.008;
int16 T2 = 0.008;
int16 TiltX;
int16 TiltY;
int lookuptable[3] = {-1,0,1};
int XLEDlookuptable[3]= {0x01,0x00,0x02};
//int YLEDlookuptable[3]= {0b00000010,0b00000000,0b00000100};
struct portmapStruct
{
int CalButton:1;
int deadoralive:1;
int ccp1flag:1;
int ccp2flag:1;
int unused:4;
}ports,portsTris;
struct ccpmappingstruct
{
int unused:1;
int CCP2:1;
int CCP1:1;
int unsued:5;
}CCPMAP, CCPMAPTRIS;
struct LEDMappingStruct
{
int LEDlights;
}LEDs,LEDsTris;
#byte ports = 0x05
#byte portsTris = 0x85
#byte LEDs = 0x06
#byte LEDsTris = 0x86
#byte CCPMAP = 0x07
#byte CCPMAPTRIS= 0x87
#bit CCP1IF = 0x0c.2
#bit CCP1IE = 0x8c.2
#bit CCP2IF = 0x0d.0
#bit CCP2IE = 0x8d.0
#bit PEIE = 0x8b.6
#byte CCP1CON = 0x17
#byte CCP2CON = 0x1d
#byte CCPR1L = 0x15
#byte CCPR1H = 0x16
#byte CCPR2L = 0x1b
#byte CCPR2H = 0x1c
#byte T1CON = 0x10
#INT_CCP1
void get_x_values(void)
{
if (CCP1IF == 1)
{
ports.ccp1flag =1;
CCP2IE = 0;
if ((CCP1CON & 0b00000111)== 0b00000101)
{
PeriodStartX=CCPR1H;
// PeriodStartX=CCP_1_HIGH;
//PeriodEndX;
//PulseEndX = PulseEndTempX;
//PeriodEndX = ;
XSync++;
CCP1CON = 0b00000100;
}
else //(CCP1CON == 0b00000100)
{
PeriodEndX=CCPR1H;
CCP1CON = 0b00000101;
}
}
else
{
ports.ccp1flag =0;
}
CCP1IF = 0;
CCP2IE = 1;
}
#INT_CCP2
void get_y_values(void)
{
if (CCP2IF == 1)
{
ports.ccp2flag =1;
CCP1IE =0;
if ((CCP2CON & 0b00000111)== 0b00000101)
{
PeriodStartY=CCPR2H;
//PeriodStartY=CCP_2_HIGH;
//PeriodEndX;
//PulseEndX = PulseEndTempX;
//PeriodEndX = ;
YSync++;
CCP2CON = 0b00000100;
}
else
{
PeriodEndY=CCPR2H;
CCP2CON = 0b00000101;
}
}
else
{
ports.ccp2flag =0;
}
CCP1IE = 1;
CCP2IF = 0;
}
void calibration(void)
{
// taking measurements at 0g
StartXCal = PeriodStartX; //Ta
EndXCal = PeriodEndX; //Tb
StartYCal = PeriodStartY;//Tc
EndYCal = PeriodEndY; //Td
XCalperiod = EndXCal - StartXCal; //Tb-Ta
YCalPeriod = EndYCal - StartYCal; //Td-Tc
T2CalPeriod =( (EndYCal - (YCalPeriod/2))-(XCalperiod/2)); //save T2 for later calculations
//[Td - ((Td-Tc)/2)]-[(Tb-Ta)/2]
return;
}
//void CalTilt(int Xacceleration, int Yacceleration)
//{
//calculate tilit on both axes
// TiltX = asin(Xacceleration/1);
// TiltY = asin(Yacceleration/1);
// return;
//}
void CalAccel(void)
{
int16 Xperiod,Yperiod;
//, AccelX,AccelY;
Xperiod = PeriodEndX - PeriodStartX; //calculate xout PMW
Yperiod = PeriodEndY - PeriodStartY; //calculate yout PWM
TiltX = (((Xperiod/T2)-0.5)/0.3); //work out acceleration on xout
TiltY = (((Yperiod/T2)-0.5)/0.3); // work out acclertion on yout
// CalTilt(AccelX,AccelY);
return;
}
void initialize_IO_ports(void)
{
portsTris = 0b00000000;
LEDsTris = 0x00;
LEDs = 0x00;
CCPMAPTRIS = 0b11111111;
CCPMAp = 0xff;
CCP1CON = 0b00000101;
CCP2CON = 0b00000101;
// enable Timer1
T1CON = 0b00000001 ;
// setup_ccp1(CCP_CAPTURE_RE);
//Configure CCP1 to capture
// setup_ccp2(CCP_CAPTURE_RE);// Configure CCP2 to capture
}
void main(void)
{
int i;
enable_interrupts(GLOBAL);
//CCP1IE = 1; // enable interrrupts to detect change
//CCP2IE = 1;//in x or y
PEIE =1;
enable_interrupts(INT_CCP1);
enable_interrupts(INT_CCP2);
initialize_IO_ports();
// if (PIN_A0 == 1) //is calibration button high
// {
// calibration(); //call calibration function
// }
while (true)
{
ports.deadoralive =1; // test to see if my pic works
delay_ms(80); // which it does
ports.deadoralive = 0;
delay_ms(80);
if (XSync > 0 || YSync >0) //if either xout or yout value have been recorded
{
CalAccel(); //go into calculating accelertaion function
for (i=0;i<3;i++)
{
if ((int)TiltX == lookuptable )
//{LEDs.LEDlights = 0x01;}
//{LEDs.LEDlights = 0x00;}
{LEDs.LEDlights = XLEDlookuptable;}
// if ((int)TiltX == 90 )
// {LEDs.LEDlights = 0x01;}
// if ((int)TiltX == -90 )
// {LEDs.LEDlights = 0x01;}
// if ((int)TiltY = lookuptable )
//{LEDs.LEDlights = YLEDlookuptable;}
}
}
//clear sync flags
XSync = 0;
Ysync = 0;
}
}
PS i have attached the datasheets to the microcontroller and accelerometer
Attachments
-
190.4 KB Views: 15
-
262.9 KB Views: 22
-
4.3 MB Views: 13