Hi Joep,

On 05/03/10 11:06 am, Joep Suijs wrote:

To prevent a negative value I would think it should be:
   if (_one_ms_delay>  0) then
I thought so too at the time and checked again yesterday. The code
provided works like (_one_ms_delay>  0), which implies there is still
a signed/unsigned issue here .

I don't think so. There seem to be 2 issues:

1.
Let me say it in different words then before: when subtracting a positive value from 1000, then the result is always smaller than 1000. Thus the conditional expression 'value <= 1000' gives always true and the if-statement can be omitted! This was the main argument in my original message. As the new comments explain: the if-statement is meant to avoid passing a negative value to _usec_delay(). So obviously (at least to me!) the conditional expression should be: '(_one_ms_delay > 0)'.

The value of _one_ms_value for the delay_1ms procedure becomes negative only with very low oscillator frequency. For example with 31 KHz it becomes -708. In the current implementation (-708 <= 1000) gives 'true' and thus _usec_delay(-708) would be compiled. Note: I say 'would be compiled' because I 'see' a very long delay, but I don't know which value is passed to _usec_delay (it could be -708 is interpreted as unsigned dword). This is related to the second issue.

2.
You are probably right about the compare of signed/unsigned/universal constants. I did some testing and found out that when _one_ms_delay and instruction_time are declared without type (thus as 'universal'), the if-branch of (_one_ms_delay > 0) is taken even when _one_ms_delay is supposed to have a negative value (e.g. with 31KHz oscillator). But when both are declared as 'sdword' constants the procedure works as designed: the else-branch is taken and _usec_delay(1) is compiled.

Summary: I propose to declare instruction_time and _one_ms_delay as sdwords and change the conditional expression into:
 if (_one_ms_delay > 0) then

Note: the same applies to some other similar procedures in this lib.


Feel free to improve on this. I'm sure you'll test it thoroughly

I would, but you spent time to calibrate the delay procedures for different oscillator frequencies. I assumed the code was OK, but I simply didn't understand it.

I would prefer you (or someone else) to redo the tests. When we come to the same conclusion we'll see who modifies the library, otherwise we need more analysis. I've included a simple blink program below to do the tests with different oscilator frequencies. No need to change delay.jal. Even no need to actually run it, the .asm output shows the difference: _usec_delay(_one_ms_delay) or _usec_delay(1).

I think I'll open an issue with globally this text.

Regards, Rob.



-- ------------------------------------------------------
-- Title: Test delay routines for a 16f690 on internal oscillator
-- ------------------------------------------------------
include 16f690                      -- target PICmicro
pragma target OSC  INTOSC_NOCLKOUT  -- internal oscillator
pragma target WDT  disabled         -- no watchdog
pragma target MCLR external         -- reset externally
--
pragma target clock    31250        -- oscillator frequency
OSCCON_IRCF = 0b000                 -- set internal oscillator
--
enable_digital_io()                 -- make all pins digital I/O
--
alias   led      is pin_C0          -- pin with the led
pin_C0_direction =  output
--
const sdword instruction_time = 400_000_000 / target_clock -- in 10ns units
const sdword _one_ms_delay = 1000 - ((14 * instruction_time) / 100)
--
forever loop
   led = !led                       -- flip
   if _one_ms_delay > 0 then
     _usec_delay(_one_ms_delay)
   else
     _usec_delay(1)
   end if
end loop
--

--
Rob Hamerling, Vianen, NL (http://www.robh.nl/)

--
You received this message because you are subscribed to the Google Groups 
"jallib" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/jallib?hl=en.

Reply via email to