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

📄 serialdrv.c~

📁 使用avr--cc2420模块程序,需要原理图的联系 QQ:120304948
💻 C~
字号:
#include <mega128.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

#include "typedef.h"

#include "app.h"

#include "serialdrv.h"
#include "timer.h"

#include "cc2420.h"
#include "intp.h"

#pragma warn-
eeprom unsigned int study_num1;
eeprom unsigned int study_num2;
eeprom unsigned int study_data1[STUDY_NO];
eeprom unsigned int study_data2[STUDY_NO];
#pragma warn+

SPSTATUS my_usart0_status;
SPSTATUS my_sp_status;

unsigned char count=0;
unsigned char int_num1=0;
unsigned char study_key=0;

void usart0_init()
{
// USART0 initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART0 Receiver: On
// USART0 Transmitter: On
// USART0 Mode: Asynchronous
// USART0 Baud rate: 115200
    UCSR0A=0x00;
    UCSR0B=0x98;
    UCSR0C=0x06;
    UBRR0H=0x00;
    UBRR0L=0x03;

	my_usart0_status.buffer[0] = (BYTE)0x00;
	my_usart0_status.data = 0;
	my_usart0_status.empty = 0; 
	memset(my_usart0_status.buffer, 0x00, USART0MAXLEN);
}

void sp_usart0_proc(BYTE data)
{
		my_usart0_status.buffer[my_usart0_status.empty ++] = data;
		if(my_usart0_status.empty >= USART0MAXLEN)
		{
			my_usart0_status.empty = 0;
		}
		
		if(my_usart0_status.empty >= USART0OVERFLOW)
		{
		    my_usart0_status.overflow = TRUE;
			my_usart0_status.empty = 0;
		}
}
 
// USART0 Receiver interrupt service routine
interrupt [USART0_RXC] void usart0_rx_isr(void)
{
    char status,data;
    status=UCSR0A;
    data=UDR0;
	if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
	{
//		while ((UCSR0A & DATA_REGISTER_EMPTY)==0);
//		UDR0 = data;
		sp_usart0_proc(data);
        timer_set_usart0();
	};
}

void usart0_proc()
{
	unsigned char szsum;
	unsigned char nloop;

    my_usart0_status.timeout = timer_get_usart0_timeout();
    if((my_usart0_status.overflow | my_usart0_status.timeout) == TRUE)
    {
        //process serial port data       
        
        if (my_usart0_status.buffer[0]==0x7e && my_usart0_status.buffer[1]==0x7e)
        {
	        szsum = 0x00;
	        for (nloop = 0; nloop < SEND_NO; nloop ++)
	        {
		        szsum += my_usart0_status.buffer[nloop];
	        }
            if (szsum == 0xff)
            { 
                cc2420_data_send[0] = SEND_NO+2;
	            for (nloop = 0; nloop < SEND_NO; nloop ++)
	            {
		            cc2420_data_send[nloop+1] = my_usart0_status.buffer[nloop];
	            }
                cc2420_send(SEND_NO+3);
            }
        }

        if(my_usart0_status.overflow == TRUE)
        {
            my_usart0_status.overflow = FALSE;
        }
        if(my_usart0_status.timeout == TRUE)
        {
            my_usart0_status.timeout = FALSE;
        }
        
	    my_usart0_status.buffer[0] = (BYTE)0x00;
	    my_usart0_status.data = 0;
	    my_usart0_status.empty = 0;
	    memset(my_usart0_status.buffer, 0x00, USART0MAXLEN);
    }
}


void usart0_send(BYTE * pBuf, unsigned char ncont)
{
	while (ncont--)
	{
        wdr();
		while ((UCSR0A & DATA_REGISTER_EMPTY)==0);
		UDR0 = *(pBuf ++);
	}
}

void sp_init()
{
	unsigned int i;

	my_sp_status.buffer[0] = (BYTE)0x00;
	my_sp_status.data = 0;
	my_sp_status.empty = 0;

	memset(my_sp_status.buffer, 0x00, SPMAXLEN);

	if (study_num1 > STUDY_NO)
		study_num1 = 0;	
	if (study_num2 > STUDY_NO)
		study_num2 = 0;	
	for (i=0;i<STUDY_NO;i++)
	{
	    if (study_data1[i] > 65500)
	        study_data1[i] = 0;
	    if (study_data2[i] > 65500)
	        study_data2[i] = 0;
	}
    int_start = 0;
    int_num = 0;
    my_sp_status.timeout = FALSE;
}

