# input and save input

#### MrDEB

##### Well-Known Member
Not sure where to start but need some advice on where to start.

Am contemplating an 8 player board game using a pic and leds. Thinking that I need to write to the ROM? But where to start.

Each player has a monetary push button

when the START led is enabled then all the players press the button in front of them thus telling the pic location of each player. Players at position 1,3,4,5,& 7. Thus port.bits 1,3,4,5 &7 are driven LOW

Each player releases their button switch and play begins.

When the led in front of each player is enabled it is that players turn to play until his / her is timed out

The play proceeds to the next player (#3 in example) until player #7 plays then back to player #1

How do I write to the ROM or RAM to enable only the port bits 1,3,4,5,&7 only.

When I read the bits in the port, how to save which bits are viable (LOW at the beginning of game) and continue until end of game.

#### Rich D.

##### Active Member
Yes, this answer / response is somewhat vague because the question is vague. Much more details are needed to give a reasonable answer.

Sounds like you are asking "How do you program micro-controllers?", first...which PIC, there are hundreds and hundreds.
If so, that is one BIG question! There are thousands of books, web sites, classes, and even whole schools devoted to this BIG question. No single thread of responses can provide all the information.

Or maybe you are asking specifically about non-volatile memory? Even that is a very big question. We have EPROMS, EEPROMS, battery-backed static RAM, or battery-backed processors that stay on and remember. There's a lot of options to make memory appear permanent, the choice depending on data size and length of time required.

Do you really even need electronics to store only 8 bits of data? A few logic chips could do that. Also consider on/off push switches or even other mechanical only means to remember player states. Heck, in some systems pneumatics are used to create simple state machines with logic and memory aspects.

#### MrDEB

##### Well-Known Member
This is the start of my code. The private sub route only has one pass to poll the switches.
By polling the switches I get # of players and their switch position.
DEVICE = 18f2221
CLOCK = 8
INCLUDE "INTOSC8.bas"
INCLUDE "SetDigitalIO.bas"

DIM PRESS AS BYTE // end of players turn portB.5 used as an index pointer
DIM Time AS WORD
DIM index AS WORD
'DIM set AS portc.0 // CHANGES DELAYMS VARIABLE test
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

// PORTC IS PLAYERS LED INDICATION

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
DIM x AS BYTE // BYTE IN FOR NEXT LOOPS

DIM led0 AS portc.0 //led test
DIM led1 AS portc.1
DIM led2 AS portc.2
DIM led3 AS portc.3
DIM led4 AS portc.4
DIM led5 AS portc.5 //led test
DIM led6 AS portc.6
DIM led7 AS portc.7
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

//PORTB ARE THE PLAYERS BUTTONS

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
DIM swt0 AS portB.0
DIM swt1 AS PORTB.1
DIM swt2 AS portB.2
DIM swt3 AS PORTB.3
DIM swt4 AS portB.4
DIM swt5 AS PORTB.5
DIM swt6 AS portB.6
DIM swt7 AS PORTB.7

DIM Buttonpress AS BYTE

DIM counter AS WORD

PRIVATE SUB START_GAME ()
//POLL THE SWITCHES FOR # OF PLAYERS AND SWITCH POSITION
IF swt0 = 1
THEN
led0 = 1
ELSE swt0=0
END IF

IF swt1 = 1
THEN
led1 = 1
ELSE swt1=0
END IF

IF swt2 = 1
THEN
led2 = 1
ELSE swt2=0
END IF

IF swt3 = 1
THEN
led3 = 1
ELSE swt3=0
END IF

IF swt4 = 1
THEN
led4 = 1
ELSE swt4=0
END IF

IF swt5 = 1
THEN
led5 = 1
ELSE swt5=0
END IF

IF swt6 = 1
THEN
led6 = 1
ELSE swt6=0

IF swt7 = 1
THEN
led7 = 1
ELSE swt7=0
END IF
END SUB

OUTPUT (led0)
OUTPUT (led1)
OUTPUT (led2)
OUTPUT (led3)
OUTPUT (led4)
OUTPUT (led5)
OUTPUT (led6)
OUTPUT (led7)

INPUT (swt0)
INPUT (swt1)
INPUT (swt2)
INPUT (swt3)
INPUT (swt4)
INPUT (swt5)
INPUT (swt6)
INPUT (swt7)

trisb =%00000000
trisc =%00000000
trisA =%00000000
PRESS = 0
Buttonpress =1
//SWITCHES
swt0 = 0
swt1 = 0
swt2 = 0
swt3 = 0
swt4 = 0
swt5 = 0
swt6 = 0
swt7 = 0
//PLAYER LEDS
led0 =0
led1 =0
led2 =0
led3 =0
led4 =0
led5 =0
led6 =0
led7 =0
Time = 1000
index = 0
WHILE true
START_GAME() // DO THIS ONLY ONCE FOR SAVING THE # OF PLAYERS AND POSITION ON BOARD (0 TO 8)
// IF PLAYERS ARE LOCATED AT POSITIONS 1, 3, 6, 7) = PORTb=01010011

WEND

#### be80be

##### Well-Known Member
ROM or RAM you don't need to write to there to save. Buttons pressed just save the start press and use it.
Now if you was wanting to save a game for later then yes you would save it to eeprom for the next time you play the game.

#### JonSea

##### Well-Known Member
You have programmed before, right?

1. You need a "SetAllDigital" command in the initial part of the program to set Port A to digital. You had this exact problem in your last saga here. It's part of: INCLUDE "SetDigitalIO.bas", but merely including it does nothing.

2.
Code:
IF swt0 = 1
THEN
led0 = 1
ELSE swt0=0
END IF
Swt0 is the state of the port pin. It is either 0 or 1 when you read the port. The else cause above is just wrong and not necessary. If it is not 1 per the IF statement, then it must be 0 without any help from you.

3.
Code:
OUTPUT (led0)
OUTPUT (led1)
OUTPUT (led2)
OUTPUT (led3)
OUTPUT (led4)
OUTPUT (led5)
OUTPUT (led6)
OUTPUT (led7)

INPUT (swt0)
INPUT (swt1)
INPUT (swt2)
INPUT (swt3)
INPUT (swt4)
INPUT (swt5)
INPUT (swt6)
INPUT (swt7)

trisb =%00000000
trisc =%00000000
trisA =%00000000
The output statements are needed. Digital pins are defined as inputs by default. The input statements are not needed.

The tris statements accomplish the same thing as the INPUT and OUTPUT statements and are not necessary. In fact, in this case, the trisB statement conflicts with what you want.

This makes no sense whatsoever, even with blatant errors corrected. Good luck.

#### MrDEB

##### Well-Known Member
What I posted was a beginning draft. I failed to use the setalldigital, thanks for the reminder.
Will remove the tris statements as well. I like using the INPUT and OUTPUT statements. Learned that from studying Jonseas code format.
Last week I was looking for the order that code needs to be in. I had it saved from the DDIY site but??
I discovered that writing code snippets and running them seems to help find bugs etc.
My next step is run the posted code but insert some sort of indicator that the code is running its coarse AND doing what it is meant to do.
Not sure if using the PRIVATE SUB ROUTINE will prevent the port bits from changing state. It should hopefully?
I have a sub routine that enables the players to input the desired elapsed time for each player that I will insert after I confirm that the # of players and their board position is correct.

#### Pommie

##### Well-Known Member
I know I'm going to regret this but .... think about the bit pattern on the switches and LEDs.

Mike.

#### MrDEB

##### Well-Known Member
That is exactly what I am thinking. I even considered using a CONSTANT array but there are 64 different combinations if my math is correct.
Am attempting to save the state of the switch at the beginning
IF swt0 = 1
THEN
led0 = 1
ELSE swt0=0
END IF
then continue on. This is suppose to save the state of each switch and position of each player thus 10000000 NOTE I may have this backwards?
Have seen examples of using the ELSE statement to be ure that swt0 is in fact 0

#### JonSea

##### Well-Known Member
The switch is the state of a port pin. If it's not 1, what do you suppose it might be? 17? 42?

#### MrDEB

##### Well-Known Member
On the subject of using ELSE, I am using an example that I have seen used many times.
As for the chip selection, I am running out of pins and looking at using a 44 pin device. Leary of the K series etc. Needs more research but contemplating charileplexing the 6 leds that are used to indicate the amount of time each player has left before losing their turn.
Researching tri-state a pins output.

#### JonSea

##### Well-Known Member
On the subject of using ELSE, I am using an example that I have seen used many times.....
I am quite certain the only pkace you have seen this used any time is in your code, because it makes absolutely no sense and doesn't do what you think it does.
.
Each of your switches is dimensioned to one of the port pins which is set to be an input. Thetefore, Swt1 always reflects the value of its digital port pin which can be either high or low, corresponding to 1 or zero. Those are the only two possible options.

Code:
If Swt1 = 1 then
(do stuff)
else
Swt1 =0
End if
If Swt1 is not equal to 1, what MUST it equal? ZERO. There is no other option. This test is idiotic.

But guess what? Even if it wasn't ZERO ALREADY, YOU don't get to set it except by changing the physical level on the port pin.

Regarding which chip to use, you proposed a mangled K40-series part number. All I said was that the K40-series part yoi suggested might not work with Swordfish, and recommended an alternative this is well supported.

#### be80be

##### Well-Known Member
If do this
Else
Do this
Dont need to do but a if if your not
Going to do something else

#### Pommie

##### Well-Known Member
Does writing to a pin in Swordfish set it to output?

Mike.

#### JonSea

##### Well-Known Member
Does writing to a pin in Swordfish set it to output?

Mike.
I do not believe so. If a pin is not declared as an output, setting it to some level doesn't work.

#### Pommie

##### Well-Known Member
I know that the functions high() and low() both set the pin to output but not sure what a write would do.

Mike.

#### tumbleweed

##### Active Member
Writing to a pin (ie Swt1 = 1) sets the port LAT bit. It doesn't do anything to the pin direction, so if it's not already an output you won't see it.

#### MrDEB

##### Well-Known Member
working on this code but looking for a pic that Swordfish supports preferably a 40+ pin count or result to charilplexing the 6 leds or some other method?
can't find any info on using swordfish to chariplexing. Blueroom electronics had an example but??
Looking over the supported devices on the Swordfish site and micro-chip site trying to sort things out.
In other words rethinking the basic project but the basic code will progress to iron out timing sequence and saving the player positions and # of players/

#### JonSea

##### Well-Known Member
Here are some hints:

1. You are not using any special features of the microcontrollers, e.g., you don't need to search high and low. A 40 pin variant of whatever 28 pin PIC will work fine. The 18F4520 should be a safe, hassle free choice (or the low voltage version if you wish).

My default choice has become the 18F25K22 or 18F26K22 for more memory, so the 18F45k22 or 18F46k22 would not be bad choices either, but there are still some issues with Swordfish supporting these chips fully. I really would not recommend them for you because your troubleshooting skills would make it tough for you to differentiate code problems from hardware issues.

2. Charlieplexing: Charlieplexing is Charlieplexing. Nothing special is needed from Swordfish to support this. It's the proper combination of output levels on pins (and understanding how to connect everything). Any article on Charlieplexing is directly applicable to Seordfish.

3. Number of players in the game/active positions: This requires a variable just like anything else. There is no reason it should be stored in EEPROM. You could store the active/inactive state of 8 players in a byte. Each bit of rhe byte represents a player. Bit 0 = 0 -->position 1 is not used. Bit 0 = 1 --> position 1 is being used.

Or, since this code shouldn't be very complex and memory-hungry, set up an array of 9 elements.

Dim Player(9)

Then, if Player(1) = 1, position 1 is being used.

Or, maybe set up a boolean variable for each player:

Dim Player1 as Boolean
Dim Player2 as Boolean

Dim Player8 as Boolean

Then, a player is true or false depending on if a position is in use.

Last edited:

#### MrDEB

##### Well-Known Member
I really want to stay away from the K series as several people have posted about their issues using the K series.
As for the Player(x) , that's where I am headed after contemplating last night after going to bed. For some reason I come up with ideas at bd time?
I have my player time-out working, had that way back in September and just getting around to inserting the snippet into the code but the time-out leds(6 of them added issues with # of I/O pins needed. Reason why I was leaning towards chariplexing. Even contemplated a resistor ladder to indicate players but don't think it will work but haven't discounted the idea.

#### be80be

##### Well-Known Member
8 players one port for 8 buttons. That leaves 14 for leds on a 28 pin chip and 24 on a 40 pin

You would be better to layout your hardware then pick a chip plus maybe we could see what you really have in mind.