Fig.2 MCP 4922 IC chip
Fig.3 Circuit
Code:
#include “mbed.h”
SPI spi(dp2, dp1, dp6); // mosi, miso, sclk
DigitalOut cs(dp14);
void sin_wave(float hz){
int T_ =(int)((1.0/hz)*1000.0);
for(int i=0;i<T_;i++){
int k =4096/2+(int)(4096.0/2.0*sin((float)(i*4095.0/T_)/4095.0*2.0*3.1416));
cs=0;
spi.write((k>> 8)|0x30);
spi.write(k&0xff);
cs=1;
wait_us(500);
}
}
void saw_wave(float hz){
int T_ =(int)((1.0/hz)*1000.0);
for(int i=0;i<T_;i++){
int k =(int)((float)i*4095.0/T_);
cs=0;
spi.write((k>> 8)|0x30);
spi.write(k&0xff);
cs=1;
wait_us(500);
}
}
int main() {
int count_=0;
spi.format(8,0);
spi.frequency(10000000);
while(1) {
count_++;
if(count_<=5){
sin_wave(1.0);
}
else if(count_>5&&count_<=10){
saw_wave(1.0);
}
else if (count_>10){
count_=0;
}
}
}