• AVR Freaks

Hot!Assembly Code to Read Humidity Sensor

Page: < 12345.. > >> Showing page 5 of 7
Author
qhb
Superb Member
  • Total Posts : 9999
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: Assembly Code to Read Humidity Sensor 2019/04/03 17:33:41 (permalink)
+2 (2)
Really?
Sorry, this is getting ridiculous. Next you'll be asking how to add 1+1.
If you have no programming capability at all, maybe you better get someone else to write it.
 

Nearly there...
#81
delfindelfin
Super Member
  • Total Posts : 387
  • Reward points : 0
  • Joined: 2017/01/19 12:32:58
  • Location: Mexico
  • Status: offline
Re: Assembly Code to Read Humidity Sensor 2019/04/03 17:43:09 (permalink)
0
Yes, I know how to program, but what I want to know is if there is a module that allows you to check for the rising edge. Something internal on the PIC16F877A that I can use to make the code simpler
post edited by delfindelfin - 2019/04/03 17:45:02

MPLAB X IDE v5.05
XC8 2.00
#82
pcbbc
Super Member
  • Total Posts : 1381
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: Assembly Code to Read Humidity Sensor 2019/04/03 20:30:51 (permalink)
+1 (1)
RB0/INT pin can generate interrupts on rising or falling edges.
TIMER0 can count rising or falling edges on RA4/T0CKI.
TIMER1 can count rising edges on RC0/T1OSO/T1CKI.

Probably there are other options too. All of the above I found by searching the datasheet for “rising edge”. It really is quite a useful document, you really should try reading it sometime!
#83
delfindelfin
Super Member
  • Total Posts : 387
  • Reward points : 0
  • Joined: 2017/01/19 12:32:58
  • Location: Mexico
  • Status: offline
Re: Assembly Code to Read Humidity Sensor 2019/04/03 20:46:24 (permalink)
0
Yeah, those were the ones that I thought I should use, but I wanted to make sure that I was I right. Now I going to spend the next days trying to remember how timers and interrupts work. Thanks

MPLAB X IDE v5.05
XC8 2.00
#84
1and0
Access is Denied
  • Total Posts : 9978
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Assembly Code to Read Humidity Sensor 2019/04/04 05:21:43 (permalink)
0
delfindelfin
Yes, I know how to program, but what I want to know is if there is a module that allows you to check for the rising edge. Something internal on the PIC16F877A that I can use to make the code simpler

Instead of trying to find short cuts, just get it to work first. How hard is it to code two loops?
 
#85
delfindelfin
Super Member
  • Total Posts : 387
  • Reward points : 0
  • Joined: 2017/01/19 12:32:58
  • Location: Mexico
  • Status: offline
Re: Assembly Code to Read Humidity Sensor 2019/04/04 05:22:04 (permalink)
0
Do you know if it is possible to use the function pow in xc8.
I need to use it for this code:

 
int k;
int decimalhumid = 0;
 
for(k=0;k<16;k=k+1)
{
    if( humidity[k] = 1 )
    {
        decimalhumid = decimalhumid + pow(2 , k) ;
 
    }
    else
    {
        decimalhumid = decimalhumid ;
    }
}
 

But I am unable to compile it
post edited by delfindelfin - 2019/04/04 05:31:03

MPLAB X IDE v5.05
XC8 2.00
#86
delfindelfin
Super Member
  • Total Posts : 387
  • Reward points : 0
  • Joined: 2017/01/19 12:32:58
  • Location: Mexico
  • Status: offline
Re: Assembly Code to Read Humidity Sensor 2019/04/04 05:24:31 (permalink)
0
1and0
delfindelfin
Yes, I know how to program, but what I want to know is if there is a module that allows you to check for the rising edge. Something internal on the PIC16F877A that I can use to make the code simpler

Instead of trying to find short cuts, just get it to work first. How hard is it to code two loops?
 


As hard as that for a strange reason the XC8 compiler doesn't detect the function pow(), or maybe I am coding something wrong (I think is that)
 

I am using pow(), because I am storing the values of the sensor in an array called rawdata, I split it into humidity[16], temperature[16] and checksum[8] and now I want to convert the binary values stored into decimal
post edited by delfindelfin - 2019/04/04 05:40:36

MPLAB X IDE v5.05
XC8 2.00
#87
1and0
Access is Denied
  • Total Posts : 9978
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Assembly Code to Read Humidity Sensor 2019/04/04 05:34:32 (permalink)
0
The pow() function is a math function, so you'll have to #include <math.h>, and it will bring in the floating point which uses lot of resources.  That said, raising 2 to a power is easily and more efficiently done by shifting 1 to the left.
post edited by 1and0 - 2019/04/04 05:36:09
#88
1and0
Access is Denied
  • Total Posts : 9978
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Assembly Code to Read Humidity Sensor 2019/04/04 05:37:59 (permalink)
0
delfindelfin
 
As hard as that for a strange reason the XC8 compiler doesn't detect the function pow(), or maybe I am coding something wrong (I think is that)

Look up the function in the XC8 User's Guide and you will find the answer.
 
Frankly, you ask too many Programming 101 and Electronics 101 questions. ;)
 
post edited by 1and0 - 2019/04/04 05:39:29
#89
1and0
Access is Denied
  • Total Posts : 9978
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Assembly Code to Read Humidity Sensor 2019/04/04 05:40:58 (permalink)
0
delfindelfin
I need to use it for this code:

    else
    {
        decimalhumid = decimalhumid ;
    }

 

What do you hope to get out of that?
 
#90
qhb
Superb Member
  • Total Posts : 9999
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: Assembly Code to Read Humidity Sensor 2019/04/04 05:52:08 (permalink)
0
This
        decimalhumid = decimalhumid + pow(2 , k)

