16x04 Encoder from ATmega328 Micro Controller

Hi I am new to Programming and Microcontrollers I want to make 16 line Input to 4 Line output ENCODED by ATmega328 Microcontroller I have 2 New ATmega328 Microcontrollers (Without Bootloader). I have Arduino Board with ATmega328 Kit. do I need extra Programmer to Program the Atmega328 Microcontroller? if not. How to program the other ATmega328 with the ARDUINO 328 Kit??? please Help

The code I prepared are as follows:-

//16 x 4 Line Encoder
// 16 input 
const int tr_01Pin =  1;  //  Input From Tr. BC547- 01
const int tr_02Pin =  2;  //  Input From Tr. BC547- 02
const int tr_03Pin =  3;  //  Input From Tr. BC547- 03
const int tr_04Pin =  4;  //  Input From Tr. BC547- 04
const int tr_05Pin =  5;  //  Input From Tr. BC547- 05
const int tr_06Pin =  6;  //  Input From Tr. BC547- 06
const int tr_07Pin =  9;  //  Input From Tr. BC547- 07
const int tr_08Pin = 10;  //  Input From Tr. BC547- 08
const int tr_09Pin = 11;  //  Input From Tr. BC547- 09
const int tr_10Pin = 12;  //  Input From Tr. BC547- 10
const int tr_11Pin = 13;  //  Input From Tr. BC547- 11
const int tr_12Pin = 14;  //  Input From Tr. BC547- 12
const int tr_13Pin = 15;  //  Input From Tr. BC547- 13
const int tr_14Pin = 16;  //  Input From Tr. BC547- 14
const int tr_15Pin = 17;  //  Input From Tr. BC547- 15
const int tr_16Pin = 18;  //  Input From Tr. BC547- 16


//BCD OUTPUT (A B C D as 0 0 0 0 - 1 1 1 1)


const int OUT_A = 25;  // Output From A
const int OUT_B = 26;  // Output From B
const int OUT_C = 27;  // Output From C
const int OUT_D = 28;  // Output From D




void setup()


{
pinMode(tr_01Pin,INPUT);  //  0000
pinMode(tr_02Pin,INPUT);  //  0001
pinMode(tr_03Pin,INPUT);  //  0010
pinMode(tr_04Pin,INPUT);  //  0011
pinMode(tr_05Pin,INPUT);  //  0100
pinMode(tr_06Pin,INPUT);  //  0101
pinMode(tr_07Pin,INPUT);  //  0110
pinMode(tr_08Pin,INPUT);  //  0111
pinMode(tr_09Pin,INPUT);  //  1000
pinMode(tr_10Pin,INPUT);  //  1001
pinMode(tr_11Pin,INPUT);  //  1010
pinMode(tr_12Pin,INPUT);  //  1011
pinMode(tr_13Pin,INPUT);  //  1100
pinMode(tr_14Pin,INPUT);  //  1101
pinMode(tr_15Pin,INPUT);  //  1110
pinMode(tr_16Pin,INPUT);  //  1111


pinMode(OUT_A,OUTPUT);
pinMode(OUT_B,OUTPUT);
pinMode(OUT_C,OUTPUT);
pinMode(OUT_D,OUTPUT);
  
}


void loop()


