من برای رهایی از مشکل بودن کار با spi قصد ندارم spi را به سریال تبدیل کنم بلکه دستگاهی ساخته شده که من از محتوای برنامه آن هیچ اطلاعی ندارم فقط می دانم که سرعت بین 100 بیت بر ثانیه تا 20 کیلو بیت بر ثانیه متغیر است و من میخواهم این سیستم را بدون تغییر بی سیم کنم همین
اما برای تهیه ماژول یک سری به جمهوری بزنید(فروشگاه جهان).برای استفاده حتما نیاز به دیتا شیت RF01 و RF02 دارید یکی از دوستان لطف کرده بود و برنامه برای انتقال دیتا نوشته بود که من با اجازه ایشون اینجا براتون می گذارم
Connections
ATMEGA8 SIDE RF12 SIDE
SCK--------------->SCK
MISO--------------->SDO
MOSI--------------->SDI
SS--------------->nSEL
INT0<---------------nIRQ
PD6: LED GREEN
PD7: LED RED
*/
#include <mega8.h>
#include <stdlib.h>
#include <stdio.h>
#include <delay.h>
#define DDR_IN 0
#define DDR_OUT 1
#define PORT_SEL PORTB
#define PIN_SEL PINB
#define DDR_SEL DDRB
#define PORT_SDI PORTB
#define PIN_SDI PINB
#define DDR_SDI DDRB
#define PORT_SCK PORTB
#define PIN_SCK PINB
#define DDR_SCK DDRB
#define PORT_SDO PORTB
#define PIN_SDO PINB
#define DDR_SDO DDRB
#define PORT_IRQ PORTD
#define PIN_IRQ PIND
#define DDR_IRQ DDRD
#define PORT_DATA PORTD
#define PIN_DATA PIND
#define DDR_DATA DDRD
#define PB7 7//--\
#define PB6 6// |
#define RFXX_SCK 5// |
#define RFXX_SDO 4// |RF_PORT
#define RFXX_SDI 3// |
#define RFXX_SEL 2// |
#define NC 1// |
#define PB0 0//--/
#define SEL_OUTPUT() DDR_SEL |= (1<<RFXX_SEL)
#define HI_SEL() PORT_SEL|= (1<<RFXX_SEL)
#define LOW_SEL() PORT_SEL&=~(1<<RFXX_SEL)
#define SDI_OUTPUT() DDR_SDI |= (1<<RFXX_SDI)
#define HI_SDI() PORT_SDI|= (1<<RFXX_SDI)
#define LOW_SDI() PORT_SDI&=~(1<<RFXX_SDI)
#define SDO_INPUT() DDR_SDO&= ~(1<<RFXX_SDO)
#define LOW_SDO() PORT_SDO&= (1<<RFXX_SDO)
#define SDO_HI() PIN_SDO&(1<<RFXX_SDO)
#define SCK_OUTPUT() DDR_SCK |= (1<<RFXX_SCK)
#define HI_SCK() PORT_SCK|= (1<<RFXX_SCK)
#define LOW_SCK() PORT_SCK&=~(1<<RFXX_SCK)
////////////////////////////////
#define RF12_IRQ 2
#define IRQ_IN() DDR_IRQ &=~(1<<RF12_IRQ)
#define WAIT_IRQ_LOW() while(PIND&(1<<RF12_IRQ))
//////////////////////////////
#define RF12_DATA 4//PD4
#define DATA_OUT() DDR_DATA|=1<<RF12_DATA
#define HI_DATA() PORT_DATA|=1<<RF12_DATA
#define LEDG_OUTPUT() DDRD|=~(1<<6)
#define LEDR_OUTPUT() DDRD|=~(1<<7)
#define LEDG_ON() PORTD&=~(1<<6)
#define LEDG_OFF() PORTD|= (1<<6)
#define LEDR_ON() PORTD&=~(1<<7)
#define LEDR_OFF() PORTD|= (1<<7)
float T1,T2;
bit edge=0;
char ChCurrentTemp=0;
// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
// Place your code here
if (edge)
{
T2=TCNT1;
TCNT1=0;
// INT0 Mode: Falling Edge
MCUCR=0x02;
edge=0;
}else
{
T1=TCNT1;
TCNT1=0;
// INT0 Mode: Rising Edge
MCUCR=0x03;
edge=1;
}
}
#define ADC_VREF_TYPE 0x40
// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input|ADC_VREF_TYPE;
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}
///////////////////////////////////////////////////
void Temperature(void)
{
char strTemp[]="000";
float tf;
tf=read_adc(0); // LM35 Sensor is connected to PORTA.0(PIN 23)
tf=(tf*500/1023);
ftoa(tf,0,strTemp);
ChCurrentTemp=atoi(strTemp); // Convert String To Integer */
/* float dc;
float t;
char strT[15],strTemp[]="000";
dc=T1/(T1+T2);
t=(dc-0.32)/0.0047;
ftoa(t,0,strTemp);
// sprintf(strT,"Temp:%s\XDFC ",strTemp);
// lcd_gotoxy(0,0);
// lcd_puts(strT);
ChCurrentTemp=atoi(strTemp); // Convert String To Integer */
}
//////////////////////////////////////////////////////////////////////
void Gas_Sensor(void)
{
char strr[7],lcd[7],StrGasPercent[10];
float tf;
tf=read_adc(0); // Gas Sensor is connected to PORTA.0(pin 40)
tf=(tf*500/1023);
ftoa(tf,2,strr);
// FltCurrentGas=atoi(strr);
// eepFltGasMedium=(eepIntGasHigh-eepFltGasLow)/100;
// eepFltGasPercent=(FltCurrentGas-eepFltGasLow)/eepFltGasMedium;
// itoa(eepFltGasPercent,StrGasPercent); // Convert int To String
// sprintf(lcd,"Gas:%s%% ",StrGasPercent);
// lcd_gotoxy(0,1);
// lcd_puts(lcd);
}
/////////////////////////////////////////////////////////////////////
// send //
//_________________________________________________________________//
void RFsend_PORT_INIT(void){
HI_SEL();
HI_SDI();
LOW_SCK();
SEL_OUTPUT();
SDI_OUTPUT();
SDO_INPUT();
SCK_OUTPUT();
}
unsigned int RFsend_WRT_CMD(unsigned int aCmd){
unsigned char i;
unsigned int temp;
LOW_SCK();
LOW_SEL();
for(i=0;i<16;i++){
temp<<=1;
if(SDO_HI()){
temp|=0x0001;
}
LOW_SCK();
if(aCmd&0x8000){
HI_SDI();
}else{
LOW_SDI();
}
HI_SCK();
aCmd<<=1;
};
LOW_SCK();
HI_SEL();
return(temp);
}
void RF12send_INIT(void){
RFsend_WRT_CMD(0x80D7);//EL,EF,433band,12.0pF
RFsend_WRT_CMD(0x8239);//!er,!ebb,ET,ES,EX,!eb,!ew,DC
RFsend_WRT_CMD(0xA640);//A140=430.8MHz
RFsend_WRT_CMD(0xC647);//19.2kbps
RFsend_WRT_CMD(0x94A0);//VDI,FAST,134kHz,0dBm,-103dBm
RFsend_WRT_CMD(0xC2AC);//AL,!ml,DIG,DQD4
RFsend_WRT_CMD(0xCA81);//FIFO8,SYNC,!ff,DR
RFsend_WRT_CMD(0xC483);//@PWR,NO RSTRIC,!st,!fi,OE,EN
RFsend_WRT_CMD(0x9850);//!mp,9810=30kHz,MAX OUT
RFsend_WRT_CMD(0xE000);//NOT USE
RFsend_WRT_CMD(0xC800);//NOT USE
RFsend_WRT_CMD(0xC400);//1.66MHz,2.2V
}
void RF12_SEND(unsigned char aByte){
while(PIND&(1<<2));//wait for previously TX over
RFsend_WRT_CMD(0xB800+aByte);
}
//void Delay_ms(unsigned char amS){
// unsigned char i;
// unsigned int j;
// for(i=0;i<amS;i++)for(j=0;j<914;j++);
//}
//_________________________________________________________________//
// send //
/////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
// Recieve //
//_________________________________________________________________//
void RFrecieve_PORT_INIT(void){
HI_SEL();
HI_SDI();
LOW_SCK();
//SET nFFS pin HI when using FIFO
HI_DATA();
SEL_OUTPUT();
SDI_OUTPUT();
SDO_INPUT();
SCK_OUTPUT();
IRQ_IN();
DATA_OUT();
}
unsigned int RFrecieve_WRT_CMD(unsigned int aCmd){
unsigned char i;
unsigned int temp;
temp=0;
LOW_SCK();
LOW_SEL();
for(i=0;i<16;i++){
if(aCmd&0x8000){
HI_SDI();
}else{
LOW_SDI();
}
HI_SCK();
temp<<=1;
if(SDO_HI()){
temp|=0x0001;
}
LOW_SCK();
aCmd<<=1;
};
HI_SEL();
return(temp);
}
void RF12recieve_INIT(void){
RFrecieve_WRT_CMD(0x80D7);//EL,EF,433band,11.5pF
RFrecieve_WRT_CMD(0x82D9);//!er,!ebb,ET,ES,EX,!eb,!ew,DC
RFrecieve_WRT_CMD(0xA640);//434MHz
RFrecieve_WRT_CMD(0xC647);//4.8kbps
RFrecieve_WRT_CMD(0x94A0);//VDI,FAST,134kHz,0dBm,-103dBm
RFrecieve_WRT_CMD(0xC2AC);//AL,!ml,DIG,DQD4
RFrecieve_WRT_CMD(0xCA81);//FIFO8,SYNC,!ff,DR
RFrecieve_WRT_CMD(0xC483);//@PWR,NO RSTRIC,!st,!fi,OE,EN
RFrecieve_WRT_CMD(0x9850);//!mp,9810=30kHz,MAX OUT
RFrecieve_WRT_CMD(0xE000);//NOT USE
RFrecieve_WRT_CMD(0xC800);//NOT USE
RFrecieve_WRT_CMD(0xC400);//1.66MHz,2.2V
}
unsigned char RF12_RECV(void){
unsigned int FIFO_data;
WAIT_IRQ_LOW();
RFrecieve_WRT_CMD(0x0000);
FIFO_data=RFrecieve_WRT_CMD(0xB000);
return(FIFO_data&0x00FF);
}
//_________________________________________________________________//
// Recieve //
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
void DataRecieve(void)
{
unsigned char i;
unsigned char ChkSum;
//Initialize command port
RFrecieve_PORT_INIT();
//Initialize RF12 chip
RF12recieve_INIT();
//Init FIFO
RFrecieve_WRT_CMD(0xCA81);
// LCD module initialization
//Enable FIFO
RFrecieve_WRT_CMD(0xCA83);
ChkSum=0;
//Receive payload data
//for(i=0;i<16;i++){
// ChkSum+=RF12_RECV();
// }
//Receive Check sum
i=RF12_RECV();
//Disable FIFO
RFrecieve_WRT_CMD(0xCA81);
delay_ms(15);
printf("%2i",i);
// LEDG_ON();
// delay_ms(250);
// LEDG_OFF();
//Package chkeck
// if(i=='T')
// {
// printf("%2i",i);
// DataSend();
// LEDG_ON();
// delay_ms(250);
// LEDG_OFF();
// }
// itoa(ChkSum, st);
// lcd_gotoxy(0,0);
// lcd_puts(st);
}
/////////////////////////////////////////////////////////
void DataSend(void)
{
unsigned int i,j;
unsigned char ChkSum;
unsigned char DeviceA='A';
RFsend_PORT_INIT();
RF12send_INIT();
DDRD|=(1<<RF12_DATA);
PORTD|=(1<<RF12_DATA);// SET nFFS pin HI when using TX register
DDRD&=~(1<<2); //PD2(INT0)
LEDR_ON();
RFsend_WRT_CMD(0x0000);//read status register
RFsend_WRT_CMD(0x8239);//!er,!ebb,ET,ES,EX,!eb,!ew,DC
ChkSum=0;
RF12_SEND(0xAA);//PREAMBLE
RF12_SEND(0xAA);//PREAMBLE
RF12_SEND(0xAA);//PREAMBLE
RF12_SEND(0x2D);//SYNC HI BYTE
RF12_SEND(0xD4);//SYNC LOW BYTE
/* RF12_SEND(0x30);//DATA BYTE 0
ChkSum+=0x30;
RF12_SEND(0x31);//DATA BYTE 1
ChkSum+=0x31;
RF12_SEND(0x32);
ChkSum+=0x32;
RF12_SEND(0x33);
ChkSum+=0x33;
RF12_SEND(0x34);
ChkSum+=0x34;
RF12_SEND(0x35);
ChkSum+=0x35;
RF12_SEND(0x36);
ChkSum+=0x36;
RF12_SEND(0x37);
ChkSum+=0x37;
RF12_SEND(0x38);
ChkSum+=0x38;
RF12_SEND(0x39);
ChkSum+=0x39;
RF12_SEND(0x3A);
ChkSum+=0x3A;
RF12_SEND(0x3B);
ChkSum+=0x3B;
RF12_SEND(0x3C);
ChkSum+=0x3C;
RF12_SEND(0x3D);
ChkSum+=0x3D;
RF12_SEND(0x3E);
ChkSum+=0x3E;
RF12_SEND(0x3F); //DATA BYTE 15
ChkSum+=0x3F; */
RF12_SEND(DeviceA);
RF12_SEND(0xAA);//DUMMY BYTE
RF12_SEND(0xAA);//DUMMY BYTE
RF12_SEND(0xAA);//DUMMY BYTE
RFsend_WRT_CMD(0x8201);
delay_ms(20);
DataRecieve();
// LEDR_OFF();
LEDR_OFF();
// delay_ms(1000);
// for(i=0;i<10000;i++)for(j=0;j<123;j++);//sleep 1 second appr.
}
//////////////////////////////////////////////////////////////
void main(void)
{
unsigned char i=0;
int IntCounter=0;
// Global enable interrupts
#asm("cli");
// 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=0x00;
DDRB=0x00;
// 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=0x00;
DDRD=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: On
// INT0 Mode: Falling Edge
// INT1: Off
// INT2: Off
GICR|=0x40;
MCUCR=0x02;
MCUCSR=0x00;
GIFR=0x40;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 1200
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x02;
UBRRL=0x08;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
// Analog Comparator Output: Off
ACSR=0x80;
SFIOR=0x00;
// ADC initialization
// ADC Clock frequency: 125.000 kHz
// ADC Voltage Reference: AVCC pin
// ADC High Speed Mode: Off
// ADC Auto Trigger Source: None
ADMUX=ADC_VREF_TYPE;
ADCSRA=0x86;
SFIOR&=0xEF;
//POWER ON indication: both LEDR and LEDG blink 3 times
LEDG_OFF();
LEDR_OFF();
LEDG_OUTPUT();
LEDR_OUTPUT();
for(i=0;i<3;i++)
{
delay_ms(200);
LEDG_ON();
LEDR_ON();
delay_ms(200);
LEDG_OFF();
LEDR_OFF();
}
LEDG_OFF();
LEDR_OFF();
while(1)
{
// delay_ms(1);
// IntCounter++;
// printf("%i",num);
// delay_ms(500);
// Delay_ms(1);
// intCounter++;
// Temperature();
// if (IntCounter==500)
// {
// IntCounter=0;
// DataSend();
// }
DataSend();
// DataRecieve();
};
مرجع:
http://www.soomael.blogfa.com/