[HELP] PIC16F877 connected to ADXL213

Discussion in 'Embedded Systems and Microcontrollers' started by icem, Mar 16, 2011.

  1. icem

    Thread Starter New Member

    Nov 15, 2009
    26
    0
    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.

    Code ( (Unknown Language)):
    1. /*#include <Test program.h>*/
    2. #include <16F877.h>
    3. #include <MATH.h>
    4. /*#device adc=8*/
    5. #FUSES NOWDT,XT                    //No Watch Dog Timer
    6. #use delay(clock=4000000)
    7. //#define forever =1
    8. unsigned char PeriodStartX;
    9. unsigned char PeriodEndX;
    10. int16 StartXCal;
    11. int16 EndXCal;
    12. int16 XSync;
    13. int16 XCalperiod;
    14.  
    15. unsigned char PeriodStartY;    
    16. unsigned char PeriodEndY;
    17. int16 StartYCal;
    18. int16 EndYCal;
    19. int16 YSync;
    20. int16 YCalPeriod ;
    21.  
    22. int16 T2CalPeriod = 0.008;
    23. int16 T2 = 0.008;
    24. int16 TiltX;
    25. int16 TiltY;
    26.  
    27. int lookuptable[3] = {-1,0,1};
    28. int XLEDlookuptable[3]= {0x01,0x00,0x02};
    29. //int YLEDlookuptable[3]= {0b00000010,0b00000000,0b00000100};
    30.  
    31. struct portmapStruct
    32. {
    33.    int CalButton:1;
    34.    int deadoralive:1;
    35.    int ccp1flag:1;
    36.    int ccp2flag:1;
    37.    int unused:4;
    38. }ports,portsTris;
    39.  
    40. struct ccpmappingstruct
    41. {
    42.  int unused:1;
    43.  int CCP2:1;
    44.  int CCP1:1;
    45.  int unsued:5;
    46. }CCPMAP, CCPMAPTRIS;
    47.  
    48. struct LEDMappingStruct
    49. {
    50.    int LEDlights;
    51. }LEDs,LEDsTris;
    52.  
    53.  
    54. #byte ports = 0x05
    55. #byte portsTris = 0x85
    56. #byte LEDs = 0x06
    57. #byte LEDsTris = 0x86
    58. #byte CCPMAP = 0x07
    59. #byte CCPMAPTRIS= 0x87
    60. #bit CCP1IF = 0x0c.2
    61. #bit CCP1IE = 0x8c.2
    62. #bit CCP2IF = 0x0d.0
    63. #bit CCP2IE = 0x8d.0
    64. #bit PEIE = 0x8b.6
    65. #byte CCP1CON = 0x17
    66. #byte CCP2CON = 0x1d
    67. #byte CCPR1L = 0x15
    68. #byte CCPR1H = 0x16
    69. #byte CCPR2L = 0x1b
    70. #byte CCPR2H = 0x1c
    71. #byte T1CON = 0x10
    72.  
    73.  
    74. #INT_CCP1
    75. void get_x_values(void)
    76. {
    77.    if (CCP1IF == 1)
    78.    {
    79.       ports.ccp1flag =1;
    80.       CCP2IE = 0;
    81.       if ((CCP1CON & 0b00000111)== 0b00000101)
    82.       {
    83.        
    84.         PeriodStartX=CCPR1H;
    85.         // PeriodStartX=CCP_1_HIGH;
    86.          //PeriodEndX;
    87.          //PulseEndX = PulseEndTempX;
    88.          //PeriodEndX = ;
    89.          XSync++;
    90.          CCP1CON = 0b00000100;
    91.       }
    92.       else //(CCP1CON == 0b00000100)
    93.       {
    94.          PeriodEndX=CCPR1H;
    95.          CCP1CON = 0b00000101;
    96.       }
    97.    }
    98.    else
    99.    {
    100.        ports.ccp1flag =0;
    101.    }
    102.     CCP1IF = 0;  
    103.     CCP2IE = 1;
    104.  
    105. }
    106. #INT_CCP2
    107. void get_y_values(void)
    108. {
    109.    if (CCP2IF == 1)
    110.    {
    111.       ports.ccp2flag =1;
    112.       CCP1IE =0;
    113.       if ((CCP2CON & 0b00000111)== 0b00000101)
    114.       {
    115.          PeriodStartY=CCPR2H;
    116.          //PeriodStartY=CCP_2_HIGH;
    117.          //PeriodEndX;
    118.          //PulseEndX = PulseEndTempX;
    119.          //PeriodEndX = ;
    120.          YSync++;
    121.          CCP2CON = 0b00000100;
    122.       }
    123.      else
    124.       {
    125.          PeriodEndY=CCPR2H;
    126.          CCP2CON = 0b00000101;
    127.       }
    128.    }
    129.    else
    130.    {
    131.        ports.ccp2flag =0;
    132.    }
    133.    CCP1IE = 1;
    134.    CCP2IF = 0;  
    135. }
    136.  
    137. void calibration(void)
    138. {
    139.    // taking measurements at 0g
    140.    StartXCal = PeriodStartX; //Ta
    141.    EndXCal = PeriodEndX;    //Tb
    142.    StartYCal = PeriodStartY;//Tc
    143.    EndYCal = PeriodEndY;    //Td
    144.    
    145.    XCalperiod = EndXCal - StartXCal; //Tb-Ta
    146.    YCalPeriod = EndYCal - StartYCal; //Td-Tc
    147.    
    148.    T2CalPeriod =( (EndYCal - (YCalPeriod/2))-(XCalperiod/2)); //save T2 for later calculations
    149.                //[Td - ((Td-Tc)/2)]-[(Tb-Ta)/2]
    150.    return;  
    151. }
    152. //void CalTilt(int Xacceleration, int Yacceleration)
    153. //{
    154.    //calculate tilit on both axes
    155.  //  TiltX = asin(Xacceleration/1);
    156.   // TiltY = asin(Yacceleration/1);
    157.  
    158. //   return;
    159. //}
    160. void CalAccel(void)
    161. {
    162.    int16 Xperiod,Yperiod;
    163.    //, AccelX,AccelY;
    164.    
    165.    Xperiod = PeriodEndX - PeriodStartX; //calculate xout PMW
    166.    Yperiod = PeriodEndY - PeriodStartY; //calculate yout PWM
    167.    TiltX = (((Xperiod/T2)-0.5)/0.3); //work out acceleration on xout
    168.    TiltY = (((Yperiod/T2)-0.5)/0.3); // work out acclertion on yout
    169.   // CalTilt(AccelX,AccelY);
    170.    return;
    171. }
    172. void initialize_IO_ports(void)
    173. {
    174.    portsTris = 0b00000000;
    175.    
    176.    LEDsTris = 0x00;
    177.     LEDs = 0x00;
    178.    CCPMAPTRIS = 0b11111111;
    179.     CCPMAp = 0xff;
    180.    CCP1CON = 0b00000101;
    181.    CCP2CON = 0b00000101;
    182.    
    183.    
    184.    // enable Timer1
    185.    T1CON = 0b00000001 ;  
    186.   // setup_ccp1(CCP_CAPTURE_RE);
    187.    //Configure CCP1 to capture
    188.   // setup_ccp2(CCP_CAPTURE_RE);// Configure CCP2 to capture
    189. }
    190.  
    191.  
    192.  
    193.  
    194. void main(void)
    195. {
    196.    int i;
    197.    enable_interrupts(GLOBAL);
    198.    //CCP1IE = 1; // enable interrrupts to detect change
    199.    //CCP2IE = 1;//in x or y
    200.    PEIE =1;
    201.    enable_interrupts(INT_CCP1);
    202.    enable_interrupts(INT_CCP2);
    203.    initialize_IO_ports();
    204.    
    205.   // if (PIN_A0 == 1) //is calibration button high
    206.  //  {
    207.   //    calibration(); //call calibration function
    208.  //  }
    209.    
    210.    while (true)
    211.    {
    212.       ports.deadoralive =1; // test to see if my pic works
    213.       delay_ms(80);         // which it does
    214.       ports.deadoralive = 0;
    215.       delay_ms(80);
    216.      
    217.       if (XSync > 0 || YSync >0) //if either xout or yout value have been recorded
    218.       {                        
    219.       CalAccel(); //go into calculating accelertaion  function
    220.           for (i=0;i<3;i++)
    221.           {
    222.              if ((int)TiltX == lookuptable[i] )
    223.              //{LEDs.LEDlights = 0x01;}
    224.              //{LEDs.LEDlights = 0x00;}
    225.              {LEDs.LEDlights = XLEDlookuptable[i];}
    226.             // if ((int)TiltX == 90 )
    227.             // {LEDs.LEDlights = 0x01;}
    228.              
    229.            //  if ((int)TiltX == -90 )
    230.             // {LEDs.LEDlights = 0x01;}
    231.             // if ((int)TiltY = lookuptable[i] )
    232.             //{LEDs.LEDlights = YLEDlookuptable[i];}  
    233.         }
    234.       }
    235.      
    236.       //clear sync flags
    237.       XSync = 0;
    238.       Ysync = 0;
    239.      }
    240.      
    241.      
    242.    }
    243.  
    244. [/i][/i][/i][/i]


    PS i have attached the datasheets to the microcontroller and accelerometer
     
  2. nickelflipper

    Active Member

    Jun 2, 2010
    280
    35
    Code ( (Unknown Language)):
    1. TiltX = (((Xperiod/T2)-0.5)/0.3); //work out acceleration on xout
    2. TiltY = (((Yperiod/T2)-0.5)/0.3); // work out acclertion on yout
    Looks like floats not integers.
     
Loading...