PDA

View Full Version : If - then statements



Russ Kincaid
- 17th May 2006, 03:53
This is my second PIC program, and I am having a problem. The IF - THEN statements look OK to me, but the compiler doesn't like it.

'device: 16F627A
S3 var portb.1 ' S3 is the timer output
S4 var portb.2 ' S4 determins whether short time or
' long time is measured. 0=short, 1=long
T var byte

green var portb.4 'Drives the green LED (physical pin 10)
red var portb.5 'Drives the red led (physical pin 11)
input s3
input s4
high green : high red 'LEDs are initally off
loop:
if s3=1 then time
goto loop

time:
if S4=0 then t =500 : endif
if s4 =1 then t = 5000 : endif
pause t
if t = 500 and s3 = 1 then bad : endif
if t = 500 and s3 = 0 then good : endif
* if t = 5000 and s3 = 0 then bad : endif
if t = 5000 and s3 = 1 then good :endif
stop ' should never get here!

In the line with the *, the compiler says that "then" is missing. Why does it not see it? the compiler does not like any of the IF - THEN statements. I did not use "endif" originally but got an error " endif missing".

Jerson
- 17th May 2006, 05:18
Where are the definitions of good and bad? Perhaps the compiler cannot see them. Is this the entire code you posted or just a fragment?

Jerson

Darrel Taylor
- 17th May 2006, 05:23
Hi Russ,

When using Inline IF statements, you don't need the ENDIF.

It's either

if S4=0 then t =500

-or-

if S4=0 then
    t =500
endif

<br>

Russ Kincaid
- 17th May 2006, 12:32
Thanks, Darrel, I removed the endif and now the compiler is happy and so am I. I guess PicBasic Pro Demo does not support "else" which got me off track in the first place.

Darrel Taylor
- 17th May 2006, 20:49
I've never used the DEMO version of PicBasic Pro. So, I can't really say for sure, but, It doesn't seem like it would be much of a DEMO if it couldn't even do IF-ELSE-ENDIF's.

Maybe there was a problem there too.

This should work.
IF S4=0 then
t =500
ELSE
t =5000
ENDIF

This will Not.
IF S4=0 then t =500 : ELSE t = 5000 : ENDIF

Russ Kincaid
- 18th May 2006, 00:27
Thanks, Darrel. I have another question: My timing was way off so I measured the frequency at clkout, it was 2.28 mHz instead of 1.0. I can adjust T to compensate, but is there a way to reset the oscillator?

Darrel Taylor
- 18th May 2006, 01:48
What type of oscillator configuration are you using?

Since you're expecting to see 1mhz on the clockout pin, I assume you're using the internal 4mhz oscillator. Otherwise you would see the crystal frequency, instead of FOSC/4.

And while the internal oscillators are Never exactly 4.0000mhz, they'll Never be at 9.12mhz (2.28*4). (unless the chip is just bad, also unlikely)

If instead, you're using an external crystal? You may have the wrong freq crystal, or the technique used to measure the frequency is affecting the oscillator, giving an incorrect reading.

But since you say that the program is running at the wrong speed to begin with, I would guess it's the wrong crystal. Or possibly the wrong or no capacitors on the crystal.

DT

Russ Kincaid
- 18th May 2006, 13:53
Thanks for the reply. There is no crystal, I am using the internal oscillator with clock out. I read somewhere that the oscillator calibration should be preserved, but did not tell me how to do it. I assume that the oscillator is running at its max. My circuit is attached.

sayzer
- 18th May 2006, 14:04
Pull up resistors as attached would be much safer.



-------------------

Dwayne
- 18th May 2006, 16:52
Sazer is right.. Pullup resisters will help....

Or, you can use internal pull up resisters. in the chip.

Dwayne

Russ Kincaid
- 18th May 2006, 20:04
Thanks, I will figure out how to use the internal pullups. But, back to the frequency; I could replace the calibration data if I knew where it was, but have not found that info yet. Does anyone know?

Darrel Taylor
- 18th May 2006, 20:25
There is no calibration register for the 627A's internal oscillator.

Several other chips have an OSCCAL register that allows you to Tweak the frequency a little. But not the 627.

You only have 2 choices, an approximate 4mhz, or an approximate 37khz.

No tweaking allowed.
<br>

Russ Kincaid
- 18th May 2006, 22:57
Thanks, Darrel. I guess the alternative is a crystal, but for now I will just make an adjustment for time.