Model Boat Mayhem

Please login or register.

Login with username, password and session length.
Pages: 1 2 3 4 [5] 6 7   Go Down

Author Topic: Help writing pic code  (Read 32009 times)

flashtwo

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 521
  • Location: Sevenoaks, Kent, England
Re: Help writing pic code
« Reply #100 on: September 22, 2014, 10:03:55 pm »

Hi Stew,

Congratulations!

The learning curve has gone from the vertical to leaning towards the horizontal!

What made it start to work? Was it the EQU values being defined in Hexadecimal or the change in the config from H'34C4' to H'30C4' i.e. the IESO being disabled.

Ian
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #101 on: September 22, 2014, 10:12:28 pm »

Hi Ian
I'm not sure, I changed them and removed the 20ms loop at the same time.
I'll have another play tomorrow and try to figure out what is going on.
Cheers
Stew
Logged

flashtwo

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 521
  • Location: Sevenoaks, Kent, England
Re: Help writing pic code
« Reply #102 on: September 23, 2014, 12:04:20 am »

Hi Stew,

I was intrigued with your WAIT1MS routine and have just realised that you are adding the 2's compliment of 255d, i.e. -1d, to do the decrement 250 times and then executing 4 instructions in the LOOP1MS to give 4us. Thus, 250d x 4 = 1000us = 1ms.

Ian
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #103 on: September 23, 2014, 07:40:39 am »

Hi Ian
I'm afraid I can't take any credit for it. I borrowed the routine from the Mark Hennessy website given to me by Mike.
Cheers
Stew
Logged

flashtwo

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 521
  • Location: Sevenoaks, Kent, England
Re: Help writing pic code
« Reply #104 on: September 23, 2014, 08:16:52 am »

Hi Stew,

This is the stage where connecting an LED to one of the outputs can help in finding any bugs in the code (680ohm in series with LED).

The LED can be switched on (BSF) at the start and then move the switch off (BCF) instruction to parts of the code where there may be a problem - its like playing "Battleships"!

Ian
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #105 on: September 23, 2014, 10:00:21 am »

Hi Ian

I'll take the advice of adding LEDs at various places.
I found an error in the equates section, I had the wrong address for INTCON.

The next stage is to put the 1ms delay into a 10ms loop using TMR0. I'm going to use 2 x 10ms loops instead of a 20ms loop, that way both servos will be inside their own loop and be totally independent.

Stew
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #106 on: September 23, 2014, 02:26:05 pm »

we're flying now

the following code gives a beautiful 1ms pulse every 20ms.
Just need to add some inputs and we're home and dry.

; EQUATES SECTION

TMR0        EQU        H'01'    ;means TMR0 is file 1.   
STATUS      EQU        H'03'    ;means STATUS is file 3.   
PORTA       EQU        H'05'    ;means PORTA  is file 5.   
PORTC       EQU        H'07'    ;means PORTB is file 7.
INTCON      EQU        H'0B'    ;INTCON is file 0BH
TRISA       EQU        H'85'    ;TRISA (the PORTA I/O selection) is file 85H
TRISC       EQU        H'87'    ;TRISC (the PORTC I/O selection) is file 87H
OPTION_REG  EQU        H'81'    ;the OPTION register is file 81H   
ZEROBIT     EQU        H'02'    ;means ZEROBIT is bit 2.   

;*********************************************************

    LIST    P=16F684    ; we are using the 16F684.
    ORG        0            ; the start address in memory is 0
    GOTO    START        ; goto start!

;**********************************************************************
; Configuration Bits
    __CONFIG    H'30C4'

;*********************************************************
; SUBROUTINE SECTION.

WAIT1MS       
    MOVLW    d'250'        ;Initial value - tweak if req.
    NOP
LOOP1MS   
    ADDLW    d'255'        ;dec W
    BTFSS    STATUS,2    ;Zero flag set?
    GOTO    LOOP1MS        ;No, keep looping
    RETURN                ;Yes, 1ms done


; CONFIGURATION SECTION

START   
    BANKSEL    TRISA   
    MOVLW   B'00011111'    ;5bits of PORTA are I/P
    MOVWF   TRISA
    CLRF    TRISC        ;PORTC is OUTPUT
    BANKSEL    PORTA
    CLRF    PORTA        ;Clears PortA.
    CLRF    PORTC        ;Clears PortC.
   



; Program starts now.

BEGIN
    MOVLW    d'217'        ;256-217=39 LOOPS
    MOVWF    TMR0   
    BANKSEL    OPTION_REG
    MOVLW   B'00000111'    ;Prescaler is /256
    MOVWF   OPTION_REG
    BANKSEL    PORTC
    BSF        PORTC,0
    CALL    WAIT1MS
    BCF        PORTC,0

