Strange code

djmikeys

New Member
Hi I wonder if anyone can tell me why my code is mis-behaving. I have a light sensor controlling the brightness of some LED's. I am using the 28x1 chip because I need 2 pwm outputs. As the light dims the LEDs fade off but after they have faded off they then start flashing full brightness - I dont want this, i just want them to stay off. I have put constraints on b1 so when there is no light the lights should just stay off I have tried putting constraints on both b1 and w1 but it still goes weird. When there is no light w1 starts giving values of around 65500. The LDR is connected to ADC1 and the LEDs to PWM1 (PORTC 1). Anyway here is the code:
-----------------------------------

main:

readadc 1, b1
debug b1

w1 = b1 - 180 *3
pwmout 1,255,w1

if b1 =>0 and b1=<175 then goto off1
if w1 =>65300 then goto off1
goto main

off1:
low portc 1
goto main
----------------------------------------------------

Any ideas?

Mike
 

Dippy

Moderator
Well, I've only hada 10 second look, and without rewriting your code as you have to learn:

1. Think what will happen if b1=179 for example. What will happen to w1? Negative? I don't think so. Any value of b1 under 181 will make W1 big or small?
2. Why don't you do your IF test before twiddling pwm? Why not have 181 as your IF test. (If 180 is some kind of threshold).

3. You only need to say IF b1<=180 then... or even IF b1<181 then...

Have a bit of think what's going on. Run through your programme in your mind or on paper one step at a time.... I haven't checked your PWM.
 

eclectic

Moderator
Mike.

I'll add just just one more point.

First off, use Dippy's "hints". They work!

Then, change

low portc 1

TO

pwmout 1 OFF

(See manual 2 pages 117/118)

It certainly works on the simulator.

e.
 

eclectic

Moderator
Oh, and another thought.

w1 = b1 - 180 *3
pwmout 1,255,w1

This means that your PWMout values will vary from

255, 3 to 255, 225.

OR, almost off to < 25% of maximum.

For an LED output, would you be better with something like

readadc 1,b1

pwmout 1,63, b1

AND, changing the cut-off value for b1 ?

Have a look at the PWM wizard.

e.
 
Top