功能實(shí)現(xiàn)通過(guò)ADC0832芯片對(duì)音頻信號(hào)的采集,轉(zhuǎn)換為數(shù)字信號(hào)給單片機(jī),然后PWM脈沖寬度調(diào)制控制水泵水柱對(duì)的高低
//請(qǐng)先了解ADC0832模數(shù)轉(zhuǎn)換的串行協(xié)議,再來(lái)讀本函數(shù),主要是對(duì)應(yīng)時(shí)序圖來(lái)理解,本函數(shù)是模擬0832的串行協(xié)議進(jìn)行的
// 1 1 0 通道
// 1 1 1 通道
unsigned char ad0832read(bit SGL,bit ODD)
{
unsigned char i=0,value=0,value1=0;
SCL=0;
DO=1;
CS=0;//開(kāi)始
SCL=1;//第一個(gè)上升沿
SCL=0;
DO=SGL;
SCL=1; //第二個(gè)上升沿
SCL=0;
DO=ODD;
SCL=1; //第三個(gè)上升沿
SCL=0; //第三個(gè)下降沿
DO=1;
for(i=0;i<8;i++)
{
SCL=1;
SCL=0; //開(kāi)始從第四個(gè)下降沿接收數(shù)據(jù)
value<<=1;
if(DO)
value++;
}
for(i=0;i<8;i++)
{//接收校驗(yàn)數(shù)據(jù)
value1>>=1;
if(DO)
value1+=0x80;
SCL=1;
SCL=0;
}
CS=1;
DO=1;
SCL=1;
if(value==value1)//與校驗(yàn)數(shù)據(jù)比較,正確就返回?cái)?shù)據(jù),否則返回0
return value;
return 0;
}
void penquan() //PWM調(diào)壓
{
date=ad0832read(1,0); //讀取ad值
h1=(255-date);//轉(zhuǎn)換
out=0; //水泵清零
delay(h1);
if(h1>70) led1=1; else led1=0; //ad值對(duì)比 點(diǎn)亮相應(yīng)的燈
if(h1>80) led2=1; else led2=0; //ad值對(duì)比 點(diǎn)亮相應(yīng)的燈
if(h1>90) led3=1; else led3=0; //ad值對(duì)比 點(diǎn)亮相應(yīng)的燈
if(h1>100) led4=1; else led4=0; //ad值對(duì)比 點(diǎn)亮相應(yīng)的燈
if(h1>110) led5=1; else led5=0; //ad值對(duì)比 點(diǎn)亮相應(yīng)的燈
if(h1>115) led6=1; else led6=0; //ad值對(duì)比 點(diǎn)亮相應(yīng)的燈
if(h1>120) led7=1; else led7=0; //ad值對(duì)比 點(diǎn)亮相應(yīng)的燈
if(h1>130) led8=1; else led8=0; //ad值對(duì)比 點(diǎn)亮相應(yīng)的燈
out=1; //關(guān)閉水泵
delay(date); //延時(shí)時(shí)間
}
void main()//主函數(shù)
{
while(1)
{
penquan(); //給定個(gè)延時(shí)次數(shù)
}
}