WAIT10MS
    BTFSS    INTCON,2    ;wait until timer0 reaches 10ms   
    GOTO    WAIT10MS
    BCF        INTCON,2
    GOTO    BEGIN2        ;next loop

BEGIN2
    MOVLW    d'217'        ;256-217=39 LOOPS
    MOVWF    TMR0   
    BANKSEL    OPTION_REG
    MOVLW   B'00000111'    ;Prescaler is /256
    MOVWF   OPTION_REG
    BANKSEL    PORTC
   
WAIT10MS2
    BTFSS    INTCON,2    ;wait until timer0 reaches 10ms   
    GOTO    WAIT10MS2
    BCF        INTCON,2
    GOTO    BEGIN        ;keep looping
   
    END

Cheers
Stew
Logged

TurboTyne

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 115
  • Location: Newcastle upon Tyne, England
Re: Help writing pic code
« Reply #107 on: September 23, 2014, 02:44:43 pm »

Hi Stew

That's great - well done!

I've been checking the messages in this thread but have not had anything to add over what Ian has been saying.

Now you have a PIC that is doing something I am sure you will soon start thinking of numerous tweaks etc.
Isn't it a great feeling when it works!

Like Ian, I do much of my de-bugging with LEDs attached to one or two pins. I have a few small subroutines that turn the leds on or off or flash them. By moving calls to these subroutines around the code it is easy to track where the programme is going before it hits the problem one is trying to solve.

Regards

Mike
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #108 on: September 23, 2014, 03:12:02 pm »

Hi Mike
Yes I found using an LED very helpful, unfortunately when I first connected it I forgot the resistor and I've blown 4 of the I/O ports on the chip. Fortunately the others still work.
Like you say it is a great feeling when things work.
cheers
Stew
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #109 on: September 24, 2014, 11:41:11 am »

Hi
I've managed eventually to get a servo to move right or left with the push of a button.
It seems that I didn't fry the i/o ports after all, nobody mentioned the CMCON0 or ANSEL registers.
These little chips will be the death of me.
cheers
Stew
Logged

TurboTyne

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 115
  • Location: Newcastle upon Tyne, England
Re: Help writing pic code
« Reply #110 on: September 24, 2014, 01:28:26 pm »

Ah, Ian said your learning curve was no longer vertical, he didn't say it was horizontal  ok2

Glad you've not blown those outputs but that possibility reminds me of a major problem I encountered a few months back.

I use serial communications between several PICs whereby a pin on each can TX or RX data pulses to the equivalent pin on the other PICs. It took me over a week of testing, using the scope and eventually reverting to simple code and LED outputs / switched inputs. What I concluded was that the ability of the data pin on each of 3 PICs to sense the input had been blown but the output function was still working. I think I inadvertently connected a 12V power wire to the wire that connects all the data pins together. The fact that output was working on the same pin that input had failed really confused me. I had to replace 3 PICs! - luckily the other PICs were not connected at the time. I'll be more careful in future (I hope). 

Mike



Logged

flashtwo

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 521
  • Location: Sevenoaks, Kent, England
Re: Help writing pic code
« Reply #111 on: September 24, 2014, 01:36:06 pm »

Hi Stew,

Congratulations, once again! It sounds like you are well underway now, progressing through all the available features.

It was fortuitous that you didn't burn out the I/O.

Addictive isn't it!

Ian
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #112 on: September 24, 2014, 01:50:37 pm »

Hi Mike

I've already blown 1 chip, I use a variable power supply and when I moved it onto my bench I managed to turn the voltage up to 20v. There was no question about if I had damaged the chip, you could see through it.

Yes Ian it is addictive, it's good to get the grey matter working again.

Next task is to make the servo move using a pot, any pointers in advance would be most appreciated.

cheers
Stew
Logged

flashtwo

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 521
  • Location: Sevenoaks, Kent, England
Re: Help writing pic code
« Reply #113 on: September 24, 2014, 03:05:38 pm »

Hi Stew,

This is where I used the 2 byte Timer 1 (TMR1), since it has the resolution to give you the range of in excess of 2000.

Configure it to count at the Internal clock rate of 1 microsecond (1us) and initialise its registers with H'F8' into the high byte and H'2F' into the low byte - this is 63535 in decimal. When you start the clock it will count up to 65535d (H'FFFF'), roll-over to zero and issue an interrupt (sorry) 2000us (2ms) later.

If you set a digital output when you start the clock and reset the output when you receive the interrupt, it will give you a 2ms pulse. This will be within the 20ms code to give a pulse every 20ms.

Now to get 1ms pulse, the timer is initialised with 64535d in it registers and the interrupt will occur after the 1ms.

As you can see, the smaller the value you initialise the Timer with, the longer the pulse width.

The 10 bit analogue input will give you a value from zero to 1023 (that's very convenient!), so if you SUBTRACT that value from 64535 it will give you a pulse range from 2023 to 1000us- voila!

As might appreciate the numbers 64535 and 63545 are not cast in concrete and may be changed to give the servo a greater range than the usual 90deg movement by applying pulses say, from 0.7ms to 2.5ms. This can be very useful if the mechanical link between the servo and, say, the gas valve does not shut the valve completely at the normal 1 or 2ms settings (depends on your mechanical arrangement - my valve is nominally shut at 2ms). If the valve is not shut at 2ms the servo may need to be presented with a 2.1ms pulse (for example).

To get the most benefit from this, you might in the near future consider using a 4MHz crystal as the system clock - it will give you the better accuracy and stability especially when you are trying to adjust the pilot flame. For the moment the internal clock is good enough.

Now you need to practice subtracting two 2-byte numbers.

If you feel that a lower resolution will do (1:255 resolution), then the single byte timers could be used, its just the pilot light I'm thinking of.

Ian
Logged

flashtwo

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 521
  • Location: Sevenoaks, Kent, England
Re: Help writing pic code
« Reply #114 on: September 24, 2014, 03:45:24 pm »

Hi Stew,

If you don't want to do subtractions, you could always just use additions, viz:

Timer setting =  pot value (0-1023) + "2ms" value.

The 2ms value could be nominally 63535d and adjustable.

Ian

Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #115 on: September 24, 2014, 05:05:27 pm »

Hi Ian
That's a good idea if I want a high resolution.
I was thinking of using the 8 bits in ADRESH and simply substituting them for the "250" in the wait 1ms routine. Just to test the theory.

I was also thinking that for the pilot flame adjustment I could use the 5 most significant bits of ADRESH instead of the "250" which would give 11.5deg movement over 32 steps which may be good enough.

thoughts
Stew
Logged

flashtwo

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 521
  • Location: Sevenoaks, Kent, England
Re: Help writing pic code
« Reply #116 on: September 24, 2014, 05:36:38 pm »

Hi Stew,

My gas valve, which has a 40TPI thread on the valve stem going to a simple conical plug seat, is driven by the servo via dog-tooth coupling. The range is nominally 1000 (open) to 2000us (closed) and I increment/decrement the completely shut position by steps of 10us, though the rest of the proportional control range moves in 1us steps.

One 10us step is enough to just open the valve for a very small flame. If I try to over-close I can hear the servo stalling.

I think a 0.5% resolution is desirable, but it all depends on the valve design.

250 steps would give you 0.4% resolution, although each step would represent 8us for a range of 2000us.

I think your method stands a good chance of working - give it bash - you can always download a different version, which is the beauty of PICs.

What's your gas valve arrangement?

Ian


Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #117 on: September 24, 2014, 10:55:03 pm »

Hi Ian
At the moment I am using a Cheddar ABC gas valve which has the pilot flame adjustment built in.
My current units have been used with standard off the peg servo gas valves but they are analogue hence infinitely variable.
Hopefully this will work but like you say if it doesn't I can easily change the software.

Stew
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #118 on: September 26, 2014, 09:42:20 am »

Hi

For the pilot flame adjustment I am going to use a pot with 2 legs connected to supply voltage and ground with the wiper connected to input pin. The datasheet recommends a maximum impedance of 10K but doesn't say what is ideal. Has anyone used a pot in this situation and what values did you find worked well?
For the boiler pressure input this is basically a voltage divider with two resistances of 15k, not ideal I know, but that's what I'm stuck with. My question is how do I calculate the acquisition time?
I've seen the formula in equation 9-1 page 72 of the datasheet but that's beyond rocket science. Would a 1ms delay be enough?
The curve is heading back to the vertical.

cheers
Stew
Logged

flashtwo

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 521
  • Location: Sevenoaks, Kent, England
Re: Help writing pic code
« Reply #119 on: September 26, 2014, 10:50:34 am »

Hi Stew,

As it says in section 9.3, the A/D convertor has a 5pF capacitor (Fig. 9.4) that has to be charged up and the source resistance Rs controls its charging rate.

In our case the voltage signal is slowly changing compared with, say, an audio input, so that the capacitor will follow our analogue input.

I think you can easily get away with the 15k circuit.

For my boiler application, I calculated (back in the mists of time) the acquisition time of approximately 20us, so, in my code I've allowed a 50us delay between setting ADCON0 for Fosc/32 clock, channel number etc., and starting the A/D conversion AD_GO.

I'd follow the example in 9.2.6 with Fosc/32 and 50us waits.  I used waits and not interrupts, I can't remember why.

Ian
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #120 on: October 03, 2014, 06:16:06 pm »

Hi All
Well after much head scratching I have now got it so that the servo moves left and right with the push of a button, and the right hand position adjustable with a pot.
There is however a small problem. If the voltage to the analogue input drops below about 170mV the servo starts glitching, and if the voltage drops below 140mV the servo shoots off and gives maximum left hand rotation.
I have tried various delays for the acquisition from 0 to 1ms but this makes no difference. I can remedy this by keeping the voltage above 170mV but I was wondering why this happens.
Below is the code for my delays

; SUBROUTINE SECTION.

SAMPLETIME
    MOVLW    d'20'        ;initial value - tweak if required
LOOPST
    ADDLW    d'255'        ;dec W
    BTFSS    STATUS,2    ;zero flag set?
    GOTO    LOOPST        ;no, keep looping
    RETURN                ;yes, sampletime done

PILOT
    BANKSEL    ADCON1
    MOVLW    B'00010000'    ;adc frc clock
    MOVWF    ADCON1
    MOVLW    B'00000100'
    MOVWF    ANSEL        ;an2 set to analogue
    BANKSEL    ADCON0
    MOVLW    B'00001001'    ;left justify,vdd ref,an2,on
    MOVWF    ADCON0
    ;CALL    WAIT1MS
    ;CALL    SAMPLETIME
    BSF    ADCON0,1
    BTFSC    ADCON0,1
    GOTO    $-1
    BCF    STATUS,0    ; use 5 most
    RRF    ADRESH,1    ; significant
    BCF    STATUS,0    ; bits of
    RRF    ADRESH,1    ; adresh
    BCF    STATUS,0    ; to give 5 bit
    RRF    ADRESH,1    ; resolution
    BCF    STATUS,0    ;
    MOVF    ADRESH,W    ;
LOOPP
    BSF    PORTA,0
    ADDLW    d'255'        ;dec W
    BTFSS    STATUS,2    ;zero flag set?
    GOTO    LOOPP        ;no, keep looping
    RETURN            ;yes pilot delay done

WAIT1MS       
    MOVLW    d'250'        ;Initial value - tweak if req.
    NOP
LOOP1MS   
    ADDLW    d'255'        ;dec W
    BTFSS    STATUS,2    ;Zero flag set?
    GOTO    LOOP1MS        ;No, keep looping
    RETURN            ;Yes, 1ms done

Cheers
Stew
Logged

flashtwo

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 521
  • Location: Sevenoaks, Kent, England
Re: Help writing pic code
« Reply #121 on: October 03, 2014, 06:50:29 pm »

Hi Stew,

Good to see that you are progressing, even with a few glitches.

Just a quick thought -

5 bit resolution gives you 1 part in 32.

5 volts divided by 32 gives you 156mV.

Have you looked at the PWM with the 'scope?

I will delve deeper.

Ian.
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #122 on: October 03, 2014, 06:59:49 pm »

Hi Ian
I haven't had a chance to get the scope out yet.
I wonder if the voltage is going so low that the ADCON0,1 bit is never getting cleared.
Stew
Logged

TurboTyne

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 115
  • Location: Newcastle upon Tyne, England
Re: Help writing pic code
« Reply #123 on: October 03, 2014, 07:02:26 pm »

Hi Stew

Yes, I was going to make a similar comment to Ians re: the 5 bit resolution. I am thinking that, as your input voltage drops to about 170 mV the upper 5 bits from the ADC will equal a mix of values of zero and 1 as random noise takes the result above or below the cut-off for detection on each ADC event. Does this cause the glitches? As the voltage drops lower, the result will be consistent zeros. So I am wondering - how do you use the value from the ADC? Can your programme deal with a zero value ? 

Regards  Mike
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #124 on: October 03, 2014, 07:12:56 pm »

Hi Mike
The analogue input is dealt with by the PILOT subroutine and it looks like a 0v input would give me about a 1ms rather than a 0ms delay, however the servo is going further than this as it's hitting the stops.
I'll put the scope on tomorrow when I finish work.
Cheers
Stew
Logged
Pages: 1 2 3 4 [5] 6 7   Go Up
 

Page created in 0.095 seconds with 22 queries.