C51與HT9170和HT9200接口程序(雙音多頻)
/********************************************
ht9200A/ht9170程序
雙音多頻接收發(fā)射程序
********************************************/
sbit DATA= P3^7 ;
sbit CLK= P3^6 ;
sbit CE= P2^7 ;
/*******************************************
DTMF 數(shù)據(jù)口:P0^0 ~ P0^3
*******************************************/
sbit DV= P2^6 ;
sbit OE= P3^2 ;
//sbit PWDN= P3^6 ;
//sbit INH= P3^7 ;
sbit DXC= P3^5 ;
sbit RING = p3^0 ;
#ifndefTRUE
defineTRUE1 ;
#endif
#ifndefFALSE
defineFALSE0 ;
#endif
uchar idata Check0 ;
uchar idata Counter0 ;
uchar idata Check1 ;
uchar idata Counter1 ;
uchar idata Check3 ;
uchar idata Counter3 ;
uchar code Table_DTMF[] = {0x0a,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0d,0x0e,0x0f,0x00,0x0b,0x0c} ;
//0123456789ABCD*#
void senddata_ht9200(uchar in)
{
uchar i;
for(i=0;i<5;i++)
{
if(((in>>i)&0x01) == 1)
DATA = 1 ;
elseDATA = 0 ;
CLK = 0 ;
CLK = 1 ;
}
}
/******************************************
發(fā)送子程序
in 為要發(fā)送的數(shù) 0-15
******************************************/
void send_ht9200(uchar in)
{
CE= 0 ;
CLK= 1 ;
time(50) ;//delay about 6 ms
EA = 0 ;
senddata_ht9200(Table_DTMF[in]) ;
EA = 1 ;
delay(100) ;//delay about 100 ms
CE= 1 ;
}
/*****************************************
DTMF 讀程序
返回 DTMF值
*****************************************/
uchar DTMF_read(void)
{
uchar temp ;
uchar i ;
EA = 0 ;
P0 = 0xff ;
OE = 1 ;
ACC = ACC ;
temp = P0&0x0f ;
OE = 0 ;
EA = 1 ;
for(i=0;i<16;i++)
{
if(temp == Table_DTMF[i])
return i ;
}
return(0xff) ;
}
/******************************************
初始化檢測(cè)程序
******************************************/
void checkRING_Init()
{
Check0 = 2 ;
Counter0 = 20 ;
}
void checkDV_Init()
{
Check1 = 2;
Counter1 = 20;
}
void checkDXC_Init()
{
Check3 = 2 ;
Counter3 = 20 ;
}
/***************************************
檢測(cè)有無振鈴RING 程序
返回TRUE :有
FALSE:無
***************************************/
uchar check_RING(void)//
{
uchar flag ;
flag = RING ;//check RING
if((flag == 0)&&(Counter0 < 30))
Counter0 ++ ;
if((flag == 1)&&(Counter0 > 0))
Counter0 -- ;
if(Counter0 == 30)
{
Counter0 = 20 ;
if(flag != Check0)
{
Check0 = flag ;
return TRUE ;
}
else return FALSE;
}
else if(Counter0 == 0)
{
Counter0 = 20 ;
Check0 = 1;
return FALSE;
}
else
return FALSE ;
}
/***************************************
檢測(cè)有無DTMF信號(hào) DV 程序
返回TRUE :有
FALSE:無
***************************************/
uchar check_DV(void)//
{
uchar flag ;
flag = DV ;//check DV
if((flag == 1)&&(Counter1<30))
Counter1 ++ ;
if((flag == 0)&&(Counter1 >0))
Counter1 -- ;
if(Counter1 == 30)
{
Counter1 = 20 ;
if(flag != Check1)
{
Check1 = flag ;
return TRUE ;
}
elsereturn FALSE ;
}
else if(Counter1 == 0)
{
Counter1 = 20 ;
Check1= 0 ;
return FALSE ;
}
else return FALSE ;
}
/***************************************
檢測(cè)有無掉線信號(hào) DXC 程序
返回TRUE :有
FALSE:無
***************************************/
uchar check_DXC(void)//
{
uchar flag ;
flag = DXC ;//check DXC
if((flag == 1)&&(Counter3<30))
Counter3 ++ ;
if((flag == 0)&&(Counter3 >0))
Counter3 -- ;
if(Counter3 == 30)
{
Counter3 = 20 ;
if(flag != Check3)
{
Check3 = flag ;
return TRUE ;
}
elsereturn FALSE ;
}
else if(Counter3 == 0)
{
Counter3 = 20 ;
Check3= 0 ;
return FALSE ;
}
else return FALSE ;
}