Hello All,
Wanting to build a christmas garland with leds from an old ten legs of ten leds each just blinking, I reused a modified old blinking software made more than 10 jahres ago. It is a multiple outputs software PWM which worked very fine.
When compiled and loaded in a PIC 16F628A (my preferred old work horse) the b1 output did not work as expected. After about 2 laps of the loop, the output stays in a solid state light ON. First I thought the watchdog was faulty, removing it the problem was still here (some "asm clrwdt" are remaining in the program).. I changed the output and the problem went to the other output. I checked all the routines involved in the program but no "d1" are involved.
I put the output on another line of the program with another variable set (line 145) "d10" n10,k10 instead of
(line 136)
"d1, n1, k1" and all works fine this way. So my attention goes to the compiler.
I join hereunder my program and the old " random3" library and the ref of the compiler used
Patrick
Compiler CommandLine: D:\JAL_V2-2019-06-04\compiler\jalv2_64.exe "D:\JAL_V2-2019-06-04\Prog_Pat\Blink\Noel_guirlande_10 cx.jal" -s "D:\JAL_V2-2019-06-04\lib" -no-variable-reuse
; led common 5V
include 16f628a -- target PICmicro
pragma target OSC INTOSC_NOCLKOUT
-- This program assumes the internal oscillator
-- is running at a frequency of 4 MHz.
pragma target clock 4_000_000 -- oscillator frequency
-- configuration memory settings (fuses)
pragma target WDT disabled -- watchdog
pragma target MCLR internal -- internal
pragma target CP ENABLED
pragma target brownout ENABLED
pragma target LVP disabled -- no Low Voltage Programming
include delay
enable_digital_io()
include random3
randomize(1)
OPTION_REG_NRBPU = low ; pull up enabled
porta_direction = all_output
porta = 255
portb_direction = all_output
portb = 255
pin_a6_direction = output -- pour pallier pb librairie
pin_a7_direction = output -- idem
pin_a0_direction = output -- idem
pin_a1_direction = output -- idem
-- attribution des variables aux pins
var bit Gyro0 is pin_b0
var bit Gyro1 is pin_b1
var bit Gyro2 is pin_b2
var bit Gyro3 is pin_b3
var bit Gyro4 is pin_b4
var bit Gyro5 is pin_b5
var bit Gyro6 is pin_a6
var bit Gyro7 is pin_a7
var bit Gyro8 is pin_a0
var bit Gyro9 is pin_a1
; var bit Warn1 is pin_a2
; var bit Warn2 is pin_a3
; var bit Avert is pin_a1
; var bit Valid_Sound is pin_a5
;
; var byte time = 0
-- var pour les gygrophares
var byte d
var byte dx
var byte d0
var byte d1
var byte d2
var byte d3
var byte d4
var byte d5
var byte d6
var byte d7
var byte d8
var byte d9
var byte d10
var byte nx = 0
var byte n0 = 0;1;23
var byte n1 = 0;2;7
var byte n2 = 0;3
var byte n3 = 0;4;15
var byte n4 = 0;5;1
var byte n5 = 0;6;27
var byte n6 = 0;7;0
var byte n7 = 0;8;19
var byte n8 = 0;9;19
var byte n9 = 0;10;19
var byte n10 = 0
var sbyte k0 = 1
var sbyte k1 = 1
var sbyte k2 = 1
var sbyte k3 = 1
var sbyte k4 = 1
var sbyte k5 = 1
var sbyte k6 = 1
var sbyte k7 = 1
var sbyte k8 = 1
var sbyte k9 = 1
var sbyte k10 = 1
; var bit a1 = low
; var bit a0 = low
; var bit a7 = low
; var bit a6 = low
; var bit data
procedure calcul (byte in nx, byte out dx) is
; if nx < 21 then dx = 0 end if
; if ( nx >= 21 ) & ( nx < 32 )
; then dx = (nx - 19) * (nx - 19) / 4 end if
; if nx >= 31 then dx = 32 end if
if nx<= 2 then dx = 2 else dx = nx end if
end procedure
procedure gyro is
for 1000 loop
asm clrwdt
-- boucle de comptage pour les steps du gyro
if n0 >= 63 then k0 = -1 end if
if n1 >= 63 then k1 = -1 end if
if n2 >= 63 then k2 = -1 end if
if n3 >= 63 then k3 = -1 end if
if n4 >= 63 then k4 = -1 end if
if n5 >= 63 then k5 = -1 end if
if n6 >= 63 then k6 = -1 end if
if n7 >= 63 then k7 = -1 end if
if n8 >= 63 then k8 = -1 end if
if n9 >= 63 then k9 = -1 end if
if n10 >= 63 then k10 = -1 end if
-- calcul des valeurs pour chaque output
nx = n0 calcul (nx, dx) d0 = dx
nx = n1 calcul (nx, dx) d1 = dx
nx = n2 calcul (nx, dx) d2 = dx
nx = n3 calcul (nx, dx) d3 = dx
nx = n4 calcul (nx, dx) d4 = dx
nx = n5 calcul (nx, dx) d5 = dx
nx = n6 calcul (nx, dx) d6 = dx
nx = n7 calcul (nx, dx) d7 = dx
nx = n8 calcul (nx, dx) d8 = dx
nx = n9 calcul (nx, dx) d9 = dx
nx = n10 calcul (nx, dx) d10 = dx
d = 1
for 63 loop -- durée ON fonction de la valeur de la table
-- ajout delay ici si nécessaire
_usec_delay(100)
if d0 > d then Gyro0 = 0 else Gyro0 = 1 end if
; if d1 > d then Gyro1 = 0 else Gyro1 = 1 end if
if d2 > d then Gyro2 = 0 else Gyro2 = 1 end if
if d3 > d then Gyro3 = 0 else Gyro3 = 1 end if
if d4 > d then Gyro4 = 0 else Gyro4 = 1 end if
if d5 > d then Gyro5 = 0 else Gyro5 = 1 end if
if d6 > d then Gyro6 = 0 else Gyro6 = 1 end if
if d7 > d then Gyro7 = 0 else Gyro7 = 1 end if
if d8 > d then Gyro8 = 0 else Gyro8 = 1 end if
if d9 > d then Gyro9 = 0 else Gyro9 = 1 end if
if d10 > d then Gyro1 = 0 else Gyro1 = 1 end if
d = d + 1
end loop
n0 = n0 + k0 if n0 == 0 then k0 = 1 n0 = 0 end if
n1 = n1 + k1 if n1 == 0 then k1 = 1 n1 = 1 end if
n2 = n2 + k2 if n2 == 0 then k2 = 1 n2 = 2 end if
n3 = n3 + k3 if n3 == 0 then k3 = 1 n3 = 3 end if
n4 = n4 + k4 if n4 == 0 then k4 = 1 n4 = 4 end if
n5 = n5 + k5 if n5 == 0 then k5 = 1 n5 = 5 end if
n6 = n6 + k6 if n6 == 0 then k6 = 1 n6 = 6 end if
n7 = n7 + k7 if n7 == 0 then k7 = 1 n7 = 7 end if
n8 = n8 + k8 if n8 == 0 then k8 = 1 n8 = 8 end if
n9 = n9 + k9 if n9 == 0 then k9 = 1 n9 = 9 end if
n10 = n10 + k10 if n10 == 0 then k10 = 1 n10 = 1 end if
end loop
end procedure
procedure tempo_F is
asm clrwdt
_usec_delay(500_000)
end procedure
procedure tempo_Fast is
asm clrwdt
_usec_delay(200_000)
end procedure
procedure flash_seq is
for 8 loop ; environ 16 sec
asm clrwdt
Gyro9 = 1 Gyro0 = 0 ; 1: éteint, 0 allumé
tempo_Fast
Gyro0 = 1 Gyro1 = 0
tempo_Fast
Gyro1 = 1 Gyro2 = 0
tempo_Fast
Gyro2 = 1 Gyro3 = 0
tempo_Fast
Gyro3 = 1 Gyro4 = 0
tempo_Fast
Gyro4 = 1 Gyro5 = 0
tempo_Fast
Gyro5 = 1 Gyro6 = 0
tempo_Fast
Gyro6 = 1 Gyro7 = 0
tempo_Fast
Gyro7 = 1 Gyro8 = 0
tempo_Fast
Gyro8 = 1 Gyro9 = 0
tempo_Fast
end loop
end procedure
procedure flash_full is
portb = 255
porta = 255
for 20 loop ; environ 15 sec
asm clrwdt
portb = !portb
porta = !porta
tempo_F
end loop
end procedure
procedure flash_rand is
for 300 loop ; environ 15 sec
asm clrwdt
portb = random_byte
porta = random_byte
_usec_delay(50_000)
end loop
end procedure
forever loop
gyro
flash_seq
flash_rand
Flash_full
end loop
--- file : ramdom3.jal
-- author : Wouter van Ooijen
-- date : 17-DEC-1998
-- purpose : pseudo-random number based on a 24-bits LFSR
-- requires : -
--
-- Copyright (C) 1998 Wouter van Ooijen
--
-- This library is free software; you can redistribute it and/or
-- modify it under the terms of the GNU Library General Public
-- License as published by the Free Software Foundation; either
-- version 2 of the License, or (at your option) any later version.
--
-- This library is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-- Library General Public License for more details.
--
-- You should have received a copy of the GNU Library General Public
-- License along with this library; if not, write to the
-- Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-- Boston, MA 02111-1307, USA.
-
var byte _random_b1
var byte _random_b2
var byte _random_b3
-- make sure there is at least one bit set!
asm bsf _random_b1, 0
procedure _random_shift is
-- from the art of electronics, p657:
-- 24 bits LFSR needs xor feedback from taps at 17, 22 and 23
assembler
movlw 0
bank btfsc _random_b3, 1
xorlw 0xFF
btfsc _random_b3, 6
xorlw 0xFF
btfsc _random_b3, 7
xorlw 0xFF
addlw 1
bank rlf _random_b1, f
bank rlf _random_b2, f
bank rlf _random_b3, f
end assembler
end procedure
var bit _random_bit at _random_b1 : 0
var byte _random_byte at _random_b1
procedure randomize( byte in n) is
_random_b1 = n
_random_b2 = 1
_random_b3 = 0
for 24 loop
_random_shift
end loop
end procedure
function random_bit return bit is
_random_shift
return _random_bit
end function
function random_byte return byte is
for 8 loop
_random_shift
end loop
return _random_byte
end function