Connect with us

programming help!!!!

Discussion in 'Microcontrollers, Programming and IoT' started by paddy, May 16, 2013.

Scroll to continue with content
  1. paddy

    paddy

    81
    0
    Sep 11, 2012
    Hi,

    I am working with PIC18F4520 microcontroller and CCS compiler.

    Please have a look on following code.
    Code:
     p=0;r=0;
     q=9;
          
          while(q>0)
          {
             restart_wdt();
             ALM[p]=hold_regs[r] & 0x0001;
             p++;
             ALM[p]=hold_regs[r] & 0x0002;
             p++;
             ALM[p]=hold_regs[r] & 0x0004;
             p++;
             ALM[p]=hold_regs[r] & 0x0008;
             p++;
             ALM[p]=hold_regs[r] & 0x0010;
             p++;
             ALM[p]=hold_regs[r] & 0x0020;
             p++;          
             ALM[p]=hold_regs[r] & 0x0040;
             p++;
             ALM[p]=hold_regs[r] & 0x0080;
             p++;
             ALM[p]=hold_regs[r] & 0x0100;
             p++;
             ALM[p]=hold_regs[r] & 0x0200;
             p++;
             ALM[p]=hold_regs[r] & 0x0400;
             p++;
             ALM[p]=hold_regs[r] & 0x0800;
             p++;
             ALM[p]=hold_regs[r] & 0x1000;
             p++;
             ALM[p]=hold_regs[r] & 0x2000;
             p++;
             ALM[p]=hold_regs[r] & 0x4000;
             p++;
             ALM[p]=hold_regs[r] & 0x8000;
             p++;        
             q--;
             r++;
          }
    
    Data is available on holding registers. So I am reading that value, checking it's individual bit and storing in an array ALM[144].
    And I have got 144 LED's which will be turn on if ALM==1.

    When I am writing 0xFFFF on any holding register, it works fine and all 16 LED's turns on. But when I am writing single bit values like 0x0001, 0x0002, 0x0004 and so on, on holding register it works only for first 6 values,Say ALM[0] to ALM[5] & ALM[8] to ALM[13] and there is no response for ALM[6] , ALM[7] ,ALM[14] & ALM[15].

    Please suggest.

    Thanks,
    Paddy
     
  2. BobK

    BobK

    7,682
    1,686
    Jan 5, 2010
    Other than ALM[0] none of them can be 1.

    ALM[1] will be either 0 or 2
    ALM[2] will be either 0 or 4
    ALM[3] will be either 0 or 8

    etc.

    If you want the ALM array to be 0s and 1s change the statements like this:

    ALM[p]=(hold_regs[r] & 0x0004) != 0;

    Bob
     
  3. gorgon

    gorgon

    603
    24
    Jun 6, 2011
    How do you read out the ALM[p] values, and how do you know there is no response? Have you checked the contents of the ALM[] array in memory?
     
  4. KrisBlueNZ

    KrisBlueNZ Sadly passed away in 2015

    8,393
    1,270
    Nov 28, 2011
    Assuming you want either 0 or 1 in each ALM[] variable, here's some replacement code:
    Code:
    u16 value;       // unsigned 16-bit value
    int r, bitn;
    int p = 0;
    
    for (r = 0; r < 9; ++r) {
      restart_wdt();
      value = hold_regs[r];
      for (bitn = 0; bitn < 16; ++bitn) {
        ALM[p++] = value & 0x0001;
        value >>= 1;
        }                  //for(bitn)
      }                   //for(r)
    Edit: Make sure the default int size of your compiler is 16 bits (at least).
     
    Last edited: May 17, 2013
  5. paddy

    paddy

    81
    0
    Sep 11, 2012
    Thanks a lot KrisBlueNZ,gorgon and Bobk for suggestions.

    I have connected LED's to check the ALM[] contents. Depending on the LED status I came to know that it is not working for ALM[6] & ALM[7]. It works okay for first 6 locations but it skips next 2 location and same process repeats.

    And this problem is resolved as I removed padded zeros as shown.

    I have changed ALM[p]=hold_regs[r] & 0x0001 to ALM[p]=hold_regs[r] & 0x1 ans so on and it worked.

    Can you suggest why it is not working when I am using padded zeros???

    Thanks,
    paddy
     
  6. BobK

    BobK

    7,682
    1,686
    Jan 5, 2010
    No. There should be no difference between those two statements. Are you sure you did not change something else?

    It would help if we could see the code that checks the ALM array and outputs to ports driving the LEDs.

    Bob
     
    Last edited: May 18, 2013
  7. gorgon

    gorgon

    603
    24
    Jun 6, 2011
    What's your type definition of the ALM[] array?

    Please show us how you read out the ALM[] to LEDs.
     
  8. paddy

    paddy

    81
    0
    Sep 11, 2012
    Yes Bobk. I have not changed anything except removing padded zeros.

    Have a look on following code.

    Code:
    for(i=0;i<8;i++)   //  Function for ALM1
           {
              if(ALM[i]>0)
              {        
                   switch(i)
                   {
                      case 0:
                      if(accept_status[0]==0)
                      {
                         output_high(pin_B7);        //turn on LED               
                         output_high(pin_A2);        //clock to latch
                         delay_us(100);
                         output_low(pin_A2);
                         delay_ms(20);
                         output_low(pin_B7);        //turn off LED               
                         output_high(pin_A2);       //clock to latch
                         delay_us(100);
                         output_low(pin_A2);
                         delay_ms(20);
                      }
                      else
                      {
                         output_high(pin_B7);
                         output_high(pin_A2);       //clock to latch
                         delay_us(100);
                         output_low(pin_A2);
                      }
                      break; 
                      
                      case 1:
                      if(accept_status[1]==0)
                      {
                         output_high(pin_B6);               
                         output_high(pin_A2);        //clock to latch
                         delay_us(100);
                         output_low(pin_A2);
                         delay_ms(20);
                         output_low(pin_B6);               
                         output_high(pin_A2);        //clock to latch
                         delay_us(100);
                         output_low(pin_A2);
                         delay_ms(20);
                      }
                      else
                      {
                         output_high(pin_B6);
                         output_high(pin_A2);       //clock to latch
                         delay_us(100);
                         output_low(pin_A2);
                      }
                      break;
                      
                      case 2:
                      if(accept_status[2]==0)
                      {
                         output_high(pin_B5);               
                         output_high(pin_A2);        //clock to latch
                         delay_us(100);
                         output_low(pin_A2);
                         delay_ms(20);
                         output_low(pin_B5);               
                         output_high(pin_A2);        //clock to latch
                         delay_us(100);
                         output_low(pin_A2);
                         delay_ms(20);
                      }
                      else
                      {
                         output_high(pin_B5);
                         output_high(pin_A2);       //clock to latch
                         delay_us(100);
                         output_low(pin_A2);
                      }
                      break; 
                      
                      case 3:
                      if(accept_status[3]==0)
                      {
                         output_high(pin_B4);               
                         output_high(pin_A2);        //clock to latch
                         delay_us(100);
                         output_low(pin_A2);
                         delay_ms(20);
                         output_low(pin_B4);               
                         output_high(pin_A2);        //clock to latch
                         delay_us(100);
                         output_low(pin_A2);
                         delay_ms(20);
                      }
                      else
                      {
                         output_high(pin_B4);
                         output_high(pin_A2);       //clock to latch
                         delay_us(100);
                         output_low(pin_A2);
                      }
                      break; 
                      
                      case 4:
                      if(accept_status[4]==0)
                      {
                         output_high(pin_B3);               
                         output_high(pin_A2);        //clock to latch
                         delay_us(100);
                         output_low(pin_A2);
                         delay_ms(20);
                         output_low(pin_B3);               
                         output_high(pin_A2);        //clock to latch
                         delay_us(100);
                         output_low(pin_A2);
                         delay_ms(20);
                      }
                      else
                      {
                         output_high(pin_B3);
                         output_high(pin_A2);       //clock to latch
                         delay_us(100);
                         output_low(pin_A2);
                      }
                      break; 
                      
                      case 5:
                      if(accept_status[5]==0)
                      {
                         output_high(pin_B2);               
                         output_high(pin_A2);        //clock to latch
                         delay_us(100);
                         output_low(pin_A2);
                         delay_ms(20);
                         output_low(pin_B2);               
                         output_high(pin_A2);        //clock to latch
                         delay_us(100);
                         output_low(pin_A2);
                         delay_ms(20);
                      }
                      else
                      {
                         output_high(pin_B2);
                         output_high(pin_A2);       //clock to latch
                         delay_us(100);
                         output_low(pin_A2);
                      }
                      break; 
                      
                      case 6:
                      if(accept_status[6]==0)
                      {
                         output_high(pin_D7);               
                         output_high(pin_A2);        //clock to latch
                         delay_us(100);
                         output_low(pin_A2);
                         delay_ms(20);
                         output_low(pin_D7);               
                         output_high(pin_A2);        //clock to latch
                         delay_us(100);
                         output_low(pin_A2);
                         delay_ms(20);
                      }
                      else
                      {
                         output_high(pin_D7);
                         output_high(pin_A2);       //clock to latch
                         delay_us(100);
                         output_low(pin_A2);
                      }
                      break;
                      
                      case 7:
                      if(accept_status[7]==0)
                      {
                         output_high(pin_D6);               
                         output_high(pin_A2);        //clock to latch
                         delay_us(100);
                         output_low(pin_A2);
                         delay_ms(20);
                         output_low(pin_D6);               
                         output_high(pin_A2);        //clock to latch
                         delay_us(100);
                         output_low(pin_A2);
                         delay_ms(20);
                      }
                      else
                      {
                         output_high(pin_D6);
                         output_high(pin_A2);       //clock to latch
                         delay_us(100);
                         output_low(pin_A2);
                      }
                      break; 
                   } //switch case ends             
              } // if ends
                else
                {                
                   switch(i)
                   {
                      case 0:
                      output_low(pin_B7);
                      break;
                      case 1:
                      output_low(pin_B6);
                      break;
                      case 2:
                      output_low(pin_B5);
                      break;
                      case 3:
                      output_low(pin_B4);
                      break;
                      case 4:
                      output_low(pin_B3);
                      break;
                      case 5:
                      output_low(pin_B2);
                      break;
                      case 6:
                      output_low(pin_D7);
                      break;
                      case 7:
                      output_low(pin_D6);
                      break;
                    }//  switch case ends
                }// else ends            
                
           } // for loop ends
        
           output_high(pin_A2);
           delay_us(100);
           output_low(pin_A2);         
    
    Hi gorgon,
    Type definition for ALM[] array is int16.
     
  9. gorgon

    gorgon

    603
    24
    Jun 6, 2011
    Shouldn't you have a 'default' break in your Switch() statements?

    How is your hardware made? I wonder since you are using 'pin_A2' as a clock for all the cases. What happens to the bits of the other cases when you clock new bits?

    I also see that the 6 ans 7 values are set with pin_Dx and the other is set with pin_Bx.
     
    Last edited: May 18, 2013
  10. paddy

    paddy

    81
    0
    Sep 11, 2012
    This is just one for loop I have shown.

    I have used HCT574 latch in between micro controller port pins and LED and I have 16 numbers of latch IC and at the output of each latch 8 LED's are connected.

    Hardware is something like this

    Micro controller--> Latch---LED
    --> Latch--LED
    --> Latch--LED

    so to turn on LED, particular port pin will be high and clock is given to corresponding latch.
     
  11. gorgon

    gorgon

    603
    24
    Jun 6, 2011
    Yes, I understand this, but when you use a common clock, you need to setup all data bits correct before clocking the data into the latch. Each time you clock data, ALL bits are read into the latch. You need to setup the 8 outputs, and THEN clock the total into the latch.
     
  12. paddy

    paddy

    81
    0
    Sep 11, 2012
    Yes. I am exactly doing the same thing as you explained. But I am not using common clock for latch. There are different I/O pins I have connected to individual latch and providing clock with this pins.

    I have used 2 Dx pins instead of Bx pins because B0 & B1 are an external interrupt pins which I have used for another purpose.
     
    Last edited: May 18, 2013
  13. gorgon

    gorgon

    603
    24
    Jun 6, 2011
    If you use the 74HCT574 there are only one clock input to this octal latch, and you clock this input for each bit you set. To get the correct contents of this 8-bit latch you need to set all bits up before you clock the latch. From your code snippet you clock the latch for each bit, and this will make the latch 'remember' only the last status, or the last case that hit the switch statement.
     
  14. paddy

    paddy

    81
    0
    Sep 11, 2012
    I am totally agree with what you are saying. I did it because I need to blink the LED in certain conditions. Every thing seems working fine and the only thing which I don't understand is when I used ALM[]=hold_regs[r] & 0x0001 condition, it will turn on first 6 LED's and skips next 2 and again turns on next 6 and skips next 2.
    But when I use ALM[]=hold_regs[r] & 0x1, everything works fine.
     
  15. KrisBlueNZ

    KrisBlueNZ Sadly passed away in 2015

    8,393
    1,270
    Nov 28, 2011
    There is a difference between those values: their size. How they are handled may be compiler-dependent. Can you post the compiled code for both versions? Preferably, use my looped version, from post #4 in this thread, because it will be much smaller.

    Did you try my looped code? Does the choice between 0x0001 and 0x1 make a difference with that code?
     
  16. gorgon

    gorgon

    603
    24
    Jun 6, 2011
    It could also be an -endian problem with the compiler. A listing of the two compiled codes/variants would be a big help.
    You should also be aware that if you optimize your code(set the option) The result may be far from what you want.

    You may also need to specify certain registers/addresses as volatile, since they may be changed by hardware outside the program control.
     
  17. BobK

    BobK

    7,682
    1,686
    Jan 5, 2010
    Both 0x1 and 0x0001 are of type int according to the C standard. If there is a difference in code generation using these two representations, that is a compiler bug.

    Bob
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-