Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Accelerometer issues

Status
Not open for further replies.

AtomSoft

Well-Known Member
I have a Accelerometer Module with a MX2125 Chip on board and it supposed to pulse out like a PWM but instead of PWM i was woundering with a multimeter i read 2.5v on a level surface can i use a A/D Conversion to get the values instead?

Here is the code i tried to create ... the only thing is when its level even if its not moving i get different values sometimes.... I assuming its a unstable power type issue.. should i just add a capactior? I was using this on a junebug in debug mode....
Code:
	list    p=18F1320
	include	<p18F1320.inc>

	CONFIG	OSC = INTIO1, WDT = OFF, LVP = OFF, DEBUG = ON
	
Axis_Port Equ PORTA				; Set Axis_Port to Equal PortB
Axis_Tris Equ TRISA				; Set Axis_Tris to Equal TrisB
X_Axis Equ 1					; Set X_Axis to Equal 1
Y_Axis Equ 2					; Set Y_Axis to Equal 4

	org	0x0000				; Beggining of Program
Init
	movlw	0x72 				; 8MHz clock select
        movwf	OSCCON				; Move to OSCILLATOR CONTROL REGISTER
	clrf	Axis_Tris			; Set All Output
	bsf	Axis_Tris, X_Axis 		; Set Axis_Tris.X_Axis as input
	bsf	Axis_Tris, Y_Axis 		; Set Axis_Tris.Y_Axis as input
	clrf	ADCON1
	movlw	b'00000101'			; Enable A/D, AN1
	movwf	ADCON0

Main
	bsf	ADCON0,GO        		; Start A/D conversion
AD_Loop
	btfsc	ADCON0,DONE			; Check if done. If not goto adloop else skip loop
        goto	AD_Loop
	goto	Main				; Loop back to main
	End
 
Yes i have seen that but the thing is that they dont specify like how to read the pulses... i think i read its like 100hz which would be about 200 cycles @ 8mhz but how in the world would i read it?

Do i need to use the timer to capture it ? What signifies like a start bit? These are things i am yet to get info on...
 
Yes i have seen that but the thing is that they dont specify like how to read the pulses... i think i read its like 100hz which would be about 200 cycles @ 8mhz but how in the world would i read it?

Do i need to use the timer to capture it ? What signifies like a start bit? These are things i am yet to get info on...

Hi,
I'll look thru the datasheet, but it looks like the output is PWM, there is no start bit.

I dont know if you are having problems using the forum today, but it seems to have slowed right down,, its just painful trying to use the forum at the moment. :confused:

I've posted a note on that thread about the forum upgrade.
 
Hello,

You can use the CCP module to figure out the pulse width. It needs to be set to capture mode and the timer you choose to use along with it configured.

1. Make sure the timer is off and clear it. Set the CCP module to watch for a rising edge first. Clear the flag CCPxIF and then wait for it to be set again, signifying a rising edge.

2. Turn on the timer, set the CCP module to watch for a falling edge, and clear the CCPxIF flag. Wait for it to be set again, and a falling edge has been detected. CCPRxL and CCPRxH now contain the 16-bit value of timer when the event occurred, which is also equal to the pulse width between rising and falling edges.

Make sure to reset the CCPxIF flag and timer before you try to repeat this for another reading. You can also make a nice little loop using indirect addressing to store a series of readings sequentially in memory so you can average the result.

https://ww1.microchip.com/downloads/en/DeviceDoc/39605F.pdf

Check out Chapter 15 and more specifically 15.3 for capture mode.

-Jacob
 

Attachments

  • ccp.JPG
    ccp.JPG
    7.4 KB · Views: 187
Last edited:
I have a Accelerometer Module with a MX2125 Chip on board and it supposed to pulse out like a PWM but instead of PWM i was woundering with a multimeter i read 2.5v on a level surface can i use a A/D Conversion to get the values instead?
Now you see why I got one with analog out!?! :p I'm lazy! Mine is easy as can be to use. Oh well, it's a good challenge for you. :D

I don't think you can use A/D to read those though. The voltage level of the pulses should be constant. I think they're read by calculating the duty cycle of the pulse train. Or am I thinking of RC gyros?
 
Can't you juse use an R-C filter on the PWM output to convert it to an analog voltage? The same way you can turn a PWM on a uC into a DAC by using a R-C filter? A buffer may be required on the input and/or output side of the R-C filter depending on the drive capability of the PWM output and the input requirements of the ADC.
 
thanks fot all the info i will try jacob.zurasky's reply in a bit and tell you the results...

futz i got this Accelerometer Module for free never intended on buying one so ill try this first then if i like i might buy 1 lol

dknguyen ill look into it after i try the other thing
 
You can use a simple RC circuit to utilize the PWM as an analogue signal

Here's an example from a PIC **broken link removed**I did up a while ago;
**broken link removed**

I typically use the opamp to buffer/isolate the signal and provide a little more drive current for applications.

A signal like this would read 1.25 volts (25% duty cycle, 5V peak);
**broken link removed**

And this would read 2.5V (50% duty cycle);
**broken link removed**
 
on hand i own 1 LM339 and 1 LM741 Would any of those work for this?
hi atom,
The LM393 is not a bad comparator, but the 741 should be put in the recycling bin.:)

I thought you were going to use the CCP as suggested by a member.?

Regards

Woops misread the LM339 for LM393... cr*p.!
 
i have not tried nothing yet just writing all suggestions .... Also at the moment i am trying to learn how to use this timer thing...

1. Make sure the timer is off and clear it. Set the CCP module to watch for a rising edge first. Clear the flag CCPxIF and then wait for it to be set again, signifying a rising edge.

I know how to do the second half of that sentence but how do i do whats in BOLD Above?

Also just to make sure ...would i do like a BTFSS on CCP1IF ... and then jump to start if it is clear(0) and if set(1) then clear it and continue to look for falling edge?
 
i have not tried nothing yet just writing all suggestions .... Also at the moment i am trying to learn how to use this timer thing...
I know how to do the second half of that sentence but how do i do whats in BOLD Above?

Also just to make sure ...would i do like a BTFSS on CCP1IF ... and then jump to start if it is clear(0) and if set(1) then clear it and continue to look for falling edge?
hi,
Which Timer are you planning to use.?

You get the next high going edge which starts the timer
You get the next low going edge which stops the timer
The count in the timer is a function of PIC internal clock and the period for which the timer was counting.

You preload the counters and start the next pulse width timing.

EDIT: erased ref to 16F690, should be 18F1320, sorry about that 'atom'

Looking thru the data for the accelerometer, I can see the values,but I cannot find the units.?
Does the 2500 represent uSec or mSec or what.?
 
Last edited:
im not sure.. I read something like 100hz but still not sure like comepared to what speed. Like is it for every 1 second it does it 100 times? Then if so that makes it every 10mSec right?

I never every made a timer i used code from other sources never understanding it.
I have read the datasheet and let me just say i dont find it too friendly to me :D
So tell me if i get it so far:

Steps:
1. Setup CCP1CON for 0101 = Capture mode, every rising edge then
2. When it triggers it will start The Timer1/3 i have to clear CCP1IF then
3. Setup CCP1CON for 0100 = Capture mode, every falling edge then
4. It will stop the timer and i do something with results in timer?
 
Lets say you choose to use timer3. It is easier to start with an initial value of the timer at zero. So before you start:

Code:
              bcf    T3CON, TMR3ON
              clrf   TMR3L
              clrf   TMR3H                   ;timer3 cleared and off

              bcf    PIR1, CCP1IF            ;clear the flag
Loop1         btfss  PIR1, CCP1IF            ;wait for the rising edge
              goto   Loop1 
              bsf    T3CON, TMR3ON           ;then start the timer
              bcf    PIR1, CCP1IF            ;clear the flag

After that, you need to change CCP1CON to watch for a falling edge.
Code:
Loop2         btfss  PIR1, CCP1IF           ;wait for the falling edge
              goto   Loop2
              bcf    PIR1, CCP1IF

Now CCPR1L and CCPR1H contain the value of timer3 when the falling edge occured, giving you the pulse width. The actual time value will depend on the prescale selected for timer3 and crystal used.

-Jacob

