PDA

View Full Version : A simple IF Statement!!!!



mslaney
- 6th December 2004, 12:14
For whatever reason, I have to humbly ask if this is right:

SW1 var porta.0
LED var portb.0

check:
IF SW1 = 1 then LED = 1
goto check
end

It compliles with no errors. Seems pretty straight forward but in anycase, its not working. Porta.0 is getting a 5v sig from SW1 but the LED won't lite.

PBP, PBPDemo, Microcode studio, MPLab, PicPro, and any other IDE I could find.

Thank you,
Mike Slaney
Just wondering IF...

mister_e
- 6th December 2004, 17:25
o.k did you declare TRIS pin direction... if not it will not work.

or try
IF SW1 = 1 then High LED

this one will work.

remind that all pin are set to input when power is apply to PIC.

in your case you can also add TRISB.0=0 this will set Portb.0 as output.

Melanie
- 6th December 2004, 18:36
No it's not right... or only partially...

Firstly you didn't tell us what PIC is involved. If you've chosen a PIC with Analog Comparators or ADC's then they usually live on PortA and they will have preference unless you've disabled them. Play exclusively with PortB ONLY... usually you can't go far wrong there.

Secondly... once you've lit your LED, you are aware it'll never switch OFF?

mslaney
- 6th December 2004, 23:13
Thanks,
I'm using a PIC16F627 on a Velleman K8048 kit with builtin buttons and LEDs. Not only was it cheap, it's cheesy too!
Here's the latest code that doesn't work:
'Set pin direction

TRISA = %11111111
TRISB = %00000000

'Set pgm vars and cons
SW1 var porta.0
LED var portb.0

'start the program

start:
if SW1 = 1 then
high LED
endif
goto start
end

As for the LED being on....If the IF worked I would have written another to get out of it!! Kind of paradoxical, no?

How does one disable the comparator feature(s) on a specific pin?
The kit is hardwired for switches on porta and LEDs on portb.

Mike

mister_e
- 6th December 2004, 23:22
here's the one who'll work...



CMCON=7 'disable analog comparator

'Set pin direction

TRISA = %11111111
TRISB = %00000000

'Set pgm vars and cons

SW1 var porta.0
LED var portb.0

'start the program

start:
if SW1 = 1 then
LED =1 'can also use High LED
pause 500 'or whatever delay you want
LED=0 'can also use LOW LED
endif

'or if you want to give LED output the same status as SW1
'you can use the following line
'
'
'while SW1
' LED=1
'wend
'LED=0
'
'
'
'or with IF THEN
'
'If SW1 then LED=1
'IF SW1=0 Then LED=0
'
'Select CASE version
'
'Select Case SW1
' Case 0
' LED=0
' Case 1
' LED=1
'End Select
'



goto start
end

mslaney
- 7th December 2004, 02:06
NEAT!
Thank you so much. As it turns out, disabling the comparator did the trick.
Where would I get more info on CMCON? I read some of the datasheet but how does the '=7' work?

mister_e
- 7th December 2004, 02:56
well how to say .... See datasheet p58 Figure 9-1

+

CMCON register p-57.

As you want to disable analog comparator, you don't need bit 3-7, we consider them as 0. Bit0 - bit 2 will be set to 1.
so CMCON=%00000111 = 7

mslaney
- 7th December 2004, 12:28
Thanks. So it's like setting the bits in TRISx. I do my best to read (well....understand) the data sheets.
So then all the commands such as VRCON, INTCON, etc, are directly accessible through PICBasic or must one use asm for some of them? One must set the bit high or low to enable the feature?
I'm very new to this. Could you recommend a good programming reference and/or tutorial for beginners? The PICBasic pro manual is the best reference but not an application guide.

mister_e
- 7th December 2004, 16:34
So then all the commands such as VRCON, INTCON, etc, are directly accessible through PICBasic or must one use asm for some of them? One must set the bit high or low to enable the feature?


all the register can be acces directly with their name under PicBasic Pro. If you want to set/verify only one bit you can also. Lets say i want to set bit 3 of VRCON

VRCON.3=1

that's it... i didn't check what is the meaning of this pin before but the method is there...


I'm very new to this. Could you recommend a good programming reference and/or tutorial for beginners? The PICBasic pro manual is the best reference but not an application guide.
you can see code example one Melabs website or Bruce website www.rentron.com

Both can sells you some books on that. The best way as i know to learn is to try, get bugs and debug.

good luck!

mslaney
- 17th December 2004, 11:39
OK, so I've tried Select, When, and If and they all work fine. Thanks.

