CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

i have to simulate a multiplexer with 6 inputs and 1 output

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ablincon



Joined: 20 Jan 2018
Posts: 2
Location: Aguascalientes

View user's profile Send private message

i have to simulate a multiplexer with 6 inputs and 1 output
PostPosted: Sat Jan 20, 2018 7:23 pm     Reply with quote

What i am trying to do is to develop a multiplexer, i have 4 inputs and 2 modifiers with 4 combinations: 0&0,1&0,0&1,1&1. So with every combination there is one output, with the 4 inputs A, B, C and D if the led is turned on, with 0&0 if i activate the switch of A the led is going to turn off and so on.

Code:

#include "C:\Users\abrah\Documents\ea.h"
#FUSES INTRC_IO //Oscilador Interno y tanto RA6 como RA7 son I/O
#FUSES NOWDT //Watch Dog Timer apagado
#FUSES PUT //Power Up Timer activado
#FUSES NOBROWNOUT //Reset ante variaciones de VCC desactivado
#FUSES NOIESO //Switch del oscilador en el encendido apagado
#FUSES NOFCMEN //detector de falla del oscilador principal apagado
#FUSES NOSTVREN //reset por desborde del stack desactivado
#FUSES NOLVP //Programación en baja tensión desactivada
#FUSES NOPROTECT //Protección contra lectura de la FLASH desactivada
#FUSES NOCPD //Protección de la EEPROM desactivada

#define A PIN_A0//set the pins
#define B PIN_A1
#define C PIN_A2
#define D PIN_A3
#define S1 PIN_B5
#define S2 PIN_B6
#define LD1 PIN_D2

int Sw1=0;//it states that Sw1 is 0 in the begining
int Sw2=0;
int Ai=0;
int Bi=0;
int Ci=0;
int Di=0;

//I am using pic 184550
//I want the multiplexer to change if the S inputs change

void main()
{

Sw1=input (S1);//Sw1 is an input
Sw2=input (S2);
Ai=input(A);//input
Bi=input(B);
Ci=input(C);
Di=input(D);


while(true)
{

if ((Sw1==0)&(Sw2==0)&(Ai==0))//if port b5, b6 and a0 is 0
output_high (LD1);//led turns on

if(input(PIN_A0)==1){//if input a0 (switch) is 1
output_toggle(PIN_D2);//output d2 led turns off
}

 
{

if ((Sw1==1)&(Sw2==0)&(Bi==0))
output_high (LD1);

if (input(PIN_A1)==1){
output_toggle(PIN_D2);
}
 
{

if ((Sw1==0)&(Sw2==1)&(Ci==0))
output_high (LD1);

if (input(PIN_A2)==1) {
output_toggle(PIN_D2);
}

{

if ((Sw1==1)&(Sw2==1)&(Di==0))
output_high (LD1);

if (input(PIN_A3)==1) {
output_toggle(PIN_D2);
   
}
 delay_ms(500);
}
}
}
}
}


//what do i need to do for you to copy/paste the program
_________________
hey i just want to learn


Last edited by ablincon on Sun Jan 21, 2018 10:29 am; edited 5 times in total
temtronic



Joined: 01 Jul 2010
Posts: 9253
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Jan 20, 2018 9:41 pm     Reply with quote

When posting for help, you need to supply a complete program that has the PIC header, fuses, etc. It needs to be able to be 'copy/pasted/compiled' so that we can test on our computers.
Also you should add //comments to your code. Usually most lines need some kind of comment to describe what action is to taken, what a variable is used for, etc. You'll notice in the examples that CCS supplies, often there's a paragraph at the beginning of the program that explains the basic operation of the program.
Also be aware that switches tied to input pins require some for of debouncing or filtering (say a .1mfd cap across the contacts) to ensure proper operation.

Jay
temtronic



Joined: 01 Jul 2010
Posts: 9253
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Jan 21, 2018 6:38 am     Reply with quote

much better !
couple more edits though..

