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

📄 cfsk.c

📁 DTMF FSK 在51 上的实现
💻 C
字号:
                                                   
//                                                                         
  
//   PROGRAM DESCRIPTION: This is the CFSK dialing and routine. This routine
//   is called in main. It use switch-case structure.It use DTMF dialing 
//   function, and then detect the answer tone. After the answer tone is 
//   detected, we will send two message packets, and between each message, 
//   a request message from CS should be detected.
//                                                                           
//                                                                           
//   ADDITIONAL DOCUMENTATION:                                               
//         Flow Chart:                                                       
//         Support Doc's:                                                    
//                                                                           
//   INPUTS:                                                                 
//   OUTPUTS:                                                                
//   REGISTERS USED:                                                         
//   SUBROUTINE CALLS:                                                       
//   CALLED BY:                                                              
//                                                                           
//   REVISION HISTORY:                                                       
//                                                                           
//   DATE        REVISED BY        DESCRIPTION OF CHANGE                     
// ------------------------------------------------------------------------- 

#include<reg51f.h>
#include"CFSK.h"
#include"main_header.h"
bit  CFSK_1ms_timer;                // CFSK 1ms counter controll;
bit  CFSK_capture_flag;				// This is capture mode enable flag;
bit  CFSK_transmit_flag;			// This is transmit function enable flag;
bit  CFSK_start_bit;				// If start bit is detected, this bit will be set.
bit  CFSK_start_tx;                 // already send 500ms carrier then set this bit.
bit  CFSK_request_flag;				// This is request command flag, set when received request.

unsigned char CFSK_process_step0;   // store CFSK process step 
unsigned char CFSK_edge_counter;    // CFSK counter used to receive bit
unsigned char CFSK_bit_counter;     // CFSK counter used to receive byte
unsigned char CFSK_byte_counter;    // CFSK counter used to receive string
unsigned char CFSK_received_byte;   // received byte, send by the receiver;
unsigned char CFSK_send_byte;       // send byte, send by the panel;
unsigned char CFSK_message_count;   // send byte counter, send by the panel;
unsigned char CFSK_message_lenth;   // send byte counter, send by the panel;
unsigned char CFSK_receive_buf[6];  // received data buffer

// 'arm report message' packet.
unsigned char CFSK_send_buf[6]={0x01, 0x00, 0x00, 0x00, 0x1C, 0x1d};

//'end of report command' message packet.
unsigned char CFSK_send_buf1[2]={0xfe, 0xfe};

unsigned int  CFSK_1ms_count;       // CFSK 1ms counter controll;	
unsigned int  previous_period;      // store the previous period time;
unsigned int  current_period;       // store the current period time;
unsigned char  test_char;


code unsigned char CFSK_PERIOD[4][2]=
{
    0x01,0xD3,  //space 1070hz half period generated by the PCA.
	0x01,0x8A,  //mark  1270hz half period generated by the PCA.
	0x00,0xe1,  //      2225hz half period for tone generation.
};

/*
  This is 1ms delay function. Every 1ms the timer 0 interrupt will increase 
  CFSK_1ms_count.
*/
void delay_1ms(unsigned int i)
{
	ET0=1;
	CFSK_1ms_count = 0;
	CFSK_1ms_timer = 1;
	while(CFSK_1ms_count<i);
	CFSK_1ms_timer = 0;
}

