PDA

View Full Version : How to read 4 el.mech.counters?



faaktoo
- 6th November 2009, 10:21
Hello !!!
I need help. I want to make a interface that will read impulses from 4 electro mechanical counters at once. The counters operate at different speeds and duration. I have solution for 2 counters, but for 4, hm? Please HELP


DEFINE OSC 8

pinin VAR PORTC.7
pinout VAR PORTC.6

TRISB = 255

B var byte

Serout2 pinout,84,["OK"]

Main:
B = PORTB
pause 10
If PORTB <> 255 then Counter
goto Main

Counter:
B = PORTB
SELECT CASE B

CASE 255
GOTO MAIN
CASE 254
Serout2 pinout,84,["1"] 'counter 1'
GOTO CEK
CASE 253
Serout2 pinout,84,["2"] 'counter 2'
GOTO CEK1
CASE 252
Serout2 pinout,84,["3"] 'counter 1 and counter 2'
GOTO CEK2
END SELECT

GOTO Counter

CEK:
B = PORTB
SELECT CASE B

CASE 255
GOTO MAIN
CASE 253
Serout2 pinout,84,["2"]
GOTO CEK1
CASE 252
Serout2 pinout,84,["2"]
GOTO CEK2
END SELECT

GOTO CEK

CEK1:
B = PORTB
SELECT CASE B

CASE 255
GOTO MAIN
CASE 254
Serout2 pinout,84,["1"]
GOTO CEK
CASE 252
Serout2 pinout,84,["1"]
GOTO CEK2
END SELECT

GOTO CEK1

CEK2:
B = PORTB
SELECT CASE B
CASE 255
GOTO MAIN
CASE 253
GOTO CEK1
CASE 254
GOTO CEK

END SELECT

GOTO CEK2

Darrel Taylor
- 7th November 2009, 02:05
Well, I can't offer any suggestions for fixing what you have, other than CTRL-A/DEL. :)

Give this a try with HyperTerminal or equivalent @ 19200 baud.
I'm assuming it's a 16F877.

<font color="#008000"><b>CLEAR </b></font><font color="#0000FF"><b><i>' Clear all User RAM
</i></b></font><font color="#008000"><b>DEFINE </b></font><b>OSC </b><font color="#800000"><b>8 </b></font><font color="#0000FF"><b><i>' change to match your crystal
</i></b></font><font color="#008000"><b>DEFINE </b></font><b>LOADER_USED </b><font color="#800000"><b>1 </b></font><font color="#0000FF"><b><i>' if using a bootloader

</i></b></font><font color="#008000"><b>DEFINE </b></font><b>HSER_BAUD </b><font color="#800000"><b>19200 </b></font><font color="#0000FF"><b><i>' Hser baud rate
</i></b></font><font color="#008000"><b>DEFINE </b></font><b>HSER_CLROERR </b><font color="#800000"><b>1 </b></font><font color="#0000FF"><b><i>' Hser clear overflow automatically
</i></b></font><font color="#008000"><b>DEFINE </b></font><b>HSER_RCSTA </b><font color="#800000"><b>90</b></font><b>h </b><font color="#0000FF"><b><i>' Hser receive status init
</i></b></font><font color="#008000"><b>DEFINE </b></font><b>HSER_TXSTA </b><font color="#800000"><b>24</b></font><b>h </b><font color="#0000FF"><b><i>' Hser transmit status init

</i></b></font><b>B </b><font color="#008000"><b>VAR BYTE
</b></font><b>LastB </b><font color="#008000"><b>VAR BYTE </b></font><font color="#0000FF"><b><i>' state of pins Last time they changed
</i></b></font><b>Diff </b><font color="#008000"><b>VAR BYTE </b></font><font color="#0000FF"><b><i>' Difference tween Last and Current bits
</i></b></font><b>Active </b><font color="#008000"><b>VAR BYTE </b></font><font color="#0000FF"><b><i>' Indicates which inputs are active(LOW)
</i></b></font><b>Idx </b><font color="#008000"><b>VAR BYTE </b></font><font color="#0000FF"><b><i>' generic loop counter
</i></b></font><b>Counter </b><font color="#008000"><b>VAR WORD</b></font>[<font color="#800000"><b>4</b></font>] <font color="#0000FF"><b><i>' The values of the counters

</i></b></font><b>OPTION_REG</b>.<font color="#800000"><b>7 </b></font>= <font color="#800000"><b>0 </b></font><font color="#0000FF"><b><i>' enable PORTB pull-ups

