www.pudn.com > TG12864.rar > tlv5638a.c
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// #include#include #include ////////////////////////////////////////////////////////////////////////////////// #define uchar unsigned char #define uint unsigned int #define ulong unsigned long ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// //define dac begain sbit SS = P0^2; //define funtion table void delay(uchar v); void dacspi (uchar i); void writedac(uint dat,bit dot); /********************延时子程序***********************************/ void delay(uchar v) { while(v!=0) v--; } /*********************通过spi口写8位数据到TLV538***************************************/ void dacspi (uchar i) { //SPSTAT=0xc0; SPDAT=i; while(1) { if(SPSTAT&0x80) break; } SPSTAT=0xc0; } /*****************************写16位数据到TLV5638并输出电压******************************************/ void writedac(uint dat,bit dot) { uint temp; uchar temp1,temp2; SPCTL=0xd8; SPSTAT=0xc0; temp=dat; if (dot==1) { temp=temp|0xd000; } else { temp=temp|0xc000; } temp1=temp&0x00ff; temp=temp>>8; temp2=temp&0x00ff; SS=0; dacspi(temp2); dacspi(temp1); SS=1; } /******************************DAC主程序************************************/ void main() { // uint loop; P0M1=0x00;P1M2=0x00; P2M1=0xc0;P2M2=0xc0; SS=1; writedac(0x0002,1); while(1) { writedac(0x0fff,0); delay(100); writedac(0x0000,0); delay(100); // for(loop=0;loop<0x0f00;loop++) // { // writedac(loop,0); // //delay(100); // } } }