How can I put multiple conditions in a when?
I have four switches and LEDs. I'd like to check the condition of each switch and turn on it's respective LED.

I tried:
When
sw1 = 1 : LED1 = 1
sw2 = 1 : LED2 = 1
and so on.

I could use multiple IF or WHEN statements but is there a way to do this in one When or Select block?
I've tried the manuals and assorted books but they only have one condition in the statement as an example.

Thanks,
Mike Slaney

mister_e
- 17th December 2004, 18:26
I always prefer to use SELECT CASE. The easy way to get multiple conditions is to refer to the PORT.

By example


SW1 VAR PORTB.0
SW2 VAR PORTB.1
SW2 VAR PORTB.2
MaskSwitch=byte

MaskSwitch=PORTB & $07
SELECT CASE MaskSwitch
CASE 1 '%00000001 or SW1
Goto Procedure_SW1

CASE 2 '%00000010 or SW2
Goto Procedure_SW2

CASE 4 '%00000100 or SW3
Goto Procedure_SW3
END SELECT

mslaney
- 19th December 2004, 22:25
OK, by looking at the structure, I understand what is supposed to happen except for the "MaskSwitch = porta & $7". What is actually happening with the & and $7? Is it a directive for the chip? I'm using a PIC16F627.

I modified the pgm to match the requirements of my particular setup and the program is compiling but when I start the program, all the LEDs turn on and stay on.
I really hate to be a pest. No really :-)

Here's what I have:

'Chip Registers

CMCON = 7 'disable comparators
TRISA = %11111111 'PortA inputs
TRISB = %00000000 'Portb outputs

'Declare and init vars

sw0 var porta.0
sw1 var porta.1
sw2 var porta.2
sw3 var porta.3

led0 var portb.0
led1 var portb.1
led2 var portb.2
led3 var portb.3

MaskSwitch var byte
MaskSwitch = porta & $7

'Set all leds low
portb = %00000000

Select Case Maskswitch
case 0
sw0 = 1
gosub sw0_proc
case 1
sw1 = 1
gosub sw1_proc
case 2
sw2 = 1
gosub sw2_proc
case 3
sw3 = 1
gosub sw3_proc
End select

sw0_proc:
LED0 = 1
sw1_proc:
led1 = 1
sw2_proc:
led2 = 1
sw3_proc:
led3 = 1
end

mister_e
- 19th December 2004, 23:14
hehe, you're just close.... you just forget a GOTO after the SELECT CASE statement.




CMCON = 7 'disable comparators
TRISA = %11111111 'PortA inputs
TRISB = %00000000 'Portb outputs

'Declare and init vars

sw0 var porta.0
sw1 var porta.1
sw2 var porta.2
sw3 var porta.3

led0 var portb.0
led1 var portb.1
led2 var portb.2
led3 var portb.3

MaskSwitch var byte

'Set all leds low
portb = %00000000


Start:

MaskSwitch = porta & $0F 'Isolate PortA.0 thru PORTA.3
' & : Logical AND bitwise operation
' $0F : %00001111

Select Case Maskswitch
case 1 'or PORTA = %00000001
gosub sw0_proc
case 2 'or PORTA = %00000010
gosub sw1_proc
case 4 'or PORTA = %00000100
gosub sw2_proc
case 8 'or PORTA = %00001000
gosub sw3_proc
End select

Goto start




sw0_proc:
While sw0
LED0 = 1
Wend
Led0=0
Return

sw1_proc:
While sw1
LED1 = 1
Wend
Led1=0
Return


sw2_proc:
While sw2
LED2 = 1
Wend
Led2=0
Return

sw3_proc:
While sw3
LED3 = 1
Wend
Led3=0
Return

mslaney
- 20th December 2004, 02:03
HA! Thanks. I also added a couple other things that I overlooked in your last post. Thanks again - you've been a great help!
Oh, nice touch with the while/wend in the subs.
Mike

bbarney
- 20th December 2004, 03:37
could you please post your finished code

mslaney
- 21st December 2004, 01:28
OK, here it is.
There is a considerable delay with the toggles. Sometime they don't react at all. I'm still trying other approaches to get the same effect but faster and more accurate. I'll keep you posted.
Thanks again for all your help

'Chip Registers

CMCON = 7 'disable comparators
TRISA = %11111111 'PortA inputs
TRISB = %00000000 'Portb outputs

'Declare and init vars

sw0 var porta.0 'Switches
sw1 var porta.1
sw2 var porta.2
sw3 var porta.3

