Cannot use codegen with C code written for Arduino board

Hi,
I am trying to make an executable mex file to change the PWM frequency of an Arduino pin since the Arduino hardware support add-on does not have a function to change the frequency of a PWM pin. To achieve this, I have been referring to the guide from the URL: https://au.mathworks.com/help/coder/ug/call-cc-code-from-matlab-code.html. In both the C source and header files, I have included the line:
#include <avr/io.h>
However, upon running codegen, I receive a message which states "??? Build error: C compilier produced errors. See the Build Log for details". If I check the build log, I can see the error message:
C:/Users/matth/Documents/MATLAB/writePWMFrequency.c:1:20: fatal error: avr/io.h: No such file or directory
#include <avr/io.h>
^
compilation terminated.
This implies that I need to somehow install avr-gcc in the MATLAB path. Is there a way to resolve my problem? For further info, this code is not for a homework task.

4 件のコメント

Arpita Agrawal
Arpita Agrawal 2022 年 12 月 20 日
Hi Matthew,
Currently, this feature is not supported in MATLAB SPKG for Arduino Hardware. We have made a note of this feature request and will consider it for future releases. However, you can use the Simulink SPKG for Arduino HW as the PWM frequency is programmable from the Simulink blocks. Generate pulse width modulated signal on analog output pin - Simulink - MathWorks India.
Thanks
Arpita
Matthew Tieu
Matthew Tieu 2022 年 12 月 20 日
Hi Arpita,
Thanks for the answer. Based on your comments, it looks like the PWM frequency is only programmable in Simulink.
Harshit Saini
Harshit Saini 2022 年 12 月 21 日
I have reproduced the error you are facing using the “Call C Code” example from the link you have provided. I moved the header file into a directory to replicate the path inside the code you are executing.
To resolve the error,
  • Make sure the path to the header file is defined correctly inside the two files it is referenced from.
i) inside the MATLAB file at the function
coder.cinclude(path)
ii) inside the C file
  • Make sure the header file exists inside the directory path where all the include files are located. As you are using “<>” syntax for including the header files, the pre-processor will search that predetermined directory path to locate the header file. If you enclose the header inside double quotations (""), the pre-processor will look for the header file in the same directory as the source file.
Matthew Tieu
Matthew Tieu 2022 年 12 月 21 日
Hi Harshit,
I have included the header file. However, the issue is that the library 'avr/io.h' is not produced by me (similar to stdio.h and string.h). It is a library for AVR microcontrollers which is developed by teh company Microchip. This library requires a separate compilier called avr-gcc.
Regards
Matthew

サインインしてコメントする。

 採用された回答

Matthew Tieu
Matthew Tieu 2022 年 12 月 20 日

0 投票

https://au.mathworks.com/matlabcentral/answers/1880177-cannot-use-codegen-with-c-code-written-for-arduino-board#comment_2527392

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeArduino Hardware についてさらに検索

製品

リリース

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by