|
|
|
![]() |
|
|||||||
|
||||||||
|
|
Thread Tools |
Rating:
|
Display Modes |
|
#1
|
|||
|
|||
|
Accelerometer code
I've been working on writing code for an accelerometer that outputs a PWM signal. I've been trying to adapt some of Kevin's code that dealt with an infrared sensor that output PWM signals.
the following is Kevin's with a few variable name changes switch(RB5_State) // current state determines how the function behaves { case 0: // falling-edge detected (beginning of the pulse) Int_4_Down_Edge_Count_High = Clock; // get a snapshot of the time tbuf = TMR1L; // TMR1L must be read before TMR1H Int_4_Down_Edge_Count_Low = TMR1H; Int_4_Down_Edge_Count_Low <<= 8; Int_4_Down_Edge_Count_Low += tbuf; break; // now wait for the rising-edge interrupt to happen... case 1: // rising-edge detected (end of the pulse) Int_4_Up_Edge_Count_High = Clock; // get a snapshot of the time tbuf = TMR1L; Int_4_Up_Edge_Count_Low = TMR1H; Int_4_Up_Edge_Count_Low <<= 8; Int_4_Up_Edge_Count_Low += tbuf; // determine the pulse-width period by determining the time // difference between the falling-edge and rising-edge interrupts if (Int_4_Up_Edge_Count_High == Int_4_Down_Edge_Count_High) { // this is quicker because the 16-bit system clock hasn't changed and therefore has no effect on the outcome Int_4_Period = Int_4_Up_Edge_Count_Low - Int_4_Down_Edge_Count_Low; } else { //this works because the pulse-width will always be less than one clock tick(= 65536 timer ticks) //(=0.0000001 seconds) 1 * 10^-7 sec (0.1 micro seconds) Int_4_Period = 65536 - Int_4_Down_Edge_Count_Low + Int_4_Up_Edge_Count_Low; } break; // now wait for another falling-edge interrupt to happen... now i have my accelerometer plugged into digital input 3 and 4 (x and y PWMs) as far as i can tell the RB5 pin never switches state because when i printf("data:%d",Int_4_Down_Edge_Count_High); or Int_4_Up_Edge_Count_High the value is always 0 so the clock value never gets assigned meaning it never entered the switch statement. It does the same thing for the interrupt on digital input 3 Any words of advice or help would be greatly appreciated. Thanks. |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Out of the Box Camera Code | russell | Programming | 9 | 21-10-2009 05:28 |
| Some things i'm trying to understand on Kevin's Accelerometer Code | Squall | Programming | 2 | 03-02-2005 12:01 |
| Team THRUST - Kevin's Code and Camera Code Combine | Chris_Elston | Programming | 3 | 31-01-2005 22:28 |
| Example accelerometer code released. | Kevin Watson | Programming | 0 | 20-01-2005 03:45 |
| heres the code. y this not working | omega | Programming | 16 | 31-03-2004 15:18 |