0

I am converting the 1s and 0s (ups and downs on a pin) into relevant 8 bit data. The pin is expected to have 8 bits sent making a byte from another sort of computer. I am currently working on making a char or array to take the 1s and 0s sent from a pin. I need this so I can transfer this data into something else in the form of number or char I received. A 1 is determined at a certain time and a 0 is determined if no 1 (high pulse) appears.

So far I have tried this:

char data = (0x00);
int valid = 0;
for(int i = 0; i <= 8; i++)
{
 //Add 1 or 0 to array
    while(pin==0)
    {
        delay(5,4);  // 60ns
        data << i = 0; // add 0 to char  
        valid = 1;
    }
    if(valid == 0)
    {
        data << i = 1;  //add 1 to char 
        delay(5,4); // wait for 50 seconds
    }
}

I thought shifting into the char in the binary sense (hexadecimal/binary) I could update the individual 1s and 0s. But I am getting errors saying this isnt a valid solution.

What should I do?

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • 1
    `for(int i = 0; i <= 8; i++)` will iterate `9` times. – Weather Vane Nov 03 '22 at 18:50
  • I'll amend that. Thanks – ImadeAnewaccount Nov 03 '22 at 18:51
  • 2
    What is `data << i = 0;` supposed to do? It is a syntax error. Also is enclosed in `while(pin==0)` and unless `pin` is a macro it won't change. – Weather Vane Nov 03 '22 at 18:51
  • I sampled an IF-statement which previous was something like data & 0x01 << i == 0... indicate that the bit in the byte is 1 or 0. It basically shifts to the position in the Byte based on the value of i. I thought I could do it again and shift into the binary form of the char and change it, bit by bit. The error I also received was "expression is not assignable" – ImadeAnewaccount Nov 03 '22 at 18:53
  • 1
    Please don't correct the *posted* code and make a 'shifting sand' question! – Weather Vane Nov 03 '22 at 19:07
  • If you are reading a digital temperature sensor with SPI interface such as [this](https://www.mouser.com/datasheet/2/268/21743B-26909.pdf) there isn't really enough information in this post: the code looks too 'wild'. I suggest you look up application examples and guidance notes. – Weather Vane Nov 03 '22 at 19:12
  • DS18B20 is the thermometer. It has 3 pins. 2 pins for power. middle pin for data which I am connected to. Just trying to collect the 8 bits into one byte so I can move this byte into other data. These 8 bits either come as a 1 or 0 depending on the low or high voltage on the pin. So when pin == 0 for 60ns, it means bit 0 has come. if it has gone high within that 60ns usually within the first 15ns, it will cancel the while loop of equalling 0 and equal bit 1 instead. – ImadeAnewaccount Nov 03 '22 at 19:25
  • You might like to look at code on previous Stackoverflow [questions](https://www.google.co.uk/search?q=stackoverflow+DS18B20+spi+digital+temperature+sensor+C+-C%2B%2B+site:stackoverflow.com) about DS18B20 – Weather Vane Nov 03 '22 at 19:28
  • @ImadeAnewaccount so you need to manage the DS18B20 sensor right? Can you specify what PIC do you use for that? – Kozmotronik Nov 05 '22 at 12:45
  • I use a PIC16F877A. Thanks – ImadeAnewaccount Nov 06 '22 at 16:50
  • `I thought shifting into the char in the binary sense`. The `char` type is not eligible for this operation you must use `unsigned char` instead. You mentioned somethings about the delay time units as nano seconds. I think you are a bit confused. You can't do any nanosecond wide operation, especially a delay since the max instruction cycle time of 877A is 125 nanoseconds. I think your delays must be in microseconds. Have you tried bit banging technique? This is the frequently used technique for one wire communication. – Kozmotronik Nov 08 '22 at 06:19

0 Answers0