PDA

View Full Version : pwm 18f2431



robertpeach
- 17th August 2009, 14:27
ive been doing a fair amount of research into writing basic code now, but at one point it gets a bit vague. im using the 18f2431, and in the data sheets it describes in assembley code (im presuming thats the language they use) and the function i require is cap1 0111 which makes PORTA.3 to measure the pwm of that specific signal. this is similar for cap2 and cap3 as well.

ive been looking through the basic language and i get a few commands, such as PULSIN, which says it measures the pulse width on a pin. would this command suffice for the 18f2431 cap1 cap2 and cap3 pins? or is there something different? since i chose the pin because of its advantages of measuring signals pwm and ffrequency etc... it would seem a bit daft having chosen it too find that any random pic would be able to just be coded with PULSIN and measure it straight off.

robertpeach
- 17th August 2009, 14:44
ok i found hpwm aswell, which is specifically for pics with hardware suited for pwm's... so may have just been a bit unobservent! is there a way for the hpwm to be continually changing? e.g. instead of just having a specific pwm i can have it changing as the input averages change etc... almost as though instead of putting a number into the code i can put temporarily saved data such as hpwm 1, var, 20000?

ok i just confused myself all over again. ive just been mixing up inputs and outputs. back to original question... is it possible to use the pulsin command to measure on the 18f2431, or does that not take advantage of the special motion feedback module that its provides...?

sorry

mackrackit
- 17th August 2009, 15:48
Original question..
The software solutions would work but you are correct in thinking the advantage of the motion feed back would be lost.

The advantage with the motion feed back is it will run in the background and should be able to be used with an interrupt. I have not used that chip yet as I heard about it the same as you from Bruce. I would go with Bruce's idea on this one.

robertpeach
- 17th August 2009, 15:59
thanks for the reply,

yeah i want to use the motion feedback module since its obvious there will be advantages using it (otherwise it would have been pointless to create). its just im a bit sketchy with how i would go about that. the motion feedback module actually continuously measures the pulse width of the input signal, so that would run in the "background" and within my code, i would create interupts that would jump to cap1 and that would give me the pwm (that it was automatically measuring) at that moment in time. and then i can temporarily save that pwm and use it for my algorithms etc. so before the START of my program i would have had to set my cap1 cap2 cap3 all to be measuring the the pwm? (using the assembley code commands to take advantage of its special features)

robertpeach
- 18th August 2009, 08:51
i realise probably the reason noone has posted is because not many ppl have used the 18f2431, but any help at all would be beneficial to me. i kinda seem to have met a dead end with it...

edit: after more digging, ive been looking into a program for interupts created by darrel taylor that seems quite convincing. it has specific interupts for the motion feedback modules.

mackrackit
- 18th August 2009, 10:31
I think you are correct that not many have used this chip. I was hopping Bruce Or Darrel would reply to this. When I get a chance I will look at the data sheet.

DTs instant interrupts works well for what I have used it for so when the motion module is undrstood they should work well for that too.

Sorry I can not be more help at this time.

robertpeach
- 18th August 2009, 11:22
thanks mack

yeah i was looking at instant interupts aswell...

my coding is quite abysmal at this time, but the general idea is what im heading for at the moment till my coding is up to scratch (hopefully picbasic pro will actually arrive today :P including a manual that might actually help!)


CAP1M3:CAP1M0 = 0101 'capture channel 1 is measuring period
CAP1REN 'capture timer5 is reset after each measurment is made
CAP2M3:CAP2M0 = 0101 'capture channel 2 is measuring period
CAP2REN 'capture timer5 is reset after each measurement is made
CAP3M3:CAP3M0 = 0101 'capture channel 3 is measuring period
CAP3REN 'capture timer5 is reset after each measurement is made


(i realise its asm)

this is just the starting for actually capturing the result. this measures directly to a buffer for each channel without the need to save it with any coding. CAP1BUF, CAP2BUF and CAP3BUF. would it be possible to have this running in the background? and not actually create an interupt to get to it? since it is already saved automatically in a buffer. cant i just during my program just "read" from the specific buffer and not actually entangle myself with the measurement processes that are going on at the CAP's.