/*
unsigned char sp_proc()
{
	unsigned char nloop = 0;

//Note: Receive: start: 9ms(low)+4.5ms(high) 0: 0.565ms(low)+0.56ms(high)=1.125ms(T) 1: 0.565ms(low)+1.685ms(high)=2.25ms(T)

    my_sp_status.timeout = timer_get_sp_timeout();
    if( my_sp_status.timeout == TRUE)
    {
        if(my_sp_status.timeout == TRUE)
        {
            my_sp_status.timeout = FALSE;
        }

   				WORK_LED = 1;
  				timer_delay(500);
   				WORK_LED = 0;
  				timer_delay(500);
   				WORK_LED = 1;
  				timer_delay(500);
   				WORK_LED = 0;
  				timer_delay(3000);
            	led_send0();
       			
        		int_start = 0;
    			OCR3CL=0x00;    //0x30
//        		int_num1=0;
        		int_num1 = int_num;
        		return 0;

        if (int_num1 == int_num)
        {
        	for (nloop = 0; nloop < int_num; nloop++)
        	{
        		if (abs(int_data[nloop]-int_data1[nloop]) > 15)
        		{
        			nloop=0;
        			break;
        		}
        	}
        	nloop = 1;
        	if (nloop)
        	{
        		for (nloop = 0; nloop < int_num; nloop++)
        		{
        			int_data1[nloop]=(int_data1[nloop]+int_data[nloop])/2;
        		}
   				WORK_LED = 1;
  				timer_delay(500);
   				WORK_LED = 0;
  				timer_delay(500);
   				WORK_LED = 1;
  				timer_delay(500);
   				WORK_LED = 0;
  				timer_delay(3000);
            	led_send0();
       			
        		int_start = 0;
    			OCR3CL=0x00;    //0x30
//        		int_num1=0;
        		int_num1 = int_num;
        		return 0;
        	}
        	else
        	{
        		for (nloop = 0; nloop < int_num; nloop++)
        			int_data1[nloop]=int_data[nloop];
        		int_num1 = int_num;
        		int_start = 0;
   				WORK_LED = 1;
  				timer_delay(500);
   				WORK_LED = 0;
        		return 1;
        	}
        }    
        else
        {
        	for (nloop = 0; nloop < int_num; nloop++)
        		int_data1[nloop]=int_data[nloop]; 
   			WORK_LED = 1;
  			timer_delay(500);
   			WORK_LED = 0;
        	int_start = 0;
        	int_num1 = int_num;
        }
    }
}
*/

