Hello, I haven't been using Arduino for about a year, but I am moving into more challenging micros at the moment. I have a problem with some of the limitations with the bootloader that is running on my board. One thing lacking is that there is no built-in PWM analog write function. What is the syntax of the library that contains the built-in AnalogWrite function? I am using an MC9S12DG256B HCS12 microcontroller, and I have been thinking about defining a few libraries to make development and coding much more streamlined. Where can I find more about how the bootloader/function libraries function?
It's not a separate library. It's part of the 'core'. On the UNO/Nano/Mega the 'init()' function in
Arduino15/packages/arduino/hardware/avr/1.8.5/cores/arduino/wiring.c is called before your setup() to set up the hardware timers for PWM.
The function analogWrite() is in Arduino15/packages/arduino/hardware/avr/1.8.5/cores/arduino/wiring_analog.c
You will also find it accesses counter timers that another processor might not have. That is the big problem in trying to port stuff that is in the core.
The PWM function on AVR Arduinos is implemented mostly in hardware, via the timer peripheral. Basically, the timers are set up to count continuously, and when the count reaches the contents of "compare registers", the hardware manipulates the output pin. All "analogWrite() really does is update the compare register values (and make sure the pin is in a mode where it is controlled by the timer.)
This is a pretty common feature of MCU Timer peripherals, but the details change between different vendors (or even different chips from the same vendor), so looking at the AVR code is probably not much use if you want to program a MC9S12DG256B.
I notice that the MC9S12DG256 has a specialized PWM peripheral, for example. You'll probably want to use that, rather than the general purpose times.
Manufacturers want people to use their product, so the data sheets usually explain the operation of any feature that people would find "handy" very well. PWM is such a common need that it wouldn't be overlooked or glossed over.
The code follows directly from that - looking at other processor code won't help you much. The PWM hardware is doing the heavy lifting. Usually the code is just configuration.
Is there an Arduino based on that chip?
I did a search of the datasheet and PWM seems to be built-in. It's also built-into the ATmega chip. which explains why you can run it continuously in the background without worrying about whatever else your Arduino is doing.
If there is no Arduino based on that chip, you'll have to use the software development kit for the particular chip (which I assume you'd be doing already anyway).
Nope. I don't think that there is C++, either.
That doesn't mean that the Arduino "analogWrite()" functionality isn't a reasonable thing to implement. In fact, trying to imagine how to implement the Arduino core on ANY new-to-you microcontroller is probably a pretty good learning experience...
I learned PWM for this micro. It takes a while to initialize the PWM. I want to make some libraries that function similarly to the Arduino core to make it simpler to program.
Libraries are a standard thing in C. Your IDE should support it, or at least provide the framework for coding them. You might have to research how it is best implemented in your tool chain.
But it really has nothing to do with Arduino. What tool chain are you using?
I did use STM Cube to implement PWM on the STM32F103, it was relatively easy but I didn't bother to make any libraries.
I'm creating functions that incorporate things from the base bootloader (all of the PWM initialization/utilization stuff.) Also, maybe some of the ADC functionality for an analog read to make something similar in functionality to Arduino. I Do not have any built-in libraries for that so far. I have to create things/function libraries that function like the Arduino core.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.