Migrating Arduino C to PICO C

Nothing to do with the subject of the post. Although the I guess you can see the “SEa”

You’d think it would be easy to move a C program from the Arduino platform to the Raspberry Pi PICO. After all, we have the wonderful libraries from Earle F. Philhower, III and the fabulous PlatformIO framework you can use to manage your project (don’t forget to perform this stuff before you try to build anything). And anyway, C is the same everywhere. Right?

Wrong.

When I write the Arduino stuff a while back I made one or two questionable design decisions. One of them was to use the char data type to hold 8 bit values. This was kind of OK back in the day (and by this I mean the 1980’s) but it turned out to be a stupid thing to do. Because some C compilers think that char holds a signed value (-128 to 127) and some think it holds unsigned (0 to 255). Of course the Arduino C compiler goes one way and the PICO C compiler goes the other way. Neither are wrong - the C language standard doesn’t specify this - and you could argue that anyone daft enough to perform maths with variables that are supposed to just hold character codes deserves all the trouble they get. But we are where we are, which in my case was looking at a program that has been working for years and has suddenly gone nuts.

I actually fixed it quite quickly once I figured out the stupid thing I’d done. The Pico debug probe proved very useful for this. I was able to set breakpoints in the timer interrupt handlers and then watch as various counters were updated. When I saw the code subtract 1 from 0 and get the answer 255 I knew exactly what had happened…..