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

📄 cc1000_mai__.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 "delay.h"
#include "cc1000.h"
#include "rf1000.h"
//#include "stdio.h"

#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7


#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
#define BEE  PORTD.3

// USART Receiver buffer
#define RX_BUFFER_SIZE 16
char rx_buffer[RX_BUFFER_SIZE];

unsigned char Registration_table[13] = {0x12,0x34,0x56,0x78,0x00,0x00,0x00,0x00,'A',0x00,0x00,0x00};
unsigned char cc1000_table[13] = {0x00,0x00,0x00,0x00,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] = {0x44,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]={0x44,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;

#if RX_BUFFER_SIZE<256
unsigned char rx_wr_index,rx_rd_index,rx_counter;
#else
unsigned int rx_wr_index,rx_rd_index,rx_counter;
#endif

// This flag is set on USART Receiver buffer overflow
bit rx_buffer_overflow;

// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void)
{
char status,data;
status=UCSRA;
data=UDR;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
   {
   rx_buffer[rx_wr_index]=data;
   if (++rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
   if (++rx_counter == RX_BUFFER_SIZE)
      {
      rx_counter=0;
      rx_buffer_overflow=1;
      };
   };
}

#ifndef _DEBUG_TERMINAL_IO_
// Get a character from the USART Receiver buffer
#define _ALTERNATE_GETCHAR_
#pragma used+
char getchar(void)
{
char data;
while (rx_counter==0);
data=rx_buffer[rx_rd_index];
if (++rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
#asm("cli")
--rx_counter;
#asm("sei")
return data;
}
#pragma used-
#endif

// Standard Input/Output functions
#include <stdio.h>

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

 if(search_counter==5)
 {
   search_counter = 0;
  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]=17;
      cc1000_table[11]=c++;
      key_flag = SendOnePacketToCC1000(cc1000_table);
     }
 }
 search_counter++;
   TCNT0=0x00;
}
// Timer 1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Place your code here
   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==7)lock_key=1;
        if(long_counter==12)
           {

             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;
           }


   TCNT1H=0xFE;
   TCNT1L=0x18;

}

void bell(unsigned char abc)
{
 for( ;abc>0;abc--)
      {
          DDRD |=0x08;
          BEE = 1;
          delay_ms(150);
          BEE =0;
          delay_ms(300);
      }
 }

// Declare your global variables here

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

// Input/Output Ports initialization
// Port B initialization
// Func7=Out Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=Out
// State7=0 State6=T State5=T State4=T State3=T State2=T State1=T State0=1
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=Out Func6=Out Func5=Out Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=1 State6=1 State5=0 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: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B 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

 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;

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 4800
UCSRA=0x00;
UCSRB=0x98;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x0C;

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

      // Place your code here
       #asm("sei")
     if(InitialCC1000())
        bell(2);
// DS1302 Real Time Clock initialization
// Trickle charger: On
// Trickle charge resistor: 2K
// Trickle charge diode(s): 1
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[0]=string[4];
                     cc1000_table[1]=string[5];
                     cc1000_table[2]=string[6];
                     cc1000_table[3]=string[7];
                    // sprintf(&cc1000_table[0],"%x%x%x%x",string[4],string[5],string[6],string[7]);
                     bell(1);
                    }
              }
             // else if(string[8] == cc1000_SetTime_show[8])
             //       key_flag = SendOnePacketToCC1000(cc1000_SetTime_show);
                delay_ms(300);
      }
}

⌨️ 快捷键说明

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