{
 if(tr_01Pin == 0)  //  0000 = 00
 {
 OUT_D == 0;  // BCD OUTPUT
 OUT_C == 0;  // BCD OUTPUT
 OUT_B == 0;  // BCD OUTPUT
 OUT_A == 0;  // BCD OUTPUT
 }

 else

 if(tr_02Pin == 1)  //  0001 = 01
 {
 OUT_D == 1;  // BCD OUTPUT
 OUT_C == 0;  // BCD OUTPUT
 OUT_B == 0;  // BCD OUTPUT
 OUT_A == 0;  // BCD OUTPUT
 } 

 else

 if(tr_03Pin == 1)  //  0010 = 02
 {
 OUT_D == 0;  // BCD OUTPUT
 OUT_C == 1;  // BCD OUTPUT
 OUT_B == 0;  // BCD OUTPUT
 OUT_A == 0;  // BCD OUTPUT
 }

 else

 if(tr_04Pin == 1)  //  0011 = 03
 {
 OUT_D == 1;  // BCD OUTPUT
 OUT_C == 1;  // BCD OUTPUT
 OUT_B == 0;  // BCD OUTPUT
 OUT_A == 0;  // BCD OUTPUT
 }

 else

 if(tr_05Pin == 1)  //  0100 = 04
 {
 OUT_D == 0;  // BCD OUTPUT
 OUT_C == 0;  // BCD OUTPUT
 OUT_B == 1;  // BCD OUTPUT
 OUT_A == 0;  // BCD OUTPUT
 }

 else

 if(tr_06Pin == 1)  //  0101 = 05
 {
 OUT_D == 1;  // BCD OUTPUT
 OUT_C == 0;  // BCD OUTPUT
 OUT_B == 1;  // BCD OUTPUT
 OUT_A == 0;  // BCD OUTPUT
 }

 else

 if(tr_07Pin == 1)  //  0110 = 06
 {
 OUT_D == 0;  // BCD OUTPUT
 OUT_C == 1;  // BCD OUTPUT
 OUT_B == 1;  // BCD OUTPUT
 OUT_A == 0;  // BCD OUTPUT
 }

 else

 if(tr_08Pin == 1)  //  0111 = 07
 {
 OUT_D == 1;  // BCD OUTPUT
 OUT_C == 1;  // BCD OUTPUT
 OUT_B == 1;  // BCD OUTPUT
 OUT_A == 0;  // BCD OUTPUT
 }

 else

 if(tr_09Pin == 1)  //  1000 =08
 {
 OUT_D == 0;  // BCD OUTPUT
 OUT_C == 0;  // BCD OUTPUT
 OUT_B == 0;  // BCD OUTPUT
 OUT_A == 1;  // BCD OUTPUT
 }

 else

 if(tr_10Pin == 1)  //  1001 = 09
 {
 OUT_D == 1;  // BCD OUTPUT
 OUT_C == 0;  // BCD OUTPUT
 OUT_B == 0;  // BCD OUTPUT
 OUT_A == 1;  // BCD OUTPUT
 }

 else

 if(tr_11Pin == 1)  //  1010 = 10
 {
 OUT_D == 0;  // BCD OUTPUT
 OUT_C == 1;  // BCD OUTPUT
 OUT_B == 0;  // BCD OUTPUT
 OUT_A == 1;  // BCD OUTPUT
 }

 else

 if(tr_12Pin == 1)  //  1011 = 11
 {
 OUT_D == 1;  // BCD OUTPUT
 OUT_C == 1;  // BCD OUTPUT
 OUT_B == 0;  // BCD OUTPUT
 OUT_A == 1;  // BCD OUTPUT
 }

 else

 if(tr_13Pin == 1)  //  1100 = 12
 {
 OUT_D == 0;  // BCD OUTPUT
 OUT_C == 0;  // BCD OUTPUT
 OUT_B == 1;  // BCD OUTPUT
 OUT_A == 1;  // BCD OUTPUT
 }

 else

 if(tr_14Pin == 1)  //  1101 = 12
 {
 OUT_D == 1;  // BCD OUTPUT
 OUT_C == 0;  // BCD OUTPUT
 OUT_B == 1;  // BCD OUTPUT
 OUT_A == 1;  // BCD OUTPUT
 }

 else

 if(tr_15Pin == 1)  //  1110 = 14
 {
 OUT_D == 0;  // BCD OUTPUT
 OUT_C == 1;  // BCD OUTPUT
 OUT_B == 1;  // BCD OUTPUT
 OUT_A == 1;  // BCD OUTPUT
 }

 else

 if(tr_16Pin == 1)  //  1111 = 15
 {
 OUT_D == 1;  // BCD OUTPUT
 OUT_C == 1;  // BCD OUTPUT
 OUT_B == 1;  // BCD OUTPUT
 OUT_A == 1;  // BCD OUTPUT
 }
  
}

these are Written in ARDUINO IDE V1.0.5 is this code ok for the 16x4 Encoder if not please help

Why employ a microcontroller for this function?

Have you examined the datasheet for the CMOS 4532, or are these chips no longer available (as the antiquity of that datasheet might suggest)?

Hi Thanks But why to use CD4532 (which is No longer available in the Market) the Substitute for the same is SN74HC147 which is 8x3 line Encoder But I would like to Design 16x4 Line Encoder don't want to Cascade two IC's and use 3 IC's for the same purpose

I have changed the CODE's which is ad Follows :-

//16 x 4 Line Encoder
// 16 input
const int tr_01Pin =  1;  //  Input From Tr. BC547- 01
const int tr_02Pin =  2;  //  Input From Tr. BC547- 02
const int tr_03Pin =  3;  //  Input From Tr. BC547- 03
const int tr_04Pin =  4;  //  Input From Tr. BC547- 04
const int tr_05Pin =  5;  //  Input From Tr. BC547- 05
const int tr_06Pin =  6;  //  Input From Tr. BC547- 06
const int tr_07Pin =  9;  //  Input From Tr. BC547- 07
const int tr_08Pin = 10;  //  Input From Tr. BC547- 08
const int tr_09Pin = 11;  //  Input From Tr. BC547- 09
const int tr_10Pin = 12;  //  Input From Tr. BC547- 10
const int tr_11Pin = 13;  //  Input From Tr. BC547- 11
const int tr_12Pin = 14;  //  Input From Tr. BC547- 12
const int tr_13Pin = 15;  //  Input From Tr. BC547- 13
const int tr_14Pin = 16;  //  Input From Tr. BC547- 14
const int tr_15Pin = 17;  //  Input From Tr. BC547- 15
const int tr_16Pin = 18;  //  Input From Tr. BC547- 16

//BCD OUTPUT (A B C D as 0 0 0 0 - 1 1 1 1)
const int OUT_APin = 25;  // Output From A
const int OUT_BPin = 26;  // Output From B
const int OUT_CPin = 27;  // Output From C
const int OUT_DPin = 28;  // Output From D

void setup()
{
    pinMode(tr_01Pin,INPUT);  //  0000
    pinMode(tr_02Pin,INPUT);  //  0001
    pinMode(tr_03Pin,INPUT);  //  0010
    pinMode(tr_04Pin,INPUT);  //  0011
    pinMode(tr_05Pin,INPUT);  //  0100
    pinMode(tr_06Pin,INPUT);  //  0101
    pinMode(tr_07Pin,INPUT);  //  0110
    pinMode(tr_08Pin,INPUT);  //  0111
    pinMode(tr_09Pin,INPUT);  //  1000
    pinMode(tr_10Pin,INPUT);  //  1001
    pinMode(tr_11Pin,INPUT);  //  1010
    pinMode(tr_12Pin,INPUT);  //  1011
    pinMode(tr_13Pin,INPUT);  //  1100
    pinMode(tr_14Pin,INPUT);  //  1101
    pinMode(tr_15Pin,INPUT);  //  1110
    pinMode(tr_16Pin,INPUT);  //  1111

    pinMode(OUT_APin,OUTPUT);
    pinMode(OUT_BPin,OUTPUT);
    pinMode(OUT_CPin,OUTPUT);
    pinMode(OUT_DPin,OUTPUT);
}
void loop()
{ 
    int tr_01State, tr_02State, tr_03State, tr_04State; 
    int tr_05State, tr_06State, tr_07State, tr_08State; 
    int tr_09State, tr_10State, tr_11State, tr_12State; 
    int tr_13State, tr_14State, tr_15State, tr_16State; 

    tr_01State = digitalRead(tr_01Pin);
    tr_02State = digitalRead(tr_02Pin);
    tr_03State = digitalRead(tr_03Pin);
    tr_04State = digitalRead(tr_04Pin);
    tr_05State = digitalRead(tr_05Pin);
    tr_06State = digitalRead(tr_06Pin);
    tr_07State = digitalRead(tr_07Pin);
    tr_08State = digitalRead(tr_08Pin);
    tr_09State = digitalRead(tr_09Pin);
    tr_10State = digitalRead(tr_10Pin);
    tr_11State = digitalRead(tr_11Pin);
    tr_12State = digitalRead(tr_12Pin);
    tr_13State = digitalRead(tr_13Pin);
    tr_14State = digitalRead(tr_14Pin);
    tr_15State = digitalRead(tr_15Pin);
    tr_16State = digitalRead(tr_16Pin);

    if(tr_01State == 1)  //  0000 = 00 
    {
        digitalWrite(OUT_DPin, 0); 
        digitalWrite(OUT_CPin, 0); 
        digitalWrite(OUT_BPin, 0); 
        digitalWrite(OUT_APin, 0);
    } 
    else 
    if(tr_02State == 1)  //  0001 = 01 
    {
        digitalWrite(OUT_DPin, 1); 
        digitalWrite(OUT_CPin, 0); 
        digitalWrite(OUT_BPin, 0);
        digitalWrite(OUT_APin, 0); 
    }   
    else 
    if(tr_03State == 1)  //  0010 = 02 
    { 
        digitalWrite(OUT_DPin, 0); 
        digitalWrite(OUT_CPin, 1); 
        digitalWrite(OUT_BPin, 0); 
        digitalWrite(OUT_APin, 0); 
    }  
    else  
    if(tr_04State == 1)  //  0011 = 03 
    {
        digitalWrite(OUT_DPin, 1); 
        digitalWrite(OUT_CPin, 1); 
        digitalWrite(OUT_BPin, 0); 
        digitalWrite(OUT_APin, 0); 
    }  
    else 
    if(tr_05State == 1)  //  0100 = 04 
    {
        digitalWrite(OUT_DPin, 0); 
        digitalWrite(OUT_CPin, 0); 
        digitalWrite(OUT_BPin, 1); 
        digitalWrite(OUT_APin, 0);
    }  
    else 
    if(tr_06State == 1)  //  0101 = 05 
    {
        digitalWrite(OUT_DPin, 1); 
        digitalWrite(OUT_CPin, 0); 
        digitalWrite(OUT_BPin, 1); 
        digitalWrite(OUT_APin, 0); 
    }  
    else  
    if(tr_07State == 1)  //  0110 = 06 
    { 
        digitalWrite(OUT_DPin, 0); 
        digitalWrite(OUT_CPin, 1); 
        digitalWrite(OUT_BPin, 1); 
        digitalWrite(OUT_APin, 0); 
    }  
    else  
    if(tr_08State == 1)  //  0111 = 07 
    {
        digitalWrite(OUT_DPin, 1); 
        digitalWrite(OUT_CPin, 1); 
        digitalWrite(OUT_BPin, 1); 
        digitalWrite(OUT_APin, 0); 
    }  
    else  
    if(tr_09State == 1)  //  1000 =08 
    { 
        digitalWrite(OUT_DPin, 0); 
        digitalWrite(OUT_CPin, 0); 
        digitalWrite(OUT_BPin, 0); 
        digitalWrite(OUT_APin, 1); 
    }  
    else  
    if(tr_10State == 1)  //  1001 = 09 
    {
        digitalWrite(OUT_DPin, 1); 
        digitalWrite(OUT_CPin, 0); 
        digitalWrite(OUT_BPin, 0); 
        digitalWrite(OUT_APin, 1);
    }  
    else 
    if(tr_11State == 1)  //  1010 = 10 
    {
        digitalWrite(OUT_DPin, 0); 
        digitalWrite(OUT_CPin, 1); 
        digitalWrite(OUT_BPin, 0); 
        digitalWrite(OUT_APin, 1); 
    }  
    else 
    if(tr_12State == 1)  //  1011 = 11 
    {
        digitalWrite(OUT_DPin, 1); 
        digitalWrite(OUT_CPin, 1); 
        digitalWrite(OUT_BPin, 0); 
        digitalWrite(OUT_APin, 1); 
    }  
    else  
    if(tr_13State == 1)  //  1100 = 12 
    {
        digitalWrite(OUT_DPin, 0); 
        digitalWrite(OUT_CPin, 0); 
        digitalWrite(OUT_BPin, 1); 
        digitalWrite(OUT_APin, 1); 
    }  
    else  
    if(tr_14State == 1)  //  1101 = 12 
    {
        digitalWrite(OUT_DPin, 1); 
        digitalWrite(OUT_CPin, 0); 
        digitalWrite(OUT_BPin, 1); 
        digitalWrite(OUT_APin, 1); 
    }  
    else  
    if(tr_15State == 1)  //  1110 = 14 
    {
        digitalWrite(OUT_DPin, 0); 
        digitalWrite(OUT_CPin, 1); 
        digitalWrite(OUT_BPin, 1); 
        digitalWrite(OUT_APin, 1); 
    } 
    else  
    if(tr_16State == 1)  //  1111 = 15 
    {
        digitalWrite(OUT_DPin, 1); 
        digitalWrite(OUT_CPin, 1); 
        digitalWrite(OUT_BPin, 1); 
        digitalWrite(OUT_APin, 1);
    }  
}