/*
  This is CFSK tone change routine. The CFSK data bit is send according to 
*/
void send_cfsk_bit(unsigned char i)
{
    i&=1;
    EC=0;
	if(i)
	{
	    // load the varible with bit 1 period value
	    PCA_buf0_high= CFSK_PERIOD[1][0];
    	PCA_buf0_low = CFSK_PERIOD[1][1];
	}
	else
	{
		// load the varible with bit 0 period value
	    PCA_buf0_high= CFSK_PERIOD[0][0];
    	PCA_buf0_low = CFSK_PERIOD[0][1];
	}
	EC=1;
	CCAPM0 = PCA_COMPARE_ON;
}
/*
	This routine send CFSK tone
*/
void send_cfsk_digit(unsigned char n)
{
    /*
	  n=0: send space 1070hz
	  n=1: send space 1270hz
	  n=2: send space 2225hz
	  fetch period value from table.
	*/
	CR=0;
	EC = 0;
	CCF1=0;

	//fix the PCA module 0 register
	//high byte
	CCAP0H = CFSK_PERIOD[n][0];
	//low byte
	CCAP0L = CFSK_PERIOD[n][1];
	
	PCA_buf0_high= CFSK_PERIOD[n][0];
    PCA_buf0_low = CFSK_PERIOD[n][1];

	CH = 0;
	CL = 0;
	CCAPM0 = PCA_COMPARE_ON;
	CCF0=0;
	EC = 1;
	CR = 1;

}
/*
	This is CFSK process, programed according to report sequence.
*/
void CFSK_process(void)
{
    switch(CFSK_process_step0)
	{
	    case 0:
		{
			// Do some initialize
		    CFSK_process_step0 = 1;
		}break;
		case 1:
		{
		    
		    CFSK_1ms_count=0;
			CFSK_1ms_timer=1;
		    dial_the_receiver();
			received_tone = 0;
			
			CFSK_1ms_timer = 1;
		
			CFSK_1ms_count=0;

			CFSK_process_step0 = 2;
		}break;
		case 2:
		{
		    //delay 2.1 seconds
			if(CFSK_1ms_count>2100)
			{
				// now let's open the answer tone detecting.
			    CFSK_1ms_count=0;
				CFSK_1ms_timer=1;
				//use count mode to detect the frequency
				DTMF_capture_flag=1;
				CEX2_first_period=1;
				DTMF_capture_window=40;//40ms
			    Timer0_reload();
				CCAPM2 = PCA_CAPTURE_ON;
				EC=1;
			    //move to next step
			    CFSK_process_step0 = 3;
			}
		}break;
		case 3:
		{
		    
			//detect the handshaketone 200ms 2300 answer tone ,
		    int i;
			for(i=0;i<5;i++)
		    {
		        while(received_tone!=2300);
			}
			//send 1270 handshake tone last 2 second
			//turn of PCA data receive channel;
			CCAPM2 = PCA_OFF;
			DTMF_capture_flag=0;
			CFSK_process_step0 = 4;
		}break;
		case 4:
		{
		    //send 2225hz handshake tone;
		    send_cfsk_digit(1);
			//start to wait 2 seconds
			CFSK_1ms_timer = 1;
			CFSK_1ms_count=0;
			Timer0_reload();

		    CFSK_process_step0 = 5;
		}break;
		case 5:
		{
            //wait 2 senconds
		    if(CFSK_1ms_count>1000)
			{
			    // reset timer to capture the request message.
				CFSK_1ms_count = 0;
				CFSK_1ms_timer = 1;
				// Initialize the flag.
				DTMF_capture_flag = 0;
				CEX2_first_period = 1;
				//turn off PCA
				CCAPM0 = PCA_OFF;
			    CFSK_process_step0= 6;
			}
		}break;
		case 6:
		{
			if(CFSK_1ms_count>500)
			{
				// clear 1ms timer
				CFSK_1ms_count = 0;
				CFSK_1ms_timer = 0;
				
				// initialize the data transmit process.
			    //enable start bit detecting
			    CFSK_capture_flag = 1;
				CCAPM2 = PCA_CAPTURE_ON;
				// clear the flag;
				CFSK_request_flag = 0;
				CFSK_process_step0= 7;
			}			
		}break;
		case 7:
		{
			// we will send 3 message
			if(CFSK_request_flag)
			{
				// the request message have been received 
				if(CFSK_start_tx)
				{
					// the 500ms carrier have been sent
					if(!CFSK_transmit_flag)
					{
						// we have sent the whole message increase the counter
						if(CFSK_message_count<2)
					    {
							// we will receive 2 bytes request message before sending.
					    	//enable start bit detecting for the next message sending.
			 				CFSK_capture_flag = 1;
					    	CCAPM2 = PCA_CAPTURE_ON;
					    	// clear the flag;
						    CFSK_request_flag = 0;
						    CCAPM0 = PCA_OFF;
						    CFSK_start_tx=0;
						    CFSK_byte_counter=0;
						    CFSK_message_count++;
				        }
					    else
					    {
					    	// we have send report for 3 times  to the CS.
					    	CFSK_message_count=0;
					    	CFSK_process_step0=8;
				        }
					}
				}
				else
				{
			 	   //load 500ms carrier. 	
				    send_cfsk_digit(1);
				    ET2=0;
				    delay_1ms(500);
				    ET0=1;
				    CFSK_start_tx=1;
				    //enable the Timer2 transmiting process,
				    CFSK_transmit_flag=1;
				    if(!CFSK_message_count)
				        CFSK_message_lenth=6;
				    else
				        CFSK_message_lenth=2;
				    Timer2_reload();
				    // load the first byte of the message, and initialize the transmiting.
			 	    // CFSK_send_byte=CFSK_send_buf[0];
				    CFSK_transmit_flag=1;
				}
			}
			else
			{
			    // the request message have not been received 
			}
		}break;
		case 8:
		{
		    CFSK_process_step0=8;
		}break;
		default:{}break;
	}
}

⌨️ 快捷键说明

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