void IRAM_ATTR __dacWrite(uint8_t pin, uint8_t value)
{
if(pin 26){
return;//not dac pin
}
pinMode(pin, ANALOG);
uint8_t channel = pin - 25;
//Disable Tone
CLEAR_PERI_REG_MASK(SENS_SAR_DAC_CTRL1_REG, SENS_SW_TONE_EN);
if (channel) {
//Disable Channel Tone
CLEAR_PERI_REG_MASK(SENS_SAR_DAC_CTRL2_REG, SENS_DAC_CW_EN2_M);
//Set the Dac value
SET_PERI_REG_BITS(RTC_IO_PAD_DAC2_REG, RTC_IO_PDAC2_DAC, value, RTC_IO_PDAC2_DAC_S); //dac_output
//Channel output enable
SET_PERI_REG_MASK(RTC_IO_PAD_DAC2_REG, RTC_IO_PDAC2_XPD_DAC | RTC_IO_PDAC2_DAC_XPD_FORCE);
} else {
//Disable Channel Tone
CLEAR_PERI_REG_MASK(SENS_SAR_DAC_CTRL2_REG, SENS_DAC_CW_EN1_M);
//Set the Dac value
SET_PERI_REG_BITS(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_DAC, value, RTC_IO_PDAC1_DAC_S); //dac_output
//Channel output enable
SET_PERI_REG_MASK(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_XPD_DAC | RTC_IO_PDAC1_DAC_XPD_FORCE);
}
}
extern void dacWrite(uint8_t pin, uint8_t value) __attribute__ ((weak, alias("__dacWrite")));
接下来看下我写的例子吧!
/*
@功能:输出两路DAC,可用来输出音频
@时间:2020/3/5
@作者:刘泽文
@QQ:2822604962
*/
#include
#include //DAC功能引用该库,可以不进行#include引用
//查看源码后得到,DAC引脚只能是这两个引脚
#define LED1 25
#define LED2 26
void setup(){
//长时候发现,DAC的IO口也可以不进行初始化
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);
}
void loop()
{
// 逐渐变亮
for (int dutyCycle = 0; dutyCycle = 0; dutyCycle = dutyCycle - 1)
{
dacWrite(LED1, dutyCycle); // 输出DAC
dacWrite(LED2, 255 - dutyCycle); // 输出DAC
delay(5);
}
}
实验结果:
IO25和IO26端口上的LED为呼吸灯闪烁,并且亮度互补。
作者:慕容流年