• AVR Freaks

Hot!Device Id calculation.

Author
AMPS
Super Member
  • Total Posts : 468
  • Reward points : 0
  • Status: offline
2019/10/19 04:12:15 (permalink)
0

Device Id calculation.

I have 74hc165 parallel in seriout register. I have 8 pin dip switch. i would like to read the status of 8 pin dip switch  & calculate the value Device_ID
 
I am using PIC18F45k40 used MPLAB X IDE 5.15 
 
the stored value should be converted to Decimal value. Currently it able to detect the DIP position  Device_Array[0]   store value in bits level
 
How to use bit level to calculate cal_Val?so i get proper device id
 
 
unsigned long byteValue = 0;
unsigned long byteValue1 = 0;

unsigned long FAULT_PISO;
unsigned long DEV_ID_PISO;

unsigned long bitValue;
unsigned long bitValue1;
unsigned int Device_ID;


 

int DEVICE_ID_CHECK(void) {
unsigned char i;
unsigned char j, k;
unsigned char value;
 
byteValue1 = 0; // accumulates image of 8 switches
bitValue1 = 0;
 
DI_PL = 0; // latch data
 

asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
 
DI_PL = 1; //ADD
 

asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
 
for (i = 0; i < 8; i++) { //TBC // 8 bits
 
bitValue1 = PISO_FAULT;
byteValue1 |= (bitValue1 << ((8 - 1) - i));
 
DI_CP = 1;

asm("nop");
asm("nop");
 
DI_CP = 0;

asm("nop");
asm("nop");
asm("nop");
 
}
 
DEV_ID_PISO = byteValue1;
 
Device_Array[0] = (unsigned char)(DEV_ID_PISO & 0xff);

while(Device_Array[0] !=0)
{ d = Device_Array[0] % 10;
dec=dec+d*pow(2,j);
Device_Array[0] =Device_Array[0] /10;
j++;
}
 
cal_Val=dec;
return(cal_Val);


}
 
 
 
 
 

void main() {

 while(1) {
  Device_ID=DEVICE_ID_CHECK();
  __delay_ms(5);
 }


}

post edited by AMPS - 2019/10/19 04:25:30

Amps
*.*.*.*.*.*.*.*.*.*.*.*.*
#1