Now the Problem is this HOW TO PROGRAM THE ATmega328 WITH THE ARDUINO BOARD I have Constructed the Circuit as in the http://arduino.cc/en/uploads/Tutorial/SimpleBreadboardAVR.png from http://arduino.cc/en/Tutorial/ArduinoISP

Thanks in advance

mkbutan: Hi Thanks But why to use CD4532 (which is No longer available in the Market) the Substitute for the same is SN74HC147 which is 8x3 line Encoder

Well, looking into it, the 74HC147 isn't a substitute as it is not able to be cascaded. This brings up another matter - as I understand it in your design so far, there is no "valid" output and that if none of the inputs are active, your outputs are not (at least initially) defined. If one input is active but then reverts to no inputs active, the last state is held. Is that what you want?

You can arrange the last state to be held simply by initialising the output state to zero, or you can add a further "else" so that it always reverts to a specified state with no inputs active.

mkbutan: But I would like to Design 16x4 Line Encoder don't want to Cascade two IC's and use 3 IC's for the same purpose

There are a lot of things involved here. Price? 4000 or 74HC chips are generally much cheaper, so that three of them are generally cheaper than one ATMega.

Board space and layout? Depends on which chips are available and suit your design. It may be more convenient to have two eight bit input arrays slightly separate. The MCU also (probably) requires a crystal.

Another concern is that of "race" conditions. The 4532 is designed to output all the valid bits pretty much simultaneously (may vary in the cascaded version) while your Arduino code changes them one after the other and exactly how quickly it does this depends on which end of the "if-else" chain it hits. I am thinking on possible improvements to this ...

I should be interested to know the purpose of this project, in order to suggest what might be the best way to implement it. Generally, there may be other functions which the MCU could incorporate to advantage, otherwise it seems rather under-utilised. :)

I am also curious to know why you have discrete buffer transistors feeding the inputs to this; why are they necessary?

mkbutan: Now the Problem is this HOW TO PROGRAM THE ATmega328 WITH THE ARDUINO BOARD I have Constructed the Circuit as in the http://arduino.cc/en/uploads/Tutorial/SimpleBreadboardAVR.png from http://arduino.cc/en/Tutorial/ArduinoISP

Didn't get round to looking at this ...