PDA

View Full Version : A code scrutiny question



kevlar129bp
- 2nd September 2007, 02:35
Hello all,

I am a newbie when it comes to picbasic, which brings me to my question. If I post my present code I am entertaining, will one of you fine programmers scrutinize it for me? It should be pretty straight-forward. I am looking at controlling relays with a pic. I don't need advice on what controller to use, and so forth...moreover, am I using the correct code or is there a more efficient way.
Anyhow, here goes:

\\CODE START//

INCLUDE "bs2defs.bas"
DEFINE OSC 20
SerialInput var PORTA.0 'serial input pin
board var byte
relay var byte
stat var byte
trisa = %00000001
trisb = %11111111
trisc = %00000000
trisd = %00000000
trise = %00000000
boardAddr = PORTB '<<<<<<<8 bit address set via dip switches

Start :

' Wait until the character “B” AND board address is received serially on porta.0
serin2 serialinput,84,[WAIT ("B",board)]
if board <> boardAddr then goto start
' Wait until the character “R” is received serially on porta.0 and put next character into B1
SERIN SerialInput,N9600,["R"],relay
' Wait until the character “S” is received serially on porta.0 and put next character into B2
SERIN SerialInput,N9600,["S"],stat

'****Send serial from pc like so?: MSComm1.Output = "B??" + Chr$(13) + "R??" + Chr$(13) + "S??" + Chr$(13)
'****Send serial from pc like so?: MSComm1.Output = "B??R??S??" + Chr$(13)

SELECT CASE relay
CASE 1
IF stat = 1 THEN HIGH PORTA.4
LOW PORTA.3
CASE 2
IF stat = 1 THEN HIGH PORTA.3
LOW PORTA.4
CASE 3
IF stat = 1 THEN HIGH PORTA.5
LOW PORTA.2
CASE 4
IF stat = 1 THEN HIGH PORTA.2
LOW PORTA.5
CASE 5
IF stat = 1 THEN HIGH PORTE.0
LOW PORTA.1
CASE 6..........

END SELECT
goto Start

\\CODE END//

I sure hope one of you fine people can assist me. If my application is unclear, please let me know. Thank you much.

Chris

mike20200
- 2nd September 2007, 06:23
you should state what kind of Uc u using.

anywhere if you want to use PORTA which is analog mode.Normally alot people for get to change it to digital mode. you need to add this command in your code

CMCON=7 'change the pin to digital
ADCON1 = 7 ' for PIC16C7xx, 16F87x, 12C67Fx
ANSEL = 0 ' register for 12F675 and 16F676

people tend to forget to set all this before using the analog pin.

Jerson
- 2nd September 2007, 07:12
Hello all,


' Wait until the character “B” AND board address is received serially on porta.0
serin2 serialinput,84,[WAIT ("B",board)]
if board <> boardAddr then goto start
' Wait until the character “R” is received serially on porta.0 and put next character into B1
SERIN SerialInput,N9600,["R"],relay
' Wait until the character “S” is received serially on porta.0 and put next character into B2
SERIN SerialInput,N9600,["S"],stat

Chris

I think you will need to change the statements as shown below


' Wait until the character “B” AND board address is received serially on porta.0
SERIN2 serialinput,84,[WAIT ("B",DEC2 board)]
if board <> boardAddr then goto start
' Wait until the character “R” is received serially on porta.0 and put next character into B1
SERIN2 SerialInput,84,["R"],DEC2 relay
' Wait until the character “S” is received serially on porta.0 and put next character into B2
SERIN2 SerialInput,84,["S"],DEC2 stat

Here I am assuming the B??R??S?? each gets a 2 digit decimal value

kevlar129bp
- 2nd September 2007, 17:14
Thank you Mike and Jerson. To explain in more detail, I'll try to answer your questions as best I can.

The mcu I plan on using is the 16F871.

On port B, I'll have 8 dip switches for the "board" address (set high or low).
0 to 256 ex: B129

There will be 24 GPIO's left over, therefore they will be the "relay".
1 to 24 ex: R22

The "stat" will either be on or off.
0 or 1 ex: S0

In the above example, I would like to:
On board 129, turn relay 22 off

On that note, I commented 2 lines in the code to possibly garner advice on the proper way to send those commands from vb.net. If any of you could comment on that as well, that would be great.
Thank you guys again.

Chris

Jerson
- 3rd September 2007, 04:28
I think the second line is good.
MSComm1.Output = "B??R??S??" + Chr$(13)

kevlar129bp
- 3rd September 2007, 18:02
Thank you Jerson,
I suppose I could try both in VB. I'll go with your suggestion first.

Chris