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

📄 app__.c

📁 使用avr--cc2420模块程序,需要原理图的联系 QQ:120304948
💻 C
📖 第 1 页 / 共 3 页
字号:
// SPI initialization
// SPI Type: Master
// SPI Clock Rate: 115.200 kHz
// SPI Clock Phase: Cycle Half
// SPI Clock Polarity: Low
// SPI Data Order: MSB First
    SPCR=0x52;
    SPSR=0x00;

	for (i=0;i<CC2420_MAX;i++)
	{
		cc2420_data_send[i]=0x00;
		cc2420_data_recive[i]=0x00;
	}
}

void cc2420_ready(void)
{
    unsigned int i;

	VREG_EN=0;
    delay_ms(100);       //100ms  //100
	VREG_EN=1;
    delay_ms(5);       //0.6ms //5
	RESETn=0;
    delay_ms(50);       //100ms  //100
	RESETn=1;
    wdr();

	CSn=0;
	i=spi(SXOSCON);
    delay_ms(2);       //0.86ms  //5
	while ((i&0x40)==0)
	{
		i=spi(SXOSCON);
        wdr();
	}

	i=spi(SFLUSHRX);
	i=spi(SFLUSHTX);
	i=spi(MAIN);
	i=spi(0xF8);
	i=spi(0x00);
	i=spi(MDMCTRL0);
	i=spi(0x02);   //0A
	i=spi(0xE2);
	i=spi(MDMCTRL1);
	i=spi(0x05);
	i=spi(0x00);
	i=spi(RSSI);
	i=spi(0xE0);
	i=spi(0x80);
	i=spi(SYNCWORD);
	i=spi(0xA7);
	i=spi(0x0F);
	i=spi(TXCTRL);
	i=spi(0xA0);
	i=spi(0xFF);
	i=spi(RXCTRL0);
	i=spi(0x12);
	i=spi(0xE5);
	i=spi(RXCTRL1);
	i=spi(0x0A);
	i=spi(0x56);
	i=spi(FSCTRL);
	i=spi(0x41);
	i=spi(0x65);
	i=spi(SECCTRL0);
	i=spi(0x01);   //03
	i=spi(0xC4);
	i=spi(SECCTRL1);
	i=spi(0x00);
	i=spi(0x00);
	i=spi(BATTMON);
	i=spi(0x00);
	i=spi(0x00);
	i=spi(IOCFG0);
	i=spi(0x00);
	i=spi(0x40);
	i=spi(IOCFG1);
	i=spi(0x00);
	i=spi(0x00);
	i=spi(FSMTC);
	i=spi(0x7A);
	i=spi(0x94);
	i=spi(MANAND);
	i=spi(0xFF);
	i=spi(0xFF);
	i=spi(MANOR);
	i=spi(0x00);
	i=spi(0x00);
	i=spi(AGCCTRL);
	i=spi(0x07);
	i=spi(0xF0);
	i=spi(AGCTST0);
	i=spi(0x36);
	i=spi(0x49);
	i=spi(AGCTST1);
	i=spi(0x08);
	i=spi(0x54);
	i=spi(AGCTST2);
	i=spi(0x01);
	i=spi(0x2A);
	i=spi(FSTST0);
	i=spi(0x02);
	i=spi(0x00);
	i=spi(FSTST1);
	i=spi(0x50);
	i=spi(0x02);
	i=spi(FSTST2);
	i=spi(0x06);
	i=spi(0x00);
	i=spi(FSTST3);
	i=spi(0x82);
	i=spi(0xDD);
	i=spi(RXBPFTST);
	i=spi(0x00);
	i=spi(0x00);
	i=spi(FSMSTATE);
	i=spi(0x00);
	i=spi(0x00);
	i=spi(ADCTST);
	i=spi(0x00);
	i=spi(0x00);
	i=spi(DACTST);
	i=spi(0x00);
	i=spi(0x00);
	i=spi(TOPTST);
	i=spi(0x00);
	i=spi(0x10);
 	i=spi(SRXON);
	CSn=1;

	CSn=0;
 	i=spi(SRXON);
	CSn=1;
    wdr();
}

void cc2420_send(unsigned char ncont)
{
    unsigned char i;

    while (SFD);
    CSn=0;
    i=spi(SFLUSHTX);
    CSn=1;

    CSn=0;
    i=spi(TXFIFO);
    for (i=0; i<ncont; i++)
    {
   	    spi(cc2420_data_send[i]);
    }
    CSn=1;

    CSn=0;
    i=spi(STXON);
    while (SFD);
    CSn=1;
}

// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
// Place your code here
    unsigned char i,j;

//	memset(cc2420_data_recive, 0x00, CC2420_MAX);
    if (FIFO)
    {
   	    CSn=0;
   	    i=spi(0x7f);
   	    if (FIFO)
   	    {
   	        cc2420_data_recive[0] = spi(SNOP);
   	        j = 0;
   	        while ((FIFO == 1) && (j < cc2420_data_recive[0]))
   	        {
                wdr();
		        cc2420_data_recive[++j] = spi(SNOP);
		        if(j > CC2420_MAX)
		            break;
   	        }
   	    }
        CSn=1;
   	    receive_ok = 1;
/*		for (nloop = 0; nloop < (RECV_NO+1); nloop ++)
		{
			cc2420_data_temp[nloop] = cc2420_data_recive[nloop];
		}*/
        wdr();

   	    CSn=0;
        i=spi(SFLUSHRX);
        i=spi(SFLUSHRX);
        CSn=1;
    }
}

unsigned char cc2420_receive(unsigned char ncont)
{
    unsigned char i,j;

    if (FIFO)
    {
   	    CSn=0;
   	    i=spi(0x7f);
   	    j = 0;
	    if (FIFO)
	    {
   	        cc2420_data_recive[0] = spi(SNOP);
   	        j = 0;
   	        while ((FIFO == 1) && (j < cc2420_data_recive[0]))
   	        {
                wdr();
		        cc2420_data_recive[++j] = spi(SNOP);
		        if(j > ncont)
		            break;
   	        }
	    }
        CSn=1;
        wdr();

   	    CSn=0;
        i=spi(SFLUSHRX);
        i=spi(SFLUSHRX);
        CSn=1;
        wdr();
        return 1;
    }
    return 0;
}

void cc2420_deal(void)
{
	unsigned char szsum;
	unsigned char nloop;
	unsigned int nTemp;

	for (nloop = 0; nloop < (RECV_NO+1); nloop ++)
	{
		cc2420_data_temp[nloop] = cc2420_data_recive[nloop];
	}
	memset(cc2420_data_recive, 0x00, CC2420_MAX);
    if (cc2420_data_temp[1]==0x7e && cc2420_data_temp[2]==0x7e)
    {
	    szsum = 0x00;
	    for (nloop = 1; nloop < (RECV_NO+1); nloop ++)
	    {
		    szsum += cc2420_data_temp[nloop];
	    }
        if (szsum == 0xff)
        {
            nTemp =  (unsigned int)cc2420_data_temp[4]*256 + (unsigned int)cc2420_data_temp[3];
            if (nTemp == HOME_ID)
            {
        	    if (cc2420_data_temp[6] == 0x00 && cc2420_data_temp[5] == 0x00)
        	    {
                    if (cc2420_data_temp[13] == 0x26)
                    {
                        SET_OUT = cc2420_data_temp[14];

                        nloop = cc2420_data_temp[14] & 0x01;      //0x07
                        switch (nloop)
                        {
					        case 0:
						        if (on_ok)
						        {
                                    timer_delay(200);
                                    led_send0(0);
                                    on_ok = 0;
                                }
                                timer_delay(1000);         //500
						        RELAY1 = 0;
						        RELAY2 = 0;
						        WORK_LED = 1;    //OFF
						        break;
					        case 1:
                                timer_delay(200);        //500
						        RELAY1 = 1;
						        RELAY2 = 0;
						        if (on_ok == 0)
						        {
                                    timer_delay(1000);
                                    led_send0(1);
                                    on_ok = 1;
                                }
						        WORK_LED = 0;    //ON
						        break;
          			    }
//					    temp_deal();
                	    data_proc();
          		    }
       		    }
        	    else
        	    {
//			        WORK_LED = !WORK_LED;    //ON
                    nTemp =  (unsigned int)cc2420_data_temp[6]*256 + (unsigned int)cc2420_data_temp[5];
                    if (nTemp == MY_ID)
                    {

                        if (cc2420_data_temp[13] == 0x22)
                        {
                            SET_OUT = cc2420_data_temp[14];

                            nloop = cc2420_data_temp[14] & 0x01;  //0x07
                            switch (nloop)
                            {
					            case 0:
						            if (on_ok)
						            {
                                        timer_delay(200);
                                        led_send0(0);
                                        on_ok = 0;
                                    }
                                    timer_delay(1000);         //500
						            RELAY1 = 0;
						            RELAY2 = 0;
						            WORK_LED = 1;    //OFF
						            break;
					            case 1:
                                    timer_delay(200);        //500
						            RELAY1 = 1;
						            RELAY2 = 0;
						            if (on_ok == 0)
						            {
                                        timer_delay(1000);
                                        led_send0(1);
                                        on_ok = 1;
                                    }
						            WORK_LED = 0;    //ON
						            break;
                            }
                        }
                        else if (cc2420_data_temp[13] == 0x2a)
                        {
                            if (cc2420_data_temp[14])
                            {
                    			study_ok = 0;
   								WORK_LED = !WORK_LED;
  								timer_delay(500);
   								WORK_LED = !WORK_LED;
    							EIFR=0x0D;
    							EIMSK=0x0D;     //0x0D  //0x01
    							int_start = 0;
        						count=0;
    							int_num = 0;
         			            study_key=0;
    							my_sp_status.timeout = FALSE;
							}
							else
							{
                    			study_ok = 1;
   								WORK_LED = !WORK_LED;
  								timer_delay(500);
   								WORK_LED = !WORK_LED;
    							EIFR=0x0D;
    							EIMSK=0x01;     //0x0D  //0x01
							}
                        }

//				        temp_deal();
                        data_proc();
                    }
                }
            }
        }
    }
}

