⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 8880._c

📁 使用8880实现DTMF通信及拨号的程序.
💻 _C
字号:
#include <iom8v.h>
#include <macros.h>
#include "8880.h"
#include "serial.h"
#include "main.h"
#include "timer.h"
char sendbuf[M8880_SEND_BUFSIZE];
char recvbuf[M8880_RECV_BUFSIZE];
M8880_STATE M8880_State;
char *noresponse="noresponse\n";
char *handdown="handdown\n";
char *c_ok="ok\n";
char *c_error="error\n";
char *c_busying="busying\n";
char *calling="calling\n";
char *answering="answering\n";
void M8880_WriteChar(char c)
{
 	 M8880_DataPort&=0xf0; 								//CLR low 4bit
 	 M8880_CtrlPort&=~(M8880_CS|M8880_RW|M8880_RS); 	//CLR CS  RW  RS hold CLK
	 M8880_DataPort|=(c&0xf);		   					//Write Data
	 M8880_CtrlPort&=~M8880_CLK;			   			//CLR CLK
	 NOP();												//wait a few time
	 NOP();
	 M8880_CtrlPort|=M8880_CS|M8880_RW|M8880_CLK|M8880_RS;  //set CS RW CLK RS 
}
char M8880_ReadChar()
{
 	 char c;
	 M8880_DataDDR&=0xf0;								//set direction to input
	 M8880_CtrlPort&=~(M8880_CS|M8880_RS);				//CLR CS RS hold RW CLK
	 M8880_CtrlPort&=~M8880_CLK;						//CLR CLK
	 NOP();												//waite a few time
	 NOP();
	 c=M8880_DataPIN&0xf;								//ReadData
	 M8880_CtrlPort|=M8880_CS|M8880_RW|M8880_CLK|M8880_RS;  //set CS RW CLK RS
	 M8880_DataDDR|=0X0F;								//restore direction
	 return c;
}
void M8880_WriteCtrl(char c,char flag)
{
 	 M8880_DataPort&=0xf0; 		 						//clear low 4bit
 	 M8880_CtrlPort&=~(M8880_CS|M8880_RW);  			//CLR CS  RW  HOLD RS CLK
	 if (flag)											//if we need to write CRB
	 	 M8880_DataPort|=((c&0xf)|M8880_BIT3);		   	//so we need to set bit3
	 else												//else
	 	 M8880_DataPort|=(c&(0xf&(~M8880_BIT3)));		//we need to clear bit3
	 M8880_CtrlPort&=~M8880_CLK;			   			//CLR CLK
	 NOP();												//wait a few time
	 NOP();
	 M8880_CtrlPort|=M8880_CS|M8880_RW|M8880_CLK|M8880_RS;		//set CS RW CLK RS
	 if (flag){ 										//if we need to write CRB ...
	 	M8880_DataPort&=0xf0;
	 	M8880_CtrlPort&=~(M8880_CS|M8880_RW);
	 	M8880_DataPort|=(c>>4);
	 	M8880_CtrlPort&=~M8880_CLK;
	 	NOP();
	 	NOP();
	 	M8880_CtrlPort|=M8880_CS|M8880_RW|M8880_CLK|M8880_RS;  //set CS RW CLK
	 }
}
char M8880_ReadState(void)
{
  	 char c;
	 M8880_DataDDR&=0xf0;
	 M8880_CtrlPort&=~(M8880_CS);
	 M8880_CtrlPort&=~M8880_CLK;
	 NOP();
	 NOP();
	 c=M8880_DataPIN&0xf;
	 M8880_CtrlPort|=M8880_CS|M8880_RW|M8880_CLK|M8880_RS;  //set CS RW CLK
	 M8880_DataDDR|=0X0F;
	 return c;	 
}
void M8880_Init(void)
{
	 M8880_State.SendCount=0;
	 M8880_State.CurSend=sendbuf;
	 M8880_State.Sending=0;
	 M8880_State.RecvIntCount=0;
	 M8880_State.RecvStartPtr=recvbuf;
	 M8880_State.RecvEndPtr=recvbuf;
	 M8880_State.CurRecv=recvbuf;
 	 M8880_DataDDR|=0xf;
	 M8880_DataPort&=~0xf;
	 M8880_CtrlPort|=M8880_CS|M8880_RW|M8880_CLK|M8880_RS;
	 M8880_CtrlDDR|=M8880_CS|M8880_RW|M8880_CLK|M8880_RS;
	 M8880_ReadState();
	 M8880_WriteCtrl(0x00,0);
	 M8880_WriteCtrl(0x00,0);
	 M8880_WriteCtrl((M8880_BIT0|M8880_BIT2),-1);
	 M8880_ReadState();
#if M8880_INT==1
	 PORTD|=0x8;
	 DDRD&=~0x8;
	 MCUCR&=~((1<<ISC11)|(1<<ISC10));
	 MCUCR|=(1<<ISC11);
	 GICR|=(1<<INT1);
	 GIFR|=(1<<INT1);
#else
	 PORTD|=0x4;
	 DDRD&=~0x4;
	 MCUCR&=~((1<<ISC01)|(1<<ISC00));
	 MCUCR|=(1<<ISC01);
	 GICR|=(1<<INT0);
	 GIFR|=(1<<INT0);
#endif 
}
char *dec="01234567890*#abcd";
#pragma interrupt_handler int0_isr:iv_INT0
void int0_isr(void)
{
  char c;
  c=M8880_ReadState();
  if (c&M8880_BIT1){
   	 M8880_State.SendCount--;
     if (M8880_State.SendCount>0){
		 M8880_State.CurSend++;
	 	 M8880_WriteChar(*M8880_State.CurSend);
	 }else{
	 	M8880_State.CurSend=sendbuf;
		M8880_State.Sending=0;
	 }
  }
  if ((c&M8880_BIT2)&&((c&M8880_BIT3)==0)){
  	 if (M8880_State.RecvIntCount>0){
	 	 M8880_State.RecvIntCount--;
	   	 *M8880_State.CurRecv++=dec[M8880_ReadChar()];
		 if (M8880_State.CurRecv==recvbuf+M8880_RECV_BUFSIZE)
		 	M8880_State.CurRecv=recvbuf;
		 if (M8880_State.RecvIntCount==0)
		    M8880_State.RecvEndPtr=M8880_State.CurRecv;
	 }
  }
}
void M8880_Send(char *x,char count)
{
 	char *c;
	if (count==0) return;
	while(M8880_State.Sending);
	M8880_State.SendCount=count;
	M8880_State.Sending=-1;
	c=sendbuf;
 	for (;count>0;count--){
		*c++=*x++;
	}
	M8880_WriteChar(*M8880_State.CurSend);
}
char number[]={10,1,2,3,4,5,6,7,8,9};
void M8880_Dial(char *x)
{
 	char *c;
	if (*x==0) return;
	while(M8880_State.Sending);
	M8880_State.SendCount=0;
	M8880_State.Sending=-1;
	c=sendbuf;
 	while(*x){
		*c++=number[(*x++)-'0'];
		M8880_State.SendCount++;
	}
	M8880_WriteChar(*M8880_State.CurSend);
}
void M8880_Recv(char* buf,char count)
{
 	if (count==0) return;
	M8880_State.RecvIntCount=count;
	while(M8880_State.RecvStartPtr==M8880_State.RecvEndPtr);
	while(M8880_State.RecvStartPtr!=M8880_State.RecvEndPtr){
	 	*buf++=*M8880_State.RecvStartPtr++;
		if (M8880_State.RecvStartPtr==recvbuf+M8880_RECV_BUFSIZE)
		   M8880_State.RecvStartPtr=recvbuf;
	}
	 *buf=0;
}
void M8880_CPDial(char *buf)
{
	 int x;
#if M8880_INT==1
	 GICR&=~(1<<INT1);
#else
	 GICR&=~(1<<INT0);
#endif
 	 M8880_WriteCtrl(M8880_BIT0|M8880_BIT1|M8880_BIT2|M8880_BIT3,-1);
	 while(*buf!=0){
	  char c;
	  c=*buf++;
	  c-=('0');
	  M8880_WriteChar(number[c]);
	  while(((x=M8880_ReadState())&M8880_BIT0)==0x0);
	 };
	 M8880_WriteCtrl(M8880_BIT0|M8880_BIT2,-1);
	 M8880_ReadState();
#if M8880_INT==1
	 GIFR|=(1<<INT1);
	 GICR|=(1<<INT1);
#else
	 GIFR|=(1<<INT0);
	 GICR|=(1<<INT0);
#endif
}
#pragma interrupt_handler int1_isr:iv_INT1
void int1_isr(void)
{
 	 if ((sysoption.calling==0) && sysoption.autoanswer){
	 	M8880_Handup;
		sysoption.calling=2;
		serial_send(answering);
		MCUCR|=0xc;
		TCCR1B=0x05;
	 }
	 if (sysoption.calling==3){
		M8880_Handdown;
	 	sysoption.calling=0;
	 	MCUCR&=~0xc;
		MCUCR|=0x8;
		serial_send(handdown);
	 }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -