PDA

View Full Version : serin trouble with PIC16F877A



thasatelliteguy
- 23rd May 2014, 07:40
I am trying to build a two part system where I will have a MC on a self-acquire satellite dish, and another MC in a handheld unit joined by BT. (This also allows for BT control from a phone or other SPP device) I fried my LCD, so I changed my attack plan to work from a terminal for now. I am having trouble getting the menu system to react properly so I hooked up another BT module TX/RX reversed and opened another terminal. I can see the terminal sending out "1" or "2" but the chip is registering it as [00]. I figured this out by putting in a debug line to echo it back before continuing the loop. I'm sure it's probly some formatting issue, but I sure don't see my error......

#CONFIG
__config _FOSC_HS & _WDT_OFF & _PWRTE_ON & _BODEN_ON & _LVP_OFF & _CPD_OFF & _WRT_OFF & _DEBUG_OFF & _CP_OFF
#ENDCONFIG


DEFINE OSC 12


TX var PORTC.6
RX var PORTC.7
CNT var word
cereal var word
i var byte


goto MainMenu


CLR:
For i = 0 to 30
serout TX, 2, [10]
next i
return


MainMenu:
gosub CLR
serout Tx, 2, ["******** MAIN MENU ********", 13, 10, 10]
serout Tx, 2, [" 1. Start Homing Sequence", 13, 10]
serout Tx, 2, [" 2. Manual JOG Mode", 13, 10]
serout Tx, 2, [" 3. Auto-Acquire Satellite", 13, 10]
serout Tx, 2, [" 4. Stow Dish for Travel", 13, 10, 10]
serout Tx, 2, ["Enter Your Selection:", 13, 10]
Serin RX, 2, cereal

If cereal = "1" then goto HomeDish
If cereal = "2" then goto JogMode
If cereal = "3" then goto AutoAcquire
If cereal = "4" then goto StowDish
serout TX, 2, [cereal] 'For debugging only
pause 500
goto MainMenu


HomeDish:
gosub CLR
serout Tx, 2, ["******** HOME DISH ********", 13, 10, 10]
serout Tx, 2, [" 1. Start Homing Sequence", 13, 10, 10]
serout Tx, 2, [" 2 Abort Homing", 13, 10, 10]
serout Tx, 2, ["Enter Your Selection:", 13, 10]
Serin RX, 2, cereal
if cereal = "1" then goto StartHoming
if cereal = "2" then goto MainMenu
serout TX, 2, [cereal] 'For debugging only
pause 500
goto HomeDish


JogMode:
gosub CLR
serout TX, 2, [" JOG MENU ", 13, 10, 10]
serout TX, 2, ["*** UNDER CONSTRUCTION *** ", 13, 10]
pause 3000
goto MainMenu


AutoAcquire:
gosub CLR
serout TX, 2, [" AUTO-ACQUIRE ", 13, 10, 10]
serout TX, 2, ["*** UNDER CONSTRUCTION *** ", 13, 10]
pause 3000
goto MainMenu


StowDish:
gosub CLR
serout TX, 2, [" STOW DISH ", 13, 10, 10]
serout TX, 2, ["*** UNDER CONSTRUCTION *** ", 13, 10]
pause 3000
goto MainMenu


StartHoming:
gosub CLR
serout TX, 2, [" Begin Homing, Homie ", 13, 10, 10]
serout TX, 2, ["*** UNDER CONSTRUCTION *** ", 13, 10]
pause 3000
goto MainMenu

EarlyBird2
- 23rd May 2014, 08:52
Move the pause statement




MainMenu:
gosub CLR
serout Tx, 2, ["******** MAIN MENU ********", 13, 10, 10]
serout Tx, 2, [" 1. Start Homing Sequence", 13, 10]
serout Tx, 2, [" 2. Manual JOG Mode", 13, 10]
serout Tx, 2, [" 3. Auto-Acquire Satellite", 13, 10]
serout Tx, 2, [" 4. Stow Dish for Travel", 13, 10, 10]
serout Tx, 2, ["Enter Your Selection:", 13, 10]
Serin RX, 2, cereal

