Hello, I'm trying to use the Analog Devices ADXL202E with a PIC16F877 running at 20 MHz. I'm writing the code in CCS PIC C PCW Compiler version 3.068. I'm trying to use the PIC to measure the PWM signal coming out of the accelerometer. I was trying to use the PWM capture port on the PIC but didn't fully understand the data being returned and I also didn't want to be limited on using a PIC with a capture port. I would like to use one of the small 8 pin PICs eventually. Basically all I want is to capture the acceleration data and send it out the serial port to a PC running a terminal program. I've read everything I could find on how to get this thing to work but I'm still missing something. Here's my code - #include <16F877.h> #device *=16 #use delay(clock=20000000) #fuses HS,NOWDT,NOPUT,NOLVP,BROWNOUT,NOCPD,WRT #use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7) //-----< Initialize all RAM >----- #ZERO_RAM #byte TRIS_A = 0x85 #byte TRIS_B = 0x86 #byte TRIS_C = 0x87 #byte TRIS_D = 0x88 #byte TRIS_E = 0x89 #define bit_scale_factor 256 long Xrise_a, Xrise_b, Yrise_a, Yrise_b, T1, T2, calx, caly,calt1; long time_ta = 0, time_tb, time_tc=0, time_td, time_te, T1X, T1Y, Zcal, Zactual, T2cal; long T2actual, T1Xcal, T1Ycal, Acceleration, K; int x; void micro_init() { port_b_pullups(TRUE); setup_adc_ports(NO_ANALOGS); setup_adc(ADC_CLOCK_DIV_2); setup_spi(FALSE); setup_psp(PSP_DISABLED); setup_timer_1(T1_INTERNAL); setup_timer_2(T2_DISABLED,0,1); setup_ccp1(CCP_OFF); setup_ccp2(CCP_OFF); return; } //******************************************************** // // _____________ _____________ // | | | // Xout | | | // ________| |_____________| // ta tb te // // T1 = tb - ta ta should be zero since it is the start of the timer // T2 = te - ta this is the period // // // // ______________ ____________ // | | | | // Yout | | | | // ______________| |_____________| |_____ // tc td // T2 = td - (td-tc)/2 - (tb/2) //************************************************************* void Measure_time_tb() { while (input(PIN_C1)==1) { ; } if (input(PIN_C1)==0) //wait for the pulse to go low { set_timer1(0); while (input(PIN_C1)==0) //wait again for the pulse to go low { ; } enable_interrupts(global); //start timer while (input(PIN_C1)==1) //wait for the pulse to transition from high to low { ; } disable_interrupts(global); // stop timer } time_tb = get_timer1(); //pulse width of Xout is the value from the timer T1X = time_tb; return; } void Measure_time_td() { //Yout if (input(PIN_C2)==0) //wait for the pulse to be low { set_timer1(0); //clear and set the timer value to 0 while (input(PIN_C2)==0) //wait again for the pulst to be low { ; } enable_interrupts(global); //start timer while (input(PIN_C2)==1) //wait for the pulse to go high { ; } disable_interrupts(global); //stop timer } time_td = get_timer1(); //pulse width of Yout is the value from the timer T1Y = time_td; return; } void Measure_time_te() { //Xout - this will measure the time between Tb and Te (low) if (input(PIN_C1)==1) //wait for pulse to be high { set_timer1(0); //clear and set the timer to 0 while (input(PIN_C1)==1) //now wait for the pulse to go high again { ; } enable_interrupts(global); //start timer while (input(PIN_C2)==0) //wait for the pulse to go low { ; //counting } disable_interrupts(global); //stop timer } time_te = get_timer1(); //pulse width of Xout is the value from the timer return; } void measure_T2() { T2 = ((time_td - (time_td/2))) - (time_tb/2); //should be per the data sheets return; } void calibrate() { //an attempt to calibrate measure_time_tb(); T1Xcal = T1X; measure_time_td(); T1Ycal = T1Y; measure_T2(); T2cal = T2; K = 4 * ((T2cal * bit_scale_factor)/T2cal); return; } void Find_Zactual() { //should be per the data sheets measure_T2(); T2actual = T2; Zactual = (Zcal*T2actual)/T2cal; return; } void main() { char cmd; micro_init(); printf(__DATE__); //it might work printf("\nReady\n\n"); //calibrate(); //Find_Zactual(); while(TRUE) { //all calculations should be per the data sheets //acceleration = (K*(T1-Zactual))/T2actual; if (kbhit()) { cmd = getc(); If (cmd == 'c') { printf("\n\n *******************************\n"); printf(" Xout Yout \n"); printf(" ********************************\n"); printf(" Time_Tb: %lu Time_Td: %lu \n", time_tb, time_td); printf(" Time_Te: %lu \n", time_te); printf(" -----------------------------------\n"); printf(" T2: %lu \n", T2); } } // calt1 = T1 - calx; // printf("\nacceleration = %lu", acceleration); delay_ms(700); //so I can see the data } } What I was trying to do was send the raw data back to the PC, then I would try the acceleration calculation as well as calibrating it. Right now I only get zeros or numbers that make no sense to me. Hopefully someone can point me in the right direction as to what I'm doing wrong. Thanks and Best Regards, -Lee -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details.