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

📄 cc1000_m8__.c

📁 CC1000的应用与MEGA8,无线双工通信.
💻 C
字号:
#include <mega8.h>
	#ifndef __SLEEP_DEFINED__
	#define __SLEEP_DEFINED__
	.EQU __se_bit=0x80
	.EQU __sm_mask=0x70
	.EQU __sm_powerdown=0x20
	.EQU __sm_powersave=0x30
	.EQU __sm_standby=0x60
	.EQU __sm_ext_standby=0x70
	.EQU __sm_adc_noise_red=0x10
	.SET power_ctrl_reg=mcucr
	#endif
#include <init_M8.h>
#include "delay.h"
#include "cc1000.h"
#include "rf1000.h"

unsigned char Registration_table[13] = {0x12,0x34,0x56,0x78,0x00,0x00,0x00,0x00,'A',0x00,0x00,0x00};
unsigned char cc1000_table[13] = {0x12,0x34,0x56,0x78,0x00,0x00,0x00,0x00,'B',0x00,0x00,0x00};
unsigned char cc1000_SetTime_show[13]={0x12,0x34,0x56,0x78,0x00,0x44,0x55,0x55,'C',0x00,10,14,0x00};
unsigned char Registration_Return_table[13] = {0x00,0x44,0x55,0x55,0x12,0x34,0x56,0x78,'a',0x00,0x00,0x00};
const unsigned char get_cc1000_table[13] = {0x87,0x65,0x43,0x21,0x12,0x34,0x56,0x78,'b',0x00,0x00,0x00};
unsigned char Self_ID[4]={0x00,0x44,0x55,0x55};
//unsigned char compare[] =  {0};
unsigned char  string[18] = {0};
unsigned char search_counter = 0 ;
unsigned char long_key_counter = 0;
unsigned char key_flag = 0;
unsigned char old_key = 0x03;
unsigned char get_key = 0;
//unsigned char Registe_flag = 0;
unsigned char b=0,c=0;
unsigned char long_counter =0;
unsigned char old_long_key =0x03;
unsigned char temp_key =0;
unsigned char get_long_key =0;
unsigned char lock_key=0;
unsigned char temp_long_key=0x03;
//unsigned char hour,min,sec;

// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Place your code here

   DDRD &=0xFC;
   get_key = PIND&0x03;

    if((get_key!=0x03)&&(lock_key==0))
     {
      //sprintf(&cc1000_table[9],"%x",get_key);
      cc1000_table[4]=Self_ID[0];
      cc1000_table[5]=Self_ID[1];
      cc1000_table[6]=Self_ID[2];
      cc1000_table[7]=Self_ID[3];
      cc1000_table[9]=get_key;
      cc1000_table[10]=16;
      cc1000_table[11]=c++;
      key_flag = SendOnePacketToCC1000(cc1000_table);
     }

}

// Timer 1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Place your code here
   TCNT1H = 0xFE; //setup
   TCNT1L = 0x18;
   Registrion();
}

// Declare your global variables here
void main(void)
{
// Declare your local variables here
 unsigned char i = 0,j = 0;

  init_m8();
while (1)
      {
      // Place your code here
       #asm("WDR")
        while(key_flag==0) ;
             key_flag = 0;
             bell(1);
           while(string!=ReceiveOnePacketFromCC1000(string,0));
             bell(1);
             delay_ms(500);
             if(string[8] == Registration_Return_table[8])
              {
                for(i=0;i<4;i++)
                 {
                  if(string[i] != Registration_Return_table[i])
                  break;
                 }
                  if(i==4)
                    {
                     for(i=0;i<4;i++)
                     cc1000_table[i]=string[4+i];
                     bell(1);
                    }
              }
             else if(string[8] == 'C')
                   {
                      delay_ms(200);
                      key_flag =  SendOnePacketToCC1000(cc1000_table);
                   }
                delay_ms(300);
      };
}
#define BEE  PORTD.3
void init_m8(void)
{

// Input/Output Ports initialization
// Port B 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
PORTB=0x07;
DDRB=0x81;

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

// Port D 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
PORTD=0xC0;
DDRD=0xE0;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 0.977 kHz
TCCR0=0x05;
TCNT0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 0.977 kHz
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
 TCCR1B = 0x00; //stop
 TCNT1H = 0xFE; //setup
 TCNT1L = 0x18;
 OCR1AH = 0x01;
 OCR1AL = 0xE8;
 OCR1BH = 0x01;
 OCR1BL = 0xE8;
 ICR1H  = 0x01;
 ICR1L  = 0xE8;
 TCCR1A = 0x00;
 TCCR1B = 0x05; //start Timer

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
MCUCR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x05;

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

// Global enable interrupts
#asm("sei")
 if(InitialCC1000())
        bell(2);
}
void bell(unsigned char abc)
{
 for( ;abc>0;abc--)
      {
          DDRD |=0x08;
          BEE = 1;
          delay_ms(200);
          BEE =0;
          delay_ms(150);
      }
 }
 void Registrion(void)
 {
   DDRD &=0xFC;
   get_long_key = PIND&0x03;
   temp_key=get_long_key;
   temp_long_key=(old_long_key<<4);
   get_long_key = get_long_key+temp_long_key;
   switch(get_long_key)
        {
           case 0x33: break;
	case 0x30:
	case 0x31:
	case 0x32: old_long_key=temp_key;
		break;
	case 0x03:
	case 0x13:
	case 0x23:
		break;
	case 0x00:
	case 0x11:
           case 0x22: long_counter++;
	           break;
	default :  long_counter=0;
	           old_long_key=temp_key;
	           break;
        }
        if(long_counter==2)lock_key=1;
        if(long_counter==8)
           {
             old_long_key=0x03;
             long_counter = 0;
             Registration_table[4]=temp_key;
             Self_ID[0]=temp_key;
             Registration_table[5]=Self_ID[1];
             Registration_table[6]=Self_ID[2];
             Registration_table[7]=Self_ID[3];
             Registration_Return_table[0]=Self_ID[0];
             Registration_Return_table[1]=Self_ID[1];
             Registration_Return_table[2]=Self_ID[2];
             Registration_Return_table[3]=Self_ID[3];
             key_flag = SendOnePacketToCC1000(&Registration_table[0]);
             lock_key=0;
           }
 }

⌨️ 快捷键说明

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