If cereal = "1" then goto HomeDish
If cereal = "2" then goto JogMode
If cereal = "3" then goto AutoAcquire
If cereal = "4" then goto StowDish
serout TX, 2, [cereal] 'For debugging only
pause 500
goto MainMenu



to before the serin statement. Also with serial communication I always use timeouts and qualifiers where I can.

thasatelliteguy
- 23rd May 2014, 09:07
OK, I put a pause 500 in front of the serin command with no change. Two questons... 1.Why did you think that would work? (Not being smart here, I assume you had logical reasoning or experience that might be of some use someday) 2. Can I use multiple qualifiers? Like if I want to reject everything except 1 2 3 or 4?? thanks!

EarlyBird2
- 23rd May 2014, 09:10
OK, I put a pause 500 in front of the serin command with no change. Two questons... 1.Why did you think that would work? (Not being smart here, I assume you had logical reasoning or experience that might be of some use someday) 2. Can I use multiple qualifiers? Like if I want to reject everything except 1 2 3 or 4?? thanks!

You have made my day :)

EarlyBird2
- 23rd May 2014, 09:20
You have a loop that is constantly being executed except for the 500 pause. You Tx then instantly Rx with no time for a response to be sent.

Well that was my thinking but obviously there is more to it than I first thought.

try

Serin RX, 2, 500,MainMenu,cereal

serin will wait 500 msecs waiting for data to come in. cereal you have as a word it only needs to be a byte.

thasatelliteguy
- 23rd May 2014, 10:26
serout Tx, 2, [" 3. Auto-Acquire Satellite", 13, 10]
serout Tx, 2, [" 4. Stow Dish for Travel", 13, 10, 10]
serout Tx, 2, ["Enter Your Selection:", 13, 10]
pause 500
Serin RX, 2, 500, MainMenu, cereal

If cereal = "1" then goto HomeDish
If cereal = "2" then goto JogMenu
If cereal = "3" then goto AutoAcquire
If cereal = "4" then goto StowDish
serout TX, 2, [cereal] 'For debugging only
serout TX, 2, ["Dingleberry"]
pause 1500
goto MainMenu

Now I can see the 500 passing as the Main Menu 'blinks' from being called over and over, but still no go. The above code still yields:
[00]Dingleberry

instead of 1Dingleberry

EarlyBird2
- 23rd May 2014, 10:30
Let me get this correct

you are sending 1 from the terminal?

but receive nothing?

EarlyBird2
- 23rd May 2014, 10:32
and you send the menu instructions and receive them on the terminal?

Are you sure this is not a hardware issue? Wires not connected to correct pins or something like that.

EarlyBird2
- 23rd May 2014, 11:00
TRISC=%10000000 is missing to set portsc.7 as input.

thasatelliteguy
- 23rd May 2014, 11:45
I did the input thing both ways...TRISC.7 = 1 and TRISC=%10000000 - no change

I have two HC-05's hooked up to the RX and TX ports. They are hooked up backwards from one another. So I see the menu show up on one terminal screen, and I see the echo of what I type(a mirror of what the MC is receiving) show up in the other terminal on my screen on a different BT-com port. The second BT (as well as the MC presumably) receives 1 or 2 or whatever I type. But the when the menus didnt respond, I added the SEROUT and 1500 pause (to read it) and underneath the menu it'll only output [00] everytime no matter what key I hit. Just to be certain I added the next line for a total of:
serout TX, 2, [cereal]
serout TX, 2, ["Dingleberry"]
pause 1500
and I get

[00]Dingleberry

then after 1.5 seonds, it dissappears and the Main Menu is reborn to await another keystroke.....

EarlyBird2
- 23rd May 2014, 11:55
Serin RX, 2, 500, MainMenu, cereal

This line goes to MainMenu if nothing is received after 500 ms therefore [00]Dingleberry will only be printed if something is received.

What is going on?

Can you send the whole code again please?

thasatelliteguy
- 23rd May 2014, 12:07
you are correct. However, when that didnt work, I removed it again. But when its there, it just cycles back (basically blinks) to the Main Menu unless you send a keystroke, in which case you get [00]Dingleberry.

[00] is Null is it not? Is it possible somewhere in the serin process it's trying to take the char before or after my keystroke? I tried 11, and 23 tho and still returns [00] even though 11 and 23 showed on the receiving terminal. I even tried it with a wait command llike wait for "A" then take the next char. Still same thing. And that was straight from the PBP manual....

#CONFIG
__config _FOSC_HS & _WDT_OFF & _PWRTE_ON & _BODEN_ON & _LVP_OFF & _CPD_OFF & _WRT_OFF & _DEBUG_OFF & _CP_OFF
#ENDCONFIG

TRISC=%10000000
DEFINE OSC 12

el var PORTD.7
TRISD.7 = 0
low el
az var PORTB.5
TRISB.5 = 0
low az
dir var PORTD.5
TRISD.5 = 0
low dir
TX var PORTC.6
TRISC.6 = 0
RX var PORTC.7


CNT var word
cereal var byte
i var byte


goto MainMenu


CLR:
For i = 0 to 30
serout TX, 2, [10]
next i
return


MainMenu:
gosub CLR
serout Tx, 2, ["******** MAIN MENU ********", 13, 10, 10]
serout Tx, 2, [" 1. Start Homing Sequence", 13, 10]
serout Tx, 2, [" 2. Manual JOG Mode", 13, 10]
serout Tx, 2, [" 3. Auto-Acquire Satellite", 13, 10]
serout Tx, 2, [" 4. Stow Dish for Travel", 13, 10, 10]
serout Tx, 2, ["Enter Your Selection:", 13, 10]
pause 500
Serin RX, 2, 500, MainMenu, cereal

If cereal = "1" then goto HomeDish
If cereal = "2" then goto JogMenu
If cereal = "3" then goto AutoAcquire
If cereal = "4" then goto StowDish
serout TX, 2, [cereal] 'For debugging only
serout TX, 2, ["Dingleberry"]
pause 1500
goto MainMenu


HomeDish:
gosub CLR
serout Tx, 2, ["******** HOME DISH ********", 13, 10, 10]
serout Tx, 2, [" 1. Start Homing Sequence", 13, 10, 10]
serout Tx, 2, [" 2 Abort Homing", 13, 10, 10]
serout Tx, 2, ["Enter Your Selection:", 13, 10]
Serin RX, 2, cereal
if cereal = "1" then goto StartHoming
if cereal = "2" then goto MainMenu
serout TX, 2, [cereal] 'For debugging only
pause 500
goto HomeDish


JogMenu:
gosub CLR
serout TX, 2, [" JOG MENU ", 13, 10, 10]
serout TX, 2, ["1. Elevation", 13, 10]
serout tx, 2, ["2. Azimuth", 13, 10]
serout tx, 2, ["3. Skew", 13, 10]
serout tx, 2, ["4. Main Menu", 13, 10, 10]
serout tx, 2, ["Enter Your Selection:"]
serin RX, 2, cereal
goto MainMenu


AutoAcquire:
gosub CLR
serout TX, 2, [" AUTO-ACQUIRE ", 13, 10, 10]
serout TX, 2, ["*** UNDER CONSTRUCTION *** ", 13, 10]
pause 3000
goto MainMenu


StowDish:
gosub CLR
serout TX, 2, [" STOW DISH ", 13, 10, 10]
serout TX, 2, ["*** UNDER CONSTRUCTION *** ", 13, 10]
pause 3000
goto MainMenu


StartHoming:
gosub CLR
serout TX, 2, [" Begin Homing, Homie ", 13, 10, 10]
serout TX, 2, ["*** UNDER CONSTRUCTION *** ", 13, 10]
pause 3000
goto MainMenu

TestPattern:
HIGH el
pause 1000
low el
pause 1000
high dir
pause 50
high el
pause 1000
low el
low dir
pause 1000
HIGH az
pause 1000
low az
pause 1000
high dir
pause 50
high az
pause 1000
low az
low dir
pause 1000
goto TestPattern

thasatelliteguy
- 23rd May 2014, 13:01
Man! I know this has to be something crazy simple to fix. This was the basis of my very first PIC project 6 weeks ago when I used a PICaxe 20M2 to build a BT controlled tank to pull wire under houses. It worked beautifully! But alas, PICAXE is a lil different from PBP, and I wiped the bootloader from that chip as soon as I got my picKIT3. It sux too cuz I want my tank running again and I cant do that either until I solve this same issue.......

EarlyBird2
- 23rd May 2014, 22:41
Man! I know this has to be something crazy simple to fix. This was the basis of my very first PIC project 6 weeks ago when I used a PICaxe 20M2 to build a BT controlled tank to pull wire under houses. It worked beautifully! But alas, PICAXE is a lil different from PBP, and I wiped the bootloader from that chip as soon as I got my picKIT3. It sux too cuz I want my tank running again and I cant do that either until I solve this same issue.......

I agree with you it has to be something simple.

Cut down the program to the bare minimum and test.



#CONFIG
__config _FOSC_HS & _WDT_OFF & _PWRTE_ON & _BODEN_ON & _LVP_OFF & _CPD_OFF & _WRT_OFF & _DEBUG_OFF & _CP_OFF
#ENDCONFIG

DEFINE OSC 12

TX var PORTC.6
RX var PORTC.7

cereal var byte
i var byte

For i = 0 to 30
serout TX, 2, [10]
next i

MainMenu:
serout Tx, 2, ["******** MAIN MENU ********", 13, 10, 10]
serout Tx, 2, [" 1. Start Homing Sequence", 13, 10]
serout Tx, 2, [" 2. Manual JOG Mode", 13, 10]
serout Tx, 2, [" 3. Auto-Acquire Satellite", 13, 10]
serout Tx, 2, [" 4. Stow Dish for Travel", 13, 10, 10]
serout Tx, 2, ["Enter Your Selection:", 13, 10]
Serin RX, 2, 500, MainMenu, cereal

serout TX, 2, [cereal] 'For debugging only
serout TX, 2, ["Dingleberry"]
goto MainMenu

EarlyBird2
- 24th May 2014, 09:28
I do not understand how you are getting [00]Dingleberry surely it should be 00Dingleberry.

Amoque
- 30th May 2014, 13:42
Your comment about getting "[00]Dingleberry" makes me wonder...

Why would you get two zeros? Your not formatting "cereal" with DEC - or any qualifier. Perhaps you should; perhaps you are getting an ASCII non printing character "printed" to your output (a non-printing character of ASCII value 1 for example)? It seems to me that somewhere, something is happening as the 00 is coming from somewhere.

You might also try adding a cereal = 65 prior to MainMenu; if you get an "A", then you'll know the serial input is getting nothing in the loop. If you don't... then something is changing the value...

EarlyBird2
- 30th May 2014, 14:29
Amoque,

I really do not understand the []. Where do they come from?

Amoque
- 31st May 2014, 13:14
Amoque,

I really do not understand the []. Where do they come from?

I don't know... I was quoting the OP. If the output is a direct quote, there is much about it I don't understand. Why TWO zeros (in addition to the brackets)? PicBasic doesn't default to double digit display; perhaps the subroutine fails and runs through another subroutine or the WDT causes a second response. Perhaps the brackets were only added for clarity.

For myself, I think that I'd:

1) Set a default value for Cereal, so as to know it was changed (or not).
2) Extend the timeout so that I could be sure that no amount of key-banging elicited an input.
3) Format the output as DEC or BIN so as to standardize the output.

thasatelliteguy
- 3rd June 2014, 02:19
It was the pins. I was on the hardware TX/RX pins and I guess it didnt like it.

Demon
- 3rd June 2014, 21:03
Do you mean it works now?

I wouldn't repaint the menu in my loop, I'd return right below it.

Robert