Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to modifying the PWM frequency on the OpenCM9.04 ? #106

Open
adluthi opened this issue Aug 16, 2021 · 2 comments
Open

How to modifying the PWM frequency on the OpenCM9.04 ? #106

adluthi opened this issue Aug 16, 2021 · 2 comments

Comments

@adluthi
Copy link

adluthi commented Aug 16, 2021

Hi all,

I am using the OpenCM9.04 board on Arduino and I want to control the frequency of the PWM generated but I don't find any way to do it....

I have found on the link at the section "4.5.5.4. Arduino Compatibility" but I don't find which timer controls the PWM signal (point 1 on the 4.5.5.4 part)...

Does anyone have an idea how to do that please?

Thank you in advance!

@ROBOTIS-Will
Copy link
Contributor

@adluthi
Copy link
Author

adluthi commented Aug 17, 2021

Hi @ROBOTIS-Will,

Thank you for your answer however since I am a new user with the OpenCM board and also because I am coding in Arduino, I don't know how the files you sent me could help me...

For example, I have done this code which allows me to set a duty cycle through serial communication but I really don't get how to tune the frequency. The setPeriod() function does not change anything. Can you guide me, please?

Thank you,

`
int pwm_pins = 12;
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;

HardwareTimer timer(TIMER_CH1);

void setup() {
Serial.begin(9600);
timer.setPeriod(5000);
}

void loop() {
// put your main code here, to run repeatedly:
uint8_t pwm_out = 0;

recvWithEndMarker();
setPWM();
}

void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;

while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

    if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
            ndx = numChars - 1;
        }
    }
    else {
        receivedChars[ndx] = '\0'; // terminate the string
        ndx = 0;
        newData = true;
    }
}

}

void setPWM() {
if (newData == true) {
analogWrite(pwm_pins, atoi(receivedChars));
Serial.println(atoi(receivedChars));
newData = false;
}
}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants