Author Topic: NEWBIE Needs Help on PIC12F683 and Proteus 8 Pro  (Read 2226 times)

0 Members and 1 Guest are viewing this topic.

Offline SuzyC

  • Frequent Contributor
  • **
  • Posts: 792
Re: NEWBIE Needs Help on PIC12F683 and Proteus 8 Pro
« Reply #25 on: December 16, 2023, 08:40:13 pm »
 __delay_ms(500);
Specsheet says in Section 4, must wait 1-second on power up before interacting with this device. This is too short.
-------------------------------------------------------------------------------
With the DHT11. you are only working with digital signals, so setting ANSEL=0 keeps every pin used in a digital logic level read or write mode.

Once you TRIS a pin = 0, the pin is in the output mode and stays in the output mode until changed by you or a reset occurs.
Once you TRIS GPO  pin =0 then you can output to an external device on that pin.
If the pin is GPO, then

GP0=1; //sets this output pin to a logical high '1' level,   
GP0=0; //sets this pin to a logical low  '0' level.
//-----------
If you TRIS a pin = 1 with ANSEL=0; //then the pin is in a digital input mode and can read only a 1 or 0 on the output level of any external device pin this pin is connected to.
If the pin is connected to a logical one hi-level voltage, (DHT pin=1)the MCU would read/see the '1'  digital logical level as being input.
//so you might code
uint8_t DHT_OutputSignalState; //Var to be used to read/examine/store the digital bit state when reading the GPO pin and
//then if GPO is connected to the DHT pin (and you have a pullup resistor to Vdd. This is as shown in your posted schematic)
DHT_OutputSignalState=GPO; //when GPO is in the digital input mode.
 
However, after TRiS'd =1 to be in digital input mode this same pin can be switched to be an analog input pin using ANSEL. in this mode, instead of reading a digital input/output level, the MCU's A2D peripheral set correctly can read any voltage level this pin is connected to externally.
You do this by setting this same set to digital input by TRIS=1 pin to an analog input mode by setting ANSEL reg pin = 1;


   
« Last Edit: December 16, 2023, 09:27:37 pm by SuzyC »
 

Offline SuzyC

  • Frequent Contributor
  • **
  • Posts: 792
Re: NEWBIE Needs Help on PIC12F683 and Proteus 8 Pro
« Reply #26 on: December 16, 2023, 08:58:34 pm »
- TRISIO0= 1 ;
- GP0=0 means the pin is on input mode and its voltage is 0V(low) and GP0 = 1 means I COULD NOT FIGURE THIS OUT LOL. Maybe this means it is now at 5V and receiving data.



=========
When you use TRIS a pin to 1, the pin is in a digital input mode, so trying to output (GP0=1;) makes no sense you don't output on a pin when it is in the input mode.
« Last Edit: December 16, 2023, 09:00:18 pm by SuzyC »
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6016
  • Country: es
Re: NEWBIE Needs Help on PIC12F683 and Proteus 8 Pro
« Reply #27 on: December 16, 2023, 09:03:39 pm »
__delay_ms(500);
Specsheet says in Section 4, must wait 1-second on power up before interacting with this device. This is too short.
Probably, I never used a DHT11, I just focused on the timings  ;).

Quote
When we make TRISIO0 = 0; this puts the pin on output mode, what happens if we make GP0 = 1; and GP0 = 0;? (Also when TRISIO0=1;)
TRISIO0=0 puts GP0 in output mode. Writing to GP0 will set the output state.

TRISIO0=1 puts GP0 in input mode. Writing to GP0 makes no effect. Reading GP0 reflects the state of the pin.

However, I explained why I'm setting GP0 high/low, due a Proteus bug.
Proteus is a bit buggy, after writting 0 to GPIO always reads 0 in input mode. Setting it to 1 in input mode seems to work.

Quote
- Why do we shift bits? What does shifting bit means? (I googled it but still...)
Shifting is the same as rotating:
Data = 01000010
Data <<=1;
Data = 10000100


Quote
- __delay_us(30);                 // Wait 30us (1=70us, 0=26us) I am a bit confused. Manuals says every bit of daha begins with a 50 us low-voltage-level and if +70= 1 and +26=0. Why do we wait only for 30 us? what about 50us at the beginning of the bit? I could not get this.
It already waited for the data going low and rising again, that's the part detecting each data bit.
After 30us the data will be 0 again (Next start) if it was a "0” bit (26us), or still 1 for a "1" bit (70us).