is an EXTREMELY inefficient way to calculate a power of 2, assuming you are using integer variables.
Try:
        decimalhumid = decimalhumid + (1 << k);

 

Nearly there...
#91
delfindelfin
Super Member
  • Total Posts : 387
  • Reward points : 0
  • Joined: 2017/01/19 12:32:58
  • Location: Mexico
  • Status: offline
Re: Assembly Code to Read Humidity Sensor 2019/04/04 06:03:06 (permalink)
0
.
post edited by delfindelfin - 2019/04/04 06:04:11

MPLAB X IDE v5.05
XC8 2.00
#92
pcbbc
Super Member
  • Total Posts : 1381
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: Assembly Code to Read Humidity Sensor 2019/04/04 06:04:43 (permalink)
+1 (1)
That looks like a well bodged together binary to integer conversion to me.
 
Assuming k is signed, and humidity[k] values are either 1 or 0:
decimalhumid = 0;
for(k=15;k>=0;k--)
{
    decimalhumid <<= 1;
    decimalhumid |= humidity[k];
}

 
But almost certainly this can be done without the intermediate humidity[k] array.  Just shift the bits into decimalhumid directly as they arrive from the sensor.
#93
1and0
Access is Denied
  • Total Posts : 9978
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Assembly Code to Read Humidity Sensor 2019/04/04 06:57:45 (permalink)
+1 (1)
delfindelfin
I am using pow(), because I am storing the values of the sensor in an array called rawdata, I split it into humidity[16], temperature[16] and checksum[8] and now I want to convert the binary values stored into decimal

As said, instead of storing the binary bits in arrays. You should just shift the bits into variables as they arrive from the sensor.
#94
delfindelfin
Super Member
  • Total Posts : 387
  • Reward points : 0
  • Joined: 2017/01/19 12:32:58
  • Location: Mexico
  • Status: offline
Re: Assembly Code to Read Humidity Sensor 2019/04/04 16:52:57 (permalink)
0
So, this is the new code. Can you help me to see it there are any errors. I am still unable to print the temperature and the humidty, it only prints 0.0 and 0.0. Also, any recommendations for debugging the circuit using pickit3?
 
post edited by delfindelfin - 2019/04/04 16:58:59

MPLAB X IDE v5.05
XC8 2.00
#95
qhb
Superb Member
  • Total Posts : 9999
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: Assembly Code to Read Humidity Sensor 2019/04/04 16:56:40 (permalink)
0
Deleted, now the code has been added.
 

Nearly there...
#96
delfindelfin
Super Member
  • Total Posts : 387
  • Reward points : 0
  • Joined: 2017/01/19 12:32:58
  • Location: Mexico
  • Status: offline
Re: Assembly Code to Read Humidity Sensor 2019/04/04 17:05:57 (permalink)
0
qhb
Deleted, now the code has been added.
 


 What do you mean by that?

MPLAB X IDE v5.05
XC8 2.00
#97
qhb
Superb Member
  • Total Posts : 9999
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: Assembly Code to Read Humidity Sensor 2019/04/04 17:10:31 (permalink)
0
I saw your initial post, which did not have the code attached, so I commented that there was no code.
I deleted that once I saw you edited the post to add the code.
I note you've only used half the algorithm I suggested to detect a rising edge.
i.e. only the "second" loop.
 

Nearly there...
#98
delfindelfin
Super Member
  • Total Posts : 387
  • Reward points : 0
  • Joined: 2017/01/19 12:32:58
  • Location: Mexico
  • Status: offline
Re: Assembly Code to Read Humidity Sensor 2019/04/04 17:48:26 (permalink)
0
Yes ...
 

void read_humidity_sensor()
{
    int i;
    int j;

    __delay_ms(0.08); // DHT22 Responds Low for 80 uS
    __delay_ms(0.08); // DHT22 Responds High for 80 uS

    humid = 0 ;
    temp = 0 ;
    checksum = 0 ;

    for(j=0;j<40;j=j+1)
    {

        i=0;

        while(i==0){ // Wait for the Rising Edge

        if (PORTCbits.RC2 == 1)
        {
            i=1;

         }
    }

    __delay_ms(0.03); // Wait 30 uS to see if data bit is High and Low

    if(j<16){ // Select Data (Temperature, Humidity or Checksum)

    if (PORTCbits.RC2 == 1)
    { 
        humid = humid + (1 << (16 - j) );

       }

    }
    else if(j >= 16 && j <= 32)
    {
        if (PORTCbits.RC2 == 1)
       {
            temp = temp + (1 << (16 - j) );

        }

    }
    else
    {
    if (PORTCbits.RC2 == 1)
    {
         checksum = checksum + (1 << (8 - j) );

    }

    }

}

}
 


MPLAB X IDE v5.05
XC8 2.00
#99
qhb
Superb Member
  • Total Posts : 9999
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: Assembly Code to Read Humidity Sensor 2019/04/04 18:04:49 (permalink)
0
delfindelfin
Yes ...
 

No...

Go back and read what I wrote again. I clearly described two loops. You only have one (and you're doing it about the most awkward way imaginable.)

Hint

        i=0;
        while(i==0){ // Wait for the Rising Edge

        if (PORTCbits.RC2 == 1)
        {
            i=1;
         }


You can get exactly the same functionality with:
    while (PORTCbits.RC2 != 1);  // wait until RC2 is high

post edited by qhb - 2019/04/04 18:05:59

Nearly there...
Page: < 12345.. > >> Showing page 5 of 7
Jump to:
© 2019 APG vNext Commercial Version 4.5