pulseIn error attiny13a
I'm trying to use pulseIn at attiny13a while at attiny85 has working with same code but there have problem when uploading the sketch with code "sg=pulseIn(2,HIGH);" //at attiny85 its work error mesage " exit status 1 too few arguments to function 'uint32_t pulseIn(uint8_t, uint8_t, uint32_t)' " When I put timeout in the code "sg=pulseIn(2,HIGH,1000000);" it can uploading but not work properly Can you help me how to use pulseIn at attiny13a? Thank you
This is related to #30. I had the same problem, pulseIn was not returning expected values. To fix:
First step is to calibrate internal oscillator if it is used by writing to OSCCAL register (I found proper value by experimenting - setting pin to HIGH then waiting with delayMicroseconds for some time and then setting the PIN low, running that in loop; I then check against oscilloscope when the time from delayMicroseconds matches my scope measurements of pulse width).
Then I used code proposed in #30 yielding new pulseIn implementation:
uint32_t pulseInx(uint8_t pin, uint8_t state, uint32_t timeout)
{
uint32_t width = 0; // Keep initialization out of time critical area
// Convert the timeout from microseconds to a number of times through
// the initial loop; it takes 16 clock cycles per iteration.
uint32_t numloops = 0;
uint32_t maxloops = microsecondsToClockCycles(timeout) >> 4; // Divide by 16
// Wait for any previous pulse to end
while (!!(PINB & _BV(pin)) == state)
{
if(numloops++ == maxloops) {return 0;}
asm("nop \n");
asm("nop \n");
}
// Wait for the pulse to start
while (!!(PINB & _BV(pin)) != state)
{
if(numloops++ == maxloops) {return 0;}
asm("nop \n");
asm("nop \n");
asm("nop \n");
}
// Wait for the pulse to stop This loop is 16 instructions long
while (!!(PINB & _BV(pin)) == state)
{
if(numloops++ == maxloops) {return 0;}
width++;
asm("nop \n");
asm("nop \n");
asm("nop \n");
}
// Convert the reading to microseconds.
return clockCyclesToMicroseconds(width << 4); // Same as multiplying by 16
}
I include that function in my code and refer to pulseInx instead of pulseIn. Very dirty solution and I am sure there are better but that got the job done for me so I called it a day.
Also make sure that CPU your frequency is set properly and matches F_CPU define.