使用AD9851模块自制函数发生器

    xiaoxiao2021-03-25  180

    AD9851是一款输出最高达到70mhz的模块,能输出方波,正弦波两种波形,模块自带滤波器输出波形较为干净 如果要输出三角波建议选用AD9850 【使用Arduino来控制9851输出波形】 功能描述: 全串口指令操作,蓝牙串口可直接控制,附带一个App用以进行远端操作(未完成) 用一个I2C芯片转接的1602液晶显示当前频率 【使用串口传入数据】 线路连接定义: 电位器10K,跳线跳到模块上用于控制方波占空比   WCLK 接 D10   FQUP 接 D9   REST 接 D8   DATA 接 D7 D4,D5,D6连接下方三个按键 A4,A5接1602液晶I2C模块的 SDA和SCL 串口控制指令: w数字 指定频率输出 例如:w1000  就是以1000hz输出 r  重置模块 源代码: #include #include //包含头文件 //#include LiquidCrystal_I2Clcd(0x27,16,2); //创建一个对象:lcd const int AdjustGreen = 11; const int AdjustRed = 12; const int WCLK = 10; const int FQUP = 9; const int REST = 8; const int DATA = 7; const int Button1 = 6; const int Button2 = 5; const int Button3 = 4; int DisWave = 1000; int FrequenceSaveSlot[9];//可以保存8个频率数据 void setup() { // put your setup code here, to run once: pinMode(AdjustGreen, OUTPUT); pinMode(AdjustRed, OUTPUT); pinMode(WCLK, OUTPUT); pinMode(FQUP, OUTPUT); pinMode(REST, OUTPUT); pinMode(DATA, OUTPUT); Serial.begin(9600); digitalWrite(AdjustGreen ,HIGH); lcd.init(); //lcd初始化函数 lcd.backlight(); //lcd开启背光灯函数 } void lcdPrintWave() { } void ad9851_reset_serial() { digitalWrite(WCLK, LOW); digitalWrite(FQUP, LOW); //restSignal digitalWrite(REST, LOW); digitalWrite(REST, HIGH); digitalWrite(REST, LOW); //wclkSignal digitalWrite(WCLK, LOW); digitalWrite(WCLK, HIGH); digitalWrite(WCLK, LOW); //fqupSignal digitalWrite(FQUP, LOW); digitalWrite(FQUP, HIGH); digitalWrite(FQUP, LOW); } //写数据 void DataJudge(unsigned char w) { if (w == 1) { digitalWrite(DATA, HIGH); } else if (w == 0) { digitalWrite(DATA, LOW); } } void ad9851_wr_serial(unsigned char w0, double frequence) { unsigned char i, w; long int y; double x; //计算频率的HEX值 x = 4294967295 / 180;//适合180M晶振/180为最终时钟频率(或30M六倍频) //如果时钟频率不为180MHZ,修改该处的频率值,单位MHz !!! frequence = frequence / 1000000; frequence = frequence * x; y = frequence; //写w4数据 w = (y >>= 0); for (i = 0; i < 8; i++) { DataJudge((w>> i) & 0x01); digitalWrite(WCLK, HIGH); digitalWrite(WCLK, LOW); } //写w3数据 w = (y >> 8); for (i = 0; i < 8; i++) { DataJudge((w>> i) & 0x01); digitalWrite(WCLK, HIGH); digitalWrite(WCLK, LOW); } //写w2数据 w = (y >> 16); for (i = 0; i < 8; i++) { DataJudge((w>> i) & 0x01); digitalWrite(WCLK, HIGH); digitalWrite(WCLK, LOW); } //写w1数据 w = (y >> 24); for (i = 0; i < 8; i++) { DataJudge((w>> i) & 0x01); digitalWrite(WCLK, HIGH); digitalWrite(WCLK, LOW); } //写w0数据 w = w0; for (i = 0; i < 8; i++) { DataJudge((w>> i) & 0x01); digitalWrite(WCLK, HIGH); digitalWrite(WCLK, LOW); } //移入始能 digitalWrite(FQUP, HIGH); digitalWrite(FQUP, LOW); } void Spiertest() { } void firstLoad() { ad9851_reset_serial(); ad9851_wr_serial(0x01, 50000); } void loop() { // put your main code here, to runrepeatedly: String Command = {""}; int inputdate = 0; int mark = 0; firstLoad(); while (1) { while(Serial.available() > 0) { Command += char(Serial.read()); delay(2); mark =1; } if(mark ==1) { //Serial.println(Command);//----------调试语句回传 if(Command.length() > 0) { for (int i = 1; i < Command.length() ; i++) { if( i ==1) inputdate += (Command[i] - '0'); else { inputdate = inputdate*10 + (Command[i]-'0'); } DisWave = inputdate;//存储用于显示的波频率 } switch (Command[0]) { case 'w': ad9851_wr_serial(0x01, inputdate); Serial.print("CurrentFrequency:"); Serial.print(inputdate); Serial.println("Hz"); lcd.clear(); lcd.print("Current:"); lcd.print(DisWave); lcd.print("Hz"); break; case 'r': ad9851_reset_serial(); break; default: break; } Command = String("");//Clear Command inputdate = 0; } } mark =0; } }
    转载请注明原文地址: https://ju.6miu.com/read-704.html

    最新回复(0)