最近到一个新公司,公司里尽然没有示波器,要人买,又要等好几天.对着数据手册写了个驱动程序,可是不对.用过的高好人帮我看一下好吗? /*--------------------------------------------
--------------------------------------------*/ void delay(INT8U n) {
while(--n){ #asm("nop"); #asm("nop"); #asm("nop"); #asm("nop"); #asm("nop"); #asm("nop");} }
/*--------------------------------------------
CS5532初始化
---------------------------------------------*/ void CS5532_INIT(void) { INT8U i,k;//temp;
PORTB.3 = 0; ADCS = 0; ADSCLK = 0; ADSDI = 0; k=0; delay_ms(20); for(i=15;i>0;i--)sendCS5532_buff(0xff); sendCS5532_buff(0xfe); //使串口复位成命令模式 delay_ms(20); sendCS5532_buff(0x03); //写配置寄存器 sendCS5532_buff(0x20); sendCS5532_buff(0x40); //注意A0,A1以后要开路 sendCS5532_buff(0x00); sendCS5532_buff(0x00); //写入32位数据,复位芯片 /* do{ sendCS5532_buff(0x0b); //读配置寄存器命令 temp = readCS5532(); i = readCS5532(); i = readCS5532(); i = readCS5532(); //读配置寄存器 temp &= 0x10; // k++; // if(k>200) break; //可以做个AD故障检测 }while(temp != 0x10); */ delay_ms(20); sendCS5532_buff(0x05); sendCS5532_buff(0b00110100); //通道1 增益64 字速率1920hz sendCS5532_buff(0b10000000); //单极性 sendCS5532_buff(0x00); sendCS5532_buff(0x00); /*sendCS5532_buff(0x41); sendCS5532_buff(0xfe); sendCS5532_buff(0); sendCS5532_buff(0); sendCS5532_buff(0); sendCS5532_buff(0xfe); sendCS5532_buff(0); sendCS5532_buff(0); sendCS5532_buff(0); sendCS5532_buff(0xfe); sendCS5532_buff(0); sendCS5532_buff(0); sendCS5532_buff(0); sendCS5532_buff(0xfe); sendCS5532_buff(0); sendCS5532_buff(0); sendCS5532_buff(0);
sendCS5532_buff(0x05); //写向所有通道设置寄存器 sendCS5532_buff(0b00110100); //通道1 增益64 字速率1920hz sendCS5532_buff(0b10000000); //双极性 sendCS5532_buff(0b00110100); //通道1 增益64 字速率1920hz sendCS5532_buff(0b10000000); //双极性
delay(10); delay(10); /* 测试读写CS5532程序是否正常 sendCS5532_buff(0x0d); //读取通道设置寄存器 i = readCS5532(); i = readCS5532(); while(i == 0b11000000)BG = 1; */ // sendCS5532_buff(0xc0); //执行连续转换
// ADCS = 1; //关闭AD } /*---------------------------------------------
发送一个字节 高位在前 ----------------------------------------------*/ void sendCS5532_buff(INT8U temp) { INT8U i;
// ADCS = 0; ADSCLK = 0; ADSDI = 0; for(i=8;i>0;i--) { if(temp&0x80) ADSDI = 1; else ADSDI = 0; temp <<=1; delay(2); ADSCLK=1; delay(5); ADSCLK=0; delay(2); } ADSDI = 0; // delay(2); // ADCS=1; }
/*-------------------------------------------
读取CS5532寄存器
-------------------------------------------*/ static INT8U readCS5532(void) { INT8U i,temp;
ADSCLK=0; delay(1); for(i=8;i>0;i--){ temp<<=1; if(ADSDO) temp |= 1; else temp &= 0xfe; delay(2); ADSCLK=1; delay(5); ADSCLK=0; delay(2); } return(temp); } /*---------------------------------------------
执行完稳定的单次转换 cs5532采样
----------------------------------------------*/ INT32U cs5532_sample(void) { INT8U i; INT32U addata_buff;
sendCS5532_buff(0x80); // sendCS5532_buff(0x0c); while(ADSDO); ADSDI = 0; delay(2); for(i=8;i>0;i--){ ADSCLK = 1; delay(5); ADSCLK = 0; delay(5); } addata_buff = readCS5532(); addata_buff <<=8; addata_buff |= readCS5532(); addata_buff <<=8; addata_buff |= readCS5532(); for(i=8;i>0;i--) { ADSCLK = 1; delay(5); ADSCLK = 0; delay(5); } return( addata_buff ); } |