Atmega first bit status - Page 1
Forum section: electronics / general

author: yalect
author_rank: Regular Contributor
timestamp: January 09, 2018, 10:17:41 am
content: Hi,I wrote this program for detecting first bit status ofvar1 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 * countPORTC = DATA;if( DATA & compare){PORTD = (1<< PIND3);}else{PORTD = (1<< PIND1);}Delay_ms(1000);}}}

author: Fire Doger
author_rank: Regular Contributor
timestamp: January 09, 2018, 10:38:26 am
content: 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-10008>>1 =8/2 = 0b0000-0100 = 48>>2 = 8/2/2= 0b0000-0010 = 28>>3 = 8/2/2/2= 0b0000-0001 = 1I can't understand from your explanation what you want to do to rewrite it.

author: yalect
author_rank: Regular Contributor
timestamp: January 09, 2018, 11:09:52 am
content: thank youyes, I corrected it I mean I want to shift right bit 8 times and read first bit if0PIND1 = 1 if1PIND0=1, the program I wrote detects only status of 0 so PIND1 =1, there is problem

author: Fire Doger
author_rank: Regular Contributor
timestamp: January 10, 2018, 02:50:20 am
content: Lets take it from the beginning. If you shift an 8bit 8 times you get 0 no matter the starting value.0b1111-1111 <Starting value0b0111-1111 <Result of 1 shift0b0011-1111 <Result of 2 shift0b0001-1111 <Result of 3 shift0b0000-1111 <Result of 4 shift0b0000-0111 <Result of 5 shift0b0000-0011 <Result of 6 shift0b0000-0001 <Result of 7 shift0b0000-0000 <Result of 8 shift--------------This is your codefor (count = 0; count < 8; count++) {DATA = (bits >> count); // output = number shifted right * countPORTC = 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-0001AND & logic0bxxxx-xxxx Your number0b0000-0001 Your mask--------------------0b0000-000x Resultif result ==0 >false, if result!=0 >trueSo 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.

author: Nusa
author_rank: Super Contributor
timestamp: January 10, 2018, 03:36:55 am
content: 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.)

author: yalect
author_rank: Regular Contributor
timestamp: January 10, 2018, 07:39:30 am
content: Thank you Nusa for your replyyes, change PIND3 to PIND0 was an error remarkable, I changed it before I simulate my program but when I check the LSD orthe Least Significant Digitit 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