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.
LUTs/LUTS.cpp@9:f6ba53e355a0, 2020-05-06 (annotated)
- Committer:
- lukeocarwright
- Date:
- Wed May 06 14:37:34 2020 +0000
- Revision:
- 9:f6ba53e355a0
- Parent:
- 7:33cb5f2db1ee
- Child:
- 10:258a1eca02cc
Outputted sin LUT in uint16_t with unit test using DEBUG macro. Allows direct writing of value to table to 16-bit DAC.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lukeocarwright | 7:33cb5f2db1ee | 1 | #include "mbed.h" |
lukeocarwright | 7:33cb5f2db1ee | 2 | #include "LUTs.h" |
lukeocarwright | 7:33cb5f2db1ee | 3 | |
lukeocarwright | 7:33cb5f2db1ee | 4 | LUTs::LUTs() |
lukeocarwright | 7:33cb5f2db1ee | 5 | { |
lukeocarwright | 7:33cb5f2db1ee | 6 | } |
lukeocarwright | 7:33cb5f2db1ee | 7 | LUTs::~LUTs() |
lukeocarwright | 7:33cb5f2db1ee | 8 | { |
lukeocarwright | 7:33cb5f2db1ee | 9 | } |
lukeocarwright | 9:f6ba53e355a0 | 10 | volatile uint16_t sin_wavtable[1024]; |
lukeocarwright | 7:33cb5f2db1ee | 11 | |
lukeocarwright | 7:33cb5f2db1ee | 12 | void LUTs::sin_wavetable() |
lukeocarwright | 7:33cb5f2db1ee | 13 | { |
lukeocarwright | 7:33cb5f2db1ee | 14 | printf("Generating Wavetable \n"); |
lukeocarwright | 9:f6ba53e355a0 | 15 | for (int i=0; i<1024; i++) { |
lukeocarwright | 9:f6ba53e355a0 | 16 | sin_d= 65535*(0.5*sin(2.0*PI*(i/1024.0))+0.5); |
lukeocarwright | 9:f6ba53e355a0 | 17 | //calculates remainder for rounding |
lukeocarwright | 9:f6ba53e355a0 | 18 | rem= fmod(sin_d,1); |
lukeocarwright | 9:f6ba53e355a0 | 19 | //printf("preround= %g -", sin_d); //DEBUG |
lukeocarwright | 9:f6ba53e355a0 | 20 | if (rem>=0.5) { |
lukeocarwright | 9:f6ba53e355a0 | 21 | sin_d=ceil(sin_d); //round UP |
lukeocarwright | 9:f6ba53e355a0 | 22 | } else { |
lukeocarwright | 9:f6ba53e355a0 | 23 | sin_d= floor(sin_d-rem); //round DOWN |
lukeocarwright | 9:f6ba53e355a0 | 24 | } |
lukeocarwright | 9:f6ba53e355a0 | 25 | // printf("Postround= %g -",sin_d); //DEBUG |
lukeocarwright | 9:f6ba53e355a0 | 26 | sin_u=((uint16_t)sin_d); |
lukeocarwright | 9:f6ba53e355a0 | 27 | //printf("sin_u= %u \n", sin_u); |
lukeocarwright | 9:f6ba53e355a0 | 28 | sin_wavtable[i]=sin_u; |
lukeocarwright | 9:f6ba53e355a0 | 29 | //sin_wavtable[i]=get_sin(i); |
lukeocarwright | 9:f6ba53e355a0 | 30 | } |
lukeocarwright | 9:f6ba53e355a0 | 31 | /*//DEBUG |
lukeocarwright | 9:f6ba53e355a0 | 32 | for (int i=0; i<1024; i=i+128) { |
lukeocarwright | 9:f6ba53e355a0 | 33 | printf("sin_wav[%d]= %u \n", i, sin_wavtable); |
lukeocarwright | 7:33cb5f2db1ee | 34 | } |
lukeocarwright | 9:f6ba53e355a0 | 35 | */ |
lukeocarwright | 9:f6ba53e355a0 | 36 | } |
lukeocarwright | 9:f6ba53e355a0 | 37 | |
lukeocarwright | 9:f6ba53e355a0 | 38 | |
lukeocarwright | 9:f6ba53e355a0 | 39 | |
lukeocarwright | 9:f6ba53e355a0 | 40 | |
lukeocarwright | 9:f6ba53e355a0 | 41 | |
lukeocarwright | 9:f6ba53e355a0 | 42 | /* |
lukeocarwright | 9:f6ba53e355a0 | 43 | uint16_t get_sin(int i) |
lukeocarwright | 9:f6ba53e355a0 | 44 | { |
lukeocarwright | 9:f6ba53e355a0 | 45 | //calculates sin val as double (0->65535) |
lukeocarwright | 9:f6ba53e355a0 | 46 | |
lukeocarwright | 9:f6ba53e355a0 | 47 | sin_d= 65535*(0.5*sin(2.0*PI*(i/1024.0))+0.5); |
lukeocarwright | 9:f6ba53e355a0 | 48 | //calculates remainder for rounding |
lukeocarwright | 9:f6ba53e355a0 | 49 | rem= fmod(sin_d,1); |
lukeocarwright | 9:f6ba53e355a0 | 50 | //printf("preround= %g -", sin_d[i]); //DEBUG |
lukeocarwright | 9:f6ba53e355a0 | 51 | if (rem>=0.5) { |
lukeocarwright | 9:f6ba53e355a0 | 52 | sin_d=ceil(sin_d); //round UP |
lukeocarwright | 9:f6ba53e355a0 | 53 | } else { |
lukeocarwright | 9:f6ba53e355a0 | 54 | sin_d= floor(sin_d-rem); //round DOWN |
lukeocarwright | 9:f6ba53e355a0 | 55 | } |
lukeocarwright | 9:f6ba53e355a0 | 56 | //printf("Postround= %g -",sin_d[i]); //DEBUG |
lukeocarwright | 9:f6ba53e355a0 | 57 | return((uint16_t)sin_d); |
lukeocarwright | 9:f6ba53e355a0 | 58 | |
lukeocarwright | 9:f6ba53e355a0 | 59 | //summed into one function |
lukeocarwright | 9:f6ba53e355a0 | 60 | return((uint16_t)floor(65535*(0.5*sin(2.0*PI*(i/1024.0))+0.5))); |
lukeocarwright | 9:f6ba53e355a0 | 61 | } |
lukeocarwright | 9:f6ba53e355a0 | 62 | */ |