Libstock prefers package manager

Package Manager

We strongly encourage users to use Package manager for sharing their code on Libstock website, because it boosts your efficiency and leaves the end user with no room for error. [more info]

Project Request

Return
Tomer Sonnenschien
tomersonn11

posted on 2014/07/04 03:20:23 PM CEST

Other Codes

Magic switch board for pic18f45k22

Hi everyone,

I'm trying to do a verison of mine to the magic switch board and since i'm new in this business,i would appreciate your help.
As a first stage i want to write a program which has three phases.

First phase- learning mode: the user turns on the four first bits of "PORT B" on. Each bit he turns-on does the following things: 1) turns-on one of the four first bits of "PORT C" on. (it doesn't matter which bit he touches and when, the first four bits of "PORT C" will be turned on from right to left)
2) every bit of "PORTB" will get a number from 1-4 accordingly, and the numbers will be sent into array in the order the user turned them on(For example, if the user turned on the third bit of "PORT B" first, the first member in the array will be 3) . Each time a number gets into the array it will be locked to the bit at "PORT C" which it turns-on (for example- 3 turns on the third bit of "PORT C", 1 turns-on the zero bit of "PORT C"...)

Second phase- Play back mode: now that the system knows which bit at "PORT B" lights which bit at "PORT C" each time the user will touch a bit from "PORT B" it will turn on the specific bit from "PORT C" which he was assigned to.

Third phase- Zeroing the system: after about 10 seconds the system will retrieve its Initial Settings.

Here is an example to the magic switch board: http://www.youtube.com/watch?v=0lGP8nQLANU

this is the code which i'm working in: // Magic switch board for pic18k45f22
short number,state,tmp;
int i=0;
int connect[4]={17,17,17,17}; // Array that holds the lighting sequence of LATC
int flag_0=0, flag_1=0, flag_2=0, flag_3=0;
sbit red at LATC0_BIT;
sbit green at LATC1_BIT;
sbit yellow at LATC2_BIT;
sbit blue at LATC3_BIT;

void Light(short number ) // Function that lights up LATC
{

switch(number)
{
case 1:
red=1;
yellow=green=blue=0;
break;
case 2:
red=green=1;
yellow=blue=0;
break;
case 3:
red=green=yellow=1;
blue=0;
break;
case 4:
red=green=yellow=blue=1;
break;
}
} // End of void Light function

short CountBit(short num) // Function that converts binary number to decimal number
{
short b,c;
for (b=0; num != 0; num = num >> 1)

if (num &1)
b++;
return b;
}

void Light_2(short state)
{
for (i=0;i<=3;i++)
{
tmp=state &1;
if (tmp)
Light (connect[i]);
state=state>>1;
}
}

void main()
{
ANSELC=0;
ANSELB=0;

TRISC=0;
LATC=0;
number=LATB&0X0F;

while(1)
{
while (i<=3) // The teaching stage
{
if (flag_0==0)
{
if (LATB0_BIT==1)
{
connect[i]=1;
flag_0=1;
i++;
}
}
if (flag_1==0)
{
if (LATB1_BIT==1)
{
connect[i]=2;
flag_1=1;
i++;
}
}
if (flag_2==0)
{
if (LATB2_BIT==1)
{
connect[i]=3;
flag_2=1;
i++;
}
}
if (flag_3==0)
{
if (LATB3_BIT==1)
{
connect[i]=4;
flag_3=1;
i++;
}
}
number=LATB&0X0F;
number=CountBit(number);
Light(number);
}// End of teaching stage

number=LATB&0X0F;
number=CountBit(number);
Light(number);


if (state!=PORTB)
{
state=PORTB;
Light_2(state);
}

}if (LATB==0)
red==green==yellow==blue==0;
}