thanks :)

mackrackit
- 18th August 2009, 11:40
You do not need interrupts. You should be able to read it just as you would an ADC or other hardware things. The interrupt option is just that with hardware that might be helpful.

I will try to get a chance to read through the data sheet later. I have some of these chips on order now. So maybe in a week or so I will be asking you questions.

robertpeach
- 18th August 2009, 12:09
ok thanks mack

unfortunately i still dont understand much about these interupts and how they can help me, but ill keep searching around and looking at example codes.

haha ill be as much help as possible ;) but dont expect much!

robertpeach
- 18th August 2009, 14:20
sorry again to post here, but since i dont actually have the program yet to play with since it hasnt arrived, i cant actually trial and error anything im doing... or any basic language im picking up.

one thing i am interested in is just reading values that would be saved at a certain filename. ive looked up the filenames in the datasheets etc..

to use my specific data value would i have to convert it to var? and then use it in my calculation algorithm.

READ F69H, w0.BYTE.o 'cap1 value written to word0 byte 0
READ F67H, W0.BYTE.1 'cap2 value written to word0 byte 1
READ F65H, W0.BYTE.2 'cap3 value written to word0 byte 2

is this an incorrect way of accessing my data? im sorry i cant actually test it myself as of yet...

mackrackit
- 19th August 2009, 00:04
You have lost me.
Where is the file being saved? Are you meaning save the data to eeprom?

This page has several samples.
http://www.melabs.com/resources/samples.htm

robertpeach
- 19th August 2009, 09:00
sorry im quite bad at explaining, but ill try my best. ill check that site out as well thanks

basically its being saved in the thing called SRAM, if im correct. which is similar to normal ram but its for special functions. Basically its just implemented as static ram. with an address of F69H for CAP1BUFH, which is the place where "capture 1 register highbyte", the place where the measurement (period in my case) has been stored temporarily, till it is overwritten by the next measurement.

robertpeach
- 19th August 2009, 09:33
ok its easier than i thought... u can just right the file address in your calculation.... e.g. (F69H)/5

am i right... or wrong again? sorry picbasicpro should be here today for me to learn from trial and error... looking at example codes and old manuals on the internet only goes so far

mackrackit
- 19th August 2009, 10:17
From Micros web site


Static RAM or SRAM
Static Random Access Memory. Program memory you can Read/Write on the target board that does not need refreshing frequently.

Calling it SRAM is a little misleading.

In your case I think you have the idea. You are talking about program variables. Variables to be used inside the program. Not permanent storage.

You have for the 18Fs four variable sizes to pick from
bit, byte, word, long

near the begining of your code you will have something like


F69H VAR BYTE


Then you can use F69H as in your example.
The value will be erased at power down.

robertpeach
- 19th August 2009, 10:51
ahhh thank you, thats cleared many things up for me. my picbasic pro just arrived so ill be testing all my attempted codings now!

cheers mack

robertpeach
- 19th August 2009, 14:45
ok well these are the bare bones of my code so far... i havnt actually created the algorithm yet so i havnt actually got a proper dutycycle to put in yet. and things like config settings etc i still have yet to choose. just hope its going in the right direction


'''''''''''''''''''''''''''''''''''Assembley to collect period on cap1 cap2 and cap3''''''
asm

cap1m0 = 0101
cap1ren = 1
cap2m0 = 0101
cap2ren = 1
cap3m0 = 0101
cap3ren = 1

endasm

'''''''''''''''''''''''''''values put in var for us in calculation''''''''''''''''''

F69H VAR BYTE 'Value of period at CAP1 ready for calculation
F67H VAR BYTE 'Value of period at CAP2 ready for calculation
F65H VAR BYTE 'Value of period at CAP3 ready for calculation

''''''''''''''''''''''''''''''''''''Algorithm for finding average''''''''''''''''''''''''