14 Replies Related Threads

    1and0
    Access is Denied
    • Total Posts : 9997
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Device Id calculation. 2019/10/19 04:49:48 (permalink)
    0
    ajitnayak87
    I have 74hc165 parallel in seriout register. I have 8 pin dip switch. i would like to read the status of 8 pin dip switch  & calculate the value Device_ID
     
    I am using PIC18F45k40 used MPLAB X IDE 5.15 
     
    the stored value should be converted to Decimal value. Currently it able to detect the DIP position  Device_Array[0]   store value in bits level
     
    How to use bit level to calculate cal_Val?so i get proper device id
    unsigned long byteValue = 0;
    unsigned long byteValue1 = 0;

    unsigned long FAULT_PISO;
    unsigned long DEV_ID_PISO;

    unsigned long bitValue;
    unsigned long bitValue1;
    unsigned int Device_ID;


    int DEVICE_ID_CHECK(void) {
    unsigned char i;
    unsigned char j, k;
    unsigned char value;
     
    byteValue1 = 0; // accumulates image of 8 switches
    bitValue1 = 0;
     
    DI_PL = 0; // latch data
     
    asm("nop");
    asm("nop");
    asm("nop");
    asm("nop");
    asm("nop");
     
    DI_PL = 1; //ADD
     
    asm("nop");
    asm("nop");
    asm("nop");
    asm("nop");
    asm("nop");
    asm("nop");
    asm("nop");
     
    for (i = 0; i < 8; i++) { //TBC // 8 bits
     
    bitValue1 = PISO_FAULT;
    byteValue1 |= (bitValue1 << ((8 - 1) - i));
     
    DI_CP = 1;

    asm("nop");
    asm("nop");
     
     DI_CP = 0;

    asm("nop");
    asm("nop");
    asm("nop");
     }
      
    DEV_ID_PISO = byteValue1;
      
    Device_Array[0] = (unsigned char)(DEV_ID_PISO & 0xff);

    while(Device_Array[0] !=0)
    { d = Device_Array[0] % 10;
    dec=dec+d*pow(2,j);
    Device_Array[0] =Device_Array[0] /10;
    j++;
    }
     
     cal_Val=dec;
    return(cal_Val);
    }
     

    void main() {

     while(1) {
      Device_ID=DEVICE_ID_CHECK();
      __delay_ms(5);
     }

    }


    • Your function is very inefficient.
    • I hope you use LATx for the output pins (DI_PL, DI_CP, etc).
    • Remove all those asm("nop") statements. No need to add latency -- I'm sure the 74HC165 is faster than your PIC device.
    • Why don't you just accumulate the device id value as the bits are shifted in?  Instead of going thru (for goodness sake) bitValue1 -> byteValue1 -> DEV_ID_PISO -> Device_Array[0] -> d -> dec -> cal_Val ????? Why all the detours?
    • Sigh... you even use the pow(2,j) function, not to mention division and modulus.
    • ... and unsigned long type for 8-bit values?
    post edited by 1and0 - 2019/10/19 05:39:22
    #2
    1and0
    Access is Denied
    • Total Posts : 9997
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Device Id calculation. 2019/10/19 05:19:33 (permalink)
    +1 (1)
    ajitnayak87
    the stored value should be converted to Decimal value. Currently it able to detect the DIP position  Device_Array[0]   store value in bits level
     
    How to use bit level to calculate cal_Val?so i get proper device id

    You do realize byteValue1 is a decimal value too (and hexadecimal) not just binary (bits level) !!!??? 
     
    Edit:  If I understand you correctly, you seem to think the 8 pin dip switch gives an 8-digit decimal value? That is, e.g.
     
    dip switch of all high equals to 11111111 in decimal ???!!!!
     
    ... because you use /10 and %10 in your conversion calculation.
     
     
    post edited by 1and0 - 2019/10/19 05:53:27
    #3
    AMPS
    Super Member
    • Total Posts : 468
    • Reward points : 0
    • Status: offline
    Re: Device Id calculation. 2019/10/20 21:21:14 (permalink)
    0
    weather conversion required? I have declared the variable as long . i will be stored it in long . if i declared as integer weather i could able to get int format which i can use directly for calculation.

    Amps
    *.*.*.*.*.*.*.*.*.*.*.*.*
    #4
    ric
    Super Member
    • Total Posts : 24605
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Device Id calculation. 2019/10/20 21:48:21 (permalink)
    0
    You still have not clearly explained what you are trying to do.
    If you want to read and save the state of eight binary switches (off or on), then all you need is an 8 bit variable, i.e. an unsigned char.
    What calculation do you want to do with this value afterwards?
     
    As already mentioned, your code looks horribly inefficient, and it is not at all clear what you are actually trying to do.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #5
    AMPS
    Super Member
    • Total Posts : 468
    • Reward points : 0
    • Status: offline
    Re: Device Id calculation. 2019/10/20 23:32:41 (permalink)
    0
    I am writing code for RS485 . Device ID being selected based on DIP position,Device ID will be selected once .
    i have 74hc165 ic connected to pullup resistor .
    Device_Array[0] stores bit position of 8 switch position.
    my question is 
     
    weather i need to convert bit value to DEc value in this case.
    or 
    If i declare variable char or int where i will get return value integer format 
     
    Return_value=2^0X Device_Array.firstbit  +2^1 X Device_Array.secbit +2^2X Device_Array.3rdbit+
    2^3X Device_Array.4th  +2^4X Device_Array.5thbit +2^5X Device_Array.6thbit+
    2^7X Device_Array.7thbit  +2^8 X Device_Array.8bit
     

    Amps
    *.*.*.*.*.*.*.*.*.*.*.*.*
    #6
    JPortici
    Super Member
    • Total Posts : 851
    • Reward points : 0
    • Joined: 2012/11/17 06:27:45
    • Location: Grappaland
    • Status: online
    Re: Device Id calculation. 2019/10/21 00:39:53 (permalink)
    0
    Ah, the mathematical approach.. It's not the best way of thinking in this case.
    Instead, ask yourself: what does the shift register do?
    - Copies the content of the parallel input (your eight bits) in a register
    - Put the value of the rightmost bit (the LSB) on the output
    - Shift the content of the register to the right by one bit, so now it's the second rightmost bit on the output.
    - Repeat until you have all the bits you need.
     
    What you have to do is the exact opposite to reconstruct the data.
    - Take one bit. But since the shift register was sending the rightmost bit you are receiving the leftmost bit.
    - Shift right the content of your register
    - repeat until you have all the bits
     
    something along the lines of

    //pseudocode
    register = 0
    74hc165_latch_data
     
    for idx from 0 to 7
      register >> 1
      if (newbit = 0)
        register += 0b10000000
      end if
      74hc165_shift_data
    next idx

    #7
    ric
    Super Member
    • Total Posts : 24605
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Device Id calculation. 2019/10/21 01:07:56 (permalink)
    0
    ajitnayak87
    I am writing code for RS485 . Device ID being selected based on DIP position,Device ID will be selected once .
    i have 74hc165 ic connected to pullup resistor .
    Device_Array[0] stores bit position of 8 switch position.
    my question is 
     
    weather i need to convert bit value to DEc value in this case.
    or 
    If i declare variable char or int where i will get return value integer format 
     
    Return_value=2^0X Device_Array.firstbit  +2^1 X Device_Array.secbit +2^2X Device_Array.3rdbit+
    2^3X Device_Array.4th  +2^4X Device_Array.5thbit +2^5X Device_Array.6thbit+
    2^7X Device_Array.7thbit  +2^8 X Device_Array.8bit
     


    You're obviously not understanding what people keep trying to tell you.
    The value is already stored in the variable as a number. It's binary, but you can think of it as hex or decimal also. It's still the same value in the variable.
    Only when you want to display it, or send it as a string, do you need to do any sort of conversion.

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #8
    1and0
    Access is Denied
    • Total Posts : 9997
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Device Id calculation. 2019/10/21 04:28:26 (permalink)
    0
    The following is an excerpt from your code in Post #1 with all the NOPs removed:
    for (i = 0; i < 8; i++) { //TBC // 8 bits
        bitValue1 = PISO_FAULT;
        byteValue1 |= (bitValue1 << ((8 - 1) - i));
     
        DI_CP = 1;
        DI_CP = 0;
    }

    What value does "byteValue1" hold at the end of this loop?
    #9
    AMPS
    Super Member
    • Total Posts : 468
    • Reward points : 0
    • Status: offline
    Re: Device Id calculation. 2019/10/21 22:10:11 (permalink)
    0
    I have tried simple code on pic18F886 now. 
    I expected 01  but it showing 255 
    Here is output when debugger is on

    #include <htc.h>
    #include <stdio.h>
    #include <math.h>
    #include "delay.h"

    __CONFIG(WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & FCMEN_ON & IESO_OFF & CP_OFF & MCLRE_ON);


    #define DI_CP RB4 // Output its clock input for 74hc765 connected to PIN2
    #define PISO_FAULT RB3 // Serial I/p (Q7) Connected to PIN 9 of 74HC765
    #define DI_PL RB5 // Load input for Connected to PIN 1 of 74HC765


    unsigned long DEV_ID_PISO;
    unsigned long FAULT_PISO;
    unsigned long byteValue1;
    unsigned long bitValue1;
    unsigned int Device_ID;
    unsigned char Device_Array[4];




    void InitController() {

     OSCCON = 0b01110010; //changes 8Mhz
     WREN = 0;
     WDTCON = 0b00010111; //2sec Watch Dog Reset //STP //ANN
     TRISB = 0b11001000;
     PORTB = 0b00000000;
     WPUB = 0b00000000;
     ANSEL = 0b00000000;
     ADCON0 = 0b00000000;
     ANSELH = 0b00000000;

     ADDEN = 0;
     T1OSCEN = 0;
     INTEDG = 0; //falling edge
     INTF = 0;
     INTE = 1;
     PIE1 = 0b00000001;
     PIR1 = 0x01;
     INTCON = 0; //ANN
     T1CON = 0b00000101; //Timer Settings //ANN
     TMR1H = 0xF0; //ANN
     TMR1L = 0x61; //ANN
     TMR1IE = 1;

     PEIE = 1; //ANN
     GIE = 1; // Global interrupt enabled.

    }



    void main(void) {
     InitController();
     while (1) {

     }

    }




    //**********************************************************************************************
    //// Parallel FAULT to Serial Conversion
    //**********************************************************************************************

    void CHK_FAULT_INPUT(void) {
     unsigned char i;
     unsigned char j, k;
     unsigned char value;

     byteValue1 = 0; // accumulates image of 8 switches
     bitValue1 = 0;

     DI_PL = 0; // latch data

     asm("nop");
     asm("nop");

     DI_PL = 1; //ADD

     asm("nop");
     asm("nop");
     asm("nop");

     for (i = 0; i < 8; i++) { //TBC // 8 bits

      bitValue1 = PISO_FAULT;
      byteValue1 |= (bitValue1 << ((8- 1) - i));

      DI_CP = 1;
      asm("nop");
      asm("nop");
      asm("nop");

      DI_CP = 0;

      asm("nop");
      asm("nop");
      asm("nop");

     }

     FAULT_PISO = byteValue1;
     Device_Array[0] = (unsigned char)(FAULT_PISO & 0xff);


    }

    //==========================================================================
    // Timer Interrupt - 5mSec
    //==========================================================================
    void interrupt isr(void) {

     asm("clrwdt"); //ANN

     if (TMR1IF) {
      TMR1IF = 0;
      TMR1H = 0xAF;
      TMR1L = 0xC8;

     }
     CHK_FAULT_INPUT();

    }


    post edited by AMPS - 2019/10/21 22:16:39

    Attached Image(s)


    Amps
    *.*.*.*.*.*.*.*.*.*.*.*.*
    #10
    AMPS
    Super Member
    • Total Posts : 468
    • Reward points : 0
    • Status: offline
    Re: Device Id calculation. 2019/10/21 22:23:50 (permalink)
    0
    if i write code this way

    #include <htc.h>
    #include <stdio.h>
    #include <math.h>
    #include "delay.h"

    __CONFIG(WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & FCMEN_ON & IESO_OFF & CP_OFF & MCLRE_ON);


    #define DI_CP RB4 // Output its clock input for 74hc765 connected to PIN2
    #define PISO_FAULT RB3 // Serial I/p (Q7) Connected to PIN 9 of 74HC765
    #define DI_PL RB5 // Load input for Connected to PIN 1 of 74HC765


    unsigned int DEV_ID_PISO;
    unsigned int FAULT_PISO;
    unsigned int byteValue1;
    unsigned int bitValue1;
    unsigned int Device_ID;
    unsigned int Device_Array[4];




    void InitController() {

      OSCCON = 0b01110010; //changes 8Mhz
      WREN = 0;
      WDTCON = 0b00010111; //2sec Watch Dog Reset //STP //ANN
      PORTA = 0b00000000;
      TRISA = 0b00111000;

      TRISB = 0b11001000;
      PORTB = 0b00000000;

      WPUB = 0b00000000;
      ANSEL = 0b00000000;
      ADCON0 = 0b00000000;

      // TRISC = 0b10000111;
      TRISC = 0b11001111;
      PORTC = 0b00000000;
      ANSELH = 0b00000000;

      ADDEN = 0;
      T1OSCEN = 0;
      INTEDG = 0; //falling edge
      INTF = 0;
      INTE = 1;
      PIE1 = 0b00000001;
      PIR1 = 0x01;
      INTCON = 0; //ANN
      T1CON = 0b00000101; //Timer Settings //ANN
      TMR1H = 0xF0; //ANN
      TMR1L = 0x61; //ANN
      TMR1IE = 1;

      PEIE = 1; //ANN
      GIE = 1; // Global interrupt enabled.

    }






    void main(void) {
     InitController();
     while (1) {
     
          Device_ID=CHK_FAULT_INPUT();
     }

    }




    //**********************************************************************************************
    //// Parallel FAULT to Serial Conversion
    //**********************************************************************************************

    int CHK_FAULT_INPUT(void) {
      unsigned char i;
      unsigned char j, k;
      unsigned char value;

      byteValue1 = 0; // accumulates image of 8 switches
      bitValue1 = 0;

      DI_PL = 0; // latch data

      asm("nop");
      asm("nop");
      
      DI_PL = 1; //ADD

      asm("nop");
      asm("nop");
      asm("nop");
      
      for (i = 0; i < 8; i++) { //TBC // 8 bits

        bitValue1 = PISO_FAULT;
        byteValue1 |= (bitValue1 << ((8- 1) - i));

        DI_CP = 1;
        asm("nop");
        asm("nop");
        asm("nop");

        DI_CP = 0;
       
        asm("nop");
        asm("nop");
        asm("nop");

      }

     FAULT_PISO = byteValue1;
    Device_Array[0] = (unsigned char)(FAULT_PISO & 0xff);
     
    return(~Device_Array[0]);
    }

    //==========================================================================
    // Timer Interrupt - 5mSec
    //==========================================================================
    void interrupt isr(void) {

      asm("clrwdt"); //ANN

      if (TMR1IF) {
        TMR1IF = 0;
        TMR1H = 0xAF;
        TMR1L = 0xC8;

      }
          
       
    }


    Attached Image(s)


    Amps
    *.*.*.*.*.*.*.*.*.*.*.*.*
    #11
    1and0
    Access is Denied
    • Total Posts : 9997
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Device Id calculation. 2019/10/21 22:33:22 (permalink)
    +1 (1)
    You expect
    00000001
    but it's showing
    11111110
     
    Do you see a pattern? Try different values to confirm what you're seeing. Learn to debug.
    #12
    AMPS
    Super Member
    • Total Posts : 468
    • Reward points : 0
    • Status: offline
    Re: Device Id calculation. 2019/10/21 23:31:20 (permalink)
    0
    Patterns are coming properly . There is no doubt wherever dip position changes particular value update with zero. in above example dip 1 position changed i used to get that bit 0 remaining 1. Device_Array[0] showing properly update value but i need reverse of it. if its 254 it should show 1
    while returning it showing FF01 instead of 0X0001
     
     

    Amps
    *.*.*.*.*.*.*.*.*.*.*.*.*
    #13
    AMPS
    Super Member
    • Total Posts : 468
    • Reward points : 0
    • Status: offline
    Re: Device Id calculation. 2019/10/22 00:03:14 (permalink)
    0
    I have made small changes in variable declaration.i am making mistake while returning the value.
     

    #include <htc.h>
    #include <stdio.h>
    #include <math.h>
    #include "delay.h"

    __CONFIG(WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & FCMEN_ON & IESO_OFF & CP_OFF & MCLRE_ON);


    #define DI_CP RB4 // Output its clock input for 74hc765 connected to PIN2
    #define PISO_FAULT RB3 // Serial I/p (Q7) Connected to PIN 9 of 74HC765
    #define DI_PL RB5 // Load input for Connected to PIN 1 of 74HC765


    unsigned int DEV_ID_PISO;
    unsigned int FAULT_PISO;
    unsigned int byteValue1;
    unsigned int bitValue1;
    unsigned int Device_ID;
    unsigned int Device_Array[4];

    //**********************************************************************************************
    //// Parallel FAULT to Serial Conversion
    //**********************************************************************************************

    unsigned int CHK_FAULT_INPUT(void) {
     unsigned char i;
     unsigned char j, k;
     unsigned char value;
     byteValue1 = 0; // accumulates image of 8 switches
     bitValue1 = 0;
     DI_PL = 0; // latch data
     asm("nop");
     asm("nop");
     DI_PL = 1; //ADD
     asm("nop");
     asm("nop");
     asm("nop");
     for (i = 0; i < 8; i++) { //TBC // 8 bits

      bitValue1 = PISO_FAULT;
      byteValue1 |= (bitValue1 << ((8- 1) - i));

      DI_CP = 1;
      asm("nop");
      asm("nop");
      asm("nop");

      DI_CP = 0;

      asm("nop");
      asm("nop");
      asm("nop");

     }

     FAULT_PISO = byteValue1;
     return(~FAULT_PISO);
    }




    void main(void) {
     InitController();
     while (1) {

      Device_ID=CHK_FAULT_INPUT();
     }

    }





    //==========================================================================
    // Timer Interrupt - 5mSec
    //==========================================================================
    void interrupt isr(void) {

     asm("clrwdt"); //ANN

     if (TMR1IF) {
      TMR1IF = 0;
      TMR1H = 0xAF;
      TMR1L = 0xC8;

     }


    }


    Attached Image(s)


    Amps
    *.*.*.*.*.*.*.*.*.*.*.*.*
    #14
    1and0
    Access is Denied
    • Total Posts : 9997
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Device Id calculation. 2019/10/22 00:32:40 (permalink)
    +1 (1)
    ajitnayak87
    Patterns are coming properly . There is no doubt wherever dip position changes particular value update with zero. in above example dip 1 position changed i used to get that bit 0 remaining 1. Device_Array[0] showing properly update value but i need reverse of it. if its 254 it should show 1

    You posted the same topic in Arduino Land seven months ago here: https://forum.arduino.cc/index.php?topic=604816.0
     
    The 74HC165 has two complementary outputs. If you want the non-complement output, why connect it to the complement output?
     
     

    while returning it showing FF01 instead of 0X0001

    As shown in the Watch window, the complement of 0x00FE is 0xFF01. That said, why do you keep insist on using 16-bit variables for 8-bit data? sad: sad

    Quoting Ric, "You need to UNDERSTAND what you are doing, and why. Not just fiddle with things until they hopefully work."
    #15
    Jump to:
    © 2019 APG vNext Commercial Version 4.5