This is an example code which can output 300Hz sine waveform using internal DAC on a microcontroller. DAC on a microcontroller has the 10bit resolution but I used only 32step when i make sine waveform


A device that I used is PIC16F1778 and developed on MPLAB X and XC8 v2.0 Compiler


unsigned int cnt=0; unsigned int sin[] = { 512, 611, 707, 796, 873, 937, 984, 1013, 1023, 1013, 984, 937, 873, 796, 707, 611, 512, 412, 316, 227, 150, 86, 39, 10, 0, 10, 39, 86, 150, 227, 316, 412 }; void main(void) { // initialize the device SYSTEM_Initialize(); DAC1CON0bits.DAC1FM = 0; // When using interrupts, you need to set the Global

// and Peripheral Interrupt Enable bits // Use the following macros to: // Enable the Global Interrupts //INTERRUPT_GlobalInterruptEnable(); // Enable the Peripheral Interrupts //INTERRUPT_PeripheralInterruptEnable(); // Disable the Global Interrupts //INTERRUPT_GlobalInterruptDisable(); // Disable the Peripheral Interrupts //INTERRUPT_PeripheralInterruptDisable(); while (1) { // Add your application code // DAC1_Load10bitInputData(sin[cnt++]); //Loading 10bit data to DAC1 DAC1REFL = (uint8_t) sin[cnt]; DAC1REFH = (uint8_t)(sin[cnt++] >> 8); //Loading DAC1 double buffer data to latch. DACLDbits.DAC1LD = 1; __delay_us(100); if(cnt == 32) cnt = 0; } }


The waveform using DAC without Voltage Follower on MCU




The waveform using DAC with Voltage Follower on MCU



The sine waveform is made by polling mode in while loop. If you remove the delay function in while loop, you can see the 8.6kHz sine waveform. It might be maximum output frequency using this microcontroller.



Sinewave_Table.xlsx


PIC16F_DAC_TO_SINE.X.zip



2019/01/02 - [Embedded] - How can i select the optimization level of compiler on MPLAB X?

2019/01/02 - [Embedded] - How much optimize depend on optimization level of compiler?

2018/08/02 - [Embedded/MCU Basic] - XC8 Compiler optimization level


Posted by KennyShin
,