unsigned char sp_proc()
{
	unsigned char nloop = 0;
	bit abs_ok = 1;

//Note: Receive: start: 9ms(low)+4.5ms(high) 0: 0.565ms(low)+0.56ms(high)=1.125ms(T) 1: 0.565ms(low)+1.685ms(high)=2.25ms(T)

    my_sp_status.timeout = timer_get_sp_timeout();
    if( my_sp_status.timeout == TRUE)
    {
        wdr();
        if ((count > 0) && (int_num1 == int_num))
        {
        	for (nloop = 0; nloop < int_num; nloop++)
        	{
        		if (abs(int_data[nloop]-int_data1[nloop]) > 20)       //20
        		{
        			abs_ok = 0;
        			break;
        		}
        	}
        	if (abs_ok)
        	{
        		if (count >= 2)
        		{
        			for (nloop = 0; nloop < int_num; nloop++)
        			{
        				int_data2[nloop]=(int_data2[nloop]+int_data1[nloop]+int_data[nloop])/3;
        			}
        			
        			if (study_key)
        			{
        		        study_num2 = int_num;
        			    for (nloop = 0; nloop < int_num; nloop++)
        			    {
        				    study_data2[nloop]=int_data2[nloop];
        			    }
        			    study_key=0;
        			    int_start = 0;
    				    OCR3CL=0x00;    //0x30
        			    count=0;
        			    int_num=0;
        			    int_num1=0;
    				    my_sp_status.timeout = FALSE;
    				    EIFR=0x0D;
            		    EIMSK=0x01;    //0: 0x01  //2&3: 0x0C  //6&7: 0xC0  //0&6&7: 0xC1   //0&2&3&6&7: 0xCD
                        study_ok = 1;
        			}
        			else
        			{
        		        study_num1 = int_num;
        			    for (nloop = 0; nloop < int_num; nloop++)
        			    {
        				    study_data1[nloop]=int_data2[nloop];
        			    }
        			    study_key++;
        			    int_start = 0;
    				    OCR3CL=0x00;    //0x30
        			    count=0;
        			    int_num=0;
        			    int_num1=0;
    				    my_sp_status.timeout = FALSE;
        			}
        			
   					WORK_LED = 0;
  					timer_delay(500);
   					WORK_LED = 1;
  					timer_delay(500);
   					WORK_LED = 0;
  					timer_delay(500);
   					WORK_LED = 1;
  					timer_delay(500);
   					WORK_LED = 0;
  					timer_delay(500);
   					WORK_LED = 1;
        			
//  					timer_delay(3000);
//            		led_send0(1);
       			
        			return 0;
        		}
        		else
        		{
        			for (nloop = 0; nloop < int_num; nloop++)
        			{
        				int_data2[nloop]=int_data1[nloop];
        				int_data1[nloop]=int_data[nloop];
        			}
                    count++ ;
   					WORK_LED = 0;
  					timer_delay(500);
   					WORK_LED = 1;
  					timer_delay(500);
   					WORK_LED = 0;
  					timer_delay(500);
   					WORK_LED = 1;
        		}
        	}
        	else
        	{
        		for (nloop = 0; nloop < int_num; nloop++)
        			int_data1[nloop]=int_data[nloop]; 
        	}
        }
        else
            if ((count > 0) && (int_num1 != int_num))
            {
        		for (nloop = 0; nloop < int_num; nloop++)
        			int_data1[nloop]=int_data[nloop]; 
            }
        else
        	if ((count == 0) && (int_num > 0))
        	{
        		for (nloop = 0; nloop < int_num; nloop++)
        			int_data1[nloop]=int_data[nloop]; 
        		count++; 
   				WORK_LED = 1;
  				timer_delay(500);
   				WORK_LED = 0;
  				timer_delay(500);
   				WORK_LED = 1;
        	}
        int_start = 0;
        int_num1 = int_num;
    	OCR3CL=0x00;    //0x30
    	my_sp_status.timeout = FALSE;
    }
    wdr();
}

void led_send0(unsigned char onoff)
{
	unsigned char nloop = 0;

//Note: send: start: 9ms(high)+4.5ms(low) 0: 0.565ms(high)+0.56ms(low)=1.125ms(T) 1: 0.565ms(high)+1.685ms(low)=2.25ms(T)

    wdr();
	timer3_initY();
	
	if (onoff)
	{
        int_num = study_num1;
        for (nloop = 0; nloop < int_num; nloop++)
            int_data[nloop]=study_data1[nloop];
	}
	else
	{
        int_num = study_num2;
        for (nloop = 0; nloop < int_num; nloop++)
            int_data[nloop]=study_data2[nloop];
	}

    TCNT1 = 0;
	OCR3CL=0x30;    //0x00: stop   0x30: start
    for (nloop = 0; nloop < int_num; nloop++)
	{
		while (TCNT1 < int_data[nloop]);     //int_data2[nloop]
		if ((nloop+1)%2)
			OCR3CL=0x00;    //0x00: stop   0x30: start
		else
			OCR3CL=0x30;    //0x00: stop   0x30: start
	}
    OCR3CL=0x00;    //0x30
    wdr();
	timer3_initN();
}


