PDA

View Full Version : Does my code look alright?



emtark
- 2nd March 2006, 15:53
Hi all,

Quick run down of what im trying to accomplish. New fishtank, want to simulate lighting patterns. I have blue and white LED strips lighting the tank, need to dim these at certain times of the day.

I break the day into 8 sections (3 hrs each obviously)
The idea is for the pic to start, then count up seconds, minutes, hours, then hour sections, and adjust the hpwm output to suit the particular section.

This is what I have come up with so far, if you could comment that would be appreciated. I'm only very new to all of this so if i can learn tips that would be great.

btw: I have listed 2 colours, i'll simply use 2 pics, or try to find a small say 18pin pic with 2 hpwm.


'===================================
'************************************************* ***************
'* Name : UNTITLED.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2006 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 3/03/2006 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************

'define dutycycle variable
dutyCycle var byte


'define clock variables
minute var byte
hour var byte
second var byte
hs var byte
ledblue var byte
ledwhite var byte

'================================================= =====
'start the main segment
main:


'add a second to the counter
second = second + 1
pause 1000

'check to see if 60 seconds have passed
if second = 60 then endsecond


'HPWM Channel,Dutycycle,Frequency
hpwm 1,ledblue,1000

'end of section, repeat main code.
goto main
'================================================= ======

'click 60 seconds to 1 minute
endsecond:
minute = minute + 1
second = 0
if minute = 60 then endminute
return

'click 60 minutes to 1 hour
'also calculates which program section of the day it is
endminute:
hour = hour + 1
minute = 0
if hour = 24 then
hour = 0
end if

if hour = 0 then hs = 0
if hour = 3 then hs = 1
if hour = 6 then hs = 2
if hour = 9 then hs = 3
if hour = 12 then hs = 4
if hour = 15 then hs = 5
if hour = 18 then hs = 6
if hour = 21 then hs = 7

goto selectpwm

return

selectpwm:
select case hs
case 0
ledwhite = 128
ledblue = 75
case 1
ledwhite = 75
ledblue = 128
case 2
ledwhite = 25
ledblue = 50
case 3
ledwhite = 75
ledblue = 50
case 4
ledwhite = 192
ledblue = 50
case 5
ledwhite = 255
ledblue = 36
case 6
ledwhite = 255
ledblue = 25
case 7
ledwhite = 224
ledblue = 50
end select
return
'====================================
'====================================

also with the hpwm, what is the ideal rate for the frequency, that 3rd variable.

Cheers guys, let me know if I need to fix anything :)

Tim.

Dave
- 2nd March 2006, 17:12
emtark, instead of:

if hour = 0 then hs = 0
if hour = 3 then hs = 1
if hour = 6 then hs = 2
if hour = 9 then hs = 3
if hour = 12 then hs = 4
if hour = 15 then hs = 5
if hour = 18 then hs = 6
if hour = 21 then hs = 7

why not:
hs = hour / 3

Dave Purola,
N8NTA

emtark
- 3rd March 2006, 00:19
I want it to keep hs as a whole number.. i.e, for hours 0, 1 and 2, hs should be 1.. then for hours 3,4,5 hs will be 2. If this will work the same way i will change that.

The two things im really worried about are the pause command and the hpwm.

Will that set the pwm output on a specific port (do i have to define the port) to make my led's appear brighter/dimmer?

Thanks again,

Tim.

Melanie
- 3rd March 2006, 07:14
Your code is shot Tim.

Your main loop is fine but... every second you GOTO (not GOSUB) to your endsecond section, which you have terminated with a RETURN.

What's your worry with the PAUSE?

A better way of keeping time would be...

Pause 999
PauseUS 1000

That way, when you've built your program and you time it, and discover you're losing five minutes a day, you'll have a 1uS step per loop adjustment (giving adjustment steps of 86.4mS per day), rather than 1mS (giving adjustment steps of 86.4 seconds per day).

