Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 16:49 11 May 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Magic switch board for pic18f45k22

Author Message
tomersonn11
Newbie

Joined: 04/07/2014
Location: Israel
Posts: 2
Posted: 04:17am 07 Jul 2014
Copy link to clipboard 
Print this post

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.

What i'm trying to do here is to send the "BUTTONS_ARRAY" after the learning mode in the "main" to the "BUTTONS_ARRAY_REPLAY" function and the output from this function to the "LEDS_ARRAY_REPLAY". Unfortunately it doesn't work. Can someone able to help me with this?

this is the code (for PIC18F45K22, mikro c version 6):

// Magic switch board for pic18k45f22
short number,state,tmp=0;
int i,j;
int LEDS_ARRAY[4]={17,17,17,17}; // Array that holds the lighting sequence of LATC
int BUTTONS_ARRAY[4]={17,17,17,17}; // Array that holds the lighting sequence of LATB
int flag_0=0, flag_1=0, flag_2=0, flag_3=0;

sbit red_BUTTON at LATB0_BIT;
sbit green_BUTTON at LATB1_BIT;
sbit yellow_BUTTON at LATB2_BIT;
sbit blue_BUTTON at LATB3_BIT;
sbit red_LED at LATC0_BIT;
sbit green_LED at LATC1_BIT;
sbit yellow_LED at LATC2_BIT;
sbit blue_LED at LATC3_BIT;

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

switch(number)
{
case 1:
red_LED=1;
yellow_LED=green_LED=blue_LED=0;
LEDS_ARRAY[0]=1;
break;
case 2:
red_LED=green_LED=1;
yellow_LED=blue_LED=0;
LEDS_ARRAY[1]=2;
break;
case 3:
red_LED=green_LED=yellow_LED=1;
blue_LED=0;
LEDS_ARRAY[2]=3;
break;
case 4:
red_LED=green_LED=yellow_LED=blue_LED=1;
LEDS_ARRAY[3]=4;
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 LEDS_ARRAY_REPLAY ( int i)
{
if (i==0)
red_LED=1;
if (i==1)
green_LED=1;
if (i==2)
yellow_LED=1;
if (i==3)
blue_LED=1;
}

int BUTTONS_ARRAY_REPLAY (int BUTTONS_ARRAY[])//
{
while (1)
{

if (red_BUTTON==1)
tmp=1;
if (green_BUTTON==1)
tmp=2;
if(yellow_BUTTON==1)
tmp=3;
if (blue_BUTTON==1)
tmp=4;
for (i=0;i<=3;i++)
{
if (tmp==BUTTONS_ARRAY)
return i;
}
}
}

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

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

while(1)
{ int i=0;
while (i<=3) // The learning stage
{
if (flag_0==0)
{
if (LATB0_BIT==1)
{
BUTTONS_ARRAY=1;

flag_0=1;
i++;
}
}
if (flag_1==0)
{
if (LATB1_BIT==1)
{
BUTTONS_ARRAY=2;

flag_1=1;
i++;
}
}
if (flag_2==0)
{
if (LATB2_BIT==1)
{
BUTTONS_ARRAY=3;

flag_2=1;
i++;
}
}
if (flag_3==0)
{
if (LATB3_BIT==1)
{
BUTTONS_ARRAY=4;

flag_3=1;
i++;
}
}
number=LATB&0X0F;
number=CountBit(number);
Light(number);
}// End of learning stage
Delay_ms(500);
LATC=0;
LATB=0;

BUTTONS_ARRAY_REPLAY (BUTTONS_ARRAY);
LEDS_ARRAY_REPLAY (i);
}if (LATB==0)
red_LED==green_LED==yellow_LED==blue_LED==0;
}


 
akashh
Senior Member

Joined: 19/01/2014
Location: India
Posts: 115
Posted: 04:58am 07 Jul 2014
Copy link to clipboard 
Print this post

I didn't get too far into your code but I think you would want to use pointers. Pass a pointer to your array of integers with int*arr in your function definition.
You can then refer to the various elements like this: arr[0], arr[1], etc.
I am not exactly sure whether that was your question?
 
tomersonn11
Newbie

Joined: 04/07/2014
Location: Israel
Posts: 2
Posted: 05:10am 08 Jul 2014
Copy link to clipboard 
Print this post

Thanks for the help i found the problem and fixed it. I have another problem now, is it ok to use definition like this one- (LATB0_BIT) at pic18f45k22?
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025