Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 4075
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 3, 20222022-06-03T11:25:26+00:00 2022-06-03T11:25:26+00:00

c – Attiny25 weird timing/behaviour

  • 0

[ad_1]

The project as follows: an attiny25 powers up a dht22 and an unltrasonic distance sensor with a MOSFET. The output of the MOSFET is also used to power a bus, there’s a 3.3K pullup on it. The main program mostly sleeps, and when it’s time, it executes this:

            if ((buff[1]<<8) + buff[0] <= powerCutoff) {
                generalStatus &= wipeMask; // Wipe lower 3 bits

                setOutput(signalPin);
                setLow(signalPin);

                MOSFETPowerOn(devPower)
                initSleep(1<<WDP2);

                measureDHT();
                measureDistance();

//                sendData();
                MOSFETPowerOff(devPower)
            }

You also need to know these:

#define MOSFETPowerOn(line) { line ## _port &= ~(1<<line); line ## _ddr |= (1<<line); asm("nop"); }
#define MOSFETPowerOff(line) { line ## _ddr &= ~(1<<line); line ## _port |= (1<<line); asm("nop"); }

#define ping(bit) { \
    bit ## _port &= ~(1<<bit); \
    bit ## _ddr |= (1<<bit); \
    bit ## _port |= (1<<bit); \
    asm("nop"); \
    bit ## _port &= ~(1<<bit); \
    asm("nop"); }

/** Devices power pin */
#define devPower PB2
#define devPower_port PORTB
#define devPower_ddr DDRB
#define devPower_pin PINB

/** Debug */
#define debug PB0
#define debug_port PORTB
#define debug_ddr DDRB
#define debug_pin PINB

void initSleep(uint8_t timing) {
    asm("WDR");
    WDTCR = 1<<WDIE | timing;

    MCUCR &= ~(1<<SM0);
    MCUCR |= 1<<SE | 1<<SM1;
    asm("sleep");
}
void measureDHT() {
    generalStatus |= 1<<fDHTMeasurementInProgress;

    initSleep(1<<WDP2); // 0.25s

    for (uint8_t i = 2; i < 7; i++) buff[i] = 0;
    dht22_status.status = dht22_inPresence;
    dht22_status.bitCounter = 0;
    dht22_status.byteCounter = 0;

    setOutput(devBus);
    _delay_us(1200); // 1ms initial low pulse
    setInput(devBus);
    _delay_us(50);
...

Clock is 8MHz. The power comes from a regulator, yes there is a small cap. I use parts of this code for many other projects, the initSleep() for example, and it works fine.

The problem is what happens between turning on the power for the dht and initiating the comms with it. As you can see in the datasheet, 1<<WDP2 fot the watchdog timer gives 0.25s sleep time. Then the measureDHT() kicks in and (almost) starts with the same 0.25s sleep, then I drive the bus low. So between power on and driving the bus low, one would expect a bit more than 500ms. Reality is 2349ms. 🙁 The rest of the timing is fine. First row is power, second row is the data from the dht, third row is the distance.
Bad timing

If I add one line after turning on the MOSFET like this:

...
                setOutput(signalPin);
                setLow(signalPin);

                MOSFETPowerOn(devPower)

                ping(debug)
                initSleep(1<<WDP2);

                measureDHT();
...

…this happens:
Correct timing

In the bottom line you see the “ping” as a little spike. The timing is suddenly correct. I ran out of ideas what it could be. I’m using Linux, my avr-gcc is v5.4.0, the programmer is an AVR Dragon. Any ideas?

[ad_2]

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 8143 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1925 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1960 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.