;---[clear screen + display menu]-------------------------------------------
</i></b></font><b>Refresh</b>:
<font color="#008000"><b>HSEROUT </b></font>[<font color="#800000"><b>27</b></font>,<font color="#FF0000">&quot;[2J&quot;</font>,<font color="#800000"><b>10</b></font>,<font color="#800000"><b>10</b></font>,<font color="#800000"><b>10</b></font>,<font color="#800000"><b>10</b></font>,<font color="#800000"><b>10</b></font>,<font color="#800000"><b>10</b></font>, <b>_
</b><font color="#FF0000">&quot; A=clear All, (1-4)=Clear Counter#, R=Refresh screen&quot;</font>]
<font color="#008000"><b>GOTO </b></font><b>DisplayCounters

Main</b>:
<font color="#0000FF"><b><i>;---[Handle incoming serial data]---------------------------------------
</i></b></font><font color="#008000"><b>IF </b></font><b>PIR1</b>.<font color="#800000"><b>5 </b></font><font color="#008000"><b>THEN </b></font><font color="#0000FF"><b><i>' If serial data has been received
</i></b></font><font color="#008000"><b>HSERIN </b></font>[<b>Idx</b>] <font color="#0000FF"><b><i>' get the byte
</i></b></font><font color="#008000"><b>SELECT CASE </b></font><b>Idx </b><font color="#0000FF"><b><i>' What did we receive?
</i></b></font><font color="#008000"><b>CASE </b></font><font color="#FF0000">&quot;A&quot;</font>,<font color="#FF0000">&quot;a&quot; </font><font color="#0000FF"><b><i>' A - Clear all counters
</i></b></font><font color="#008000"><b>FOR </b></font><b>Idx </b>= <font color="#800000"><b>0 </b></font><font color="#008000"><b>TO </b></font><font color="#800000"><b>3
</b></font><b>Counter</b>(<b>Idx</b>) = <font color="#800000"><b>0
</b></font><font color="#008000"><b>NEXT </b></font><b>Idx
</b><font color="#008000"><b>GOTO </b></font><b>DisplayCounters
</b><font color="#008000"><b>CASE </b></font><font color="#FF0000">&quot;R&quot;</font>,<font color="#FF0000">&quot;r&quot; </font><font color="#0000FF"><b><i>' Refresh ANSI Terminal screen
</i></b></font><font color="#008000"><b>GOTO </b></font><b>Refresh
</b><font color="#008000"><b>CASE </b></font><font color="#FF0000">&quot;1&quot;</font>,<font color="#FF0000">&quot;2&quot;</font>,<font color="#FF0000">&quot;3&quot;</font>,<font color="#FF0000">&quot;4&quot; </font><font color="#0000FF"><b><i>' numbers clear that counter only
</i></b></font><b>Counter</b>(<b>Idx </b>- <font color="#FF0000">&quot;1&quot;</font>) = <font color="#800000"><b>0
</b></font><font color="#008000"><b>GOTO </b></font><b>DisplayCounters
</b><font color="#008000"><b>CASE ELSE
HSEROUT </b></font>[<font color="#800000"><b>8</b></font>,<font color="#800000"><b>7</b></font>] <font color="#0000FF"><b><i>' send backspace + bell
</i></b></font><font color="#008000"><b>END SELECT
ENDIF
</b></font><font color="#0000FF"><b><i>'---[Count Pulses]------------------------------------------------------
</i></b></font><b>B </b>= <b>PORTB </b>&amp; <font color="#800000"><b>$F0 </b></font><font color="#0000FF"><b><i>' isolate upper 4-bits
</i></b></font><b>Diff </b>= <b>B </b>^ <b>LastB </b><font color="#0000FF"><b><i>' see if any of them changed
</i></b></font><font color="#008000"><b>IF </b></font><b>Diff </b>!= <font color="#800000"><b>0 </b></font><font color="#008000"><b>THEN </b></font><font color="#0000FF"><b><i>' if any of them changed
</i></b></font><b>LastB </b>= <b>B
Active </b>= <b>Diff </b>&amp; ~<b>B </b><font color="#0000FF"><b><i>' determine which ones are Active
</i></b></font><font color="#008000"><b>IF </b></font><b>Active </b>!= <font color="#800000"><b>0 </b></font><font color="#008000"><b>THEN
IF </b></font><b>Active</b>.<font color="#800000"><b>4 </b></font><font color="#008000"><b>THEN </b></font><b>Counter</b>(<font color="#800000"><b>0</b></font>) = <b>Counter</b>(<font color="#800000"><b>0</b></font>) + <font color="#800000"><b>1 </b></font><font color="#0000FF"><b><i>' increment actives
</i></b></font><font color="#008000"><b>IF </b></font><b>Active</b>.<font color="#800000"><b>5 </b></font><font color="#008000"><b>THEN </b></font><b>Counter</b>(<font color="#800000"><b>1</b></font>) = <b>Counter</b>(<font color="#800000"><b>1</b></font>) + <font color="#800000"><b>1
</b></font><font color="#008000"><b>IF </b></font><b>Active</b>.<font color="#800000"><b>6 </b></font><font color="#008000"><b>THEN </b></font><b>Counter</b>(<font color="#800000"><b>2</b></font>) = <b>Counter</b>(<font color="#800000"><b>2</b></font>) + <font color="#800000"><b>1
</b></font><font color="#008000"><b>IF </b></font><b>Active</b>.<font color="#800000"><b>7 </b></font><font color="#008000"><b>THEN </b></font><b>Counter</b>(<font color="#800000"><b>3</b></font>) = <b>Counter</b>(<font color="#800000"><b>3</b></font>) + <font color="#800000"><b>1
</b></font><font color="#008000"><b>GOTO </b></font><b>DisplayCounters
</b><font color="#008000"><b>ENDIF
ENDIF
PAUSE </b></font><font color="#800000"><b>20
</b></font><font color="#008000"><b>GOTO </b></font><b>Main

</b><font color="#0000FF"><b><i>'--[Display Counter Values]-------------------------------------------------
</i></b></font><b>DisplayCounters</b>:
<font color="#008000"><b>HSEROUT </b></font>[<font color="#800000"><b>27</b></font>,<font color="#FF0000">&quot;[H&quot;</font>] <font color="#0000FF"><b><i>' home cursor (ANSI)
</i></b></font><font color="#008000"><b>FOR </b></font><b>Idx </b>= <font color="#800000"><b>0 </b></font><font color="#008000"><b>TO </b></font><font color="#800000"><b>3 </b></font><font color="#0000FF"><b><i>' display all counter values
</i></b></font><font color="#008000"><b>HSEROUT</b></font>[<font color="#008000"><b>DEC </b></font>(<b>Idx</b>+<font color="#800000"><b>1</b></font>),<font color="#FF0000">&quot;: &quot;</font>,<font color="#008000"><b>DEC5 </b></font><b>Counter</b>(<b>Idx</b>),<font color="#800000"><b>13</b></font>,<font color="#800000"><b>10</b></font>]
<font color="#008000"><b>NEXT </b></font><b>Idx
</b><font color="#008000"><b>HSEROUT </b></font>[<font color="#800000"><b>13</b></font>,<font color="#800000"><b>10</b></font>]
<font color="#008000"><b>GOTO </b></font><b>Main
</b>

Cheers,

Ioannis
- 7th November 2009, 11:29
Nice programming Darrel.

I was just wondering, are really typing all these control characters < / i >< / b > < / font >< font color="#008000">< b > or is there a trick for this effects?

Ioannis

mackrackit
- 7th November 2009, 11:50
http://www.picbasic.co.uk/forum/showthread.php?t=11794
this is how it is done :)

Ioannis
- 7th November 2009, 12:25
As always there is the obvious in front of us but we try to see further....

Thanks,
Ioannis

Acetronics2
- 8th November 2009, 11:38
;---[clear screen + display menu]-------------------------------------------
</i></b></font><b>Refresh</b>:
<font color="#008000"><b>HSEROUT </b></font>[<font color="#800000"><b>27</b></font>,<font color="#FF0000">&quot;[2J&quot;</font>,<font color="#800000"><b>10</b></font>,<font color="#800000"><b>10</b></font>,<font color="#800000"><b>10</b></font>,<font color="#800000"><b>10</b></font>,<font color="#800000"><b>10</b></font>,<font color="#800000"><b>10</b></font>, <b>_
</b><font color="#FF0000">&quot; A=clear All, (1-4)=Clear Counter#, R=Refresh screen&quot;</font>]
<font color="#008000"><b>GOTO </b></font><b>DisplayCounters



Hi, Darrel

Could you explain the second Item in the list: "[2J"

and further



DisplayCounters:
HSEROUT [27,"[H"] ' home cursor (ANSI)


this one : "[H" ???

May be simple for you, but unknown for me !!!

BTW: Thanks for the example ... as I just begin serial comms use to pass parameters to and from my PICs and the PC. ( PC is an horrible black box ... I never look into, as you've understood : LOL )

Alain

Ioannis
- 8th November 2009, 13:30
Alain, this is the ANSI escape codes for the terminals. It does exactly what Darrel remarked, Home position in the terminal window.

Ioannis

Acetronics2
- 8th November 2009, 13:40
Hi, Ioannis

Thanks, but what I do not understand is ... both place cursor @ home position ...

further question will be :

is there any table or link to these codes ??? ...

Alain

Acetronics2
- 8th November 2009, 13:46
Ok Ioannis

With the right words ... Google is my friend !

http://ascii-table.com/ansi-escape-sequences.php

Thanks

Alain

Ioannis
- 8th November 2009, 16:37
Now I got what you meant Alain. But then you caught me on the turn. I was going to post the link for the Escape codes.

I tried them few times, but my terminal was getting confused often. I had to restart it to make it work properly.

Ioannis

faaktoo
- 9th November 2009, 08:57
Man. You are the best! I tried with 16F870 and works perfect.
Now I must write the counters data on internal eprom and send via RS485 to "master" computer on Request. I think I'll be able to in this. For now, thank you very,very much.

PS
I hope you will help me again if it is needed.
It's nice to have your own guru(mentor) :)