Hi,
There are still some glitches to sort out with the peripherals, which we'll work on as we go.
Next try spinning a motor with the REMOTE. For this SERVO motor control is needed.
Can someone scan these 3x examples of CODE and recommend which would is best for me to start with please?
The bottom one was kindly written by 'E'
C.
There are still some glitches to sort out with the peripherals, which we'll work on as we go.
Next try spinning a motor with the REMOTE. For this SERVO motor control is needed.
Can someone scan these 3x examples of CODE and recommend which would is best for me to start with please?
The bottom one was kindly written by 'E'
C.
Code:
2.3.8 Using internal PWM modules
Internal PWM modules (more precisely: PWM modes of CCP modules) are turned on using
PWMON statement.
This statement has two arguments.
The first argument is module number and it must be a constant in the range 1-3.
The second argument is used for mode selection.
Internal PWM module can be used on three different output frequencies
for each of four duty cycle resolutions supported by PWMON statement (10-bit, 9-bit, 8-bit and 7-
bit).
So, PWM module can be turned on with PWMON statement in 12 modes.
Here is the list of all modes at 4MHz clock frequency
(for other clock frequencies, the values should be proportionally adjusted):
mode 1: 10-bit, 244Hz
mode 2: 10-bit, 977Hz
mode 3: 10-bit, 3906Hz
mode 4: 9-bit, 488Hz
mode 5: 9-bit, 1953Hz
mode 6: 9-bit, 7813Hz
mode 7: 8-bit, 977Hz
mode 8: 8-bit, 3906Hz
40
mode 9: 8-bit, 15625Hz
mode 10: 7-bit, 1953Hz
mode 11: 7-bit, 7813Hz
mode 12: 7-bit, 31250Hz
The PWM module is initially started with 0 duty cycle,
so the output will stay low until the duty cycle is changed.
PWM module can be turned off with PWMOFF statement.
It has only one argument - module number.
The duty cycle of PWM signal can be changed with PWMDUTY statement.
Its first argument is module number.
The second argument is duty cycle and it can be a constant in the range 0-1023 or byte
or word data type variable.
User must take care to use the proper value ranges for all PWM modes (0-1023 for 10-bit
resolution,
0-511 for 9-bit resolution, 0-255 for 8-bit resolution and 0-127 for 7-bit resolution).
Here is one example example:
DIM duty AS BYTE
PWMON 1, 9
loop:
ADCIN 0, duty
PWMDUTY 1, duty
GOTO loop
Code:
2.3.9 Interfacing Radio Control (R/C) servos.
For writing applications to interface R/C servos there are two statements available
SERVOIN and SERVOOUT.
R/C servo is controlled by a train of pulses (15-20 pulses per second)
whose length define the position of the servo arm.
The valid length of pulses is in the range 1-2ms.
These two statements have two arguments.
The first argument of both statements is the microcontroller pin where the servo
signal is received or transmitted.
41
For SERVOIN statement that pin should be previously setup as an input pin
and for SERVOOUT statement the pin should be setup for output.
The second argument of SERVOIN statement must be a Byte variable where the length
of the pulse will be saved.
The pulses are measured in 10us units, so it is possible to measure pulses in the
range 0.01-2.55ms.
The value stored in the variable for normal servos should be in the range 100-200.
The second argument of the SERVOOUT statement should be a Byte variable or constant
that determines the length of the generated pulse.
For proper operation of the target servo SERVOOUT statement should be executed
15-20 times during one second.
Here is an example of the servo reverse operation:
DIM length AS BYTE
TRISB.0 = 1
TRISB.1 = 0
loop:
SERVOIN PORTB.0, length
IF length < 100 THEN length = 100
IF length > 200 THEN length = 200
length = length - 100
length = 100 - length
length = length + 100
SERVOOUT PORTB.1, length
GOTO loop
Code:
'18F2431 RX DATA to PWM 160218 1200
fine CONFIG1L = 0x00
Define CONFIG1H = 0x02
Define CONFIG2L = 0x0e
Define CONFIG2H = 0x20
Define CONFIG3L = 0x18 'PWMPIN outputs active on RESET
Define CONFIG3H = 0x80
Define CONFIG4L = 0x80
Define CONFIG4H = 0x00
Define CONFIG5L = 0x0f
Define CONFIG5H = 0xc0
Define CONFIG6L = 0x0f
Define CONFIG6H = 0xe0
Define CONFIG7L = 0x0f
Define CONFIG7H = 0x40
'Connect to PC UART, 9600 Baud
'PORTA.0 n/u
'PORTA.1 =
'PORTA.2
'PORTA.3
'PORTA.4
'PORTA.5
'PORTA.6
'PORTA.7
Define SIMULATION_WAITMS_VALUE = 1 'else 0 for PIC
'PORTC.0 n/u
'PORTC.1
'PORTC.2
'PORTC.3 n/u
'PORTC.4
'PORTC.5
'PORTC.6 n/u
'PORTC.7 n/u
AllDigital
ADCON0 = %00000000 'A_D
ADCON1 = %00000000
ADCON2 = %10000000
ADCON3 = %00000000
ADCHS = %00000000 'A_D
ANSEL0 = %00000000 'A_D
OSCCON = %01110010 'internal 8Mhz clock
OSCTUNE = %00000000 'Oscillator tuning register for EUSART
'PCPWM MODULE SEE AN899 PAGE 5 ALSO SECTION 22.0 D/S
'PWMCON0 = %01010100
PWMCON0 = %01000100 'ptmod '%01010100 turns them all
PWMCON1 = %00000000
DTCON = %00000000 'Power control
FLTCONFIG = %00000000
OVDCOND = %00000000
OVDCONS = %00000000
PTPERL = 0xff 'register
PTPERH = 0xff 'register
PTMRL = %00000000
PTMRH = %00000000
PTCON0 = %00000000 'ptckps'
PTCON1 = %00000000 'PTEN
IPR3 = %00000000 'TMR
PTMRL = %0000000
PTMRH = %0000000
TRISA = %00000000
TRISB = %00000000
TRISC = %00000000
'assign PWM pins
PWMon 1, 8 'RC1 CCP1, Mode at 8MHz at 7812Hz, to change this look at the Manual Tables
PWMon 2, 8 'RC2 CCP2
DFLTCON = 0
PWMduty 1, 0
PWMduty 2, 0
Hseropen 9600
verify_uart:
Hserout "PWM", CrLf
WaitMs 200
Enable High
Dim duty1 As Byte
Dim x As Word
x = 0
'pwm0 And pwm1 are controlled by the PDC0H:PDC0L
'PWM2 and PWM3 are controlled by PDC1H:PDC1L
main:
duty1 = 128
If x <= 10 Then Gosub fwd
If x >= 10 Then Gosub rev
x = x + 1
If x = 20 Then x = 0
Goto main
End
fwd:
ASM: bcf CCP2CON,DC2B0
ASM: bcf ccp2con,dc2b1
ASM: movlw 0x00
ASM: movwf ccpr2l
PWMduty 1, duty1
Return
rev:
ASM: bcf CCP1CON,DC1B0
ASM: bcf ccp1con,dc1b1
ASM: movlw 0x00
ASM: movwf ccpr1l
PWMduty 2, duty1
Return
On High Interrupt
Save System
Resume
Last edited: