Revision: 1273
Author: sebastien.lelong
Date: Mon Aug 31 00:41:28 2009
Log: backport PWM changes to cheetah template + simplify template (no more
function/procedure)
http://code.google.com/p/jallib/source/detail?r=1273
Modified:
/trunk/include/peripheral/pwm/pwm_ccp1.jal
/trunk/include/peripheral/pwm/pwm_ccp10.jal
/trunk/include/peripheral/pwm/pwm_ccp2.jal
/trunk/include/peripheral/pwm/pwm_ccp3.jal
/trunk/include/peripheral/pwm/pwm_ccp4.jal
/trunk/include/peripheral/pwm/pwm_ccp5.jal
/trunk/include/peripheral/pwm/pwm_ccp6.jal
/trunk/include/peripheral/pwm/pwm_ccp7.jal
/trunk/include/peripheral/pwm/pwm_ccp8.jal
/trunk/include/peripheral/pwm/pwm_ccp9.jal
/trunk/tools/pwm/pwm_ccpx.tmpl
/trunk/tools/pwm/pwm_ccpx_generator.py
/trunk/tools/pwm/pwm_generate_all.sh
=======================================
--- /trunk/include/peripheral/pwm/pwm_ccp1.jal Sat Aug 29 13:10:39 2009
+++ /trunk/include/peripheral/pwm/pwm_ccp1.jal Mon Aug 31 00:41:28 2009
@@ -121,4 +121,3 @@
pwm1_set_dutycycle_highres(duty << 2)
end procedure
-
=======================================
--- /trunk/include/peripheral/pwm/pwm_ccp10.jal Sat Aug 29 13:10:39 2009
+++ /trunk/include/peripheral/pwm/pwm_ccp10.jal Mon Aug 31 00:41:28 2009
@@ -121,4 +121,3 @@
pwm10_set_dutycycle_highres(duty << 2)
end procedure
-
=======================================
--- /trunk/include/peripheral/pwm/pwm_ccp2.jal Sat Aug 29 13:10:39 2009
+++ /trunk/include/peripheral/pwm/pwm_ccp2.jal Mon Aug 31 00:41:28 2009
@@ -121,4 +121,3 @@
pwm2_set_dutycycle_highres(duty << 2)
end procedure
-
=======================================
--- /trunk/include/peripheral/pwm/pwm_ccp3.jal Sat Aug 29 13:10:39 2009
+++ /trunk/include/peripheral/pwm/pwm_ccp3.jal Mon Aug 31 00:41:28 2009
@@ -121,4 +121,3 @@
pwm3_set_dutycycle_highres(duty << 2)
end procedure
-
=======================================
--- /trunk/include/peripheral/pwm/pwm_ccp4.jal Sat Aug 29 13:10:39 2009
+++ /trunk/include/peripheral/pwm/pwm_ccp4.jal Mon Aug 31 00:41:28 2009
@@ -121,4 +121,3 @@
pwm4_set_dutycycle_highres(duty << 2)
end procedure
-
=======================================
--- /trunk/include/peripheral/pwm/pwm_ccp5.jal Sat Aug 29 13:10:39 2009
+++ /trunk/include/peripheral/pwm/pwm_ccp5.jal Mon Aug 31 00:41:28 2009
@@ -121,4 +121,3 @@
pwm5_set_dutycycle_highres(duty << 2)
end procedure
-
=======================================
--- /trunk/include/peripheral/pwm/pwm_ccp6.jal Sat Aug 29 13:10:39 2009
+++ /trunk/include/peripheral/pwm/pwm_ccp6.jal Mon Aug 31 00:41:28 2009
@@ -121,4 +121,3 @@
pwm6_set_dutycycle_highres(duty << 2)
end procedure
-
=======================================
--- /trunk/include/peripheral/pwm/pwm_ccp7.jal Sat Aug 29 13:10:39 2009
+++ /trunk/include/peripheral/pwm/pwm_ccp7.jal Mon Aug 31 00:41:28 2009
@@ -121,4 +121,3 @@
pwm7_set_dutycycle_highres(duty << 2)
end procedure
-
=======================================
--- /trunk/include/peripheral/pwm/pwm_ccp8.jal Sat Aug 29 13:10:39 2009
+++ /trunk/include/peripheral/pwm/pwm_ccp8.jal Mon Aug 31 00:41:28 2009
@@ -121,4 +121,3 @@
pwm8_set_dutycycle_highres(duty << 2)
end procedure
-
=======================================
--- /trunk/include/peripheral/pwm/pwm_ccp9.jal Sat Aug 29 13:10:39 2009
+++ /trunk/include/peripheral/pwm/pwm_ccp9.jal Mon Aug 31 00:41:28 2009
@@ -121,4 +121,3 @@
pwm9_set_dutycycle_highres(duty << 2)
end procedure
-
=======================================
--- /trunk/tools/pwm/pwm_ccpx.tmpl Fri Jul 3 10:23:14 2009
+++ /trunk/tools/pwm/pwm_ccpx.tmpl Mon Aug 31 00:41:28 2009
@@ -1,145 +1,126 @@
-#def header
--- Title: hardware PWM control, dedicated to register $ccp_name
--- Author: Stef Mientki, Copyright (C) 2002-2006, all rights reserved.
--- Adapted-by: Sebastien Lelong
--- Compiler: >=2.4g
+#def main
+--
-------------------------------------------------------------------------------------
+-- Title: hardware PWM control, dedicated to module CCP${num}
+-- Author: Stef Mientki, Copyright (C) 2002-2006, all rights reserved.
+-- Adapted-by: Sebastien Lelong, Rob Hamerling.
+-- Compiler: 2.4l
--
-- This file is part of jallib (http://jallib.googlecode.com)
-- Released under the ZLIB license
(http://www.opensource.org/licenses/zlib-license.html)
--
--- Description: performs PWM operations on $ccp_name
--- The target must support this.
+-- Description:
+-- Performs PWM operations on the CCP${num} module.
+-- This file is automatically included by pwm_hardware.jal
+-- when the target PIC has a CCP${num} module.
--
--- Notes: this is a heavy refactoring of the original pwm_hardware.jal
--- Stef's lib
+-- Notes: 1. This is a heavy refactoring of the original pwm_hardware.jal
+-- (Stef's lib)
+-- 2. Partly rewritten for JalV2 version 2.4l and Jallib revision
1171.
+-- Reduced memory occupation. Added procedure for lowres PWM.
--
-#end def
-
-
-#def global_vars
-var byte ${ccprxl}_shadow = 0
-var byte ${ccpxcon}_shadow = 0
-#end def
-
-
-
-#def pwm_set_dutycycle
--- Sets dutycyle in low resolution mode.
--- The 2 LSbits take their value from ${ccpxcon}_shadow, by default 0
--- This means if duty$num = 0, there's no pwm, if duty$num = 255, you're
closed to
--- (but not at) the maximum pwm
-procedure pwm${num}_set_dutycycle(byte in duty) is
- ${ccprxl}_shadow = duty
- $CCPRXL = ${ccprxl}_shadow -- reload 8 high order bits of dutycycle
- $CCPXCON = ${ccpxcon}_shadow -- reload 2 low order bits of dutycycle
+--
-------------------------------------------------------------------------------------
+
+var byte _ccpr${num}l_shadow = 0 -- 8 MSbits
of duty cycle
+var byte _ccp${num}con_shadow = 0b0000_0000 -- shadow
+var bit*2 _ccp${num}con_shadow_dc${num}b at _ccp${num}con_shadow : 4
-- 2 LSbits of duty cycle
+var bit*4 _ccp${num}con_shadow_ccp${num}m at _ccp${num}con_shadow : 0
-- mode pattern
+
+
+-- PWM mode on
+-- Restore duty cycle from shadow registers
+-- Note: pin_CCP${num}_direction should be set to output!
+procedure pwm${num}_on() is
+
+ _ccp${num}con_shadow_ccp${num}m = 0b1100 -- set PWM
mode
+ CCPR${num}L = _ccpr${num}l_shadow -- restore
duty cycle
+ CCP${num}CON = _ccp${num}con_shadow -- activate
CCP module
+
end procedure
--- sets dutycyle in high resolution mode
--- the word passed to the procedure represents the high resolution value
--- of the duty cycle:
--- * duty${num}h<0:1> are the 2 LSbits
--- * duty${num}h<2:9> are the 8 MSbits
---
--- Because duty cycle is coded on 10 bits, the given value can't greater
than 1024
--- If not active yet, calling this procedure will enable PWM
-procedure pwm${num}_set_dutycycle_highres(word in duty) is
-
- -- split word into 8 MSb and 2LSb
- var word dutyl = duty & 0b_0000_0011
- var word dutyh = duty >> 2
-
- ${ccprxl}_shadow = byte(dutyh)
- ${ccpxcon}_shadow = (byte(dutyl) & 0b_0000_0011) << 4
-
- $CCPRXL = ${ccprxl}_shadow
- -- reload while (re)activating pwm
- ${ccpxcon}_shadow = ${ccpxcon}_shadow | 0b_0000_1100
- $CCPXCON = ${ccpxcon}_shadow
+-- PWM mode off
+-- retain duty cycle setting in shadow registers
+procedure pwm${num}_off() is
+
+ _ccp${num}con_shadow_ccp${num}m = 0b0000 -- set
CCP/PWM off
+ CCP${num}CON = _ccp${num}con_shadow
+
end procedure
-#end def
-#def pwm_on_off
-procedure pwm${num}_on() is
- -- pwm mode on, while keeping 2 LSbits
- ${ccpxcon}_shadow = ${ccpxcon}_shadow | 0b_0000_1100
- $CCPXCON = ${ccpxcon}_shadow
-end procedure
-
-
-procedure pwm${num}_off() is
- -- pwm mode off, but keep 2 LSbits values
- ${ccpxcon}_shadow = ${ccpxcon}_shadow & !0b_0000_1100
- $CCPXCON = ${ccpxcon}_shadow
+-- Set dutycycle with 10-bits resolution, allowing 1024 PWM steps.
+-- The 'duty' argument is a (max) 10-bits absolute value for the duty
cycle:
+-- * duty<1:0> are the 2 LSbits
+-- * duty<9:2> are the 8 MSbits
+-- Allowed range: 0..1023
+-- Note: pin_CCP${num}_direction should be set to output!
+procedure pwm${num}_set_dutycycle_highres(word in duty) is
+
+ if (duty > 1023) then -- upper limit
+ duty = 1023
+ end if
+ _ccpr${num}l_shadow = byte(duty >> 2)
+ _ccp${num}con_shadow_dc${num}b = byte(duty) & 0b11
+
+ pwm${num}_on() -- activate PWM
+
end procedure
-#end def
-
-#def include
--- include common/factored part of pwm
-include pwm_common
-#end def
-
-
-#def pwm_set_percent
--- Following procedures needs variables defined in pwm_common
--- so they are listed here
-
--- This procedure set duty cycle using a percentage
--- According to what has been computed by pwm_set_frequency(),
--- it converts the duty percent to a value suitable for the
--- selected pwm frequency.
-procedure pwm${num}_set_percent_dutycycle(byte in percent) is
-
- -- due to a limitation in JAL, the brackects below are VERY essential
- var dword _duty${num}_1 = ((percent * 4) * _pr2_1) / 100
- var dword _duty${num}_4 = ((percent * 4) * _pr2_4) / 100
- var dword _duty${num}_16 = ((percent * 4) * _pr2_16) / 100
-
- var byte dutyh
- var byte dutyl
- -- select suitable duty value
- if _pr2_1 == 0 then
- ;;pragma error -- pwm frequency is too high
- elsif _pr2_1 <= 256 then
- ;;${ccprxl}_shadow = byte(_duty${num}_1 / 4) -- 8 high order
bits of dutycycle
- ;;${ccpxcon}_shadow = byte((_duty${num}_1 & 0b_0000_0011) * 16)
- dutyh = byte(_duty${num}_1 / 4) -- 8 high order bits of
dutycycle
- dutyl = byte((_duty${num}_1 & 0b_0000_0011) * 16)
- elsif _pr2_4 <= 256 then
- ;;${ccprxl}_shadow = byte(_duty${num}_4 / 4)
- ;;${ccpxcon}_shadow = byte((_duty${num}_4 & 0b_0000_0011) * 16)
- dutyh = byte(_duty${num}_4 / 4)
- dutyl = byte((_duty${num}_4 & 0b_0000_0011) * 16)
-
- elsif _pr2_16 <= 256 then
- -- Stef is a sioux: x * 16 <=> x << 4 !!!
- ;;${ccprxl}_shadow = byte(_duty${num}_16 / 4)
- ;;${ccpxcon}_shadow = byte((_duty${num}_16 & 0b_0000_0011) * 16)
- dutyh = byte(_duty${num}_16 / 4)
- dutyl = byte((_duty${num}_16 & 0b_0000_0011) * 16)
- else
- ;;pragma error -- pwm frequency is too low to be realized by this
routine,
- -- use the post scaler and interrupt
- end if
-
- -- build a word storing duty cycle high resolution'ed
- var word duty_tmp = dutyh << 2
- duty_tmp = dutyh | dutyl
- pwm${num}_set_dutycycle_highres(duty_tmp)
-
+
+
+-- Set dutycycle with 8-bits resolution allowing 255 PWM steps.
+-- The 'duty' argument is the 8-bits absolute value for the duty cycle:
+-- * duty<1:0> are the 2 LSbits
+-- * duty<7:2> are the 6 MSbits
+-- Allowed range: 0..255
+-- Beware that steps 256..1023 are not available. In other words
+-- the available PWM range is 25% of the highres procedure.
+-- This procedure is particularly suitable with higher frequencies
+-- whereby the PWM resolution is limited to 256 steps or less!
+procedure pwm${num}_set_dutycycle_lowres(byte in duty) is
+
+ _ccpr${num}l_shadow = duty >> 2
+ _ccp${num}con_shadow_dc${num}b = duty & 0b11
+
+ pwm${num}_on() -- activate PWM
+
+end procedure
+
+
+-- Set dutycycle for 10-bits resolution but allowing only 255 PWM steps.
+-- This procedure is equivalent to pwm${num}_set_dutycycle_highres(), but
+-- the low order 2 bits of the 10-bits duty cycle are set to 0.
+-- This means that only every 4th of the available 1023 steps can be
selected
+-- and consequently max 255 PWM steps are available.
+-- This procedure is for user convenience, allowing to specify an 8 bits
+-- value for the duty cycle is for many applications satisfactory.
+-- Calling this procedure will also activate PWM.
+-- Note: pin_CCP${num}_direction should be set to output!
+procedure pwm${num}_set_dutycycle(byte in duty) is
+
+ pwm${num}_set_dutycycle_highres(word(duty) << 2)
end procedure
-#end def
-#def main
-$header
-$global_vars
-$pwm_set_dutycycle
-$pwm_on_off
-$include
-$pwm_set_percent
-#end def
-
+-- Set a percentage duty cycle, allowing max 100 PWM steps.
+-- Allowed range: 0..100
+-- The duty cycle will be set to the specified percentage of the maximum
+-- for the current PWM frequency.
+-- Note: The number of available PWM steps can be lower than 100 with
+-- (very) high PWM frequencies.
+-- Note: pin_CCP${num}_direction should be set to output!
+procedure pwm${num}_set_percent_dutycycle(byte in percent) is
+
+ var word duty
+ if (percent == 0) then
+ duty = 0
+ elsif (percent >= 100) then
+ duty = _pr2_shadow_plus1 - 1
+ else
+ duty = word(percent) * (_pr2_shadow_plus1 / 4) / 25 -- (factor
PR2/100)
+ end if
+ pwm${num}_set_dutycycle_highres(duty << 2)
+
+end procedure
+#end def
$main
=======================================
--- /trunk/tools/pwm/pwm_ccpx_generator.py Sat May 23 11:49:17 2009
+++ /trunk/tools/pwm/pwm_ccpx_generator.py Mon Aug 31 00:41:28 2009
@@ -36,13 +36,10 @@
tmplsrc = "".join(file(tmpl_file,"r").readlines())
klass = Cheetah.Template.Template.compile(tmplsrc)
tmpl = klass()
- tmpl.ccp_name = "CCP%s" % num
- tmpl.CCPXCON = "CCP%sCON" % num
- tmpl.CCPRXL = "CCPR%sL" % num
- tmpl.ccpxcon = tmpl.CCPXCON.lower()
- tmpl.ccprxl = tmpl.CCPRXL.lower()
tmpl.num = num
- print tmpl.main()
+ fout = file("pwm_ccp%s.jal" % num,"w")
+ fout.write(tmpl.main())
+ #print tmpl.main()
sys.exit(0)
=======================================
--- /trunk/tools/pwm/pwm_generate_all.sh Sat May 23 11:49:17 2009
+++ /trunk/tools/pwm/pwm_generate_all.sh Mon Aug 31 00:41:28 2009
@@ -1,8 +1,8 @@
#!/bin/bash
-for num in `echo 1 2 3 4 5`
+for num in `echo 1 2 3 4 5 6 7 8 9 10`
do
- python pwm_ccpx_generator.py pwm_ccpx.tmpl $num
> ../../include/peripheral/pwm/pwm_ccp$num.jal
+ python pwm_ccpx_generator.py pwm_ccpx.tmpl $num && mv
pwm_ccp${num}.jal ../../include/peripheral/pwm/
done
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"jallib" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/jallib?hl=en
-~----------~----~----~----~------~----~------~--~---