HW 8 channel SERVO/MOTOR output on 18F4431 (Oshonsoft)

Thread Starter

camerart

Joined Feb 25, 2013
3,853
I used only three values because the oscilloscope in the simulator shows only four.
I set the fourth as a 1ms tick, 100us on 900us off in the main loop.
I can look the rest separately.

The shift_reg is set to one in the interrupt, when the first 20ms has passed, so I did not want to wait that.

Waitms value does not matter. The interrupt determines most of the simulation time.
Hi J,
I'm watching the Microcontroller view, and the 8PINs appear to all be the same?
Should I be watching the OScilloscope for accurate readings?
I am trying to load it on a live PIC, and watch on a 'live' Oscilloscope.

I always set the [ Waitms value ] but it looks like it only helps with WAITMS in the main LOOP.
C

There seems to be something wrong, as the SIM is working but 'live' isn't. I'm not programming accurate today, and keep making mistakes :(
C
 

Thread Starter

camerart

Joined Feb 25, 2013
3,853
Hi J,
The program that was working last night, sometimes is intermittent again.
I keep trying to add your suggestions, but it appears to break.

I can't get a LED to work in the main LOOP, at the same time as the INTERRUPT fires, in SIM or live?
C
 

Thread Starter

camerart

Joined Feb 25, 2013
3,853
Sorry.... There was no main at the time so I didn't think it nessesary... Of course it was as more code would be added.

Sorry C
Hi I,
I was just about to report to ETO.
I am always reluctant to change given programs, but you're happy with SAVE SYSTEM just after the ON HIGH INTERRUPT line?

No need for apologies :)
C.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,853
Show the main loop.
Is it too fast to see the led?
If not, the program might be stuck in the interrupt.
Hi J,
I think it does get stuck in the INTERRUPT.
Here's the MAIN LOOP:

//Main loop
loop:
Toggle RD0
WaitMs 500
//Nothing needed here for servos, it's all in the CCP interrupt.
//Rest of the program here onwards.
Goto loop

I've tried changing the WAITMS to 1ms and letting the SIM run withought the speed up line.
C
 

Thread Starter

camerart

Joined Feb 25, 2013
3,853
Show the main loop.
Is it too fast to see the led?
If not, the program might be stuck in the interrupt.
Hi J,
I now have a prorgam that outputs the 6x PWS, but the LEDs stay ON. Could it be that the INTERRUPT interrupts all of the time, because with a logic probe, it sounds like it?

There is a supicion, that it could be the OSH SIM at fault.
C
 

jjw

Joined Dec 24, 2013
823
At least the interrupt does not get past "if index > 15" because the index is not updated after all pins have been pulsed.
Try adding index=index+1 before that if statement.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,853
At least the interrupt does not get past "if index > 15" because the index is not updated after all pins have been pulsed.
Try adding index=index+1 before that if statement.
Hi J,
I'm not sure what you mean!
'live' the INTERRUPT works, and the CH PWMs show on the oscilloscope, but the LEDs in the MAIN LOOP don't flash.

I asked could it be that the LEDs don't flash becuse the INTERRUPT doesn't stop interrupting?
C
 

jjw

Joined Dec 24, 2013
823
I understand what you asked.
There is a bug in the interrupt routine, msg #68, but it should not be the reason.

Is portd.0 set as output?
 

Thread Starter

camerart

Joined Feb 25, 2013
3,853
I understand what you asked.
There is a bug in the interrupt routine, msg #68, but it should not be the reason.

Is portd.0 set as output?
Hi J,
Yes, PORTD is set to '0'

I've tried lots of ideas, but can't get consistancy.
Regarding too much INTERRUPT, I haven't tested timing yet, and also recall it may not be written correctly yet. (Later)

Meanwhile I've dug out the 2018 program, that you may have written, and added up to 5xCH, which is simulating ok. I'd like to move some SERVOs, so I'll concentrate on that for a while.
C
 

jjw

Joined Dec 24, 2013
823
Hi J,
Yes, PORTD is set to '0'

I've tried lots of ideas, but can't get consistancy.
Regarding too much INTERRUPT, I haven't tested timing yet, and also recall it may not be written correctly yet. (Later)

Meanwhile I've dug out the 2018 program, that you may have written, and added up to 5xCH, which is simulating ok. I'd like to move some SERVOs, so I'll concentrate on that for a while.
C
TRISD = 0 ?
 

Thread Starter

camerart

Joined Feb 25, 2013
3,853
Hi J,
Bear in mind, that I'm working with 2x programs, so watch out for mistakes!
I've marked the programs reading with SW for the 2018 re-surfaced program and HW for the ETO program.
In the sim, with a logic probe, the CH PINS appear to be switching and the LED appears to be switching in the SW program, but no LED in the HW.

I have the LED flashing at 20ms, and wait for it.

Here are Oscilloscope views of SW and HW CH shows CH1 and CH shows LED.
I've messed with the clocks, so I'll tidy them up, to be the same.
C
 

Attachments

jjw

Joined Dec 24, 2013
823
It works!
Just change the led loop in the main faster.
I have 500us on, 500us off in the simulator and the led blinks at about the same pace as the pins.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,853
It works!
Just change the led loop in the main faster.
I have 500us on, 500us off in the simulator and the led blinks at about the same pace as the pins.
Hi J,
I'm working on 2x programs, so can you let me know which one you're refering to each time please, or I'll make mistakes?

The purpose of the LED flash is so I can see something's happening 'live' at 500uS would appear to be always on. I've tried ms, to match the SERVO.

