| Home |
Last Modified: 19 February 1999 |
6172 CAN Micro Controller Module AN008
This application example is not yet completely tested
4A PWM OUTPUT
This example shows how to implement a PWM driven output, usable for e.g. motor or hydraulic valve control.
header "PWM output AN008"
;this sequencer program allows to set the Pwm speed conguration and duty
;cycle.
;if the MOS driver detects an error, an ERROR will be signalled to the
;application layer (the CANopen application layer then will send an
;emergency message)
;memory locations
PwmCfg: equ 16 ;PWM frequency / resolution (16 bit)
PwmVal: equ 18 ;PWM value (duty cycle)
ErrorC: equ 1 ;ERROR code to signal to application layer
EnaBit: equ 3 ;OUT12 is CMOS driver enable (when low)
ResFlt: equ 2 ;OUT11 is FAULT latch reset (when low)
Fault: equ 4 ;IN5 is FAULT input (when high)
;-------------------------------------------------------------------------
;init sequence
sequence seq_0
ldwc high 7500 ;set PWM frequency default to 1000 Hz
stwm PwmCfg ;resolution 100/7500 percent
stwm PWMFH
ldwc low 7500
stwm PwmCfg+1
stwm PWMFL
ldwc 0 ;set PWM duty cycle to 0
stwm PwmVal
stwm PwmVal+1
stwm PWMDH
stwm PWMDL ;write to PWMDL initializes PWM output
resb ResFlt ;prepare reset FAULT low
resb EnaBit ;prepare ENABLE low
srdy ;READY high, FAULT and ENABLE low
setb ResFlt ;enable FAULT latch
seqcl Seq_4,1,Fault ;start sequence 4 when Fault input is high
enasq 0 ;enable sequences
endsq
;-------------------------------------------------------------------------
;write new config/duty cycle to PWM registers
sequence seq_3,,PwmVal+1 ;start after write of PwmVal + 1
ldwm PwmCfg ;just copy 4 bytes to pwm registers
stwm PWMFH
ldwm PwmCfg+1
stwm PWMFL
ldwm PwmVal
stwm PWMDH
ldwm PwmVal+1
stwm PWMDL
endsq
;-------------------------------------------------------------------------
;this sequence is executed when the FAULT input went from low to high
sequence seq_4
error ErrorC ;signal error to application
;prevent sequence 2 from re-executing as long as FAULT stays high
seqcl Seq_3,0,Fault ;sequence 3 when Fault is low
endsq
;-------------------------------------------------------------------------
;this sequence is executed when the FAULT input went from high to low
sequence seq_5
errof ErrorC ;signal end of error to application
;prevent sequence 3 from re-executing as long as FAULT stays low
seqcl Seq_2,1,Fault ;sequence 2 when Fault is high
endsq
;-------------------------------------------------------------------------
;this sequence is executed once a second to clear the fault latch
;this makes shure that overload errors are signalled maximum once a second
;the FAULT output of the MOSFET driver is pulsed low as long as there is a
;short circuit/overload of the MOSFETs
sequence seq_6,100 ;interval = 100 * 10 mS
resb ResFlt ;reset FAULT
setb ResFlt ;enable FAULT latch
endsq
end
CANopen object dictionary configuration
Assuming that all 6172 objects are in their default state, the following objects need to be adjusted (if a PDO is used):
|
Index |
Subindex |
Value |
Description |
|---|---|---|---|
|
Use receive PDO1 for complete adjustment of the PWM output (frequency/resolution and duty cycle) |
|||
|
1600h |
0 |
1 |
1 object in receive PDO 1 |
|
1600h |
1 |
63200520h |
memory location 16-19 |
|
Use receive PDO2 for adjustment of just the duty cycle |
|||
|
1601h |
0 |
2 |
2 object in receive PDO 2 |
|
1601h |
1 |
62001308h |
memory locations 18 |
|
1601h |
2 |
62001408h |
memory locations 19 |
A parameter of the PWM output can be changed by:
This program uses the standard QBasic procedure and functions in Canoappl.bas
APPLICATION:
'This simple program is made for one 6172 and a 6390 as CAN bus interface
'the following assumptions are done:
'- 6390 is connected to COM2
'- 6172 ID = 1, CANbus speed = 50 kBAUD
'- assembled sequence file is "AN008.seq"
'- this program asks for PWM frequency and duty cycle and sends this
' to the 6172 using one PDO
'- the program ends when nothing (just enter) is entered...
CLS
PRINT "CD Systems 6172 AN006 DEMO program"
PRINT
Activate6390 2, 19200 '6390 works at COM2, 19200 BAUD
SetCANBUSSpeed 50 'CAN BUS at 50 kBAUD
C% = CheckSequence%(1, "AN008.seq") 'download sequence if not already done
C% = PutU8% (1, &h1600, 0, 1) '1 object in receive PDO 1
C% = PutU32% (1, &h1600, 1, &h63200520) 'receive PDO 1 to memory 16-19
C% = PutU8% (1, &h1601, 0, 2) '2 objects in receive PDO 2
C% = PutU32% (1, &h1601, 1, &h62001308) 'receive PDO 2 to memory 18
C% = PutU32% (1, &h1601, 2, &h62001408) 'receive PDO 2 to memory 19
SendNMTCommand (1, 1) 'node in operational state
PDO1COB% = (1 + 512) * 32 'receive PDO1 COB ID
PDO2COB% = (1 + 768) * 32 'receive PDO2 COB ID
DO
CLS
PRINT "CD Systems 6172 AN008 DEMO program"
PRINT
PRINT
LINE INPUT "Enter desired PWM frequency (115-1E6 Hz): ", FRQ$
IF FRQ$ <> ""
FRQ! = VAL (FRQ$)
IF FRQ! < 115 THEN FRQ! = 115
IF FRQ! > 1E6 THEN FRQ! = 1E6
LINE INPUT "Enter desired PWM duty-cycle (0 - 100 %): ", DUT$
IF DUT$ <> ""
PRINT
DUT% = VAL (FRQ$)
IF DUT% < 0 THEN DUT% = 0
IF DUT% > 100 THEN DUT% = 100
PWMC! = 7.5E6 / FRQ! ' convert frequency
PWMD! = (PWMC! / 100) * DUT% ' convert duty cycle
H1% = INT (PWMC! / 256): L1% = PWMC! - (H1% * 256)
H2% = INT (PWMD! / 256): L2% = PWMD! - (H2% * 256)
D$ = CHR$(L2%) + CHR$(H2%) + CHR$(L1%) + CHR$(H1%)
SendToCAN (CANIDS$ (PDO1COB% + 4) + D$)
PRINT "New value transmitted...";
DELAY (2)
END IF
END IF
LOOP UNTIL (FRQ$ = "") OR (DUT$ = "")
SendNMTCommand (1, 2) 'stop node
END