'yet to write it



'''''''''''''''''''''''''''''''''''pwm output'''''''''''''''''''''''''''''''''''''''''''' '''

HPWM 1, 127, 20000

'obviously unfinished and dutycycle would not be a fixed 50%

robertpeach
- 19th August 2009, 16:27
whoops just realised the pwm output wasnt quite anything...


DutyCycle var byte 'byte being the one that is answer of algorithm when thats created
CCP1CON=0 'output of pwm pin
TRISC= $00000000

HPWM 1,DutyCycle,20000

end


hopefully that should work

robertpeach
- 20th August 2009, 10:07
hey sorry again,

getting the right ideas but im a little stuck on one bit...

i need to set the PWM period by writing to the PR2 register and then set the PWM duty cycle by writing to the CCPR1L register and CCP1CON<5:4> bits. would this require assembley language and how would i do it?



afterwards i need to make the CCP1 pin an output by clearing the
TRISC<2> bit. would that just be trisc.2 = 0 ?


also i need to set the TMR2 prescale value and enable Timer2
by writing to T2CON. but im not too sure on how to work out the prescale value? and how to write to it?
and i need to configure the CCP1 module for PWM operation, how would i decide upon specific configurations? leave it as standard config until i find there is a problem then try and sort out the config?

will this give me hpwm? since i cant find anywhere in the datasheet that its hardwarepwm, but i am presuming it is since its whole purpose seems to revolve around pwm outputs and ccp.

additionally if i am updating the pwm constantly as it is giving an output... will this cause any delay in the output? for example when it updates... there will be a significant pause in the pwm?

robertpeach
- 20th August 2009, 14:39
unforunately since i cant actually test it yet without the real algorithms, i just wanna know if this sounds about right?



'''''''''''''''''''''''''''''''''''Assembley to collect period on cap1 cap2 and cap3''''''
asm

cap1m0 = 0101
cap1ren = 1
cap2m0 = 0101
cap2ren = 1
cap3m0 = 0101
cap3ren = 1

endasm

'''''''''''''''''''''''''''''''''Read values taken and write to var (no longer needed since file name can be used in calculations)''''''''''''''''''

start:

F69H VAR BYTE 'Value of period at CAP1 ready for calculation
F67H VAR BYTE 'Value of period at CAP2 ready for calculation
F65H VAR BYTE 'Value of period at CAP3 ready for calculation

''''''''''''''''''''''''''''''''''''Algorithm for finding power''''''''''''''''''''''''

averageperiod var byte 'saving average period to ram
averageperiod = (F69H+F67H+F65H)/3 'average period found using 3 periods

'for now im using arbitary random scaling factors, until i have the actual graphs to get the real rpm against power

rpmvalue var byte 'saving rpm value to ram
rpmvalue=60/(8*averageperiod) 'finding rpm value using current period measurement

power var byte 'saving power to ram

IF rpmvalue<=300 Then
power=rpmvalue/15
else
power=rpmvalue/10
endif


' now i need to link power to the arbitary pwm output
'this is a standard quadratic function (quadratic in example case anyway)

dutycycle var byte 'byte being the one that is answer of algorithm when thats created
dutycycle = (power^2)+(5*power)+3 'finding dutycycle using rpmvalue


'''''''''''''''''''''''''''''''''''pwm output (possibly need interupts so as not to cause delay in hpwm output'''''''''''''''''''''''''''''''''''''''''''' '''


CCP1CON=0 'output of pwm pin
TRISC= $00000000

HPWM 1,DutyCycle,20000

goto start

end

mackrackit
- 21st August 2009, 04:45
I am not going to comment much on you code as I am still reading the data sheet and my chips have not arrived yet. But I do not think the ASM stuff is quite right.

Have you seen this thread? Post #4.
http://www.picbasic.co.uk/forum/showthread.php?t=7095

robertpeach
- 21st August 2009, 08:55
yeah i didnt really know where i was going with the assembley stuff... just kinda wrote down a few commands that seemed kinda suitable...