Friday, 11 October 2013

Useful pdf about interrupts and clocks

Found a useful reference for interrupts and clocks with good examples of setting all the bits to get them working properly. Handy if you've not done it for a couple o'years like me.
But watch out, the = print as - in Mozilla PDF reader!

Thursday, 10 October 2013

NXP signalling the AVR

I spent an eternity reading PortD instead of PinD. Thank goodness for the power of web searching. Shame everyone programs in C. They are scared of the power.

NXP code


DigitalOut m1(LED1);
DigitalOut m2(LED2);

DigitalInOut DATA(p21);
DigitalInOut CTL(p23);

void set_data(unsigned char i) {
    DATA.write(i);
    m1.write(i);
}

void set_ctl(unsigned char i) {
    CTL.write(i);
    m2.write(i);
}

int main() {
    CTL.output();
    DATA.output();
    
    while(1) {
        set_data(0);
        set_ctl(1);
        wait(3);
        set_data(1);
        set_ctl(0);
        wait(2);
    }
 }

AVR Code

.include "m16def.inc"

; set portB to output
ser r16
out ddrb, r16
clr r16
out portb, r16

; set portD to input with pull-ups
out portd, r16
out ddrd, r16
ser r16
out portd, r16

; do the do
main:
    in r16, pind
    out portb, r16
    rjmp main

Wednesday, 9 October 2013

AVR signalling the NXP

The NXP code is pretty basic

#include "mbed.h"

DigitalOut led(LED1);
DigitalIn DATA(p23);

int main() {
    
    while(1) {
        led.write(0);
        while(DATA);
        led.write(1);
        while(!DATA);
    }
}

The AVR code was a bit trickier as it uses the timer interrupt. Took me a while to get my head round it

.include "TN13DEF.INC"

.org 0000   ;  reset interrupt vector
  rjmp on_reset

.org 0012   ;  timer0 overflow interrupt vector
  rjmp tim0_overflow

on_reset:
  ser r16
  out ddrb, r16 ; set the pin I/O

  ldi r16, 0b_0000_0101 ; set the clock multiplier
  out tccr0b, r16

  ldi r16, 0b_0000_010 ; turn the timer on
  out timsk0, r16

  sei ; turn on interrupts

main:
  nop ; I walk and walk, do nothing 
  rjmp main

tim0_overflow:
  ; negate portb
  in r17, portb
  com r17
  out portb, r17
  reti

Getting the AT-Tiny working with the STK500

Fairly simple, follow online instructions.

  1. AT-TINY13 in SCKT3400D1
  2. Connect the ISP6PIN socket to SPROG1 with the 6-pin cable provided
  3. Connect the PORTE/RST to PORTB/PB5 and PORTE/XT1 to PORTB/PB3
  4. Jumpers VTARGET, AREF, RESET, XTAL1, OSCSEL close; others open

Using it is a bit annoying because you have to take the cables on and off to use the pins.

Tuesday, 8 October 2013

Decisions Decisions - Smart or Stupid Pumps?

Do I put the control next to the pumps and free up the controller to just send messages or do I keep them dumb and use the controller to do everything ?

The programmer in me wants to put an AT-TINY13 at every pump and let them do the delivery. OTOH guy says put everything in the middle because there's so much spare CPU power.

An AT-Tiny is about £1. It is attractive to me to make intelligent pumps. But it means programming the AVR and whatever I use as the controller and get them to talk to each other. But this also means that the controller can be less powerful and all it needs to do is talk whatever protocol I devise. A rich protocol, which could even be 9p, seems to solve engineering problems. By standardising with this idea I can expand the range of devices in the family as I go along. I will only have to implement the protocol on the central controller once and not care about that is attached to it.
I'm looking for excuses to make smart pumps. The action at the pump end will be the same whatever controller is used, whether it is the central one or not is irrelevant. If I start with AVR assembler to control the pumps I can always use an AVR CPU to do it and they are not rare.
There we go, that is what I shall do.

Time to Propose a Protocol

At the moment I have a 3 wire serial system. 1 GND, one marked TX and one RX. I need to solidify this idea in text, write some code and test it in real hardware. If things don't go well the next option is to add another wire, which I still have space for if I stick to three peristaltic pumps. Which is no problem for this project. L293D's are under £2 each.

After a couple of days cogitating I have made a decision. I considered a variable length with the msb marking another byte like in UTF-8. However this is over complicated for this little job so I have decided on a fixed length scheme.

One possibility.

The minimum number size bound the amount of liquid in millilitres.
1111 1111 1111 1111 is 65535 which is 65.535 litres. This fits the bill for the 30 litres in the specification.
So the protocol, at the moment, is three bytes long
aaaa aaaP - action and parity bit
bbbb bbbb - lower bits of value
cccc cccc - upper bits of value

The next idea

I realised that it is unlikely that a pump will get a 1.01L instruction. They will usually get 10ml or 10L. However there may be the possibility that the controller has a small and large pump on the same L239D. So let's encode the magnitude in the action, I have plenty of those to go round and the amount can just be a single byte.
aaaa aaaP - action and parity bit
vvvv vvvv - value
This is the most satisfying to me. A message will then fit into a 16 bits.

The Protocol v0.1

ActionValueDescription
0000 00001010 1010All Off
0000 0001vvvv vvvvPump 1, v ml
0000 0010vvvv vvvvPump 2, v ml
0000 0100vvvv vvvvPump 3, v ml
0000 1000vvvv vvvvPump 4, v ml
0001 0000vvvv vvvvPump 1, v L
0010 0000vvvv vvvvPump 2, v L
0100 0000vvvv vvvvPump 3, v L
1000 0000vvvv vvvvPump 4, v L
1111 11111010 1010All on until further notice

The values in the All On and All Off are to make sure that a signal is being sent and not just some line noise.

Wednesday, 2 October 2013

Aceeed

There are three acids listed in Resh's hydroponic book

  • Nitric acid
  • Sulfuric acid
  • Hydrochloric acid
  • Phosphoric acid

The trouble will be to find a suitable hose that is acid resistant.

The peristaltic pumps come with silicone tubing and for this the news is not so good. Because of that I have included citric acid in the list.

Acid 20 °CPVCSilicone
Nitricup to 50%Maybe - test it
Sulfuric up to 95%No
Hydrochloric50% - concentratedNo
Phosphoricup to 85%Maybe - test it
Citric10% - SaturatedExcellent

It seems a citric acid solution is preferable for my uses.