void cc2420_test(void)
{
	unsigned char szsum;
	unsigned char nloop;
	unsigned int nTemp;

	memset(cc2420_data_recive, 0x00, CC2420_MAX);
    if (cc2420_receive(RECV_NO+3))
    {
        if (cc2420_data_recive[1]==0x7e && cc2420_data_recive[2]==0x7e)
        {
	        szsum = 0x00;
	        for (nloop = 1; nloop < (RECV_NO+1); nloop ++)
	        {
		        szsum += cc2420_data_recive[nloop];
	        }
            if (szsum == 0xff)
            {
                nTemp =  (unsigned int)cc2420_data_temp[4]*256 + (unsigned int)cc2420_data_temp[3];
                if (nTemp == HOME_ID)
                {
        	        if (cc2420_data_temp[6] == 0x00 && cc2420_data_temp[5] == 0x00)
        	        {
                        if (cc2420_data_temp[13] == 0x26)
                        {
                            SET_OUT = cc2420_data_temp[14];

                            nloop = cc2420_data_temp[14] & 0x01;      //0x07
                            switch (nloop)
                            {
					            case 0:
						            if (on_ok)
						            {
                                        timer_delay(200);
                                        led_send0(0);
                                        on_ok = 0;
                                    }
                                    timer_delay(1000);         //500
						            RELAY1 = 0;
						            RELAY2 = 0;
						            WORK_LED = 1;    //OFF
						            break;
					            case 1:
                                    timer_delay(200);        //500
						            RELAY1 = 1;
						            RELAY2 = 0;
						            if (on_ok == 0)
						            {
                                        timer_delay(1000);
                                        led_send0(1);
                                        on_ok = 1;
                                    }
						            WORK_LED = 0;    //ON
						            break;
          			        }
//					        temp_deal();
                	        data_proc();
          		        }
       		        }
        	        else
        	        {
//			            WORK_LED = !WORK_LED;    //ON
                        nTemp =  (unsigned int)cc2420_data_temp[6]*256 + (unsigned int)cc2420_data_temp[5];
                        if (nTemp == MY_ID)
                        {

                            if (cc2420_data_temp[13] == 0x22)
                            {
                                SET_OUT = cc2420_data_temp[14];

                                nloop = cc2420_data_temp[14] & 0x01;  //0x07
                                switch (nloop)
                                {
					                case 0:
						                if (on_ok)
						                {
                                            timer_delay(200);
                                            led_send0(0);
                                            on_ok = 0;
                                        }
                                        timer_delay(1000);         //500
						                RELAY1 = 0;
						                RELAY2 = 0;
						                WORK_LED = 1;    //OFF
						                break;
					                case 1:
                                        timer_delay(200);        //500
						                RELAY1 = 1;
						                RELAY2 = 0;
						                if (on_ok == 0)
						                {
                                            timer_delay(1000);
                                            led_send0(1);
                                            on_ok = 1;
                                        }
						                WORK_LED = 0;    //ON
						                break;
                                }
                            }
                        	else if (cc2420_data_temp[13] == 0x2a)
                        	{
                            	if (cc2420_data_temp[14])
                            	{
                    				study_ok = 0;
   									WORK_LED = !WORK_LED;
  									timer_delay(500);
   									WORK_LED = !WORK_LED;
    								EIMSK=0x0D;     //0x0D  //0x01
    								EIFR=0x0D;
    							    int_start = 0;
        						    count=0;
    							    int_num = 0;
         			                study_key=0;
   							        my_sp_status.timeout = FALSE;
								}
								else
								{
                    				study_ok = 1;
    								EIMSK=0x01;     //0x0D  //0x01
    								EIFR=0x0D;
   									WORK_LED = !WORK_LED;
  									timer_delay(500);
   									WORK_LED = !WORK_LED;
								}
                        	}
//				            temp_deal();
                            data_proc();
                        }
                    }
                }
            }
        }
    }
}

void data_proc(void)
{
    unsigned char i;
	unsigned char szsum;
	unsigned char nloop;
	unsigned char *pTemp;
	unsigned int nTemp;


    memset(cc2420_data_send, 0x00, CC2420_MAX);

//    i = ~(PINC & 0x08);
//    i = i >> 4;
	if (IN1)
		i = 0;
	else
		i = 1;

	cc2420_data_send[0] = SEND_NO+2;

	cc2420_data_send[1] = 0x7e;
	cc2420_data_send[2] = 0x7e;

	nTemp = HOME_ID;
	pTemp = (unsigned char*)&nTemp;
	cc2420_data_send[3] = *(pTemp + 0);
	cc2420_data_send[4] = *(pTemp + 1);

	nTemp = MY_ID;
	pTemp = (unsigned char*)&nTemp;
	cc2420_data_send[5] = *(pTemp + 0);
	cc2420_data_send[6] = *(pTemp + 1);

	cc2420_data_send[7] = 0x00;
	cc2420_data_send[8] = 0x00;
	cc2420_data_send[9] = 0x00;
	cc2420_data_send[10] = 0x00;
	cc2420_data_send[11] = 0x00;
	cc2420_data_send[12] = 0x00;

	cc2420_data_send[13] = i;

	cc2420_data_send[14] = SET_OUT;

	szsum = 0x00;
	for (nloop = 1; nloop < SEND_NO; nloop ++)
	{
		szsum += cc2420_data_send[nloop];
	}
	cc2420_data_send[SEND_NO] = 0xFF - szsum;

    cc2420_send(SEND_NO+3);
}

#include <mega128.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "typedef.h"

#include "app.h"

#include "rtc.h"
#include "serialdrv.h"
#include "timer.h"
#include "intp.h"

bit int_start = 0;
unsigned int int_data[STUDY_NO];
unsigned int int_data1[STUDY_NO];
unsigned int int_data2[STUDY_NO];
unsigned char int_num = 0;

void INTP_init(void)
{
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Rising Edge
// INT1: Off
// INT2: On
// INT2 Mode: Falling Edge
// INT3: On
// INT3 Mode: Rising Edge
// INT4: Off
// INT5: Off
// INT6: Off
// INT7: Off
    EICRA=0xE3;
    EICRB=0x00;
    EIMSK=0x01;     //0x0D  //0x01
    EIFR=0x0D;
}

// External Interrupt 2 service routine
interrupt [EXT_INT2] void ext_int2_isr(void)
{
// Place your code here

    if (int_start)
    {
//    	OCR3CL=0x30;    //0x30
        int_data[int_num++] = TCNT1;
    }
    else
    {
//    	OCR3CL=0x30;    //0x30
        TCNT1 = 0;
        int_start = 1;
        int_num = 0;
		timer_set_sp();
        wdr();
	    memset(int_data, 0x00, STUDY_NO);
    }
}

// External Interrupt 3 service routine
interrupt [EXT_INT3] void ext_int3_isr(void)
{
// Place your code here

    if (int_start)
    {
//    	OCR3CL=0x00;    //0x30  //0x00
        int_data[int_num++] = TCNT1;
    }
}

⌨️ 快捷键说明

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