Author Topic: Atmega first bit status  (Read 1126 times)

0 Members and 1 Guest are viewing this topic.

Offline yalectTopic starter

  • Regular Contributor
  • *
  • Posts: 123
Atmega first bit status
« on: January 09, 2018, 10:17:41 am »
Hi,
I wrote this program for detecting first bit status of var 1 or 0 Via Pins 0 AND 1 of PORTD but it detects  0 status only and it doesn't work for 1, I can't know what's the problem.
 void main() {
  int bits = 100;
  int compare = 0x00000001;
  int count, DATA,
  DDRC = 0xff;
  DDRD = (1<<PIND0)|(1<<PIND1);
  while(1)
  {
    for (count = 0; count < 8; count++)
    {
      DATA = (bits >> count);  // output = number shifted right * count
      PORTC = DATA;
      if( DATA & compare)
     {
     PORTD = (1<< PIND3);
     }
     else
    {
     PORTD = (1<< PIND1);
     }

     Delay_ms(1000);
    }
 }
}
 

Offline Fire Doger

  • Regular Contributor
  • *
  • Posts: 207
  • Country: 00
  • Stefanos
Re: Atmega first bit status
« Reply #1 on: January 09, 2018, 10:38:26 am »
Fast mistake:
DDRD = (1<<PIND0)|(1<<PIND1);

PORTD = (1<< PIND3);
}
else
{
PORTD = (1<< PIND1);

PinD3 is input.

also ">>something" is division by 2^something, or just shift right, not sure if this is what you want to do.
8 = 0b0000-1000
8>>1 =8/2 =  0b0000-0100 = 4
8>>2 = 8/2/2= 0b0000-0010 = 2
8>>3 = 8/2/2/2= 0b0000-0001 = 1

I can't understand from your explanation what you want to do to rewrite it.
« Last Edit: January 09, 2018, 10:42:51 am by Fire Doger »
 

Offline yalectTopic starter

  • Regular Contributor
  • *
  • Posts: 123
Re: Atmega first bit status
« Reply #2 on: January 09, 2018, 11:09:52 am »
thank you
yes, I corrected it I mean I want to shift right bit 8 times and read first bit if 0 PIND1 = 1 if 1 PIND0=1, the program I wrote detects only status of 0 so PIND1 =1, there is problem
 

Offline Fire Doger

  • Regular Contributor
  • *
  • Posts: 207
  • Country: 00
  • Stefanos
Re: Atmega first bit status
« Reply #3 on: January 10, 2018, 02:50:20 am »
Lets take it from the beginning. If you shift an 8bit 8 times you get 0 no matter the starting value.
0b1111-1111 <Starting value
0b0111-1111 <Result of 1 shift
0b0011-1111 <Result of 2 shift
0b0001-1111 <Result of 3 shift
0b0000-1111 <Result of 4 shift
0b0000-0111 <Result of 5 shift
0b0000-0011 <Result of 6 shift
0b0000-0001 <Result of 7 shift
0b0000-0000 <Result of 8 shift

--------------
This is your code
for (count = 0; count < 8; count++) {
      DATA = (bits >> count);  // output = number shifted right * count
      PORTC = DATA;
      if( DATA & compare){
        //output
     }
     Delay_ms(1000);
}

Everything inside for (Red) will be executed 8 times.
So you will make the shift, compare, output, delay 8 times, in 1 second interval and then again from the start. Is this what you want?
It works because first shift is >>0 and you actually are doing 7 shifts.

Lets examine shifts for 100= 0b0110-0100:
Loop #1: DATA = (bits >> 0); = bits (true)
Loop #2: DATA = (bits >> 1); = 0b0011-0010 (false)
Loop #3: DATA = (bits >> 2); = 0b0001-1001 (true)
Loop #4: DATA = (bits >> 3); = 0b0000-1100 (false)
Loop #5: DATA = (bits >> 4); = 0b0000-0110 (false)
Loop #6: DATA = (bits >> 5); = 0b0000-0011 (true)
Loop #7: DATA = (bits >> 6); = 0b0000-0001 (true)
Loop #8: DATA = (bits >> 7); = 0b0000-0000 (false)

Your mask is 0x00-00-00-01 its the same with 0x01 for this code. = 0b0000-0001

AND & logic
0bxxxx-xxxx    Your number
0b0000-0001   Your mask
--------------------
0b0000-000x  Result

if result ==0 >false, if result!=0 >true
So your mask checks only the Least Significant Digit (LSD)
if the LSD is 1 the result is true, if the LSD is 0 the result is false. Above I marked the result of the "if" on every loop for basic number 100.

If you want to Most Significant Digit (MSD) then your mask will be 0x80 (0b1000-0000)

If you don't see activity on a pin try different one, maybe its burned or a peripheral may have the control of it.
« Last Edit: January 10, 2018, 02:53:10 am by Fire Doger »
 

Offline Nusa

  • Super Contributor
  • ***
  • Posts: 2416
  • Country: us
Re: Atmega first bit status
« Reply #4 on: January 10, 2018, 03:36:55 am »
I think if you change PIND3 to PIND0 you'll probably get the result you want. That's the obvious error.

(Fire Doger, yes that's what he wants, if you've seen his multiple previous threads on this subject. Remember the first shift is for a value of 0, which is no change, and the loop exits after 7 so there is no 8 shift.)
 

Offline yalectTopic starter

  • Regular Contributor
  • *
  • Posts: 123
Re: Atmega first bit status
« Reply #5 on: January 10, 2018, 07:39:30 am »
Thank you Nusa for your reply
yes, change PIND3 to PIND0 was an error remarkable, I changed it before I simulate my program but when I check the LSD or the Least Significant Digit it dosn't work such on the program I compared the shifted Digit with 0x01 if true or ==1 the PIND0 =1 but not working, PIND0 =0 for all states
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf