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

📄 app__.c

📁 使用avr--cc2420模块程序,需要原理图的联系 QQ:120304948
💻 C
📖 第 1 页 / 共 3 页
字号:
#include <mega128.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <delay.h>
#include <spi.h>

#include "typedef.h"
#include "serialdrv.h"

#include "app.h"

#include "timer.h"
#include "rtc.h"
#include "cc2420.h"
#include "18B20.h"
#include "intp.h"


bit system_initial_flag;
bit study_ok = 1;

unsigned char timer_init_code[8] =
{
	0x30, 0x31, 0x16, 0x23, 0x03, 0x05, 0x07, 0x00
};

void system_initial()
{
	system_initial_flag = FALSE;

	CPU_init();
/*
    ts_init();

    delay_ms(1000);
    wdr();

    ts_FindDevices();

   	rtc_init();
//	usart0_init();
*/
	timer_init();
    sp_init();
    wdr();

    delay_ms(100);
    cc2420_initial();
    cc2420_ready();
    INTP_init();

    wdr();
	EINT;
    timer_delay(100);

	EINT;
	system_initial_flag = TRUE;
    wdr();

}

void main(void)
{
//	rtc_set_flag = TRUE;

	system_initial();

//    v_Set1302(timer_init_code);

//	rtc_set_flag = FALSE;

	workingloop();
}


void workingloop()
{

	while(1)
	{
        wdr();
        if (receive_ok)
        {
            receive_ok = 0;
            cc2420_deal();
//			WORK_LED = !WORK_LED;
        }
        if ((study_ok == 0) && (int_start == 1))
        {
            sp_proc();
        }
//        cc2420_test();
        wdr();
        if (all_sending)
        {
            all_sending = FALSE;

            EIMSK=0x00;     //0x0D  //0x01
            cc2420_ready();
            wdr();
            delay_ms(10);
   	        CSn=0;
            spi(SFLUSHRX);
            spi(SFLUSHRX);
            CSn=1;
            receive_ok = 0;
    		EIFR=0x0D;
            if (study_ok)
                EIMSK=0x01;     //0x0D  //0x01
            else
            {
                EIMSK=0x0D;     //0x0D  //0x01
                int_start = 0;
                int_num = 0;
        		count=0;
            }

            data_proc();
			WORK_LED = !WORK_LED;
        }
/*        wdr();
        if (reset_cc2420)
        {
//            EIMSK=0x00;     //0x0D  //0x01
//            TIMSK=0x00;
//            while (1);
            reset_cc2420 = FALSE;
            EIMSK=0x00;     //0x0D  //0x01
            cc2420_ready();
            wdr();
            delay_ms(100);
   	        CSn=0;
            spi(SFLUSHRX);
            spi(SFLUSHRX);
            CSn=1;
            receive_ok = 0;
    		EIFR=0x0D;
            if (study_ok)
                EIMSK=0x01;     //0x0D  //0x01
            else
            {
                EIMSK=0x0D;     //0x0D  //0x01
                int_start = 0;
                int_num = 0;
        		count=0;
            }
//			WORK_LED = !WORK_LED;
        }*/
	}
}

void CPU_init(void)
{
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=Out Func3=In Func2=Out Func1=Out Func0=Out
// State7=P State6=P State5=P State4=1 State3=P State2=1 State1=1 State0=1
PORTB=0xFF;
DDRB=0x17;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=Out Func6=In Func5=Out Func4=In Func3=In Func2=In Func1=Out Func0=In
// State7=1 State6=T State5=0 State4=T State3=T State2=T State1=0 State0=T
PORTD=0x80;     //0x82
DDRD=0xA2;

// Port E initialization
// Func7=In Func6=In Func5=Out Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=0 State4=T State3=T State2=T State1=T State0=T
PORTE=0x00;
DDRE=0x20;

// Port F initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTF=0x00;
DDRF=0x00;

// Port G initialization
// Func4=In Func3=In Func2=In Func1=In Func0=In
// State4=T State3=T State2=T State1=T State0=T
PORTG=0x00;
DDRG=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// Watchdog Timer initialization
// Watchdog Timer Prescaler: OSC/2048k
#pragma optsize-
WDTCR=0x1F;
WDTCR=0x0F;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

}


#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 char study_num1;
eeprom unsigned char 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 char 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()
{

⌨️ 快捷键说明

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