| if (val < 0) val = 0; // TODO: saturate instruction? | if (val < 0) val = 0; // TODO: saturate instruction? | ||||
| else if (val > 4095) val = 4095; | else if (val > 4095) val = 4095; | ||||
| *(int16_t *)&(DAC0_DAT0L) = val; | *(int16_t *)&(DAC0_DAT0L) = val; | ||||
| #elif defined(__MKL26Z64__) | |||||
| SIM_SCGC6 |= SIM_SCGC6_DAC0; | |||||
| if (analog_reference_internal == 0) { | |||||
| // use 3.3V VDDA power as the reference (this is the default) | |||||
| DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACRFS | DAC_C0_DACSWTRG; // 3.3V VDDA | |||||
| } else { | |||||
| // use whatever voltage is on the AREF pin | |||||
| DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACSWTRG; // 3.3V VDDA | |||||
| } | |||||
| if (val < 0) val = 0; | |||||
| else if (val > 4095) val = 4095; | |||||
| *(int16_t *)&(DAC0_DAT0L) = val; | |||||
| #endif | #endif | ||||
| } | } | ||||
| #define SIM_SCGC5_TSI ((uint32_t)0x00000020) // Touch Sense Input TSI Clock Gate Control | #define SIM_SCGC5_TSI ((uint32_t)0x00000020) // Touch Sense Input TSI Clock Gate Control | ||||
| #define SIM_SCGC5_LPTIMER ((uint32_t)0x00000001) // Low Power Timer Access Control | #define SIM_SCGC5_LPTIMER ((uint32_t)0x00000001) // Low Power Timer Access Control | ||||
| #define SIM_SCGC6 (*(volatile uint32_t *)0x4004803C) // System Clock Gating Control Register 6 | #define SIM_SCGC6 (*(volatile uint32_t *)0x4004803C) // System Clock Gating Control Register 6 | ||||
| #define SIM_SCGC6_DAC0 ((uint32_t)0x80000000) // DAC on Kinetis-L | |||||
| #define SIM_SCGC6_RTC ((uint32_t)0x20000000) // RTC Access | #define SIM_SCGC6_RTC ((uint32_t)0x20000000) // RTC Access | ||||
| #define SIM_SCGC6_ADC0 ((uint32_t)0x08000000) // ADC0 Clock Gate Control | #define SIM_SCGC6_ADC0 ((uint32_t)0x08000000) // ADC0 Clock Gate Control | ||||
| #define SIM_SCGC6_FTM1 ((uint32_t)0x02000000) // FTM1 Clock Gate Control | #define SIM_SCGC6_FTM1 ((uint32_t)0x02000000) // FTM1 Clock Gate Control |
| analogWriteDAC0(val); | analogWriteDAC0(val); | ||||
| return; | return; | ||||
| } | } | ||||
| #elif defined(__MKL26Z64__) | |||||
| if (pin == A12) { | |||||
| uint8_t res = analog_write_res; | |||||
| if (res < 12) { | |||||
| val <<= 12 - res; | |||||
| } else if (res > 12) { | |||||
| val >>= res - 12; | |||||
| } | |||||
| analogWriteDAC0(val); | |||||
| return; | |||||
| } | |||||
| #endif | #endif | ||||
| max = 1 << analog_write_res; | max = 1 << analog_write_res; |