The LED flashes in the simulator, but as you can see in the Oscilloscope, not, 'live'
C.
 
Last edited:

jjw

Joined Dec 24, 2013
823
I am talking about the program from ETO, the HW program.
Show your version of the HW program if you have made any changes.

I said in #70 there is a bug in the HW program. Forget it.
There is no bug.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,853
I am talking about the program from ETO, the HW program.
Show your version of the HW program if you have made any changes.

I said in #70 there is a bug in the HW program. Forget it.
There is no bug.
Hi J,
As there are a couple of questions about the SW prog (which is working), so I'll leave it for now.

Re HW:
The Oscilloscope view in #75 shows the LED isn't flashing. I'll re-check everything and try again.
C
 

Thread Starter

camerart

Joined Feb 25, 2013
3,853
Hi J,
OSC 32mHz
LED WAITus 500.

Sim shows LED only Flash.
LIVE PWM only.

Here's the program:
In case of mistakes.
C
Code:
'18f4431 32mhz XTL REMOTE PWM HW 130522 1200
Define CONFIG1L = 0x00
Define CONFIG1H = 0x06  '8mHz XTLx4 32mHz    0x02
Define CONFIG2L = 0x0e
Define CONFIG2H = 0x20
Define CONFIG3L = 0x1c
Define CONFIG3H = 0x9d
Define CONFIG4L = 0x80
Define CONFIG4H = 0x00
Define CONFIG5L = 0x0f
Define CONFIG5H = 0xc0
Define CONFIG6L = 0x0f
Define CONFIG6H = 0xe0
Define CONFIG7L = 0x0f
Define CONFIG7H = 0x40

//File:   servo_pwm_test_v2.C
//Author: robert jenkins, converted to BASIC by Ian Rogers

//Created On 09 May 2022, 09:45


//Better optimised version, more consistent Output timing And
//no timer Interrupt Or anything needed in main program section,
//beyond the initialisations.

//The main program loop can have delays Or wait loops,
//As Long As interrupts are never blocked.

Define CLOCK_FREQUENCY = 32
ANSEL0 = 0
ANSEL1 = 1

Dim index As Byte

Dim servo_times(8) As Word

Dim time_now As Word
Dim time_set As Word

Dim shift_reg As Byte

//With this timing, 1mS = 1000 counts;
//servo 1mS to 2mS = 1000 to 2000,
//with 1500 as mid point.

//For demonstration, I'm setting the servo times
//to various fixed values.

//Set them from your program as needed.


servo_times(0) = 1500  //Port bit 0, Mid position
servo_times(1) = 1100  //Port bit 1, Near min position
servo_times(2) = 1900  //Port bit 2, Near max position
servo_times(3) = 1300  //Port bit 3, Under mid position
servo_times(4) = 1700  //Port bit 4, Over mid position
servo_times(5) = 1200  //Port bit 5, etc
servo_times(6) = 1800  //Port bit 6
servo_times(7) = 1400  //Port bit 7

TRISB = 0  'Port C all outputs changed &
TRISD = 0  'Port D all outputs changed &

T1CON = 0x31  //8:1, switch on

//Without an output pin defined, just an interrupt from CCP
CCP1CON = 0x02  //ccp_compare_toggle

//And enable the interrupts for the hardware used
//the reference for CCP1, so no time interrupt required.
PIE1.CCP1IE = 1
'INTCON1.PEIE = 1  //INTCON bit 6'ADDED #81 &
'INTCON1.GIE = 1
INTCON.PEIE = 1  //INTCON bit 6'ADDED #81 &
INTCON.GIE = 1  'CHANGED &
'NOTE, line from old program CCP1IP = 1  'High Priority interrupt for CCP1


//Main loop
loop:

Toggle RD6  'Something to do
Toggle RD7  'Something to do

WaitUs 500

//Nothing needed here for servos, it's all in the CCP interrupt.
//Rest of the program here onwards.


Goto loop

End                                               

//INT_CCP1
On High Interrupt
Save System
'PIR1.CCP1IF = 0  //clear IF ADDED #81 &

shift_reg = ShiftLeft(shift_reg, 1)  //Shift the bit left one place.
//shift_reg = shift_reg + shift reg would do the same.
LATB = shift_reg  //Write the shift register bits to the port CHANGED &

//Increment index for next servo time value
index = index + 1

If index < 8 Then  //All servos already done?
    //No, do next servo

    //Set the duration for this output, as a new offset
    //from the last compare time.
    time_set = time_set + servo_times(index)

    CCPR1L = time_set.LB  //Write the compare time to CCP 1 register.
    CCPR1H = time_set.HB

Else
    //No output needed as the port is already updated.
    //output_c(0); // Turn off all servo bits.

    //restarting the sequence.

    time_set = time_set + 1500  //add 1.5mS for the next interrupt
    //Update CCP
    CCPR1L = time_set.LB  //Write the compare time to CCP 1 register.
    CCPR1H = time_set.HB
index = index + 1  '<<<<<<<<<<<<<&
    If index > 15 Then
        //15 * average 1.5mS = 22.5mS, a reasonable frame repeat time
    
        //Set up for a new sequence
        index = 0
        shift_reg = 1  //Set a bit that will be at port bit 0.
    
        time_now.LB = TMR1L  //Get the timer present count
        time_now.HB = TMR1H
        time_set = time_now + servo_times(index)  //And set the first duration
        LATB = shift_reg  //Write the shift register bits to the port CHANGED &
        CCPR1L = time_set.LB  //Write the compare time to CCP 1 register.
        CCPR1H = time_set.HB
    Endif
Endif


Resume
 
Top