A quite stupid question on C, 3 wire serial interface

Status
Not open for further replies.

glenjoy

Banned
Joined
Jan 1, 2004
Messages
962
Helped
72
Reputation
146
Reaction score
20
Trophy points
1,298
Location
Philippines
Activity points
0
Hi,

I have seen this code and wondering if this is really working to get an 8 bit MSB first data. Please post comments:

unsigned char read_0831(){

unsigned char bit;

clk = 0;
bit = d_out;
clk = 1;

return bit;
}

unsigned int start_read(){

unsigned char i;
unsigned int adc_data;

adc_data = 0;
csel = 0;

for(i=0;i<8;i++) {

adc_data = adc_data | read_0831() <<1;
csel = 1;

}

}

1. Is the content of the char bit 1000 000 or 0000 0001 if d_out is equal to one.

2. Is this code right? because I am confused with the rotate left if it will put in adc_data the correct arrangement of bits from the adc.

I am sorry to ask this, but I am a beginner in C, I am used in assembly programming.

Thanks.
 

Salam,

1. Is the content of the char bit 1000 000 or 0000 0001 if d_out is equal to one.

0000 0001

2. Is this code right? because I am confused with the rotate left if it will put in adc_data the correct arrangement of bits from the adc.

You can use this simple code to read byte MSB first
Assume your data in is called 'd_in'

Code:
    unsigned char temp;
    temp=0;
    for (i=1;i<=8;i++) {
         temp = temp << 1;
         temp = temp | (unsigned char) d_in;
     }

your result = temp

Bye
 

Can someone explain me what will happen in this piece of code?

unsigned int start_read(){

unsigned char i;
unsigned int adc_data;

adc_data = 0;
csel = 0;

for(i=0;i<8;i++) {

adc_data = adc_data | read_0831() <<1;
csel = 1;

}

}
 

glenjoy, the code looks broken.
For each input bit, you want to shift left the accumulator and then insert the input bit into accumulator bit 0. Try replacing this:
adc_data = adc_data | read_0831() <<1;
with this:
adc_data = (adc_data << 1) | read_0831();

For that to work, read_0831() must return either 0 or 1.

For clarity, it's a good idea to add extra parenthesis to C expressions involving shifts and logical operators. That also helps you avoid making bugs, because C's precedence is sometimes surprising.

To answer your second question, this statement:
adc_data = adc_data | read_0831() <<1;
is equivalent to
adc_data = adc_data | (read_0831() <<1);
which keeps putting your input bit into accumulator bit 1. Not what you want.


As you learn C, you will have many questions. You will like this site:
https://www.eskimo.com/~scs/C-faq/top.html

By the way, highlight your source code and click the "Code" button so we can see your indenting, like SphinX did.
 

    glenjoy

    Points: 2
    Helpful Answer Positive Rating
while it is good for learning purposes but for time critical operations like this assembly is a better choice.
 

Hi,

Thanks for the help I had corrected the code:

Code:
#include "C:\SourceCode\Website\0831_lm35\0831_lm35.h"


#bit d_out = 0x05.2
#bit clk   = 0x05.3
#bit csel  = 0x05.4

unsigned char adc_value;

 


unsigned char read_0831(){

   unsigned char bit;

   clk = 0;
   bit = d_out;
   clk = 1;

   return bit;
}

unsigned char start_read(){

unsigned char i;
unsigned char adc_data;



   adc_data = 0x00;
   csel  = 0;


[color=blue]for(i=0;i<10;i++) {

   adc_data = (adc_data <<1)| read_0831();[/color]
   

}
   return adc_data;
   csel = 1;

}
 

Happy it's working. Good luck learning C.

That's easier to read.
Looks like the "color" tag doesn't work inside of a "code" tag.

Bluechem, assembler is a better choice only if the C compiler has a poor optimizer.
 

these days compilers are very efficient, but every language has a purpose! C is very convinient but never can beat assembly in speed no matter how good the compiler is.
 

C can beat assembly in speed.

Speed with reference to debugging of code and change of microcontroller platform.

But of course, C cannot beat assembly in speed in execution.

But these days, microcntrollers gets being faster and memory size goes big and people wants their projects to be done fast.

I think C applies, I have been an assembly user also, but due to time demand in development, I am now trying to look at C.
 

I concede! An expert assembly language programmer can write equal or faster code than an optimizing compiler. But I really mean *expert*. Many modern CPUs have multiple execution units and fancy memory transfer pipelines that are very difficult to optimize by hand. If you do that in assembler, you'll have to start over when a new version of the CPU comes out.

Ok, that usually doesn't happen to 8-bit micros. In critical tight loops, I inspect what the compiler generated, and if I don't like it, I fiddle with the C (hoping to get lucky), or I write the loop in assembler.
 

echo47 said:
In critical tight loops, I inspect what the compiler generated, and if I don't like it, I fiddle with the C (hoping to get lucky), or I write the loop in assembler.
I definately have a different approach. I like C but I don't have any obsession about it. I usually write time critical modules in assembly and the body of program in c. I have created a library of such modules and I usually re-use it in different applications(sometimes with minor modifications).
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…