📄 cc1000&m8.txt
字号:
#include <mega8.h>
#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};
const unsigned char Registration_Return_table[13] = {0x87,0x65,0x43,0x21,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};
const unsigned char Self_ID[4]={0x87,0x65,0x43,0x21};
//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;
#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)&&(cc1000_table[3]==0x00))
{
Registration_table[4]=Self_ID[0];
Registration_table[5]=Self_ID[1];
Registration_table[6]=Self_ID[2];
Registration_table[7]=Self_ID[3];
key_flag = SendOnePacketToCC1000(&Registration_table[0]);
}
else if(get_key!=0x03)
{
sprintf(&cc1000_table[9],"%x%x%x",get_key,b++,c++);
cc1000_table[4]=Self_ID[0];
cc1000_table[5]=Self_ID[1];
cc1000_table[6]=Self_ID[2];
cc1000_table[7]=Self_ID[3];
key_flag = SendOnePacketToCC1000(&cc1000_table[0]);
}
}
search_counter++;
TCNT0=0x00;
}
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
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// 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=0x01;
// 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);
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(1000);
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);
}
}
delay_ms(1000);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -