PDA

View Full Version : Serin2 and Button command



ero
- 24th February 2005, 08:04
Dear Friends,

Is there anybody can explain me why Serin2 and Button command do not work together?.
I write a simple program just receive some bytes from another pic and same program was controlling one button if it is pressed or not. If pressed it was planned to make one led on or off (Toggle).
When I start the program the serin2 command is working very well. But the button was not working. If I cancel (take out) the Serin2 command the button is working.

I looked to the registers by one simulating program the PORT A where the button is connected was never changing.
I tried also the SERIN command but the result was again same.
I need really your helps.

Thanks
ERO

mister_e
- 24th February 2005, 12:47
reason is simple... serin hserin serin2 will wait untill they receive at least the number of variable or character you decide to receive... once they receive, you'll be able to jump to your button statement. BUT you can use a timeout label to exit serin after xyz time and proceed to another statement and do a loop. be carefull with that. other statement must not take too much time and you time out label should not be too small too. If so, you have chance to miss or have strange result when receiving data.

If your PIC have USART, a great way is to use the USART interrupt.
OR using kinda flow control. One specific interrupt pin that tell to your PIC that data is knocking on your PIC's door. This pin make your program jump to your serial data reception.

ero
- 24th February 2005, 13:11
@miter_e,

Thank you for the reply. I was thinking that something was making busy the port and everything is clear now.
I will try your suggestions.

Thanks again
ERO

ero
- 25th February 2005, 11:37
@Mister_e,

Yesterday, I made a lot of try in order to find a way for using the button. I have to tell you the result is still neagtive.
First of all I used Tiemout and label and tried from 1 ms up to 250 ms. If I put 1 ms the button working very well but meanwhile serin does not receive any data. If use 2 ms. serin starts to receive datas and the button is working randomly. It means you push the button (continuously on) it is not working but if you make continuously on/off one moment the button is working.
All other figures from 3-250 ms. does not work.

I tried to use also RB0 interrupt for the serial input. In this way the serin is working very well but button again does not work.

I was using 16F628 pic and it has USART and I decided to use also HSERIN it is also not working. I was so nervous and finally I stop the work and I put mechanical switch to my system.
I gave this information just for your information.

Thanks
ERO

mister_e
- 26th February 2005, 00:09
do you still have your code ?!?

ero
- 26th February 2005, 07:28
@Mister_e,

I give you here the code. It is changed a lot of time but it is the last one which I used and by this program button is working a little bit for instance after 1-2 minutes it is making on and again 2-3 minutes later it making off also.

OPTION_REG.7=1

@ DEVICE pic16F628
@ DEVICE pic16F628, WDT_ON
@ DEVICE pic16F628, PWRT_ON
@ DEVICE pic16F628, PROTECT_OFF
@ DEVICE pic16F628, MCLR_OFF
@ DEVICE pic16F628, INTRC_OSC_NOCLKOUT

'---------------------------Variab..------------------------------
AL VAR BYTE
TOPLAM var byte
KOMBI VAR BYTE
GIRIS VAR PORTB.0
ROLE VAR PORTA.1
TUS var PORTA.6
LED VAR PORTB.3
YANDI VAR byte
SONDU VAR Byte
I VAR BYTE
'-----------------------------MAIN ---------------------------
CMCON=7
CLEAR:TRISA=%01100000
TRISB=%00000011
PAUSE 500:AL=88:sondu=0:KOMBI=0:LOW LED

'-------------------------------------------------------------------------------
START:

BUTTON TUS,0,200,5,I,1,BAK

SerIn2 GIRIS,1646,10,ATLA,[WAIT ("ER"),AL]

ATLA:
IF AL=88 THEN
IF SONDU=1 THEN ARA 'if already off then ara
GOSUB SON 'gosub off
GOTO START
ENDIF

IF AL=66 THEN
IF YANDI=1 THEN ARA 'if already on then ara
GOSUB YAN 'gosub on
GOTO START
ENDIF
pause 1

ARA: GOTO START
'--------------------if button is pushed -------------------------
BAK: IF KOMBI=1 THEN 'if already on then gosub offf
GOSUB SON
AL=88
GOTO START
ENDIF
GOSUB YAN 'it is already off then gosub on
AL=66
GOTO START

YAN: '------------------on section-------------------
KOMBI=1
YANDI=1:SONDU=0
HIGH ROLE
HIGH LED
pause 1
RETURN

SON: '-----------------off section---------------------
LOW LED:KOMBI=0
SONDU=1:YANDI=0
LOW ROLE
pause 1
RETURN

mister_e
- 27th February 2005, 10:30
some things can cause this. Let's look this snip



IF AL=88 THEN
IF SONDU=1 THEN ARA 'if already off then ara
GOSUB SON 'gosub off
GOTO START
ENDIF


You exit several time from a IF THEN and/or a nested IF THEN ELSE with a goto... not a good practice. You probably overflow the stack pointer.

as i read SONDU will be 0 or 1 so


ReturnToStart var Bit


Start:
ReturnToStart = 0

IF AL=88 THEN
IF SONDU=0 THEN
GOSUB SON 'gosub off
endif
ReturnToStart = 1
ENDIF
if ReturnToStart then GotoStart

about the BUTTON statement... i'm not a fan of this statement. i'll prefer do 2-3 lines myself.

Let's say i want to monitor a pushButon on PORTA.1


TRISA.1=1
PushButton VAR PORTA.1
start:
If PushButton then ' Pushbutton active
While PushButton ' wait untill it's release
Wend
Pause 50 ' debounce time
Gosub XYZ
endif

ero
- 28th February 2005, 07:58
Dear Steve,
Thank you for the tricks. You right I checked the program again and I saw that there are a lot of cunfusions. It was working but as you have said the reason can be the stack due to that cınfusions.Anyhow, I changed the program to make one loop only. I taken out all the zig-zags in the program. I did not yet tried. I was busy. I will try it within two days and let you know the result.

It would be good idea if I can check the program with one debugger. It is a pity that I have not mcs plus. By the way I would see where the program was going without touching the button command.

Thanks again
ERO

ero
- 1st March 2005, 08:46
Steve,

Yesterday evening, I tested all the possibilities again after I made a new program which is only one loop. It was checking the button first after it was passing to SERIN2 command and passing to control what was arrived by SERIN2. Anyhow I have to tell you that the serin or serin2 command does not work with any button control. I tested all the timeout figures starting from 2 ms up to 2000 ms. I am sure there is a bug in the compiler.
I tested again the HSERIN command by making some changing on the pcb and I saw that it is working very well. Today I will change all the PCB and the program for working with HSERIN command.

I wanted to give you again the result of my work which can be intersting for other people.
Thanks again for your helps.

Regards
Ero