Hi,
I am not able to generate Pwmout with my code. My board is nucleof401re.
I want to generate pulses of a specific frequency, which depends upon the rate entered by the user.
Initial code works fine which includes taking data from the keypad, PWM part doesn’t work.
Code-:
PwmOut clk(D9);
frequency = 212*rate // rate is entered through keypad
unit_vol_time = 60000 / (rate);
while(value)
{
period = 1/frequency;
clk.period(period);
ThisThread::sleep_for(unit_vol_time);
//delay(unit_vol_time);//millis
clk.suspend();
value--;
if(value==0)
{
printf("Job Over\n\r");
}
else
{
printf("Value Remaining : %d",value);
}
The output for this code as seen in coolterm shows the following error:
Ok, then I think the reason of this error is zero value in the period variable. Whitch si result form period = 1/frequency; So try to verify that calculation and correct usage.
You probably need to modify it to something like this
I verified the calculations and also modified it but nothing worked. I also tried to replace frequency with a fixed number but that also showed the same error.
It seems there is some problem when a serial terminal is in use.
After searching the web , I found this- https://os.mbed.com/questions/82584/Error-Status-0x80FF0100-Code-256-Module-/
It is hard to test your code when is not complete.
In that old question what you mentioned also Ralph Fulchiero confirmed same what I wrote above.
The problem is 0 in the period variable what is passed as parametr to the clk.period() method.
period = 1/frequency;
clk.period(period);
I modified your code because it is not complete and tried it on MbedOS 6.8 with Nucleo-L432KC.
When I treid to printf the period variable it was always 0.0000. Then I modified the code to
float period = (float)1 / (float)frequency;
the result was 0.0021515.
When I passed zero to the clk.period() method, it was crash, but when I pass regular float value, code was running.
#include "select_example.h"
#ifdef KEY4X4
// Include --------------------------------------------------------------------
#include "mbed.h"
#include "Keypad.h"
#include"string.h"
#include"string"
#include<string>
#include<iostream>
// Definition -----------------------------------------------------------------
#if (MBED_MAJOR_VERSION == 2) || (MBED_MAJOR_VERSION == 5)
# define WAIT_100MS() {wait_us(100000);}
#else
# define WAIT_100MS() {ThisThread::sleep_for(100ms);}
#endif
// Object ---------------------------------------------------------------------
DigitalOut dir(D4);
PwmOut clk(D10);
DigitalOut my_led(LED1);
// X Y Z W A B C D OUT(XYZW), IN(ABCD)
Keypad key(PC_3,PC_2,PC_0,PC_1,PB_0,PA_4,PA_1,PA_0);
// RAM ------------------------------------------------------------------------
// ROM / Constant data --------------------------------------------------------
// X*A = *, X*B = 7, X*C = 4, X*D = 1
// Y*A = 0, Y*B = 8, Y*C = 5, Y*D = 2
// Z*A = #, Z*B = 9, Z*C = 6, Z*D = 3
// W*A = A, W*B = B, W*C = C, W*D = D
// key_table[0]=? is not used!
const char *const key_table = "?123A456B789C*0#D";
// 1234567890123456
// Function prototypes --------------------------------------------------------
extern void print_revision(void);
//------------------------------------------------------------------------------
// Control Program
//------------------------------------------------------------------------------
long enter_value()
{ uint32_t key_num,i;
uint32_t counter = 0;
char c;
int keyoo;
int sum=0;
string lv_string = " ";
string final_string;
print_revision();
printf("Start Key-Pad test 4x4 keys\r\n");
for ( i = 0; i < 3; i++) {
while(((key_num = key.read()) == 0)) { // waiting for keypad manipulation
WAIT_100MS();
}
c = *(key_table + key_num);
lv_string+=c;
}
keyoo = stoi(lv_string)
return(keyoo);
}
int main()
{
int value,rate;
int frequency;
float period;
int unit_vol_time;
clk.write(0.5);
while(1)
{
printf("Enter the volume\n\r");
value = enter_value();
printf("%d",value);
printf("\n\rEnter the rate\n\r");
rate = enter_value();
printf("%d",rate);
if(rate>200)
{
rate=200;
}
frequency = (rate) * 212;
unit_vol_time = 60000 / (rate);
printf("\n\rGiven rate: %d\n\r",rate);
printf("\n\rGiven frequency: %d\n\r",frequency);
printf("\n\rGiven Unit Volume Time: %d\n\r",unit_vol_time);
dir = 0;
while(value)
{
float period = (float)1/(float)frequency;
clk.period(period);
ThisThread::sleep_for(unit_vol_time);
//delay(unit_vol_time);//millis
//clk.suspend();
value--;
if(value==0)
{
printf("Job Over\n\r");
}
else
{
printf("Volume Remaining : %d",value);
}
}
}
}
#endif // #ifdef KEY4X4
Pwm problem has been solved.
I want to replicate notone() arduino function using suspend() ,but it suspends the pwm all together and i am not able to figure out where to place resume() for this code.
tone( 12, frequency );
delay(unit_vol_time);//millis
noTone(12);
I did not tested it but for PWM OFF maybe you can try to set duty cycle to 0 - clk.write(0.0) and set it back to 0.5 when you will need it but I have no personal experiences with playing tones.