led0 var portb.0 'LEDs
led1 var portb.1
led2 var portb.2
led3 var portb.3

MaskSwitch var byte 'Select/Case Var


portb = %00000000 'Set portb low

start:
MaskSwitch = porta & $0F 'Still trying to get my hands
'around this one.

Select Case Maskswitch 'Wait for user input

case 1
gosub sw0_proc

case 2
gosub sw1_proc

case 4
gosub sw2_proc

case 8
gosub sw3_proc

End select
Goto start

sw0_proc: 'Stop waitin and start workin
while sw0
toggle led0
wend
return

sw1_proc:
while sw1
toggle led1
wend
return

sw2_proc:
while sw2
toggle led2
wend
return

sw3_proc:
while sw3
toggle led3
wend
return

end

mister_e
- 21st December 2004, 07:13
Sill close... try this... should work



'Chip Registers

CMCON = 7 'disable comparators
TRISA = %11111111 'PortA inputs
TRISB = %00000000 'Portb outputs

'Declare and init vars

sw0 var porta.0 'Switches
sw1 var porta.1
sw2 var porta.2
sw3 var porta.3

led0 var portb.0 'LEDs
led1 var portb.1
led2 var portb.2
led3 var portb.3

MaskSwitch var byte 'Select/Case Var


portb = %00000000 'Set portb low

start:
MaskSwitch = porta & $0F 'Care about only PORTA.0 thru PORTA.3


Select Case Maskswitch 'Wait for user input

case 1
gosub sw0_proc

case 2
gosub sw1_proc

case 4
gosub sw2_proc

case 8
gosub sw3_proc

End select
Goto start

sw0_proc: 'Stop waitin and start workin
toggle led0 'toggle Port LED
while sw0 'waiting untill sw is release
wend
pause 20 'cheap debounce delay
return

sw1_proc:
toggle led1 'toggle Port LED
while sw1 'waiting untill sw is release
wend
pause 20 'cheap debounce delay
return

sw2_proc:
toggle led2 'toggle Port LED
while sw2 'waiting untill sw is release
wend
pause 20 'cheap debounce delay
return

sw3_proc:
toggle led3 'toggle Port LED
while sw3 'waiting untill sw is release
wend
pause 20 'cheap debounce delay
return

mslaney
- 21st December 2004, 17:21
Thanks, I tried your code as is but it didn't do anything. It compiled ok though - maybe I just missed a tiny thing or two.
Anyway, at this point in my study I am trying to turn an LED on and off with the same switch. I figured toggle would do it and I was right but there was a significant delay in the reaction time. It seemed like it was waiting to go through the loop and require me to press the button at the specific time in that cycle. If I hold the button down for a period of time it will toggle the LED but only if I am in the right part of the cycle. Get it? I'm not sure if I worded this correctly and it is based entirely on a grand assumption!!! :-)

Thanks,
Mike Slaney
Gone Mad in Massachusetts

mister_e
- 21st December 2004, 18:22
Must be a hardware problem now. It's working perfect here.

do you put any kind of pull down resistor to the inputs of your PIC. Case not... it will not working. Pull down between 1k and 10k will be enough.

Be sure that you correctly copy/paste previous code.

Be sure MCLR pin is correctly tie to VCC (by resistor or direct)

Be sure you have un-noisy 5V line. 10uF tantalum or 47uF electrolitic + 0.1 uF is suitable.

can you try adding this line to the top of your code(must use MPASM to compile)



@ __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _LVP_OFF