Code: [Select]
uint8_t dht11_read() {
    uint8_t data = 0;       
    for (uint8_t i = 0; i < 8; i++) {   // Receive 8 bits
        data <<=1;                      // Shift data
        while(DHT11_PIN);               // Wait until pin goes low (start of data)
        __delay_us(5);                  // Wait 5us to prevent noise
        while(!DHT11_PIN);              // Wait until pin goes high (data)
        __delay_us(30);                 // Wait 30us (1=70us, 0=26us)              <-----
        data |= DHT11_PIN;              // Store bit
    }
    return data;
}

The shift is placed first because we don't want to rotate the last bit.
At first data=0 so it makes no effect
« Last Edit: December 16, 2023, 09:49:07 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline SuzyC

  • Frequent Contributor
  • **
  • Posts: 792
Re: NEWBIE Needs Help on PIC12F683 and Proteus 8 Pro
« Reply #28 on: December 16, 2023, 09:15:09 pm »
In this snippet;
- Why do we shift bits? What does shifting bit means? (I googled it but still...)


The hum and temp are serially sent out from the DHT  in bit order, highest bit first.

It makes sense to send the highest bit first and yet put the bit received into the rightmost bit position of the receiving data byte.

Shifting the value read left then allows the next lower bit to be placed in the rightmost bit position of the data word being read.

After the last bit is input, all the bits have then marched(shifted left) into their correct data position.

Each shift marches all the bits in the receiving word left one position. After 8 shifts, all bits are in their correct position.

« Last Edit: December 17, 2023, 05:08:57 am by SuzyC »
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12920
Re: NEWBIE Needs Help on PIC12F683 and Proteus 8 Pro
« Reply #29 on: December 16, 2023, 09:17:00 pm »
Technically, writing to a port pin that's in input mode *DOES* have an effect but it is hidden until the pin is next made an output.

Look at the datasheet FIGURE 4-2: BLOCK DIAGRAM OF GP1

If you write to the port while any of its pins are in input mode, you preload the output latches for those pins with whatever you write, without affecting the external pin. The latched level then takes effect immediately when you change the pins mode to output, but has no effect until then.  This behaviour is commonly used, presetting the output latch low, to fake an open drain I/O pin, output controlled by its TRIS bit, e.g. when bit-banging I2C.

As usual on the older PICs, the RMW effect must be carefully considered as the output latches aren't accessible for readback while in input mode.
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6016
  • Country: es
Re: NEWBIE Needs Help on PIC12F683 and Proteus 8 Pro
« Reply #30 on: December 16, 2023, 09:39:58 pm »
Let's pulse a led when reading the data, so you can visually see how it's doing:
Code: [Select]
uint8_t dht11_read() {
    uint8_t data = 0;       
    for (uint8_t i = 0; i < 8; i++) {   // Receive 8 bits
        data <<=1;                      // Shift data
        while(DHT11_PIN);               // Wait until pin goes low (start of data)
        __delay_us(5);                  // Wait 5us to prevent noise
        while(!DHT11_PIN);              // Wait until pin goes high (data)
        __delay_us(30);                 // Wait 30us (1=70us, 0=26us)
        data |= DHT11_PIN;              // Store bit
        LED_GREEN=1;
        LED_GREEN=0;                    // Small LED pulse
    }
    return data;
}

Remember:
After 30us the data will be 0 again (Next start) if it was a "0” bit (26us), or still 1 for a "1" bit (70us).



Let's suppose this byte:



This is how the shifting/rotation works:

Quote
dht11_read() flow

                          DATA            BIT
data=0;            00000000

data<<=1;       00000000
data|=BIT;        00000000        0

data<<=1;       00000000
data|=BIT;        00000001        1

data<<=1;       00000010
data|=BIT;        00000010        0

data<<=1;       00000100
data|=BIT;        00000100        0

data<<=1;       00001000
data|=BIT;        00001001        1

data<<=1;       00010010
data|=BIT;        00010011        1

data<<=1;       00100110
data|=BIT;        00100111        1

data<<=1;       01001110
data|=BIT;        01001110        0

return data;      01001110         0
« Last Edit: December 16, 2023, 10:09:21 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf