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

📄 app__.c

📁 使用avr--cc2420模块程序,需要原理图的联系 QQ:120304948
💻 C
📖 第 1 页 / 共 3 页
字号:
	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
}
*/

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


#include "typedef.h"

#include "app.h"

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

TIMERTICK timer1;
TIMERTICK timer2;
TIMERTICK timer3;
TIMERTICK timer4;

TIMERTICK timer5;
TIMERTICK timer6; // sp
TIMERTICK timer7; // g20_sp
TIMERTICK timer8; // usart0

//unsigned char naaa[10];

bit b_set_sp = FALSE;
bit b_set_g20_sp = FALSE;
bit b_set_usart0 = FALSE;
bit all_sending = FALSE;
bit reset_cc2420 = FALSE;

/*
uchar CLK_MIN = 0;
uchar CLK_SEC = 0;
uchar CLK_HOUR = 0;
uchar CLK_DAY = 0;
uchar CLK_MONTH = 0;
uchar CLK_YEAR = 0;
*/
UINT call_time = 0;
//UINT free_time = 0;

void timer_set_sp()
{
    b_set_sp = TRUE;
    timer6.bUsed = TRUE;
    timer6.ltimes = SPTIMEOUT;
}

void timer_set_g20_sp()
{
    b_set_g20_sp = TRUE;
    timer7.bUsed = TRUE;
    timer7.ltimes = SPTIMEOUT;
}

void timer_set_usart0()
{
    b_set_usart0 = TRUE;
    timer8.bUsed = TRUE;
    timer8.ltimes = USART0TIMEOUT;
}

unsigned char timer_get_sp_timeout()
{
    wdr();

    if(b_set_sp == TRUE)
    {
        if(timer6.bUsed == FALSE)
        {
            b_set_sp = FALSE;
            return TRUE;
        }
        else
            return FALSE;
    }

    return FALSE;
}

unsigned char timer_get_g20_sp_timeout()
{
    wdr();

    if(b_set_g20_sp == TRUE)
    {
        if(timer7.bUsed == FALSE)
        {
            b_set_g20_sp = FALSE;
            return TRUE;
        }
        else
            return FALSE;
    }
    return FALSE;
}

unsigned char timer_get_usart0_timeout()
{
    wdr();

    if(b_set_usart0 == TRUE)
    {
        if(timer8.bUsed == FALSE)
        {
            b_set_usart0 = FALSE;
            return TRUE;
        }
        else
            return FALSE;
    }
    return FALSE;
}

void timer0_init()
{
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 115.200 kHz
// Mode: CTC top=OCR0
// OC0 output: Disconnected
    ASSR=0x00;
    TCCR0=0x0C;
    TCNT0=0x00;
    OCR0=0x73;
}

void timer2_init()
{
/*
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: 7372.800 kHz
// Mode: CTC top=OCR2
// OC2 output: Disconnected
TCCR2=0x09;
TCNT2=0x00;
OCR2=0xD0;
*/
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
    TCCR2=0x00;
    TCNT2=0x00;
    OCR2=0x00;
}

void timer1_init()
{
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 115.200 kHz
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// OC1C output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
// Compare C Match Interrupt: Off

    TCCR1A=0x00;
    TCCR1B=0x03;      //0x04
    TCNT1H=0x00;
    TCNT1L=0x00;
    ICR1H=0x00;
    ICR1L=0x00;
    OCR1AH=0x00;
    OCR1AL=0x00;
    OCR1BH=0x00;
    OCR1BL=0x00;
    OCR1CH=0x00;
    OCR1CL=0x00;
}

void timer3_initY()
{
// Timer/Counter 3 initialization
// Clock source: System Clock
// Clock value: 7372.800 kHz
// Mode: Ph. & fr. cor. PWM top=ICR3
// Noise Canceler: Off
// Input Capture on Falling Edge
// OC3A output: Discon.
// OC3B output: Discon.
// OC3C output: Non-Inv.
// Timer 3 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
// Compare C Match Interrupt: Off

    TCCR3A=0x08;
    TCCR3B=0x11;
    TCNT3H=0x00;
    TCNT3L=0x00;
    ICR3H=0x00;
    ICR3L=0x61;
    OCR3AH=0x00;
    OCR3AL=0x00;
    OCR3BH=0x00;
    OCR3BL=0x00;
    OCR3CH=0x00;
    OCR3CL=0x00;    //0x30  //0x00
}

void timer3_initN()
{
// Timer/Counter 3 initialization
// Clock source: System Clock
// Clock value: Timer 3 Stopped
// Mode: Normal top=FFFFh
// Noise Canceler: Off
// Input Capture on Falling Edge
// OC3A output: Discon.
// OC3B output: Discon.
// OC3C output: Discon.
// Timer 3 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
// Compare C Match Interrupt: Off
    TCCR3A=0x00;
    TCCR3B=0x00;
    TCNT3H=0x00;
    TCNT3L=0x00;
    ICR3H=0x00;
    ICR3L=0x00;
    OCR3AH=0x00;
    OCR3AL=0x00;
    OCR3BH=0x00;
    OCR3BL=0x00;
    OCR3CH=0x00;
    OCR3CL=0x00;
}

void timer_init()
{
    timer0_init();
    timer1_init();
    timer2_init();
    timer3_initN();

// Timer(s)/Counter(s) Interrupt(s) initialization
    TIMSK=0x02;
    ETIMSK=0x00;

	timer1.bUsed = FALSE;
	timer2.bUsed = FALSE;
	timer3.bUsed = FALSE;
	timer4.bUsed = FALSE;
	timer5.bUsed = FALSE;
	timer6.bUsed = FALSE;
	timer7.bUsed = FALSE;
	timer8.bUsed = FALSE;
}

TIMERTICK *  timer_settimer(DWORD ltimes)
{
    wdr();

    if(timer1.bUsed != TRUE)
    {
        timer1.bUsed = TRUE;
        timer1.ltimes = ltimes;
        return &timer1;
    }

    if(timer2.bUsed != TRUE)
    {
        timer2.bUsed = TRUE;
        timer2.ltimes = ltimes;
        return &timer2;
    }

    if(timer3.bUsed != TRUE)
    {
        timer3.bUsed = TRUE;
        timer3.ltimes = ltimes;
        return &timer3;
    }
    if(timer4.bUsed != TRUE)
    {
        timer4.bUsed = TRUE;
        timer4.ltimes = ltimes;
        return &timer4;
    }

    return NULL;
}

unsigned char timer_checktimeout(TIMERTICK * ptimer)
{
    wdr();

    if(ptimer == NULL)
        return TRUE;

    if(ptimer->bUsed != TRUE)
        return TRUE;

    if(ptimer->ltimes <=0)
        return TRUE;
    else
        return FALSE;
}

void timer_delay(DWORD ltimes)
{
	timer5.bUsed = TRUE;
	timer5.ltimes = ltimes;

    wdr();
	while(timer_checktimeout(&timer5) != TRUE);
    wdr();
}

// Timer 2 output compare interrupt service routine
interrupt [TIM2_COMP] void timer2_comp_isr(void)
{
// Place your code here

//	call_time++;
}

