PDA

View Full Version : Can someone tell me if I am going in the right direction?



MatthewM
- 19th June 2008, 23:14
Hello all,

So I am attempting to write a program for the Lab X1 and Lab X2 board to talk to one another serially, I have started to put together a code in what I think is reasonably close to what I need to do. Unfortunately, I am having a hard time finding out just how to express some types of commands, as such, I am pretty sure what I have is dead wrong in some way. Can someone tell me if I am at least in the ballpark with what I need to be writing? Any help is very much appreciated.

What I am trying to do is as follows:

The code on the Lab X1 sends a command serially to turn on a led on the Lab X2, right now I just have the program continuously sending the command, just so I can figure out how to send it.

The code on the Lab X2 receives to code and turns on LED2, and keeps it on. It also turns on LED3, to show that the loop is in fact working.

I have the two boards connected by pin D0 on the Lab X1 and pin B0 on the Lab X 2

Here are the codes I have written thus far, they do not work at all, besides turning on LED3.

Lab X1 code:

DEFINE OSC 20
INCLUDE "modedefs.bas"
TRISD.0=1

LED VAR BYTE

LOOP:
LED=%00000001
SERout PORTD.0,T2400,[LED]
GOTO LOOP

END

And the Lab X2 code:

DEFINE OSC 20
INCLUDE "modedefs.bas"
TRISB=%00000001

LED VAR BYTE
LED=%00000000

LOOP:
PORTB.2=1
SERIn PORTB.0,T2400,[LED]
IF LED=%00000001 THEN
PORTB.1=1
ELSE
GOTO LOOP
ENDIF
GOTO LOOP

END

Thank you for putting up with my extremely novice understanding of PicBasic Pro.
Matt

skimask
- 20th June 2008, 14:24
DEFINE OSC 20
INCLUDE "modedefs.bas"
TRISD.0=1<<<===You've set D.0 to an INPUT here!!! Might want an OUPUT!
LED VAR BYTE
LOOP: LED=1 : SERout PORTD.0,T2400,[LED] : PAUSE 5 : GOTO LOOP
END

And the Lab X2 code:

DEFINE OSC 20
INCLUDE "modedefs.bas"
TRISB=%00000001<<<===And here too, but this is correct
LED VAR BYTE : LED=0
LOOP: PORTB.2=1 : SERIn PORTB.0,T2400,[LED]
IF LED=1 THEN
PORTB.1=1
ELSE
PORTB.1=0<<<===Might want to turn the LED off if you don't receive anything
PAUSE 100
ENDIF
GOTO LOOP
END

MatthewM
- 20th June 2008, 19:49
Okay, I fixed the rather obvious problems you pointed out (dumb on my part). But it still dosn't work on the receiving end...Is there something wrong with my SERIN command? I believe I told it to write the serial value to LED variable. But that seems to be the only thing left that could be wrong.

Thank you for the help,
Matt

MatthewM
- 20th June 2008, 19:57
Actually, after some investigation, it turned out to be a hardware malfunction. Thank you again for the help.

So, my Serial commands where more or less correct? Wow...I was sure with the little understanding of serial communication I have that they would be dead wrong.

Thanks again man, this will help me out tons.
Matt

MatthewM
- 21st June 2008, 01:05
Actually, it's not working, after making changes to my program, I realized that it isn't working at all. (nothing responded to any changes in the code, what I took to be working, was really just the serial connection powering a led directly, damn my optimism)

Anyways, here is what I have now

Transmitting Code:

CLEAR

DEFINE OSC 20 'Defines oscillator rate
INCLUDE "modedefs.bas"
ADCON1=%11111111 'Sets all ports to digital
TRISD.0=0 'Sets Portd.0 to output
LED VAR BYTE : LED=1 'Declares and defines LED variable
LOOP: 'Start of main loop
SEROUT PORTD.0,T2400,[LED] : PAUSE 50 'Sends LED value serially
'out of portd.0
GOTO LOOP 'Repeat forever
END

Receiving Code:

CLEAR

DEFINE OSC 20 'Defines oscillator
INCLUDE "modedefs.bas"
ADCON1=%11111111 'Sets all ports to digital
TRISB=%00001000 'Sets Portb.4 to input
LED VAR BYTE : LED=0 'Declares and sets LED variable to 0
LOOP: SERIN PORTB.4,T2400,[LED]: PAUSE 50 'Checks portb.4 for serial imput
IF LED=1 THEN 'If LED equals 1, turn LED 2 on and 3
PORTB.1=1 'off, otherwise turn 3 on and two off
PORTB.2=0
ELSE
PORTB.1=0
PORTB.2=2
ENDIF
PAUSE 100 'Pause to see results
GOTO LOOP 'Repeat
END

Thanks, I apologize for my lack of knowledge.
Matt

manwolf
- 21st June 2008, 01:40
Matt

In the transmitting code. you turn the LED on with
LED=1

Then you enter the loop to send to the Port.

Then on the receiving side you keep reading the LED value.

and apply some logic



IF LED=1 THEN 'If LED equals 1, turn LED 2 on and 3
PORTB.1=1 'off, otherwise turn 3 on and two off
PORTB.2=0
ELSE
PORTB.1=0
PORTB.2=2
ENDIF


Maybe you should check your logic and make sure it works on the LAB-X2 before you go to the Serial port.

I might comment out this - 'SERIN PORTB.4,T2400,[LED]: PAUSE 50' and toggle the LED on and off. Then go from there.

Archangel
- 21st June 2008, 03:33
Hello Matthew,
I am writing from my wife's computer and that usually gets me in trouble, as I am unabel to test anything, having said that here goes . . . It looks as if you are sending out binary 1 or %00000001 and expecting to capture the same at the receiver. Methinks serin / serout doesn't work that way, I think it is looking for the hex or ASCII equiv. so if you serout 1 in binary, your receiver is looking for $31 or 49 Decimal, you could change the loop to send out $31, OR 49, and look to receive the same SERIN PortB.0,T2400,[#LED] . Just my thoughts, probably wrong.
OR you can have the IF THEN loop expect anything except zero.
JS

MatthewM
- 21st June 2008, 05:45
Thank you everyone for the help...I have tried all your ideas, and though they seem to be getting me closer, it's not quite working right yet. I am sorry this is so hard for me to figure out, I am sure I am making some dumb mistake.

Right now I am using portb.7 for the input pin, as to not interfere with the used components on the lab X2 pin. So far I have no response to anything from the output program, the codes I am currently using are as follows.

Transmitting Program
CLEAR

DEFINE OSC 20 'Defines oscillator rate
INCLUDE "modedefs.bas"
ADCON1=%11111111 'Sets all ports to digital
TRISD.0=0 'Sets Portd.0 to output
LED VAR BYTE : LED=1 'Declares and defines LED variable
LOOP: 'Start of main loop
SERout PORTD.0,T2400,[LED] : PAUSE 50 'Sends LED value serially
IF LED=1 THEN
LED=0
ELSE
LED=1
ENDif 'out of portd.0
GOTO LOOP 'Repeat forever
END

Receiving Program:

CLEAR

DEFINE OSC 20 'Defines oscillator
INCLUDE "modedefs.bas"
ADCON1=%11111111 'Sets all ports to digital
TRISB=%10000000 'Sets Portb.4 to input
LED VAR BYTE 'Declares and sets LED variable to 0
LOOP:
SERIn PORTB.7,T2400,[LED]: PAUSE 50 'Checks portb.4 for serial imput
IF LED=1 THEN 'If LED equals 1, turn LED 2 on and 3
PORTB.1=1 'off, otherwise turn 3 on and two off
PORTB.2=0
ELSE
PORTB.1=0
PORTB.2=1
ENDIF
PAUSE 100 'Pause to see results
GOTO LOOP 'Repeat
END

I am wondering is possibly I am not using the right defines?

Thanks,
Matt

skimask
- 21st June 2008, 05:50
TX Side:
Send byte value of "1" continuously with 50ms between each byte sent (1 = %00000001, as in the number right after 0, just to keep us both straight here)


DEFINE OSC 20 'Defines oscillator rate
INCLUDE "modedefs.bas"
CLEAR
LED VAR BYTE : LED = 1 : ADCON1=$ff : TRISD.0=0
LOOP: SEROUT PORTD.0,T2400,[LED] : PAUSE 50 : GOTO LOOP
END


Rx Side:
Start with the LED off. If a byte of value = %00000001 is received, turn the LED on PortB.1 on.
If a value of %0000001 is received, PortB.1 will turn on and stay on.
If something other than a value of %00000001 is received, the SERIN command will take it and PortB.2 will flash for 1/10 second then go back to trying again.
If nothing is received after 2 seconds, both LEDs will flash for 1 second then go back to trying again.


DEFINE OSC 20 'Defines oscillator
INCLUDE "modedefs.bas"
CLEAR
LED VAR BYTE : LED = 0 :ADCON1=$ff : TRISB=8
LOOP: SERIN PORTB.4,T2400,2000,nothing,[LED]
IF LED=1 THEN
PORTB.1=1
ELSE
PORTB.2 = 1 : pause 100 : PORTB.2 = 0
ENDIF
GOTO LOOP
nothing: PortB=3:pause 100:portb.0:pause 100:portb=3:pause 100:portb=0:pause 100
PortB=3:pause 100:portb.0:pause 100:portb=3:pause 100:portb=0:pause 100
PortB=3:pause 100:portb.0:pause 100:goto loop
END

Of course, this assumes all the hardware is working correctly...

skimask
- 21st June 2008, 05:52
It looks as if you are sending out binary 1 or %00000001 and expecting to capture the same at the receiver. Methinks serin / serout doesn't work that way, I think it is looking for the hex or ASCII equiv. so if you serout 1 in binary, your receiver is looking for $31 or 49 Decimal, you could change the loop to send out $31, OR 49, and look to receive the same SERIN PortB.0,T2400,[#LED] . Just my thoughts, probably wrong.
OR you can have the IF THEN loop expect anything except zero.
JS

Nope, he's got it right. It only gets goofy if you start using the # or the quotes or other modifiers. If you LED=1 then SEROUT xxxx , [LED], you'll send a binary value of %00000001, same thing on the receiving end. If you SERIN xxxx , [LED] and LED contains %00000001, then you've received LED=1.

Archangel
- 21st June 2008, 05:56
Nope, he's got it right. It only gets goofy if you start using the # or the quotes or other modifiers. If you LED=1 then SEROUT xxxx , [LED], you'll send a binary value of %00000001, same thing on the receiving end. If you SERIN xxxx , [LED] and LED contains %00000001, then you've received LED=1.Well then, here at Crownhill University, today I learned something :)
Thanks

skimask
- 21st June 2008, 06:04
Well then, here at Crownhill University, today I learned something :)
Thanks
Sorry...I hate it when other people do that to me...:(
Now your head is just that much more cluttered with information.
Today I learned not to spend 7 hours in the sunlight while mowing the lawn without sunscreen in a T-shirt.

Archangel
- 21st June 2008, 06:06
Thank you everyone for the help...I have tried all your ideas, and though they seem to be getting me closer, it's not quite working right yet. I am sorry this is so hard for me to figure out, <b>I am sure I am making some dumb mistake.</b>

Right now I am using portb.7 for the input pin, as to not interfere with the used components on the lab X2 pin. So far I have no response to anything from the output program, the codes I am currently using are as follows.

Transmitting Program
CLEAR

DEFINE OSC 20 'Defines oscillator rate
INCLUDE "modedefs.bas"
ADCON1=%11111111 'Sets all ports to digital
TRISD.0=0 'Sets Portd.0 to output
LED VAR BYTE : LED=1 'Declares and defines LED variable
LOOP: 'Start of main loop
SERout PORTD.0,T2400,[LED] : PAUSE 50 'Sends LED value serially
IF LED=1 THEN
LED=0
ELSE
LED=1
ENDif 'out of portd.0
GOTO LOOP 'Repeat forever
END

Receiving Program:

CLEAR

DEFINE OSC 20 'Defines oscillator
INCLUDE "modedefs.bas"
ADCON1=%11111111 'Sets all ports to digital
TRISB=%10000000 'Sets Portb.4 to input
LED VAR BYTE 'Declares and sets LED variable to 0
LOOP:
SERIn PORTB.7,T2400,[LED]: PAUSE 50 'Checks portb.4 for serial imput
IF LED=1 THEN 'If LED equals 1, turn LED 2 on and 3
PORTB.1=1 'off, otherwise turn 3 on and two off
PORTB.2=0
ELSE
PORTB.1=0
PORTB.2=1
ENDIF
PAUSE 100 'Pause to see results
GOTO LOOP 'Repeat
END

I am wondering is possibly I am not using the right defines?

Thanks,
Matt Hi Matt, well my other idea was wrong (again) so then, How are you setting the config statements? http://www.picbasic.co.uk/forum/showthread.php?t=543
Do you have a blinky setup to confirm the chips are actually alive ? Have you checked the connections pin for pin to be sure corrupt old breadboard isn't killing your time? I just trashed another one yesterday . . .<b>BTW mistakes are not dumb, people who will not admit to them, well . . . it's your call.</b>

MatthewM
- 21st June 2008, 06:11
As far as I can tell, the hardware is fine, they have both worked for other programs I have ran on them every time.

Do I need the "INCLUDE "modedefs.bas"?

Your codes worked intermittently to show that there was no serial signal received. In that one time it flashed both lights three times, then went dark...No amount of resetting or even reprogramming reproduced the results. I am going to try retesting the Lab X2 board, but last time I did, all components worked fine as expected (using a sample program off of melabs). Right now I can't get any of the light to light up...I have a feeling I haven't defined something right, but as far as I can tell, it's fine. This is really a odd set of results.

Matt

Archangel
- 21st June 2008, 06:16
"modedefs.bas" has the serout mode in it, you can use the mode command instead of T2400 if you prefer, it has some other stuff in it too, go ahead and open and inspect it to see for yourself what's in there. MAKE sure nothing is hooked to your OSC pins except the reasonator or crystal and caps. Make sure your config is set to the proper OSC for the freq. you are using XT for 4mhz HS for higher. If you are using the default configs without having changed it to HS then that is your first problem.

MatthewM
- 21st June 2008, 06:18
Okay, when the pin is not connected at all, it gives the expected two lights flashing like SkiMasks code defined. But with the pin is in, it does nothing.

Archangel
- 21st June 2008, 06:22
Okay, when the pin is not connected at all, it gives the expected two lights flashing like SkiMasks code defined. But with the pin is in, it does nothing.

PIN ? What pin? the serial connection? try a 10k pullup on that line.

MatthewM
- 21st June 2008, 06:23
Yes, the serial connection, if I disconnect it, the two lights flash, otherwise nothing happens if it is plugged in.

Archangel
- 21st June 2008, 06:25
HS OSC setting provides more electrical power to the OSC than XT setting does, sometimes it is possible to have XT work intermittently for 20, not reliable though.

add this to your code:
ADCON0=0

MatthewM
- 21st June 2008, 06:33
What do you mean?

Also, I tried the 10k pullup, no effect. Still goes dark with the jumper is connecting the pins.

Matt

Archangel
- 21st June 2008, 06:40
Matt, do you have the 2 LABX boards hooked to a common power supply? You should have a common ground between them. ADCON0=0 disables A/D too

MatthewM
- 21st June 2008, 06:50
Yes, I have a common ground. What do I want for my ADCON settings?

Right now I am using the codes that where given to me by skimask, unchanged.

MatthewM
- 21st June 2008, 07:00
Well guys, thanks again for your time, it's unimaginably nice to have people to ask questions, instead of manuals...Lol. But I need to sleep, I have a rather large amount of family events to contend with tomorrow, so I need to turn in. Hopefully sleeping on it will give me an idea.

Matt

skimask
- 21st June 2008, 07:05
Alright...time to completely forget ALL about this serial business...
TX Side:


DEFINE OSC 20 'Defines oscillator rate
ADCON1=$ff : Output PortD.0 'output on PortD.0
LOOP: PortD.0 = 1 : pause 500 : PortD.0 = 0 : pause 500 : goto loop
END

Should flash an LED connected to PortD.0 once per second, 1/2 on, 1/2 off...

RX Side:


DEFINE OSC 20
temp var byte : Input PortB.7
Loop:
if PortB.7 = 1 then 'checks logic level on Portb.7
PortB.0 = 1 'if it's high, set B.0 high
else
PortB.0 = 0 'otherwise set it low
endif
temp = temp + 1 : portB.1 = temp.7 'increase the temp variable, and set b.1 to match the temp variable's bit 7
goto loop
END

PortD.0 on the TX, connected to PortB.7 on the RX.
LED connected to PortB.0 <- follows PortB.7, which should follow PortB.7
LED connected to PortB.1 <- should flash quickly showing that the loop is running...if you think it's flashing too fast to see it, change temp var byte to temp var word and change temp = temp + 1 : portb.1 = temp.7 to temp = temp + 1 : portb.1 = temp.15

If that doesn't work, you've got hardware and/or config problems somewhere and it's time to write a blinky for both individually and get the pins themselves working.

Archangel
- 21st June 2008, 08:17
Yes, I have a common ground. What do I want for my ADCON settings?

Right now I am using the codes that where given to me by skimask, unchanged.

ADCON0 and ADCON1 are 2 seperate registers, do different things, related to A/D converters
ADCON0 = 0 shuts off A/D
ADCON1 = $FF? i would set as ADCON1 = 6 or 7 , data sheet chart shows all digital with those settings, no VREFs . . .

MatthewM
- 22nd June 2008, 15:11
I completely redid the code and got it working, using information from this thread, and
others on this site, finally. I am not sure exactly what was wrong with the other one, but I am assuming it was a config issue, since this code seems to work, maybe the addition of a qualifier too. Regardless, I probably wouldn't have been able to figure it out without your help.

Thanks for your time and help, it saved me days of flirting with manuals and project books.
Matt