Re: [PIC] Learning C
Isaac Marino Bavaresco <[email protected]>
| Newsgroups | gmane.comp.hardware.microcontrollers.pic |
|---|---|
| Message-ID | <[email protected]> |
I rarely use other people's code, unless it is deemed to be of very high
quality. I prefer to study the code and write my own version based on
it. It is slow but has proven to save a lot of problems in the long run.
I write the absolute majority of my code in C (C99 if possible) and try
to keep with GCC. If a platform doesn't have a GCC port it is very
likely that I won't adopt it. That's not the case for PICs, which I use
for so many years that it predates the GCC wide availability.
My LCD routines were written in C in the early 1990s for 8051 MCUs and I
still use them for ARM and MIPS (PIC32). If they were written in
assembly language I would have needed to recreate them at least five
times (AVR, PIC, PIC18F, PIC32, ARM).
I rewrote a large part of FATFS, because the code was so complicated and
obscure that I could not understand it. I think that I found a few
mistakes in the implementation during the process.
I also wrote my own RTOS because I was not satisfied with the code of
FreeRTOS.
Cheers,
Isaac
Em 10/11/2024 21:36, smplx escreveu:
> Hi Isaac,
> The point isn't really what I could or should do, it's more to do with
> what someone else has written and the rest of us have to use. How
> often have you come across some code that was abandoned by the
> original author many years ago and has been hacked by uncareing
> individuals to fix an immediate problem. How often have you come
> across comments that no longer reflect what the code actually does.
>
> And to make the NaN situation even more interesting a floating point
> library might implement NaNs correctly (or not) where the "noddy" MCU
> compiler doesn't. This is another reason why it's best to learn C in a
> proven high quality dev environment like a PC. Look at the early PIC C
> compilers - I dare you not to laugh.
>
> Another way to express a NaN's character is to say "it is unordered".
>
> Friendly Regards
> Sergio Masci
>
> On Sun, 10 Nov 2024, Isaac Marino Bavaresco wrote:
>
>> But you could use also the function 'isnan', which is easier to
>> understand for someone reading your code.
>>
>> There are also other functions like 'isinf', etc.
>>
>>
>> Interestingly, a NaN is not greater, nor smaller nor equal to
>> anything, not even itself. It is only different than everything.
>>
>>
>> Cheers,
>>
>> Isaac
>>
>>
>> Em 10/11/2024 20:09, smplx escreveu:
>>> Hi Colin,
>>>
>>> You might be intested to know that to check for floating point
>>> errors / exceptions in C you would use:
>>>
>>> if (x == x)...
>>> or
>>> if (x != x)...
>>>
>>> where x is a float
>>>
>>> e.g.
>>> float x
>>>
>>> x = 1.0 / 0.0;
>>>
>>> if (x != x)
>>> { // x is a NaN which is an error
>>> }
>>>
>>> Here clearly "x 'is the same as' x" but the test is "x 'is a NaN'".
>>>
>>> Friendly Regards
>>> Sergio
>>>
>>> On Sun, 10 Nov 2024, CDB wrote:
>>>
>>>> I have always separated the difference of = and == in C by
>>>> referring to the latter as 'is the same as'
>>>>
>>>> if (X is the same as Y) then ...... if (X == Y) then
>>>> x = y x now contains the value of Y.
>>>>
>>>> Colin
>>>>
>>>>
>>>> --
>>>>
>>>> [email protected] 11 November 2024
>>>>
>>>> Hosted by: www.superdomains.com.au
>>>>
>>>> This email is to be considered private if addressed to a named
>>>> individual or Personnel Department, and public if addressed to a
>>>> blog, forum or news article.
>>>>
>>>>
>>>>
>>>> __________________________________
>>>> Sent from eM Client | www.emclient.com <https://www.emclient.com/get>
>>>>
>>>>
>>>> On 11/11/2024 08:24:15, "smplx" <[email protected]> wrote:
>>>>
>>>>>
>>>>>
>>>>> On Sun, 10 Nov 2024, Neil wrote:
>>>>>
>>>>>> Agree with a couple things from other comments such as C being
>>>>>> high-level assembler and using a PC at first (you can just use
>>>>>> gcc already on Linux).
>>>>>>
>>>>>> Already knowing a structured language is very helpful.
>>>>>> But IMO you'll have to wrap your head around pointers, which can
>>>>>> get confusing. I find this C visualizer very helpful for figuring
>>>>>> out some algorithm... https://pythontutor.com/c.html
>>>>>>
>>>>>> One more note: = != ==
>>>>>> Or in other words, the equal sign ( = ) and the double equal size
>>>>>> ( == ) are very different things in C. The first assigns a value
>>>>>> and the other compares values. But people new to C will
>>>>>> invariably use a single equal for a comparison and the code is
>>>>>> true every time, as C will compile it as valid code, do the
>>>>>> assignment, then the comparison. You have been warned ;P
>>>>>
>>>>> Sorry to wade in here but:
>>>>>
>>>>> if (y = x)...
>>>>>
>>>>> assigns x to y then compares the result to 0. 0 is nomally taken to
>>>>> mean false and "not 0" as true. So it is almost alway true except
>>>>> in the case where x is 0.
>>>>>
>>>>> I'm not sure what happens if x and y are floats and x is a NaN or
>>>>> if x and y are structures.
>>>>>
>>>>> if you're looking for a real headache try debugging a program that
>>>>> contains:
>>>>>
>>>>> if (! x == y)...
>>>>>
>>>>> Trying to learn C on bare metal is like trying to learn to fly by
>>>>> crashing a lot. It's a lot quicker and easier if you learn on a
>>>>> simulator.
>>>>>
>>>>> Friendly Regards
>>>>> Sergio Masci
>>>>>
>>>>> -- http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
>>>>> View/change your membership options at
>>>>> https://mailman.mit.edu/mailman/listinfo/piclist
>>>> --
>>>> http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
>>>> View/change your membership options at
>>>> https://mailman.mit.edu/mailman/listinfo/piclist
>>>>
>>
>> --
>> O software antivírus Avast realizou uma checagem de vírus neste e-mail.
>> www.avast.com
>> --
>> http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
>> View/change your membership options at
>> https://mailman.mit.edu/mailman/listinfo/piclist
>>
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
https://mailman.mit.edu/mailman/listinfo/piclist