Skip to content

Instantly share code, notes, and snippets.

@TG9541 TG9541/solar.fs
Last active Sep 24, 2018

Embed
What would you like to do?
Simple solar heating controller based on a MINDEV board
\res MCU: STM8S103
\res export PA_ODR
\res export PA_DDR
\res export PA_CR1
\res export PC_DDR
\res export PC_CR1
#require ]B!
1 CONSTANT _COL \ PA1 low side KTY10 solar collector
2 CONSTANT _STO \ PA2 low side KTY10 storage tank
3 CONSTANT _REL \ PA3 Relais
4 CONSTANT _AIN \ PC4/AIN2 ref=2000R to VCC
700 CONSTANT MAXTEMP \ safe storage temp
NVM
#require @inter
VARIABLE LPFCOL \ LPF value collector
VARIABLE LPFSTO \ LPF value storage
VARIABLE VALCOL \ collector value
VARIABLE VALSTO \ storage value
VARIABLE CSDIFF \ collector-storage diff (>0: col is warmer)
VARIABLE PUMPON \ current pump activation state
VARIABLE THRESH \ threshold value
VARIABLE HYSTER \ hysteresis value
\ interpolation table ADC digits * 32 -> temperature * 10
CREATE dig2tem 14 , \ number of value pairs
\ KTY10 datasheet, ADC @ 2000R to VCC
\ i T kT RT adcT adcT*32
12603 , -300 , \ 1 -30 0.625 1250 394 12603
13321 , -200 , \ 2 -20 0.685 1370 416 13321
14022 , -100 , \ 3 -10 0.748 1496 438 14022
14714 , 0 , \ 4 0 0.815 1630 460 14714
15394 , 100 , \ 5 10 0.886 1772 481 15394
16058 , 200 , \ 6 20 0.961 1922 502 16058
16705 , 300 , \ 7 30 1.040 2080 522 16705
17333 , 400 , \ 8 40 1.123 2246 542 17333
17934 , 500 , \ 9 50 1.209 2418 560 17934
18532 , 600 , \ 10 60 1.300 2600 579 18532
19080 , 700 , \ 11 70 1.394 2788 596 19080
19619 , 800 , \ 12 80 1.492 2984 613 19619
20136 , 900 , \ 13 90 1.594 3188 629 20136
20632 , 1000 , \ 14 100 1.700 3400 645 20632
: lpf32 ( n1 a -- n2 )
\ low pass filter, multiplies n1 by 32, uses a as LPF memory
( a ) DUP >R @ DUP 32 / - + DUP R> !
;
: init ( -- )
[ 0 PA_ODR _COL ]B! \ open drain output
[ 1 PA_DDR _COL ]B!
[ 0 PA_CR1 _COL ]B!
[ 0 PA_ODR _STO ]B! \ open drain output
[ 1 PA_DDR _STO ]B!
[ 0 PA_CR1 _STO ]B!
[ 1 PA_DDR _REL ]B! \ PA RELais output
[ 1 PA_CR1 _REL ]B! \ push-pull output
[ 0 PC_DDR _AIN ]B! \ PC4 AIN2 as input
[ 0 PC_CR1 _AIN ]B! \ no pull-up
0 PUMPON ! 10 THRESH ! 30 HYSTER !
;
: REL.on ( -- )
[ 1 PA_ODR _REL ]B!
;
: GetAin ( -- n )
2 ADC! ADC@
;
: REL.off ( -- )
[ 0 PA_ODR _REL ]B!
;
: SetCol ( -- )
[ 1 PA_DDR _COL ]B! [ 0 PA_DDR _STO ]B!
;
: SetSto ( -- )
[ 0 PA_DDR _COL ]B! [ 1 PA_DDR _STO ]B!
;
: measure ( -- ) \ background task
\ get collector and storage in odd and even cycles
GetAin ( ain ) TIM 1 AND 0= IF
SetSto \ switch to storage sensor
LPFCOL lpf32 dig2tem @inter VALCOL !
ELSE
SetCol \ switch to collector sensor
LPFSTO lpf32 dig2tem @inter VALSTO ! THEN
VALCOL @ VALSTO @ - CSDIFF !
;
: act
VALCOL ?
VALSTO ?
CSDIFF @ DUP . CR
( cs-diff ) THRESH @ - \ apply activation threshold
( diff ) PUMPON @ 0= IF
HYSTER @ - THEN \ apply hysteresis if pump is off
MAXTEMP VALSTO @ < IF
( diff ) DROP -1 THEN \ limit storage temperature
( limdiff ) 0< IF
REL.off 0 PUMPON !
ELSE
REL.on 1 PUMPON ! THEN
;
: start ( -- )
init
[ ' measure ] LITERAL BG !
BEGIN
act
?KEY IF 13 = ELSE 0 THEN
UNTIL
;
' start 'BOOT !
RAM
@TG9541

This comment has been minimized.

Copy link
Owner Author

commented Sep 16, 2018

Solar collector and hot water storage have a KTY10 sensor each. Both sensors are in a voltage divider with a 2000R 1% "reference resistor" (the accuracy is not important because a robust ratiometric comparator is all that's needed).

Features:

  • linearisation and low pass filter for both sensors
  • robust measurement in a background task
  • safety limit for the water temperature in the storage

The compiled code requires about 820 bytes (> 2500 bytes free when the MINDEV binary is used)

Sensor circuit:

(VCC)
   |
 2000R
   |
   *------*---(PC4)
   |      |
 KTY10  KTY10
  COL    STO
   |      |
 (PA1)  (PA2)

There is a project on Hack-A-Day to track the progress.

There is also a Gist that adds an nRF24L01 module for wireless transmission of the solar collector and storage temperatures.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.