this will set at least internal oscillator and disable MCLR pin. (+others things... don't care about for now)

regards

mslaney
- 21st December 2004, 19:30
cool, I 'll try/check that when I get home from work. Aren't there internal pullup/down resistors in the PIC16F627?
Thanks
Mike

mister_e
- 21st December 2004, 22:43
in fact there's some internal pullups on some portb pins... as i remind not on all and we must enable them before. in this case, it will revert the logical state... easy to revert too.

anyway, let me know, what's happen now.

regards

Chadhammer
- 15th February 2005, 01:21
Originally posted by mister_e
I always prefer to use SELECT CASE. The easy way to get multiple conditions is to refer to the PORT.

By example


SW1 VAR PORTB.0
SW2 VAR PORTB.1
SW2 VAR PORTB.2
MaskSwitch=byte

MaskSwitch=PORTB & $07
SELECT CASE MaskSwitch
CASE 1 '%00000001 or SW1
Goto Procedure_SW1

CASE 2 '%00000010 or SW2
Goto Procedure_SW2

CASE 4 '%00000100 or SW3
Goto Procedure_SW3
END SELECT


My project is using an LCD and a PIC16F870. I think I need tables of some sort and thought this might be the right direction. I would like to put some LCD messages out based on the conditions of 3 or 4 inputs pins and probably have 2 or 3 output pins besides the LCD out.

I have a question about your code. I am using the LCD routine and PORTB.3 is being used by it. Let's say I want:
___________________________

MaskSwitch=PORTB & $07
SELECT CASE MaskSwitch
CASE 16 '%00010000 or SW1
Goto Message_1
___________________________

I need an input on PORTB.4. as well as 0-2. Is this feasable or will the that disrupt PORTB.3 in any way and mess up my LCD_Out routine?

Also I would need to have something like CASE 16 but I need to ignore %00010XXX (PORTB 0-2). Do I have to build a case for each of the 3 Bin inputs I want to ignore? I think so, but not sure. Or can I exclude them somehow?

How about a CASE 0 ' or %00000000 ? Is that a valid case?

What's the "& %07" after the PORTB for? Setting the Input/ouptuts on PORTB? How can I do this beyond PORTB.3 without upsetting the LCD routine on PORTB.3 (Enable for the LCD?)?

Appreciated
Ultra new at PBP & Programming

mister_e
- 15th February 2005, 04:22
I need an input on PORTB.4. as well as 0-2. Is this feasable or will the that disrupt PORTB.3 in any way and mess up my LCD_Out routine?


First send your schematic... will be more easy to figure what you want to do and how. Maybe you'll need some hardware modification.



How about a CASE 0 ' or %00000000 ? Is that a valid case?


yes



What's the "& %07" after the PORTB for? Setting the Input/ouptuts on PORTB? How can I do this beyond PORTB.3 without upsetting the LCD routine on PORTB.3 (Enable for the LCD?)?


Here's a little basic knowledge tutorial.

& mean a bitwise AND

so the Maskswitch = PORTB & %07 will mean
[list]
1. Get the value of the PORTB
2. Take this value, do a bitwise AND with 7 Hexadecimal (or %0000111)
3. place the result in MaskSwitch variable
[list]

for a example, if portb = %1111101, value of MaskSwitch will be %00000101. As you see, it return only the first three bits of PORTB

Chadhammer
- 16th February 2005, 01:39
I am using the same ports described here for the LCD but on a 16F870 (28 pin DIP).
http://rentron.com/PicBasic-LCD.htm

The LCD our routine uses the RB3 for the enable and pins RA0-RA3 for the data to the LCD. I'd like to use the rest of the ports on B (RB0-RB2 & RB4-RB7) for inputs and outputs and maybe a couple on Port C also.

I want to examine 4 opto outputs (4 PIC pins) checking for AC and DC along with Current X-formers or sensors (Hall effect / Allegro) for each. I want to output the condition of the AC and DC to the LCD mode based on those conditions. I want to use a switch to force one mode or the other (AC/DC) and drive relays based on what's available at the opto and or switch input. AC/DC/Auto select.

mister_e
- 16th February 2005, 03:20
In my case, i'll prefer to use only one PORT for the LCD. something like PORTB<5:0>. That way, it will leave you all the analog pins on PORTA you can need for you application.

i hate to have one device on multiple ports if i don't need any specific function to this port like PWM or CCP.

you can change the default LCD setting using DEFINE. Look PBP manual in the LCDOUT section.

at the end, it will look like that



' Lcd defines
' ===========
'
define LCD_DREG PORTB ' Set data pin of LCD to
define LCD_DBIT 0 ' PORTB.0-PORTB.3

define LCD_RSREG PORTB ' Set RS bit of LCD to
define LCD_RSBIT 4 ' PORTB.4

define LCD_EREG PORTB ' Set E bit of LCD to
define LCD_EBIT 5 ' PORTB.5


by using alias to ouputs/input, it make life easier to access to them


TRISC = 255 ' set input to all pin of PORTA
TRISB=0 ' set output to all pin of PORTB
OptoCoupler var PORTB.6
PushButton var PORTA.1
ADCON1=7 ' disable A/D converter

start:
If PushButton then ' is pushbutton is pressed
OptoCoupler = 1 ' activate opto coupler
endif

Chadhammer
- 17th February 2005, 20:58
I compiled it in a new folder and was building of the old ASM in MPLAB. Kept getting output to row 1 completely black on the LCD. Figured it out last night finally.

Thanks for the help. Much apreciated