PIC32MZ I2S generator
Posted: 20 Jan 2021 17:41
Hello,
Here is a small example for how to use the PIC32MZ I2S master.
My code, generate 100Hz, 1KHz, 10KHz, 20KHz, out to I2S, at 180kbps, 24bit.
The I2S must be connected to a 24bit I2S DAC.
For test, I used EasyPic Fusion V7, PIC32MZEFH144, with the folowing config:
Clock form external on board 24MHz OSC, divide by 4, multiply with 63 and PLL divide with 2, final clock 189MHz.
PORTB L, leds must be ON, RG0 switch to down, RG0 button to change the freq.
Enjoy!
Here is a small example for how to use the PIC32MZ I2S master.
My code, generate 100Hz, 1KHz, 10KHz, 20KHz, out to I2S, at 180kbps, 24bit.
The I2S must be connected to a 24bit I2S DAC.
For test, I used EasyPic Fusion V7, PIC32MZEFH144, with the folowing config:
Clock form external on board 24MHz OSC, divide by 4, multiply with 63 and PLL divide with 2, final clock 189MHz.
PORTB L, leds must be ON, RG0 switch to down, RG0 button to change the freq.
Enjoy!
Code: Select all
program I2S_Generator;
const sin_table_const_100hz : array[1800] of longint = (
0x800000,0x807261,0x80e4c3,0x815723,0x81c983,0x823be1,0x82ae3d,0x832097,
0x8392ef,0x840544,0x847796,0x84e9e4,0x855c2e,0x85ce74,0x8640b5,0x86b2f1,
0x872528,0x879759,0x880985,0x887ba9,0x88edc7,0x895fde,0x89d1ed,0x8a43f5,
0x8ab5f4,0x8b27eb,0x8b99d9,0x8c0bbd,0x8c7d98,0x8cef69,0x8d6130,0x8dd2ec,
0x8e449d,0x8eb642,0x8f27dc,0x8f996a,0x900aeb,0x907c5f,0x90edc6,0x915f20,
0x91d06c,0x9241aa,0x92b2d9,0x9323f9,0x93950a,0x94060b,0x9476fc,0x94e7dd,
0x9558ad,0x95c96c,0x963a1a,0x96aab6,0x971b40,0x978bb7,0x97fc1c,0x986c6d,
0x98dcab,0x994cd5,0x99bceb,0x9a2ced,0x9a9cd9,0x9b0cb0,0x9b7c72,0x9bec1e,
0x9c5bb3,0x9ccb32,0x9d3a99,0x9da9ea,0x9e1922,0x9e8843,0x9ef74b,0x9f663b,
0x9fd511,0xa043ce,0xa0b272,0xa120fb,0xa18f6a,0xa1fdbe,0xa26bf6,0xa2da14,
0xa34815,0xa3b5fa,0xa423c3,0xa4916f,0xa4fefe,0xa56c6f,0xa5d9c2,0xa646f7,
0xa6b40e,0xa72105,0xa78dde,0xa7fa97,0xa8672f,0xa8d3a8,0xa94000,0xa9ac37,
0xaa184d,0xaa8441,0xaaf013,0xab5bc3,0xabc750,0xac32bb,0xac9e02,0xad0925,
0xad7424,0xaddf00,0xae49b6,0xaeb448,0xaf1eb4,0xaf88fb,0xaff31b,0xb05d16,
0xb0c6e9,0xb13096,0xb19a1c,0xb2037a,0xb26cb0,0xb2d5bd,0xb33ea2,0xb3a75f,
0xb40ff2,0xb4785b,0xb4e09a,0xb548b0,0xb5b09a,0xb6185a,0xb67fef,0xb6e758,
0xb74e95,0xb7b5a6,0xb81c8b,0xb88343,0xb8e9cd,0xb9502b,0xb9b65a,0xba1c5c,
0xba822f,0xbae7d3,0xbb4d48,0xbbb28e,0xbc17a4,0xbc7c8a,0xbce140,0xbd45c6,
0xbdaa1a,0xbe0e3d,0xbe722f,0xbed5ee,0xbf397c,0xbf9cd7,0xbfffff,0xc062f4,
0xc0c5b6,0xc12844,0xc18a9e,0xc1ecc4,0xc24eb5,0xc2b071,0xc311f8,0xc37349,
0xc3d464,0xc43549,0xc495f8,0xc4f670,0xc556b1,0xc5b6bb,0xc6168c,0xc67626,
0xc6d588,0xc734b1,0xc793a1,0xc7f258,0xc850d6,0xc8af1a,0xc90d24,0xc96af3,
0xc9c888,0xca25e2,0xca8301,0xcadfe4,0xcb3c8b,0xcb98f7,0xcbf526,0xcc5118,
0xccaccd,0xcd0846,0xcd6380,0xcdbe7d,0xce193c,0xce73bc,0xcecdfe,0xcf2801,
0xcf81c4,0xcfdb49,0xd0348d,0xd08d91,0xd0e655,0xd13ed9,0xd1971b,0xd1ef1d,
0xd246dc,0xd29e5b,0xd2f597,0xd34c91,0xd3a348,0xd3f9bd,0xd44fef,0xd4a5dd,
0xd4fb88,0xd550ef,0xd5a612,0xd5faf0,0xd64f8a,0xd6a3de,0xd6f7ee,0xd74bb8,
0xd79f3c,0xd7f27b,0xd84573,0xd89825,0xd8ea90,0xd93cb4,0xd98e91,0xd9e026,
0xda3173,0xda8279,0xdad336,0xdb23ab,0xdb73d6,0xdbc3b9,0xdc1353,0xdc62a3,
0xdcb1a9,0xdd0065,0xdd4ed7,0xdd9cff,0xddeadb,0xde386d,0xde85b4,0xded2ae,
0xdf1f5e,0xdf6bc1,0xdfb7d8,0xe003a3,0xe04f21,0xe09a52,0xe0e536,0xe12fcc,
0xe17a15,0xe1c411,0xe20dbe,0xe2571c,0xe2a02d,0xe2e8ee,0xe33161,0xe37984,
0xe3c158,0xe408dc,0xe45010,0xe496f5,0xe4dd88,0xe523cc,0xe569be,0xe5af60,
0xe5f4b0,0xe639af,0xe67e5d,0xe6c2b8,0xe706c2,0xe74a79,0xe78dde,0xe7d0f0,
0xe813af,0xe8561b,0xe89833,0xe8d9f8,0xe91b6a,0xe95c87,0xe99d51,0xe9ddc6,
0xea1de6,0xea5db2,0xea9d29,0xeadc4b,0xeb1b17,0xeb598e,0xeb97af,0xebd57a,
0xec12ef,0xec500e,0xec8cd6,0xecc948,0xed0562,0xed4126,0xed7c93,0xedb7a8,
0xedf265,0xee2ccb,0xee66d8,0xeea08e,0xeed9eb,0xef12ef,0xef4b9b,0xef83ee,
0xefbbe8,0xeff389,0xf02ad0,0xf061be,0xf09852,0xf0ce8c,0xf1046c,0xf139f2,
0xf16f1d,0xf1a3ee,0xf1d864,0xf20c80,0xf24040,0xf273a4,0xf2a6ae,0xf2d95c,
0xf30bae,0xf33da4,0xf36f3f,0xf3a07d,0xf3d15e,0xf401e4,0xf4320c,0xf461d8,
0xf49147,0xf4c059,0xf4ef0e,0xf51d65,0xf54b5f,0xf578fb,0xf5a639,0xf5d319,
0xf5ff9c,0xf62bc0,0xf65785,0xf682ec,0xf6adf5,0xf6d89f,0xf702ea,0xf72cd5,
0xf75662,0xf77f8f,0xf7a85d,0xf7d0cc,0xf7f8da,0xf82089,0xf847d8,0xf86ec7,
0xf89556,0xf8bb84,0xf8e152,0xf906c0,0xf92bcd,0xf95079,0xf974c4,0xf998ae,
0xf9bc37,0xf9df5f,0xfa0226,0xfa248b,0xfa468f,0xfa6831,0xfa8971,0xfaaa4f,
0xfacacc,0xfaeae6,0xfb0a9f,0xfb29f5,0xfb48e8,0xfb6779,0xfb85a8,0xfba374,
0xfbc0dd,0xfbdde4,0xfbfa87,0xfc16c8,0xfc32a6,0xfc4e20,0xfc6937,0xfc83eb,
0xfc9e3b,0xfcb828,0xfcd1b1,0xfcead6,0xfd0398,0xfd1bf6,0xfd33f0,0xfd4b86,
0xfd62b8,0xfd7986,0xfd8fef,0xfda5f5,0xfdbb96,0xfdd0d2,0xfde5ab,0xfdfa1e,
0xfe0e2d,0xfe21d8,0xfe351d,0xfe47fe,0xfe5a7a,0xfe6c91,0xfe7e44,0xfe8f91,
0xfea079,0xfeb0fc,0xfec11a,0xfed0d2,0xfee026,0xfeef14,0xfefd9c,0xff0bc0,
0xff197d,0xff26d6,0xff33c9,0xff4056,0xff4c7d,0xff583f,0xff639c,0xff6e92,
0xff7923,0xff834e,0xff8d13,0xff9672,0xff9f6c,0xffa7ff,0xffb02d,0xffb7f4,
0xffbf56,0xffc652,0xffcce7,0xffd317,0xffd8e0,0xffde44,0xffe341,0xffe7d8,
0xffec09,0xffefd4,0xfff338,0xfff637,0xfff8cf,0xfffb01,0xfffccd,0xfffe33,
0xffff33,0xffffcc,0xffffff,0xffffcc,0xffff33,0xfffe33,0xfffccd,0xfffb01,
0xfff8cf,0xfff637,0xfff338,0xffefd4,0xffec09,0xffe7d8,0xffe341,0xffde44,
0xffd8e0,0xffd317,0xffcce7,0xffc652,0xffbf56,0xffb7f4,0xffb02d,0xffa7ff,
0xff9f6c,0xff9672,0xff8d13,0xff834e,0xff7923,0xff6e92,0xff639c,0xff583f,
0xff4c7d,0xff4056,0xff33c9,0xff26d6,0xff197d,0xff0bc0,0xfefd9c,0xfeef14,
0xfee026,0xfed0d2,0xfec11a,0xfeb0fc,0xfea079,0xfe8f91,0xfe7e44,0xfe6c91,
0xfe5a7a,0xfe47fe,0xfe351d,0xfe21d8,0xfe0e2d,0xfdfa1e,0xfde5ab,0xfdd0d2,
0xfdbb96,0xfda5f5,0xfd8fef,0xfd7986,0xfd62b8,0xfd4b86,0xfd33f0,0xfd1bf6,
0xfd0398,0xfcead6,0xfcd1b1,0xfcb828,0xfc9e3b,0xfc83eb,0xfc6937,0xfc4e20,
0xfc32a6,0xfc16c8,0xfbfa87,0xfbdde4,0xfbc0dd,0xfba374,0xfb85a8,0xfb6779,
0xfb48e8,0xfb29f5,0xfb0a9f,0xfaeae6,0xfacacc,0xfaaa4f,0xfa8971,0xfa6831,
0xfa468f,0xfa248b,0xfa0226,0xf9df5f,0xf9bc37,0xf998ae,0xf974c4,0xf95079,
0xf92bcd,0xf906c0,0xf8e152,0xf8bb84,0xf89556,0xf86ec7,0xf847d8,0xf82089,
0xf7f8da,0xf7d0cc,0xf7a85d,0xf77f8f,0xf75662,0xf72cd5,0xf702ea,0xf6d89f,
0xf6adf5,0xf682ec,0xf65785,0xf62bc0,0xf5ff9c,0xf5d319,0xf5a639,0xf578fb,
0xf54b5f,0xf51d65,0xf4ef0e,0xf4c059,0xf49147,0xf461d8,0xf4320c,0xf401e4,
0xf3d15e,0xf3a07d,0xf36f3f,0xf33da4,0xf30bae,0xf2d95c,0xf2a6ae,0xf273a4,
0xf24040,0xf20c80,0xf1d864,0xf1a3ee,0xf16f1d,0xf139f2,0xf1046c,0xf0ce8c,
0xf09852,0xf061be,0xf02ad0,0xeff389,0xefbbe8,0xef83ee,0xef4b9b,0xef12ef,
0xeed9eb,0xeea08e,0xee66d8,0xee2ccb,0xedf265,0xedb7a8,0xed7c93,0xed4126,
0xed0562,0xecc948,0xec8cd6,0xec500e,0xec12ef,0xebd57a,0xeb97af,0xeb598e,
0xeb1b17,0xeadc4b,0xea9d29,0xea5db2,0xea1de6,0xe9ddc6,0xe99d51,0xe95c87,
0xe91b6a,0xe8d9f8,0xe89833,0xe8561b,0xe813af,0xe7d0f0,0xe78dde,0xe74a79,
0xe706c2,0xe6c2b8,0xe67e5d,0xe639af,0xe5f4b0,0xe5af60,0xe569be,0xe523cc,
0xe4dd88,0xe496f5,0xe45010,0xe408dc,0xe3c158,0xe37984,0xe33161,0xe2e8ee,
0xe2a02d,0xe2571c,0xe20dbe,0xe1c411,0xe17a15,0xe12fcc,0xe0e536,0xe09a52,
0xe04f21,0xe003a3,0xdfb7d8,0xdf6bc1,0xdf1f5e,0xded2ae,0xde85b4,0xde386d,
0xddeadb,0xdd9cff,0xdd4ed7,0xdd0065,0xdcb1a9,0xdc62a3,0xdc1353,0xdbc3b9,
0xdb73d6,0xdb23ab,0xdad336,0xda8279,0xda3173,0xd9e026,0xd98e91,0xd93cb4,
0xd8ea90,0xd89825,0xd84573,0xd7f27b,0xd79f3c,0xd74bb8,0xd6f7ee,0xd6a3de,
0xd64f8a,0xd5faf0,0xd5a612,0xd550ef,0xd4fb88,0xd4a5dd,0xd44fef,0xd3f9bd,
0xd3a348,0xd34c91,0xd2f597,0xd29e5b,0xd246dc,0xd1ef1d,0xd1971b,0xd13ed9,
0xd0e655,0xd08d91,0xd0348d,0xcfdb49,0xcf81c4,0xcf2801,0xcecdfe,0xce73bc,
0xce193c,0xcdbe7d,0xcd6380,0xcd0846,0xccaccd,0xcc5118,0xcbf526,0xcb98f7,
0xcb3c8b,0xcadfe4,0xca8301,0xca25e2,0xc9c888,0xc96af3,0xc90d24,0xc8af1a,
0xc850d6,0xc7f258,0xc793a1,0xc734b1,0xc6d588,0xc67626,0xc6168c,0xc5b6bb,
0xc556b1,0xc4f670,0xc495f8,0xc43549,0xc3d464,0xc37349,0xc311f8,0xc2b071,
0xc24eb5,0xc1ecc4,0xc18a9e,0xc12844,0xc0c5b6,0xc062f4,0xbfffff,0xbf9cd7,
0xbf397c,0xbed5ee,0xbe722f,0xbe0e3d,0xbdaa1a,0xbd45c6,0xbce140,0xbc7c8a,
0xbc17a4,0xbbb28e,0xbb4d48,0xbae7d3,0xba822f,0xba1c5c,0xb9b65a,0xb9502b,
0xb8e9cd,0xb88343,0xb81c8b,0xb7b5a6,0xb74e95,0xb6e758,0xb67fef,0xb6185a,
0xb5b09a,0xb548b0,0xb4e09a,0xb4785b,0xb40ff2,0xb3a75f,0xb33ea2,0xb2d5bd,
0xb26cb0,0xb2037a,0xb19a1c,0xb13096,0xb0c6e9,0xb05d16,0xaff31b,0xaf88fb,
0xaf1eb4,0xaeb448,0xae49b6,0xaddf00,0xad7424,0xad0925,0xac9e02,0xac32bb,
0xabc750,0xab5bc3,0xaaf013,0xaa8441,0xaa184d,0xa9ac37,0xa94000,0xa8d3a8,
0xa8672f,0xa7fa97,0xa78dde,0xa72105,0xa6b40e,0xa646f7,0xa5d9c2,0xa56c6f,
0xa4fefe,0xa4916f,0xa423c3,0xa3b5fa,0xa34815,0xa2da14,0xa26bf6,0xa1fdbe,
0xa18f6a,0xa120fb,0xa0b272,0xa043ce,0x9fd511,0x9f663b,0x9ef74b,0x9e8843,
0x9e1922,0x9da9ea,0x9d3a99,0x9ccb32,0x9c5bb3,0x9bec1e,0x9b7c72,0x9b0cb0,
0x9a9cd9,0x9a2ced,0x99bceb,0x994cd5,0x98dcab,0x986c6d,0x97fc1c,0x978bb7,
0x971b40,0x96aab6,0x963a1a,0x95c96c,0x9558ad,0x94e7dd,0x9476fc,0x94060b,
0x93950a,0x9323f9,0x92b2d9,0x9241aa,0x91d06c,0x915f20,0x90edc6,0x907c5f,
0x900aeb,0x8f996a,0x8f27dc,0x8eb642,0x8e449d,0x8dd2ec,0x8d6130,0x8cef69,
0x8c7d98,0x8c0bbd,0x8b99d9,0x8b27eb,0x8ab5f4,0x8a43f5,0x89d1ed,0x895fde,
0x88edc7,0x887ba9,0x880985,0x879759,0x872528,0x86b2f1,0x8640b5,0x85ce74,
0x855c2e,0x84e9e4,0x847796,0x840544,0x8392ef,0x832097,0x82ae3d,0x823be1,
0x81c983,0x815723,0x80e4c3,0x807261,0x800000,0x7f8d9e,0x7f1b3c,0x7ea8dc,
0x7e367c,0x7dc41e,0x7d51c2,0x7cdf68,0x7c6d10,0x7bfabb,0x7b8869,0x7b161b,
0x7aa3d1,0x7a318b,0x79bf4a,0x794d0e,0x78dad7,0x7868a6,0x77f67a,0x778456,
0x771238,0x76a021,0x762e12,0x75bc0a,0x754a0b,0x74d814,0x746626,0x73f442,
0x738267,0x731096,0x729ecf,0x722d13,0x71bb62,0x7149bd,0x70d823,0x706695,
0x6ff514,0x6f83a0,0x6f1239,0x6ea0df,0x6e2f93,0x6dbe55,0x6d4d26,0x6cdc06,
0x6c6af5,0x6bf9f4,0x6b8903,0x6b1822,0x6aa752,0x6a3693,0x69c5e5,0x695549,
0x68e4bf,0x687448,0x6803e3,0x679392,0x672354,0x66b32a,0x664314,0x65d312,
0x656326,0x64f34f,0x64838d,0x6413e1,0x63a44c,0x6334cd,0x62c566,0x625615,
0x61e6dd,0x6177bc,0x6108b4,0x6099c4,0x602aee,0x5fbc31,0x5f4d8d,0x5edf04,
0x5e7095,0x5e0241,0x5d9409,0x5d25eb,0x5cb7ea,0x5c4a05,0x5bdc3c,0x5b6e90,
0x5b0101,0x5a9390,0x5a263d,0x59b908,0x594bf1,0x58defa,0x587221,0x580568,
0x5798d0,0x572c57,0x56bfff,0x5653c8,0x55e7b2,0x557bbe,0x550fec,0x54a43c,
0x5438af,0x53cd44,0x5361fd,0x52f6da,0x528bdb,0x5220ff,0x51b649,0x514bb7,
0x50e14b,0x507704,0x500ce4,0x4fa2e9,0x4f3916,0x4ecf69,0x4e65e3,0x4dfc85,
0x4d934f,0x4d2a42,0x4cc15d,0x4c58a0,0x4bf00d,0x4b87a4,0x4b1f65,0x4ab74f,
0x4a4f65,0x49e7a5,0x498010,0x4918a7,0x48b16a,0x484a59,0x47e374,0x477cbc,
0x471632,0x46afd4,0x4649a5,0x45e3a3,0x457dd0,0x45182c,0x44b2b7,0x444d71,
0x43e85b,0x438375,0x431ebf,0x42ba39,0x4255e5,0x41f1c2,0x418dd0,0x412a11,
0x40c683,0x406328,0x400000,0x3f9d0b,0x3f3a49,0x3ed7bb,0x3e7561,0x3e133b,
0x3db14a,0x3d4f8e,0x3cee07,0x3c8cb6,0x3c2b9b,0x3bcab6,0x3b6a07,0x3b098f,
0x3aa94e,0x3a4944,0x39e973,0x3989d9,0x392a77,0x38cb4e,0x386c5e,0x380da7,
0x37af29,0x3750e5,0x36f2db,0x36950c,0x363777,0x35da1d,0x357cfe,0x35201b,
0x34c374,0x346708,0x340ad9,0x33aee7,0x335332,0x32f7b9,0x329c7f,0x324182,
0x31e6c3,0x318c43,0x313201,0x30d7fe,0x307e3b,0x3024b6,0x2fcb72,0x2f726e,
0x2f19aa,0x2ec126,0x2e68e4,0x2e10e2,0x2db923,0x2d61a4,0x2d0a68,0x2cb36e,
0x2c5cb7,0x2c0642,0x2bb010,0x2b5a22,0x2b0477,0x2aaf10,0x2a59ed,0x2a050f,
0x29b075,0x295c21,0x290811,0x28b447,0x2860c3,0x280d84,0x27ba8c,0x2767da,
0x27156f,0x26c34b,0x26716e,0x261fd9,0x25ce8c,0x257d86,0x252cc9,0x24dc54,
0x248c29,0x243c46,0x23ecac,0x239d5c,0x234e56,0x22ff9a,0x22b128,0x226300,
0x221524,0x21c792,0x217a4b,0x212d51,0x20e0a1,0x20943e,0x204827,0x1ffc5c,
0x1fb0de,0x1f65ad,0x1f1ac9,0x1ed033,0x1e85ea,0x1e3bee,0x1df241,0x1da8e3,
0x1d5fd2,0x1d1711,0x1cce9e,0x1c867b,0x1c3ea7,0x1bf723,0x1bafef,0x1b690a,
0x1b2277,0x1adc33,0x1a9641,0x1a509f,0x1a0b4f,0x19c650,0x1981a2,0x193d47,
0x18f93d,0x18b586,0x187221,0x182f0f,0x17ec50,0x17a9e4,0x1767cc,0x172607,
0x16e495,0x16a378,0x1662ae,0x162239,0x15e219,0x15a24d,0x1562d6,0x1523b4,
0x14e4e8,0x14a671,0x146850,0x142a85,0x13ed10,0x13aff1,0x137329,0x1336b7,
0x12fa9d,0x12bed9,0x12836c,0x124857,0x120d9a,0x11d334,0x119927,0x115f71,
0x112614,0x10ed10,0x10b464,0x107c11,0x104417,0x100c76,0xfd52f,0xf9e41,
0xf67ad,0xf3173,0xefb93,0xec60d,0xe90e2,0xe5c11,0xe279b,0xdf37f,
0xdbfbf,0xd8c5b,0xd5951,0xd26a3,0xcf451,0xcc25b,0xc90c0,0xc5f82,
0xc2ea1,0xbfe1b,0xbcdf3,0xb9e27,0xb6eb8,0xb3fa6,0xb10f1,0xae29a,
0xab4a0,0xa8704,0xa59c6,0xa2ce6,0xa0063,0x9d43f,0x9a87a,0x97d13,
0x9520a,0x92760,0x8fd15,0x8d32a,0x8a99d,0x88070,0x857a2,0x82f33,
0x80725,0x7df76,0x7b827,0x79138,0x76aa9,0x7447b,0x71ead,0x6f93f,
0x6d432,0x6af86,0x68b3b,0x66751,0x643c8,0x620a0,0x5fdd9,0x5db74,
0x5b970,0x597ce,0x5768e,0x555b0,0x53533,0x51519,0x4f560,0x4d60a,
0x4b717,0x49886,0x47a57,0x45c8b,0x43f22,0x4221b,0x40578,0x3e937,
0x3cd59,0x3b1df,0x396c8,0x37c14,0x361c4,0x347d7,0x32e4e,0x31529,
0x2fc67,0x2e409,0x2cc0f,0x2b479,0x29d47,0x28679,0x27010,0x25a0a,
0x24469,0x22f2d,0x21a54,0x205e1,0x1f1d2,0x1de27,0x1cae2,0x1b801,
0x1a585,0x1936e,0x181bb,0x1706e,0x15f86,0x14f03,0x13ee5,0x12f2d,
0x11fd9,0x110eb,0x10263,0xf43f,0xe682,0xd929,0xcc36,0xbfa9,
0xb382,0xa7c0,0x9c63,0x916d,0x86dc,0x7cb1,0x72ec,0x698d,
0x6093,0x5800,0x4fd2,0x480b,0x40a9,0x39ad,0x3318,0x2ce8,
0x271f,0x21bb,0x1cbe,0x1827,0x13f6,0x102b,0xcc7,0x9c8,
0x730,0x4fe,0x332,0x1cc,0xcc,0x33,0x0,0x33,
0xcc,0x1cc,0x332,0x4fe,0x730,0x9c8,0xcc7,0x102b,
0x13f6,0x1827,0x1cbe,0x21bb,0x271f,0x2ce8,0x3318,0x39ad,
0x40a9,0x480b,0x4fd2,0x5800,0x6093,0x698d,0x72ec,0x7cb1,
0x86dc,0x916d,0x9c63,0xa7c0,0xb382,0xbfa9,0xcc36,0xd929,
0xe682,0xf43f,0x10263,0x110eb,0x11fd9,0x12f2d,0x13ee5,0x14f03,
0x15f86,0x1706e,0x181bb,0x1936e,0x1a585,0x1b801,0x1cae2,0x1de27,
0x1f1d2,0x205e1,0x21a54,0x22f2d,0x24469,0x25a0a,0x27010,0x28679,
0x29d47,0x2b479,0x2cc0f,0x2e409,0x2fc67,0x31529,0x32e4e,0x347d7,
0x361c4,0x37c14,0x396c8,0x3b1df,0x3cd59,0x3e937,0x40578,0x4221b,
0x43f22,0x45c8b,0x47a57,0x49886,0x4b717,0x4d60a,0x4f560,0x51519,
0x53533,0x555b0,0x5768e,0x597ce,0x5b970,0x5db74,0x5fdd9,0x620a0,
0x643c8,0x66751,0x68b3b,0x6af86,0x6d432,0x6f93f,0x71ead,0x7447b,
0x76aa9,0x79138,0x7b827,0x7df76,0x80725,0x82f33,0x857a2,0x88070,
0x8a99d,0x8d32a,0x8fd15,0x92760,0x9520a,0x97d13,0x9a87a,0x9d43f,
0xa0063,0xa2ce6,0xa59c6,0xa8704,0xab4a0,0xae29a,0xb10f1,0xb3fa6,
0xb6eb8,0xb9e27,0xbcdf3,0xbfe1b,0xc2ea1,0xc5f82,0xc90c0,0xcc25b,
0xcf451,0xd26a3,0xd5951,0xd8c5b,0xdbfbf,0xdf37f,0xe279b,0xe5c11,
0xe90e2,0xec60d,0xefb93,0xf3173,0xf67ad,0xf9e41,0xfd52f,0x100c76,
0x104417,0x107c11,0x10b464,0x10ed10,0x112614,0x115f71,0x119927,0x11d334,
0x120d9a,0x124857,0x12836c,0x12bed9,0x12fa9d,0x1336b7,0x137329,0x13aff1,
0x13ed10,0x142a85,0x146850,0x14a671,0x14e4e8,0x1523b4,0x1562d6,0x15a24d,
0x15e219,0x162239,0x1662ae,0x16a378,0x16e495,0x172607,0x1767cc,0x17a9e4,
0x17ec50,0x182f0f,0x187221,0x18b586,0x18f93d,0x193d47,0x1981a2,0x19c650,
0x1a0b4f,0x1a509f,0x1a9641,0x1adc33,0x1b2277,0x1b690a,0x1bafef,0x1bf723,
0x1c3ea7,0x1c867b,0x1cce9e,0x1d1711,0x1d5fd2,0x1da8e3,0x1df241,0x1e3bee,
0x1e85ea,0x1ed033,0x1f1ac9,0x1f65ad,0x1fb0de,0x1ffc5c,0x204827,0x20943e,
0x20e0a1,0x212d51,0x217a4b,0x21c792,0x221524,0x226300,0x22b128,0x22ff9a,
0x234e56,0x239d5c,0x23ecac,0x243c46,0x248c29,0x24dc54,0x252cc9,0x257d86,
0x25ce8c,0x261fd9,0x26716e,0x26c34b,0x27156f,0x2767da,0x27ba8c,0x280d84,
0x2860c3,0x28b447,0x290811,0x295c21,0x29b075,0x2a050f,0x2a59ed,0x2aaf10,
0x2b0477,0x2b5a22,0x2bb010,0x2c0642,0x2c5cb7,0x2cb36e,0x2d0a68,0x2d61a4,
0x2db923,0x2e10e2,0x2e68e4,0x2ec126,0x2f19aa,0x2f726e,0x2fcb72,0x3024b6,
0x307e3b,0x30d7fe,0x313201,0x318c43,0x31e6c3,0x324182,0x329c7f,0x32f7b9,
0x335332,0x33aee7,0x340ad9,0x346708,0x34c374,0x35201b,0x357cfe,0x35da1d,
0x363777,0x36950c,0x36f2db,0x3750e5,0x37af29,0x380da7,0x386c5e,0x38cb4e,
0x392a77,0x3989d9,0x39e973,0x3a4944,0x3aa94e,0x3b098f,0x3b6a07,0x3bcab6,
0x3c2b9b,0x3c8cb6,0x3cee07,0x3d4f8e,0x3db14a,0x3e133b,0x3e7561,0x3ed7bb,
0x3f3a49,0x3f9d0b,0x400000,0x406328,0x40c683,0x412a11,0x418dd0,0x41f1c2,
0x4255e5,0x42ba39,0x431ebf,0x438375,0x43e85b,0x444d71,0x44b2b7,0x45182c,
0x457dd0,0x45e3a3,0x4649a5,0x46afd4,0x471632,0x477cbc,0x47e374,0x484a59,
0x48b16a,0x4918a7,0x498010,0x49e7a5,0x4a4f65,0x4ab74f,0x4b1f65,0x4b87a4,
0x4bf00d,0x4c58a0,0x4cc15d,0x4d2a42,0x4d934f,0x4dfc85,0x4e65e3,0x4ecf69,
0x4f3916,0x4fa2e9,0x500ce4,0x507704,0x50e14b,0x514bb7,0x51b649,0x5220ff,
0x528bdb,0x52f6da,0x5361fd,0x53cd44,0x5438af,0x54a43c,0x550fec,0x557bbe,
0x55e7b2,0x5653c8,0x56bfff,0x572c57,0x5798d0,0x580568,0x587221,0x58defa,
0x594bf1,0x59b908,0x5a263d,0x5a9390,0x5b0101,0x5b6e90,0x5bdc3c,0x5c4a05,
0x5cb7ea,0x5d25eb,0x5d9409,0x5e0241,0x5e7095,0x5edf04,0x5f4d8d,0x5fbc31,
0x602aee,0x6099c4,0x6108b4,0x6177bc,0x61e6dd,0x625615,0x62c566,0x6334cd,
0x63a44c,0x6413e1,0x64838d,0x64f34f,0x656326,0x65d312,0x664314,0x66b32a,
0x672354,0x679392,0x6803e3,0x687448,0x68e4bf,0x695549,0x69c5e5,0x6a3693,
0x6aa752,0x6b1822,0x6b8903,0x6bf9f4,0x6c6af5,0x6cdc06,0x6d4d26,0x6dbe55,
0x6e2f93,0x6ea0df,0x6f1239,0x6f83a0,0x6ff514,0x706695,0x70d823,0x7149bd,
0x71bb62,0x722d13,0x729ecf,0x731096,0x738267,0x73f442,0x746626,0x74d814,
0x754a0b,0x75bc0a,0x762e12,0x76a021,0x771238,0x778456,0x77f67a,0x7868a6,
0x78dad7,0x794d0e,0x79bf4a,0x7a318b,0x7aa3d1,0x7b161b,0x7b8869,0x7bfabb,
0x7c6d10,0x7cdf68,0x7d51c2,0x7dc41e,0x7e367c,0x7ea8dc,0x7f1b3c,0x7f8d9e
); code;
const sin_table_const_1Khz : array[180] of longint = (
0x800000,0x847796,0x88edc7,0x8d6130,0x91d06c,0x963a1a,0x9a9cd9,0x9ef74b,
0xa34815,0xa78dde,0xabc750,0xaff31b,0xb40ff2,0xb81c8b,0xbc17a4,0xbfffff,
0xc3d464,0xc793a1,0xcb3c8b,0xcecdfe,0xd246dc,0xd5a612,0xd8ea90,0xdc1353,
0xdf1f5e,0xe20dbe,0xe4dd88,0xe78dde,0xea1de6,0xec8cd6,0xeed9eb,0xf1046c,
0xf30bae,0xf4ef0e,0xf6adf5,0xf847d8,0xf9bc37,0xfb0a9f,0xfc32a6,0xfd33f0,
0xfe0e2d,0xfec11a,0xff4c7d,0xffb02d,0xffec09,0xffffff,0xffec09,0xffb02d,
0xff4c7d,0xfec11a,0xfe0e2d,0xfd33f0,0xfc32a6,0xfb0a9f,0xf9bc37,0xf847d8,
0xf6adf5,0xf4ef0e,0xf30bae,0xf1046c,0xeed9eb,0xec8cd6,0xea1de6,0xe78dde,
0xe4dd88,0xe20dbe,0xdf1f5e,0xdc1353,0xd8ea90,0xd5a612,0xd246dc,0xcecdfe,
0xcb3c8b,0xc793a1,0xc3d464,0xbfffff,0xbc17a4,0xb81c8b,0xb40ff2,0xaff31b,
0xabc750,0xa78dde,0xa34815,0x9ef74b,0x9a9cd9,0x963a1a,0x91d06c,0x8d6130,
0x88edc7,0x847796,0x800000,0x7b8869,0x771238,0x729ecf,0x6e2f93,0x69c5e5,
0x656326,0x6108b4,0x5cb7ea,0x587221,0x5438af,0x500ce4,0x4bf00d,0x47e374,
0x43e85b,0x400000,0x3c2b9b,0x386c5e,0x34c374,0x313201,0x2db923,0x2a59ed,
0x27156f,0x23ecac,0x20e0a1,0x1df241,0x1b2277,0x187221,0x15e219,0x137329,
0x112614,0xefb93,0xcf451,0xb10f1,0x9520a,0x7b827,0x643c8,0x4f560,
0x3cd59,0x2cc0f,0x1f1d2,0x13ee5,0xb382,0x4fd2,0x13f6,0x0,
0x13f6,0x4fd2,0xb382,0x13ee5,0x1f1d2,0x2cc0f,0x3cd59,0x4f560,
0x643c8,0x7b827,0x9520a,0xb10f1,0xcf451,0xefb93,0x112614,0x137329,
0x15e219,0x187221,0x1b2277,0x1df241,0x20e0a1,0x23ecac,0x27156f,0x2a59ed,
0x2db923,0x313201,0x34c374,0x386c5e,0x3c2b9b,0x400000,0x43e85b,0x47e374,
0x4bf00d,0x500ce4,0x5438af,0x587221,0x5cb7ea,0x6108b4,0x656326,0x69c5e5,
0x6e2f93,0x729ecf,0x771238,0x7b8869
); code;
const sin_table_const_10Khz : array[18] of longint = (
0x800000,0xabc750,0xd246dc,0xeed9eb,0xfe0e2d,0xfe0e2d,0xeed9eb,0xd246dc,
0xabc750,0x800000,0x5438af,0x2db923,0x112614,0x1f1d2,0x1f1d2,0x112614,
0x2db923,0x5438af
); code;
const sin_table_const_20Khz : array[9] of longint = (
0x800000,0xd246dc,0xfe0e2d,0xeed9eb,0xabc750,0x5438af,0x112614,0x1f1d2,
0x2db923
); code;
var rData : dword;
i : dword;
sin_table : array[20000] of longint;
next_data : longint;
led_cnt : dword;
sin_len,sin_cnt : dword;
led_pos,setup_cnt : dword;
procedure I2S_Int(); iv IVT_SPI1_TX; ilevel 7; ics ICS_SRS;
begin
SPI1TXIF_bit := 0;
SPI1BUF := next_data;
if inc(sin_cnt) >= sin_len then sin_cnt := 0;
next_data := sin_table[sin_cnt];
if inc(led_cnt) >= 180000 then
begin
led_cnt := 0;
LATB.led_pos := not LATB.led_pos; // Invert PORTB
LATB.7 := not LATB.7; // Invert PORTB
end;
end;
procedure setup_100Hz;
begin
LATB := 0; // Initialize PORTB value
sin_cnt := 0;
sin_len := 1800;
led_pos := 0;
i := 0;
while i < sin_len do
begin
sin_table[i] := sin_table_const_100hz[i] - 0x800000;
inc(i);
end;
next_data := sin_table[0];
end;
procedure setup_1KHz;
begin
LATB := 0; // Initialize PORTB value
sin_cnt := 0;
sin_len := 180;
led_pos := 1;
i := 0;
while i < sin_len do
begin
sin_table[i] := sin_table_const_1Khz[i] - 0x800000;
inc(i);
end;
next_data := sin_table[0];
end;
procedure setup_10KHz;
begin
LATB := 0; // Initialize PORTB value
sin_cnt := 0;
sin_len := 18;
led_pos := 2;
i := 0;
while i < sin_len do
begin
sin_table[i] := sin_table_const_10Khz[i] - 0x800000;
inc(i);
end;
next_data := sin_table[0];
end;
procedure setup_20KHz;
begin
LATB := 0; // Initialize PORTB value
sin_cnt := 0;
sin_len := 9;
led_pos := 3;
i := 0;
while i < sin_len do
begin
sin_table[i] := sin_table_const_20Khz[i] - 0x800000;
inc(i);
end;
next_data := sin_table[0];
end;
procedure Init_I2S;
begin
SPI1CON := 0; // Stops and resets the SPI1.
SPI1CON2 := 0; // Reset audio settings
SPI1BRG := 0;
rData := SPI1BUF; // clears the receive buffer
SPI1TXIP0_bit := 1;
SPI1TXIP1_bit := 1;
SPI1TXIP2_bit := 1;
SPI1TXIF_bit := 0;
SPI1TXIE_bit := 1;
SPI1STATCLR := 0x40; // clear the Overflow
SPI1CON2 := 0x0000008A; // Right-Justified Mode, AUDEN =1, AUDMON=1
SPI1BRG := 0x00; // to generate 180kbps sample rate, REFO1CLK 23.04Mhz, SPI1BRG div 2
SPI1CON := 0x20810C2C; // Master mode, CKP = 0, FRMPOL = 1, MSTN = 1
ON_SPI1CON_bit := 1; // SPI ON
end;
procedure Init_REFCLK; // 23.04Mhz
begin
REFO1CON := 0;
REFO1CON := 0x00020001; // N = 2, PBCLK1 94.5MHz (24 MHz / 4 * 63 /2 / 2)
REFO1TRIM := 0x0D000000; // M = 26
ON_REFO1CON_bit := 1; // 23.04Mhz
end;
begin
ANSELA := 0; // Initialize AN pins as digital
ANSELB := 0; // Initialize AN pins as digital
ANSELC := 0; // Initialize AN pins as digital
ANSELD := 0; // Initialize AN pins as digital
ANSELE := 0; // Initialize AN pins as digital
ANSELF := 0; // Initialize AN pins as digital
ANSELG := 0; // Initialize AN pins as digital
ANSELH := 0; // Initialize AN pins as digital
ANSELJ := 0; // Initialize AN pins as digital
TRISB := 0; // initialize PORTB as output
LATB := 0; // Initialize PORTB value
TRISG.0 := 1; // button
setup_1KHz;
setup_cnt := 1;
Unlock_IOLOCK();
// BCLK, RD1
PPS_Mapping(_RPD2, _OUTPUT, _SDO1); // DAT, RD2
PPS_Mapping(_RPD4, _OUTPUT, _SDO1); // LRCK, RD4, SS1
Lock_IOLOCK();
Init_REFCLK; // 23.04Mhz
Init_I2S;
EnableInterrupts(); // Enable all interrupts
while true do
begin
if PORTG.0 = 1 then
begin
delay_ms(100);
while PORTG.0 = 1 do nop;
delay_ms(100);
while PORTG.0 = 1 do nop;
if inc(setup_cnt) > 3 then setup_cnt := 0;
DisableInterrupts(); // Enable all interrupts
case setup_cnt of
0 : setup_100Hz;
1 : setup_1KHz;
2 : setup_10KHz
else setup_20KHz;
end;
Init_I2S;
EnableInterrupts(); // Enable all interrupts
end;
end;
end.