/*
unsigned char sp_proc()
{
	unsigned char nloop = 0;
	unsigned char nloop1 = 0;
	unsigned char value = 0;

//Note: Receive: start: 9ms(low)+4.5ms(high) 0: 0.565ms(low)+0.56ms(high)=1.125ms(T) 1: 0.565ms(low)+1.685ms(high)=2.25ms(T)

    my_sp_status.timeout = timer_get_sp_timeout();
    if( my_sp_status.timeout == TRUE)
    {
        if(my_sp_status.timeout == TRUE)
        {
            my_sp_status.timeout = FALSE;
        }
        if ((int_data[0] >= 256 && int_data[0] <= 262) && (int_data[1] >= 386 && int_data[1] <= 392))
        {
			memset(my_sp_status.buffer, 0x00, SPMAXLEN);
            for(nloop = 2; nloop < int_num; (nloop= nloop + 2))
            {
                if (((int_data[nloop] - int_data[nloop-1]) >= 13 && (int_data[nloop] - int_data[nloop-1]) <= 19)
                && ((int_data[nloop+1] - int_data[nloop]) >= 45 && (int_data[nloop+1] - int_data[nloop]) <= 51))
//                if (((int_data[nloop] - int_data[nloop-1]) >= 10 && (int_data[nloop] - int_data[nloop-1]) <= 22)
//                && ((int_data[nloop+1] - int_data[nloop]) >= 42 && (int_data[nloop+1] - int_data[nloop]) <= 54))
                {
                    value = value ^ 0x01;
                    value = value << 1; 
                }
                else if (((int_data[nloop] - int_data[nloop-1]) >= 13 && (int_data[nloop] - int_data[nloop-1]) <= 19)
                && ((int_data[nloop+1] - int_data[nloop]) >= 13 && (int_data[nloop+1] - int_data[nloop]) <= 19))
//                else if (((int_data[nloop] - int_data[nloop-1]) >= 10 && (int_data[nloop] - int_data[nloop-1]) <= 22)
//                && ((int_data[nloop+1] - int_data[nloop]) >= 10 && (int_data[nloop+1] - int_data[nloop]) <= 22))
                {
                    value = value << 1; 
                }
                else
                {
                    nloop = 0;
                    break;
                }
                if (nloop % 16 == 0)
                {
                    my_sp_status.buffer[nloop1++] = value;
                    value = 0; 
                }
            }
            my_sp_status.buffer[nloop1++] = value;
 			timer_delay(3000);
            led_send1(my_sp_status.buffer);
        }
        int_start = 0;
    	OCR3CL=0x00;    //0x30
    }
	nloop1 ++;
}

void led_send1(unsigned char * seed_data)
{
	unsigned char nloop = 0;
	unsigned char nloop1 = 0;
	unsigned char value = 0;
	unsigned char sed_data[6]={0x82,0xc0,0x02,0x9c,0x00,0x00};

//Note: send: start: 9ms(high)+4.5ms(low) 0: 0.565ms(high)+0.56ms(low)=1.125ms(T) 1: 0.565ms(high)+1.685ms(low)=2.25ms(T)
//T2:  28.2uS

	OCR3CL=0x30;    //0x00: stop   0x30: start
//    delay_us(8600);
    call_time=0;
    while (call_time<310);      //319
	OCR3CL=0x00;    //0x30
//    delay_us(3500);
    call_time=0;
    while (call_time<125);      //159
    value=*seed_data;
	OCR3CL=0x30;    //0x00
//    delay_us(540);            //565
    call_time=0;
    while (call_time<19);
	OCR3CL=0x00;    //0x30
	if (value&0x80)
	{
//    	delay_us(1580);         //1685
    	call_time=0;
    	while (call_time<59);   //59.75
 	}
   	else 
   	{
//    	delay_us(535);
    	call_time=0;
    	while (call_time<19);   //20
    }
    for (nloop = 0; nloop < 6; nloop++)
    {
    	value=*seed_data++;
		OCR3CL=0x30;    //0x00
    	for (nloop1=0;nloop1<8;nloop1++)
    	{
//    		delay_us(540);          //565
	    	call_time=0;
	    	while (call_time<19);  //20
			OCR3CL=0x00;         //0x30
			if (value&0x80)
			{
//    			delay_us(1580);     //1685
    			call_time=0;
    			while (call_time<59);   //59.75
 			}
   			else 
   			{
//    			delay_us(535);
    			call_time=0;
    			while (call_time<19);   //20
    		}
			value=value<<1;
			OCR3CL=0x30;    //0x00
    	} 
    } 
	OCR3CL=0x00;    //0x30
}
*/

⌨️ 快捷键说明

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