Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
- Committer:
- bcostm
- Date:
- 2016-03-16
- Revision:
- 4:6a66cf97f2cf
- Parent:
- 3:f4b02d11940b
- Child:
- 6:1b7486249c9a
File content as of revision 4:6a66cf97f2cf:
#include "mbed.h"
AnalogOut my_output(PA_4);
#define PI (3.141592653589793238462)
#define AMPLITUDE (1.0) // x * 3.3V
#define PHASE (PI * 1) // 2*pi is one period
#define RANGE (0x7FFF)
#define OFFSET (0x7FFF)
// Configuration for sinewave output
#define BUFFER_SIZE (360)
uint16_t buffer[BUFFER_SIZE];
void calculate_sinewave(void);
int main() {
printf("Sinewave example\n");
calculate_sinewave();
while(1) {
// sinewave output
for (int i = 0; i < BUFFER_SIZE; i++) {
my_output.write_u16(buffer[i]);
wait_us(10);
}
}
}
// Create the sinewave buffer
void calculate_sinewave(void){
for (int i = 0; i < BUFFER_SIZE; i++) {
double rads = (PI * i)/180.0; // Convert degree in radian
buffer[i] = (uint16_t)(AMPLITUDE * (RANGE * (cos(rads + PHASE))) + OFFSET);
}
}