sorry, what do you mean by doing it using hardware? You mean like the capture compare module on the chip?
sorry, what do you mean by doing it using hardware? You mean like the capture compare module on the chip?
Last edited by Triode; 6th September 2009 at 10:58 PM.
-Paul
Last edited by 3v0; 6th September 2009 at 11:36 PM.
Please post questions to the forums. PM's are for personal communication.
BCHS/3v0's Tutorials
Junebug USB PIC programmer kit., USB Bit Whacker,
The 15 Minute Printed Circuit Board! (+drill time)
Yeah, I could set it up in software, or at least I know how I'd try, but I have no idea how to set up hardware capture.
I'm looking at this document about it now.
Capture/Compare/PWM
and also this
Pic Midrange Libraries Reference Manual
-Paul
I found an appnote that should make you day.
PICmicro CCP and ECCP Tips ‘n Tricks
http://ww1.microchip.com/downloads/e...Doc/41214a.pdf
Check out
TIP #3 Measuring Pulse Width1. Configure control bits CCPxM3:CCPxM0I would try to break the task up into 3 states and some ISR code. I used letters for the states to prevent confusion with the above steps.
(CCPxCON<3:0>) to capture every rising
edge of the waveform.
2. Configure Timer1 prescaler so that Timer1 will
run WMAX without overflowing.
3. Enable the CCP interrupt (CCPxIE bit).
4. When CCP interrupt occurs, save the captured
timer value (t1) and reconfigure control bits to
capture every falling edge.
5. When CCP interrupt occurs again, subtract
saved value (t1) from current captured value
(t2) – this result is the pulse width (W).
6. Reconfigure control bits to capture the next
rising edge and start process all over again
(repeat steps 3 through 6).
(state A) step 1-2 seq=B
(state B) step 3, block on ISR finding value, seq=C
step 4 and 5 will be done in the ISR
(state C) copy or process W value, seq=A
The thing that get a bit more difficult is that you now have another source of interrupts. Also within the timer1 ISR code you need to know if you have just found a leading or a trailing edge.
My suggestion is to get the method working as simple example then break it up into states.
If it was easy everybody would do it
3v0
Last edited by 3v0; 7th September 2009 at 04:37 AM.
Please post questions to the forums. PM's are for personal communication.
BCHS/3v0's Tutorials
Junebug USB PIC programmer kit., USB Bit Whacker,
The 15 Minute Printed Circuit Board! (+drill time)
Well hey! that pretty much hands it to me. I'm just getting my PWM source going, and this should really speed things along!
-Paul
I added some text to the previous post.
Please post questions to the forums. PM's are for personal communication.
BCHS/3v0's Tutorials
Junebug USB PIC programmer kit., USB Bit Whacker,
The 15 Minute Printed Circuit Board! (+drill time)
It will indeed still take a while, I just meant it will help me get past the current snag. The final goal of making a device plug into two ports of a servo receiver and control two motors in two directions is a way off. But one by one I'm knocking parts of it off the list.
-Paul
Hi i want to send a array of data through the serial port but when i try to use a pointer to address the array and then write
at start of program tempptr=tempdata;
then
putcUSART(*tempptr);
tempptr++;
it gives a syntax error.
On the other hand tempdata[counter] works fine.
Syed
I am using C18 and wanted to use a pointer to index an array(a very very simple application).But i got errors so i used the conventional method.My question is
Can you index a numeric array using the built in functions of the USART.I tried and i got errors..am i doing it incorrectly?
PS: I have already done it the other way but wanted to clear my self about the abilities of C18.
Syed
I would guess that the function expects a ram based variable and wont work with a rom pointer, try,
Mike.Code:static rom char HW[]="Hello World!"; rom char *P; char i; P=HW; // or P=&HW[0]; while(*P){ i=*P++; putcUSART(i); }
Last edited by Pommie; 7th September 2009 at 07:03 AM. Reason: corrected case.
I am using a RAM Array already, but since it does not accept a ram array pointer so guess it was not working.I would rather use the conventional array indexing approach since my application allows it.The code you provide works.Thanks for you reply.
Syed
Alright, I coded a C18 version of what 3v0 posted above in post #139. The code is below, I have just included the part for the CCP setup, the interrupt and the timer setup:
configuration
code withen interrupt functionCode://configure timer1 OpenTimer1(0b10010101); PIE1bits.TMR1IE = 1; //periferial interupt register, 1 = timer 1 interupt enabled PIR1bits.TMR1IF = 0; //clears the timer 1 interupt flag //configure CCP1 for capture, rising edge CCP1CON = 0b0000101;
So far it is working ok. The value I get ranges from 178 with the knob all the way in one direction to 143 all the way in the other, at a given position the value varies by as much as 9, but that variance increases towards the extremes, varying only by 3 in the neutral center position.Code://ccp interrupt if(PIR1bits.CCP1IF == 1) { if(PWM1Edge == 1)//if detecting rising { interrupttest3++; PWM1RiseTime = CCPR1;//save the low timer value for the rise time CCP1CON = 0b0000100;//switch to detect falling edge PWM1Edge = 0;//switch to indicate falling edge is next } else //detecting falling { interrupttest2++; PWM1Width = CCPR1 - PWM1RiseTime; CCP1CON = 0b0000101;//switch to detect rising edge PWM1Edge = 1;//switch to indicate rising edge is next } interrupttest1++; PIR1bits.CCP1IF = 0; //clear the flag } }
I'm looking for suggestions on how this could be improved. As I am not very good with timers, and CCP1 depends on timer1, I suspect that I could improve the settings there to get higher resolution. Would the resolution go up as the prescaler goes down or vice versa? Part of my concern is that I haven't really followed this step "Configure Timer1 prescaler so that Timer1 will run WMAX without overflowing." since I don't really understand prescalers. So, I'll need to read up on them and get that part fine tuned.
Any other suggestions on how this could be improved would be appreciated.
details: Im using a traxas 2215 receiver at 27mhz, it is connected to the 3 pin port of my junebug (RB3,UV5,GND)
Last edited by Triode; 17th September 2009 at 05:27 AM.
-Paul
You don't mention the speed of your chip but as it's a Junebug I assume you set it to 8MHz. If this is the case then I would set the prescaler to 2 so each timer 1 count = 1uS. This will mean that typical servo pulses should give you readings from 1000(1mS) to 2000(2mS). The prescaler is how much to divide the clock by before feeding it to timer 1 - it is already divided by 4 and so is effectively 2MHz.
Mike.
Yes, I'm using the 18F1320 on the junebug. I don't remember setting the speed of the chip, but I'll check on that.
I also have a question about debug mode. So far it seems as if it runs at full speed, if I have a LED set to blink every 1000mS, it seems to keep blinking every second weather I'm in debug mode or not, and the capture timing seems correct too. Does debug run at full speed?
-Paul