1) add indentation for the conitional IF (....)

something like...

if (input(PIN_A3)==1) {
output_toggle(PIN_D2);
}

the braces {} show what is to happen
the indented line of code, keeps it visually organized
both of these make it easier to spot silly typing mistakes.


2) add a delay at the end of the main() loop,say delay_ms(500);. This gives the overall program a +-1/2 second timing.

...
check pins
do tests
control LEDs
delay
...back to check pins

Also returns are not needed

and you still need a processor include as the first list

like ...#include <18f46k22.h>
unless it's in the .h
#include "C:\Users\abrah\Documents\ea.h"
If so, you should add a comment which PIC you're using.While you know now what it is,we don't and maybe that PIC doesnt have those PINs, or fuses. We also can't copy/paste/compile.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19561

View user's profile Send private message

PostPosted: Sun Jan 21, 2018 9:34 am     Reply with quote

It's perhaps worth separating the tasks. Also there is the question of whether you want the multiplexer to change if the S inputs change?. As shown they are read once at the start, when normally they need to be tested every time round the loop. You then confuse things by testing these at the start, and performing actions based on these values, and yet also testing inputs separately, using the pin names, rather than the defines. Use the defines. Not actually quite sure of what you really want to do (a multiplexer would just duplicate the selected input to the output), but an approach like:
Code:

//headers omitted

int8 S; //Since the inputs are always read before this is used,
//it does not need to be initialised.

void main()
{
    while (TRUE)
    { //assuming the switches really do need to update every loop

       S=input (S1) & input(S2)*2;
       //S now has a number 0 to 3, reflecting the four possible combinations
       //of S1 & S2
       switch (S) {
       case 0b00:
          //Both S1 and S2 low
          output_bit(LD1, input(A)); //copy the A input to the output
          break;
      case 0b01:
          //S1 high S2 low
          output_bit(LD1,input(B)); //copy the B input to the output
          break;
      case 0b10:
          //S2 high and S1 low
          output_bit(LD1, input(C)); //copy the C input to the output
          break;
      case 0b11:
          //Both S1 & S2 high
          output_bit(LD1,input(D)); //copy the D input to the output
          break;
      }
   }
}


This is a multiplexer mirroring the pin selected by the pattern on S1/S2 to LD1.
ablincon



Joined: 20 Jan 2018
Posts: 2
Location: Aguascalientes

View user's profile Send private message

PostPosted: Sun Jan 21, 2018 10:31 am     Reply with quote

Ttelmah wrote:
It's perhaps worth separating the tasks. Also there is the question of whether you want the multiplexer to change if the S inputs change?. As shown they are read once at the start, when normally they need to be tested every time round the loop. You then confuse things by testing these at the start, and performing actions based on these values, and yet also testing inputs separately, using the pin names, rather than the defines. Use the defines. Not actually quite sure of what you really want to do (a multiplexer would just duplicate the selected input to the output), but an approach like:
Code:

//headers omitted

int8 S; //Since the inputs are always read before this is used,
//it does not need to be initialised.

void main()
{
    while (TRUE)
    { //assuming the switches really do need to update every loop

       S=input (S1) & input(S2)*2;
       //S now has a number 0 to 3, reflecting the four possible combinations
       //of S1 & S2
       switch (S) {
       case 0b00:
          //Both S1 and S2 low
          output_bit(LD1, input(A)); //copy the A input to the output
          break;
      case 0b01:
          //S1 high S2 low
          output_bit(LD1,input(B)); //copy the B input to the output
          break;
      case 0b10:
          //S2 high and S1 low
          output_bit(LD1, input(C)); //copy the C input to the output
          break;
      case 0b11:
          //Both S1 & S2 high
          output_bit(LD1,input(D)); //copy the D input to the output
          break;
      }
   }
}


This is a multiplexer mirroring the pin selected by the pattern on S1/S2 to LD1.

Yes i want the multiplexer to change if the S inputs change
_________________
hey i just want to learn
Ttelmah