// Timer 0 output compare interrupt service routine
interrupt [TIM0_COMP] void timer0_comp_isr(void)
{
/*	uchar tempsec = 0;
	uchar tempmin = 0;
	uchar temphour = 0;
	uchar tempday = 0;
	uchar tempmonth = 0;
	uchar tempyear = 0;  */

	if(timer1.bUsed == TRUE){
		if(timer1.ltimes >0)
			timer1.ltimes --;
		else
			timer1.bUsed = FALSE;}

	if(timer2.bUsed == TRUE){
		if(timer2.ltimes >0)
			timer2.ltimes --;
		else
			timer2.bUsed = FALSE;}

	if(timer3.bUsed == TRUE){
		if(timer3.ltimes >0)
			timer3.ltimes --;
		else
			timer3.bUsed = FALSE;}

	if(timer4.bUsed == TRUE){
		if(timer4.ltimes >0)
			timer4.ltimes --;
		else
			timer4.bUsed = FALSE;}

	if(timer5.bUsed == TRUE){
		if(timer5.ltimes >0)
			timer5.ltimes --;
		else
			timer5.bUsed = FALSE;}

	if(timer6.bUsed == TRUE){
	    if(timer6.ltimes > 0)
	        timer6.ltimes --;
	    else
	        timer6.bUsed = FALSE;}

	if(timer7.bUsed == TRUE){
	    if(timer7.ltimes > 0)
	        timer7.ltimes --;
	    else
	        timer7.bUsed = FALSE;}

	if(timer8.bUsed == TRUE){
	    if(timer8.ltimes > 0)
	        timer8.ltimes --;
	    else
	        timer8.bUsed = FALSE;}

	if (call_time++ >= CALL_TIMER)     //60000
    {
    	call_time = 0;
    	all_sending = TRUE;
//		WORK_LED = !WORK_LED;
/*		if (free_time++ >= 10)    //120
		{
    		free_time = 0;
			reset_cc2420 = TRUE;
		}*/
    }

/*
	if (free_time++ > 100)    //3000
	{
	    free_time = 0;

	    if(system_initial_flag  != TRUE)
		    return;

	    if(rtc_set_flag == TRUE)
	        return;

	    v_Get1302(naaa);

	    tempsec = CLK_SEC;
	    tempmin = CLK_MIN;
	    temphour = CLK_HOUR;
	    tempday = CLK_DAY;
	    tempmonth = CLK_MONTH;
	    tempyear = CLK_YEAR;

	    CLK_SEC = bcd2hex(naaa[0]);
	    CLK_MIN = bcd2hex(naaa[1]);
	    CLK_HOUR = bcd2hex(naaa[2]);
	    CLK_DAY = bcd2hex(naaa[3]);
	    CLK_MONTH = bcd2hex(naaa[4]);
	    CLK_YEAR = bcd2hex(naaa[6]);

    }*/
}

/* bcd to hex */

unsigned int bcd2hex(BYTE bdata)
{
    unsigned int tmp;
//    tmp = (((unsigned int)(bdata >> 4) * 10) + (unsigned int)(bdata & 0x0f));
    tmp = (unsigned int)bdata / 16 * 10 + (unsigned int)bdata % 16;
    return tmp;
}
#include <mega128.h>
#include <spi.h>
#include <delay.h>

#include "typedef.h"
#include "app.h"
#include "cc2420.h"
#include "serialdrv.h"
#include "18B20.h"

bit receive_ok = 0;
bit on_ok = 0;

unsigned char cc2420_data_send[CC2420_MAX];
unsigned char cc2420_data_recive[CC2420_MAX];
unsigned char cc2420_data_temp[CC2420_MAX];

#pragma warn-
eeprom unsigned char SET_OUT;
#pragma warn+

unsigned int HOME_ID;
unsigned int MY_ID;
unsigned int CALL_TIMER;

// SPI functions

// SPI interrupt service routine
/*interrupt [SPI_STC] void spi_isr(void)
{
unsigned char data;
data=SPDR;
// Place your code here

}*/

void myid_no(unsigned char value)
{

    HOME_ID = 10001;
	MY_ID = 18180 + value;
    CALL_TIMER = 60000 + value;        //60000
}

void cc2420_initial(void)
{
    unsigned int i;

    myid_no(NAME_NO);

	if (SET_OUT > 1)        //8
	    SET_OUT = 0;

	if (SET_OUT)
	{
        RELAY1 = 1;
		RELAY2 = 0;
        delay_ms(1000);
        wdr();
        led_send0(1);
		WORK_LED = 0;    //ON
		on_ok = 1;
	}
	else
	{
	    RELAY1 = 0;
		RELAY2 = 0;
//        delay_ms(1000);
        wdr();
//        led_send0(0);
		WORK_LED = 1;    //OFF
	}

// CC2420 initialization

⌨️ 快捷键说明

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