Edit - The CCP module does not control the timer, it just captures its value when an event happens. You have to manually control it. Also, I could not find a link to the datasheet for the accelerometer, do you have one? I might be able to help more if I can see what the range of pulse widths the accelerometer outputs.
 
Last edited:
Ok im trying the below code when in animate mode in debug (using junebug here RB3)
i test when its a rising set the timer and then check for a falling now this seems to happen and the value in TMR3L is 0x06. Would this be my pulse width? Now But when i make the MX2125 Tilt i get the same value of 6.. How am i supposed to use this 6 value? Sorry for not understanding this more...

This is my code:
Code:
	list    p=18F1320
	include	<p18F1320.inc>

	CONFIG	OSC = INTIO1, WDT = OFF, LVP = OFF, DEBUG = ON
	
Axis_Port Equ PORTB				; Set Axis_Port to Equal PortB
Axis_Tris Equ TRISB				; Set Axis_Tris to Equal TrisB
X_Axis Equ 3					; Set X_Axis to Equal 3

	org	0x0000					; Beggining of Program
Init
	movlw	0x62 				; 8MHz clock select
    movwf	OSCCON				; Move to OSCILLATOR CONTROL REGISTER
	clrf	Axis_Tris			; Set All Output
	bsf		Axis_Tris, X_Axis 	; Set Axis_Tris.X_Axis as input

Main
	bcf		T3CON, TMR3ON
	clrf	TMR3L
	clrf	TMR3H                ;timer3 cleared and off

	movlw	0x05				 ; Rising Edge Selection
	movwf	CCP1CON	

	bcf		PIR1, CCP1IF            ;clear the flag
Loop1
	btfss	PIR1, CCP1IF            ;wait for the rising edge
	goto	Loop1 
	bsf		T3CON, TMR3ON           ;then start the timer
	bcf		PIR1, CCP1IF            ;clear the flag
	movlw	0x04					;Falling Edge Selection
	movwf	CCP1CON	
Loop2
	btfss  PIR1, CCP1IF           ;wait for the falling edge
	goto   Loop2
	bcf    PIR1, CCP1IF
	goto	Main				; Loop back to main
	End
 
Just edited code forgot to set the T3CON:
Code:
	list    p=18F1320
	include	<p18F1320.inc>

	CONFIG	OSC = INTIO1, WDT = OFF, LVP = OFF, DEBUG = ON
	
Axis_Port Equ PORTB				; Set Axis_Port to Equal PortB
Axis_Tris Equ TRISB				; Set Axis_Tris to Equal TrisB
X_Axis Equ 3					; Set X_Axis to Equal 3

	org	0x0000					; Beggining of Program
Init
	movlw	0x72 				; 8MHz clock select
    movwf	OSCCON				; Move to OSCILLATOR CONTROL REGISTER
	clrf	Axis_Tris			; Set All Output
	bsf		Axis_Tris, X_Axis 	; Set Axis_Tris.X_Axis as input
	movlw	b'10111100'
	movwf	T3CON

Main
	bcf		T3CON, TMR3ON
	clrf	TMR3L
	clrf	TMR3H        ;timer3 cleared and off

	movlw	0x05				 ; Rising Edge Selection
	movwf	CCP1CON	

	bcf		PIR1, CCP1IF            ;clear the flag
Loop1
	btfss	PIR1, CCP1IF            ;wait for the rising edge
	goto	Loop1 
	bsf		T3CON, TMR3ON           ;then start the timer
	bcf		PIR1, CCP1IF            ;clear the flag
	movlw	0x04					;Falling Edge Selection
	movwf	CCP1CON	
Loop2
	btfss	PIR1, CCP1IF           ;wait for the falling edge
	goto	Loop2
	bcf		T3CON, TMR3ON           ;then start the timer
	bcf		PIR1, CCP1IF
	goto	Main				; Loop back to main
	End

But now my results are so different each time i check it. I have a break point on main and everytime it completes a whol rise/fall it shows me results in watch window. Now each time i press play i get totally different results so far off its not funny some times like the TMR3L is 04 then its 3E so its too far for a good result i say maybe something i wrote wrong?
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top