PWM Signal Generation with STM32F7 through external DAC - Page 1
Forum section: electronics / general

author: A.Ersoz
author_rank: Regular Contributor
timestamp: July 03, 2019, 09:32:27 pm
content: Hello guys,I have designed a custom PCB with STM32F767. My first target is that generating analog square pulses with controlling its amplitude, frequency, and pulse width. For doing this purpose, I use the STM32 and 8-bit DAC (DAC082S085).With my code, I can generate step signal with my desired amplitude from two outputs of the DAC.My question is that how can I generate square wave signal with amplitude, pulse width, and frequency control?My code isCode:uint8_t testdata_out = 51; // 1V Amplitude as an outputuint8_t DAC_A_Write = 0x1; // DAC's A output is selected 0 0 0 1uint8_t DAC_B_Write = 0x5; // DAC's B output is selected 0 1 0 1uint16_t DACA_Buf;uint16_t DACB_Buf;HAL_GPIO_WritePin(DAC_SYNC_GPIO_Port, DAC_SYNC_Pin, GPIO_PIN_RESET);DACA_Buf = DAC_A_Write<<12 | testdata_out<<4;HAL_SPI_Transmit(&hspi2,(uint8_t*)&DACA_Buf,16,100);HAL_Delay(1);HAL_GPIO_WritePin(DAC_SYNC_GPIO_Port, DAC_SYNC_Pin, GPIO_PIN_SET);HAL_GPIO_WritePin(DAC_SYNC_GPIO_Port, DAC_SYNC_Pin, GPIO_PIN_RESET);DACB_Buf = DAC_B_Write<<12 | testdata_out<<4;HAL_SPI_Transmit(&hspi2,(uint8_t*)&DACB_Buf,16,100);HAL_Delay(1);HAL_GPIO_WritePin(DAC_SYNC_GPIO_Port, DAC_SYNC_Pin, GPIO_PIN_SET);Thank you

author: A.Ersoz
author_rank: Regular Contributor
timestamp: July 04, 2019, 07:23:50 pm
content: I have tried something such as generating sine wave.Code:uint16_t i;uint16_t testdata_out={2048, 2098, 2148, 2198, 2248, 2298, 2348, 2398,2447, 2496, 2545, 2594, 2642, 2690, 2737, 2784,2831, 2877, 2923, 2968, 3013, 3057, 3100, 3143,3185, 3226, 3267, 3307, 3346, 3385, 3423, 3459,3495, 3530, 3565, 3598, 3630, 3662, 3692, 3722,3750, 3777, 3804, 3829, 3853, 3876, 3898, 3919,3939, 3958, 3975, 3992, 4007, 4021, 4034, 4045,4056, 4065, 4073, 4080, 4085, 4089, 4093, 4094,4095, 4094, 4093, 4089, 4085, 4080, 4073, 4065,4056, 4045, 4034, 4021, 4007, 3992, 3975, 3958,3939, 3919, 3898, 3876, 3853, 3829, 3804, 3777,3750, 3722, 3692, 3662, 3630, 3598, 3565, 3530,3495, 3459, 3423, 3385, 3346, 3307, 3267, 3226,3185, 3143, 3100, 3057, 3013, 2968, 2923, 2877,2831, 2784, 2737, 2690, 2642, 2594, 2545, 2496,2447, 2398, 2348, 2298, 2248, 2198, 2148, 2098,2048, 1997, 1947, 1897, 1847, 1797, 1747, 1697,1648, 1599, 1550, 1501, 1453, 1405, 1358, 1311,1264, 1218, 1172, 1127, 1082, 1038, 995, 952,910, 869, 828, 788, 749, 710, 672, 636,600, 565, 530, 497, 465, 433, 403, 373,345, 318, 291, 266, 242, 219, 197, 176,156, 137, 120, 103, 88, 74, 61, 50,39, 30, 22, 15, 10, 6, 2, 1,0, 1, 2, 6, 10, 15, 22, 30,39, 50, 61, 74, 88, 103, 120, 137,156, 176, 197, 219, 242, 266, 291, 318,345, 373, 403, 433, 465, 497, 530, 565,600, 636, 672, 710, 749, 788, 828, 869,910, 952, 995, 1038, 1082, 1127, 1172, 1218,1264, 1311, 1358, 1405, 1453, 1501, 1550, 1599,1648, 1697, 1747, 1797, 1847, 1897, 1947, 1997};uint8_t DAC_A_Write = 0x1; // DAC's A output is selected 0 0 0 1uint8_t DAC_B_Write = 0x5; // DAC's B output is selected 0 1 0 1uint16_t DACA_Buf;uint16_t DACB_Buf;for (i=0; i<256; i++){HAL_GPIO_WritePin(DAC_SYNC_GPIO_Port, DAC_SYNC_Pin, GPIO_PIN_RESET);//DACA_Buf = DAC_A_Write<<12 | testdata_out<<4;DACA_Buf = DAC_A_Write<<12 | testdata_out<<4;HAL_SPI_Transmit(&hspi2,(uint8_t*)&DACA_Buf,16,100);HAL_Delay(1);HAL_GPIO_WritePin(DAC_SYNC_GPIO_Port, DAC_SYNC_Pin, GPIO_PIN_SET);}for (i=0; i<256; i++){HAL_GPIO_WritePin(DAC_SYNC_GPIO_Port, DAC_SYNC_Pin, GPIO_PIN_RESET);DACB_Buf = DAC_B_Write<<12 | testdata_out<<4;HAL_SPI_Transmit(&hspi2,(uint8_t*)&DACB_Buf,16,100);HAL_Delay(1);HAL_GPIO_WritePin(DAC_SYNC_GPIO_Port, DAC_SYNC_Pin, GPIO_PIN_SET);}But I didn't still get result

author: thm_w
author_rank: Super Contributor
timestamp: July 04, 2019, 09:08:03 pm
content: So the code you posted in the first post is working and you are getting a square wave out on an oscilloscope?The DAC is 8-bit so you should be sending values from 0 to 255. But testdata_out has values up to 4095, which would not work.Try sending 0 to 255 in a loop, then see if you get a linear sawtooth wave on the oscilloscope maybe.I assume this is formatting the data correctly for said DAC?:DACB_Buf = DAC_B_Write<<12 | testdata_out<<4;

author: A.Ersoz
author_rank: Regular Contributor
timestamp: July 04, 2019, 09:14:04 pm
content: In first post code, I get step function signal which means 1V step signal.QuoteThank you I will try this.QuoteExactly, I set the DAC output B and data value.

author: A.Ersoz
author_rank: Regular Contributor
timestamp: July 04, 2019, 09:34:33 pm
content: Yes, it works now, but there is a problem. When the positive phase of the signal reaches to peak, it goes to zero. The other thing is that sine wave is not continuous. After at the end of the one full sine wave, there is a long delay. What do you think about these two problems? I also attached the output signal with a photo.Updated code isCode:uint16_t testdata_out={128,131,134,137,141,144,147,150,153,156,159,162,165,168,171,174,177,180,183,186,188,191,194,196,199,202,204,207,209,212,214,216,219,221,223,225,227,229,231,233,234,236,238,239,241,242,244,245,246,247,249,250,250,251,252,253,254,254,255,255,255,256,256,256,256,256,256,256,255,255,255,254,254,253,252,251,250,250,249,247,246,245,244,242,241,239,238,236,234,233,231,229,227,225,223,221,219,216,214,212,209,207,204,202,199,196,194,191,188,186,183,180,177,174,171,168,165,162,159,156,153,150,147,144,141,137,134,131,128,125,122,119,115,112,109,106,103,100,97,94,91,88,85,82,79,76,73,70,68,65,62,60,57,54,52,49,47,44,42,40,37,35,33,31,29,27,25,23,22,20,18,17,15,14,12,11,10,9,7,6,6,5,4,3,2,2,1,1,1,0,0,0,0,0,0,0,1,1,1,2,2,3,4,5,6,6,7,9,10,11,12,14,15,17,18,20,22,23,25,27,29,31,33,35,37,40,42,44,47,49,52,54,57,60,62,65,68,70,73,76,79,82,85,88,91,94,97,100,103,106,109,112,115,119,122,125};/* USER CODE BEGIN 3 */for (i=0; i<256; i++){HAL_GPIO_WritePin(DAC_SYNC_GPIO_Port, DAC_SYNC_Pin, GPIO_PIN_RESET);//DACA_Buf = DAC_A_Write<<12 | testdata_out<<4;DACA_Buf = DAC_A_Write<<12 | testdata_out<<4;HAL_SPI_Transmit(&hspi2,(uint8_t*)&DACA_Buf,16,100);HAL_Delay(1);HAL_GPIO_WritePin(DAC_SYNC_GPIO_Port, DAC_SYNC_Pin, GPIO_PIN_SET);}for (i=0; i<256; i++){HAL_GPIO_WritePin(DAC_SYNC_GPIO_Port, DAC_SYNC_Pin, GPIO_PIN_RESET);DACB_Buf = DAC_B_Write<<12 | testdata_out<<4;//DACB_Buf = DAC_B_Write<<12 | testdata_out<<4;HAL_SPI_Transmit(&hspi2,(uint8_t*)&DACB_Buf,16,100);HAL_Delay(1);HAL_GPIO_WritePin(DAC_SYNC_GPIO_Port, DAC_SYNC_Pin, GPIO_PIN_SET);}

author: A.Ersoz
author_rank: Regular Contributor
timestamp: July 04, 2019, 09:47:25 pm
content: I solved the problem about going to zero when the signal reaches to peak with using different look up table. But the delay is still there.Code:uint16_t testdata_out={128,131,134,137,140,143,146,149,152,156,159,162,165,168,171,174,176,179,182,185,188,191,193,196,199,201,204,206,209,211,213,216,218,220,222,224,226,228,230,232,234,236,237,239,240,242,243,245,246,247,248,249,250,251,252,252,253,254,254,255,255,255,255,255,255,255,255,255,255,255,254,254,253,252,252,251,250,249,248,247,246,245,243,242,240,239,237,236,234,232,230,228,226,224,222,220,218,216,213,211,209,206,204,201,199,196,193,191,188,185,182,179,176,174,171,168,165,162,159,156,152,149,146,143,140,137,134,131,128,124,121,118,115,112,109,106,103,99, 96, 93, 90, 87, 84, 81,79, 76, 73, 70, 67, 64, 62, 59, 56, 54, 51, 49, 46, 44, 42, 39,37, 35, 33, 31, 29, 27, 25, 23, 21, 19, 18, 16, 15, 13, 12, 10,9, 8, 7, 6, 5, 4, 3, 3, 2, 1, 1, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 3, 4, 5, 6, 7, 8,9, 10, 12, 13, 15, 16, 18, 19, 21, 23, 25, 27, 29, 31, 33, 35,37, 39, 42, 44, 46, 49, 51, 54, 56, 59, 62, 64, 67, 70, 73, 76,79, 81, 84, 87, 90, 93, 96, 99, 103,106,109,112,115,118,121,124};

author: A.Ersoz
author_rank: Regular Contributor
timestamp: July 04, 2019, 10:33:32 pm
content: I also solved the delay problem between sequential sine waves with deleting for loop in the middle of main code.Thanks