利用芯片MAX518实现DA转换,采取的是I2C通讯. 实际输出的模拟电压信号可以用示波器观察,类似正弦波图象. #include"pic.h" #include"pic1687x.h" unsigned char i; unsigned char j; unsigned char n;
const char table[ ] = {0X80,0X86,0X8D, 0X93,0X99,0X9F,0XA5,0XAB,0XB1,0XB7,0XBC,0XC2,0XC7,0XCC,0XD1,0XD6,0XDA,0XDF,0XE3,0XE7,0XEA,0XEE,0XF1,0XF4,0XF6,0XF8,0XFA,0XFC,0XFD,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFE,0XFD,0XFB,0XF9,0XF7,0XF5,0XF2,0XEF,0XEC,0XE9,0XE5,0XE1,0XDD,0XD8,0XD4,0XCF,0XCA,0XC5,0XBF,0XBA,0XB4,0XAE,0XA8,0XA2,0X9C,0X96,0X90,0X89,0X83,0X80,0X79,0X72,0X6C,0X66,0X60,0X5A,0X55,0X4E,0X48,0X43,0X3D,0X38,0X33,0X2E,0X29,0X25,0X20,0X1C,0X18,0X15,0X11,0X0E,0X0B,0X09,0X07,0X05,0X03,0X02,0X00,0X00,0X00,0X00,0X00,0X00,0X01,0X02,0X04,0X06,0X08,0X0A,0X0D,0X10,0X13,0X16,0X1A,0X1E,0X22,0X27,0X2B,0X30,0X35,0X3A,0X40,0X45,0X4C,0X51,0X57,0X5D,0X63,0X69,0X6F,0X76,0X7C};
//const 表示将table 数组的数据存放在ROM中,不变化的数据和程序放在ROM
void I2Cinitial() { SSPCON=0X08; //00001000主控I2C工作模式4/(F(SSPADD+1)) SSPCON2=0X00; SSPSTAT=0X80; SSPADD=0X02;
TRISC3=1; TRISC4=1; TRISA4=0; RA4=0; //关闭74HC165芯片占线功能
GIE=0; SSPEN=1; SSPIF=0; }
void ACK() { do { ; }while(SSPIF==0) //等待总线发送数据字节完成. SSPIF=0; }
void I2Cout() { SEN=1; do { RSEN=1; }while(SSPIF==0) //等待总线发送数据字节完成. SSPIF=0; SSPBUF=0X58; ACK(); SSPBUF=0X01; ACK(); SSPBUF=j; ACK(); PEN=1; ACK(); }
void main() { I2Cinitial(); while(1) { for(i=0;i<=127;i++) j=table; I2Cout(); } }
|