Joined: 11 Mar 2010
Posts: 19561

View user's profile Send private message

PostPosted: Sun Jan 21, 2018 12:04 pm     Reply with quote

Slightly puzzled, since 6 inputs would require a minimum of 3 address bits. Your example shows just two, and only four input pins. However the approach I posted will work for any number of inputs. Just read the third input pin and multiply this by 4, then make the case lines be for '0b000' etc..
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Mon Jan 22, 2018 5:37 am     Reply with quote

Ttelmah wrote:
Slightly puzzled, since 6 inputs would require a minimum of 3 address bits. Your example shows just two, and only four input pins.


Yes, but don't take the 6 inputs quite so literally. It's clear that what the poster meant was six "inputs": four actual inputs and two address/select lines. What he's after is a simple 4:1 mux, but doesn't, yet, quite know the naming conventions of such things.
Ttelmah



Joined: 11 Mar 2010
Posts: 19561

View user's profile Send private message

PostPosted: Mon Jan 22, 2018 5:54 am     Reply with quote

Well, that is what I posted, so hopefully he will find it does what is needed. Smile
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Mon Jan 22, 2018 6:06 am     Reply with quote

It's interesting that we get two simulations of MSI logic functions in quick succession. Coincidence? probably, but maybe not...
Ttelmah



Joined: 11 Mar 2010
Posts: 19561

View user's profile Send private message

PostPosted: Mon Jan 22, 2018 6:50 am     Reply with quote

Have to agree. Probably a particular course, where the teacher starts by trying to show the students that you can simulate almost any simple logic with a micro-controller.
johnmarti42



Joined: 21 Jan 2018
Posts: 3

View user's profile Send private message

what this is for S=input (S1) & input(S2)*2;
PostPosted: Thu Jan 25, 2018 8:10 am     Reply with quote

ablincon wrote:
Ttelmah wrote:
It's perhaps worth separating the tasks. Also there is the question of whether you want the multiplexer to change if the S inputs change?. As shown they are read once at the start, when normally they need to be tested every time round the loop. You then confuse things by testing these at the start, and performing actions based on these values, and yet also testing inputs separately, using the pin names, rather than the defines. Use the defines. Not actually quite sure of what you really want to do (a multiplexer would just duplicate the selected input to the output), but an approach like:
Code:

//headers omitted

int8 S; //Since the inputs are always read before this is used,
//it does not need to be initialised.

void main()
{
    while (TRUE)
    { //assuming the switches really do need to update every loop

       S=input (S1) & input(S2)*2;
       //S now has a number 0 to 3, reflecting the four possible combinations
       //of S1 & S2
       switch (S) {
       case 0b00:
          //Both S1 and S2 low
          output_bit(LD1, input(A)); //copy the A input to the output
          break;
      case 0b01:
          //S1 high S2 low
          output_bit(LD1,input(B)); //copy the B input to the output
          break;
      case 0b10:
          //S2 high and S1 low
          output_bit(LD1, input(C)); //copy the C input to the output
          break;
      case 0b11:
          //Both S1 & S2 high
          output_bit(LD1,input(D)); //copy the D input to the output
          break;
      }
   }
}


This is a multiplexer mirroring the pin selected by the pattern on S1/S2 to LD1.

Yes i want the multiplexer to change if the S inputs change


what the *2 function is for?
Ttelmah



Joined: 11 Mar 2010
Posts: 19561

View user's profile Send private message

PostPosted: Thu Jan 25, 2018 8:35 am     Reply with quote

I'm creating a value from 0 to 3, from the 'binary' values from the two inputs.

The input from S2, needs to either be shifted or multiplied by two to move it to the next bit. The compiler is smart and will replace *2, with a shift, so you end or with a two bit value, with the value read from S1 in the bottom bit, and S2 in the next bit.

Remember this needs S1, and S2 #defined as shown in the original post, or to match whatever pins you want, or it won't work/compile.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group