emtark
- 3rd March 2006, 13:57
Hi all, thanks for the quick replies.

I have made some changes, hopefully this looks better. I was also planning on developing this for the pic16f627a, not too worried if i use 2 pics, one for white, one for blue. Just need something that my programmer is compatible with. That specific pic isn't listed under the drop box near the menu, does that mean that it won't work?

Also here is my updated code:



'define oscillator speed
DEFINE osc 20


'define dutycycle variable
dutyCycle var byte


'define clock variables
minute var byte
hour var byte
second var byte
hs var byte
ledblue var byte
ledwhite var byte

'================================================= =====
'start the main segment
main:


'add a second to the counter, and pause
second = second + 1
pause 999
pauseus 1000

'check to see if 60 seconds have passed
if second = 60 then endsecond


'HPWM Channel,Dutycycle,Frequency
hpwm 1,ledblue, 5000

'end of section, repeat main code.
goto main
'================================================= ======

'click 60 seconds to 1 minute
endsecond:
minute = minute + 1
second = 0
if minute = 60 then endminute
goto main


'click 60 minutes to 1 hour
'also calculates which program section of the day it is
endminute:
hour = hour + 1
minute = 0
if hour = 24 then
hour = 0
end if

if hour = 0 then hs = 0
if hour = 3 then hs = 1
if hour = 6 then hs = 2
if hour = 9 then hs = 3
if hour = 12 then hs = 4
if hour = 15 then hs = 5
if hour = 18 then hs = 6
if hour = 21 then hs = 7

goto selectpwm

selectpwm:
select case hs
case 0
ledwhite = 128
ledblue = 75
case 1
ledwhite = 75
ledblue = 128
case 2
ledwhite = 25
ledblue = 50
case 3
ledwhite = 75
ledblue = 50
case 4
ledwhite = 192
ledblue = 50
case 5
ledwhite = 255
ledblue = 36
case 6
ledwhite = 255
ledblue = 25
case 7
ledwhite = 224
ledblue = 50
end select

goto main


Hopefully it looks like it will work.

Thanks again,

Tim.

sayzer
- 3rd March 2006, 15:35
Hi emtark,

Make sure you do not put your pic circuit somewhere near the heater of your tank. If the temp changes couple of degrees per half an hour (or per hour), then tolerance (precision) of your OSC will change a lot and will provide a very fast running timer (or vice versa).

May be you can give a long pause to every hour.

Just a thought.

emtark
- 4th March 2006, 05:43
Hi,

the pic and oscillator will be sitting in a project box under the tank, in the drawer. In terms of coding, I can't seem to get the microcode studio to compile this for me. Can someone do a compilation, into ASM for a pic 16f627a, just so i can make sure the programming works. I'll spend tonight trying to figure out why it isn't compiling.

Hopefully the code will be ok, and I can get the circuit going :D

Cheers guys,

Tim.

btw: for those interested I have just added the water to the tank and it looks sweet!

emtark
- 27th March 2006, 12:47
OK I still can't get this to work.. I'm probably going to have to uninstall and reinstall these programs, what exactly do i need to compile this..

MicroCode Studio complains about ICD or something, and not have PicBasic Pro 2.20 or higher.. where can I get these.

Also if anyone has another quick once-over of the code can they see any errors or why it may not work.

Thanks in advance,

Tim.

Melanie
- 27th March 2006, 13:44
Programs and Upgrades can be obtained from MeLabs... www.melabs.com

Your compiler version is around five years or so old, it would be prudent to upgrade before posting any "my program doesn't work" messages as it's unlikely anyone else has versions that old still installed on their computer to compare against.

emtark
- 29th March 2006, 12:20
OK I deleted everything and i've completely started again. When I attempt to compile my code, im getting "Macro HPWM?TBW not found in macro file."

Any help on this is appreciated :)

oh btw: I'm using pic 16f627A