Tiếp theo , ta ứng dụng PIC trong việc thu nhận sóng RF , điều khiển Rb từ xa
mạch phát :
Code:
#include <16F88.h>
#device 16F88*=16 ADC=8
#fuses XT,NOWDT,NOPROTECT,NOLVP,NOPUT,NOBROWNOUT,NODEBUG
#use delay(clock=4000000)
#use fast_io(b)
#define AN0 0b10110111
#define AN1 0b01010100
#define AN2 0b01001000
#define AN3 0b10001011
#define Av 0b01011010
#define Bv 0b11001101
#define stop 0b10100101
#define Cv 0b00110010
#define Dv 0b10010110
#bit TX =0x06.5 // RB5
// 24v = 0101 1010 , AN0= 1011 0111
// 12v = 1100 1101 , AN1= 0101 0100
// stop = 1010 0101 , AN2= 0100 1000
// -12v = 0011 0010 , AN3= 1010 1011
// -24v = 1001 0110
int8 adc0,adc1,adc2,adc3;
unsigned char buff[2]; // dia chi & data
void readADC()
{ setup_adc(adc_clock_internal);
setup_adc_ports(sAN0);
set_adc_channel(0);
adc0=read_adc();delay_us(20);
setup_adc_ports(sAN1);
set_adc_channel(1);
adc1=read_adc();delay_us(20);
setup_adc_ports(sAN2);
set_adc_channel(2);
adc2=read_adc();delay_us(20);
setup_adc_ports(sAN3);
set_adc_channel(3);
adc3=read_adc();delay_us(20);
}
void send_data(unsigned char *data)
{ unsigned char i,j;
for (i=0;i<10;i++) // send preamble 16 times
{TX=1;
delay_us(4890); // 5 mS
TX=0;
delay_us(4890); // 5 mS
};
delay_us(14738); // Delay 15000 uS // syn bit
//-----------------
TX=1; // start bit
delay_us(4890); // 5 mS
TX=0;
delay_us(4890); // 5 mS
//-----------------
for (j=0;j<2;j++)
{ for(i=0;i<8;i++)
{if((data[j]&0x80)==0x80)
{TX=1;delay_us(4890); // 5 mS
TX=0;delay_us(4890); // 5 mS
}
else{TX=0;delay_us(4890); // 5 mS
TX=1;delay_us(4890); // 5 mS
};
data[j]=data[j]<<1;
};
};
//------ send stop bit -------
TX=1;delay_us(4890); // 5 mS
TX=0;delay_us(4890); // 5 mS
}
void main()
{ set_tris_B(0x00);RB1=0;TX=0;delay_ms(500);RB1=1;
while(1)
{ readADC();RB1=1;
if(((0<=adc0)&&(adc0<40))&&((90<adc1&&adc1<140))){RB1=0;buff[0]=AN0;buff[1]=Av;send_data(buff);};//toi 24V
if(((40<=adc0)&&(adc0<=90))&&((90<adc1&&adc1<140))){RB1=0;buff[0]=AN0;buff[1]=Bv;send_data(buff);};//toi 12V
if(((150<=adc0)&&(adc0<=185))&&((90<adc1&&adc1<140))){RB1=0;buff[0]=AN0;buff[1]=Cv;send_data(buff);};//lui 12V
if(((185<adc0)&&(adc0<255))&&((90<adc1)&&(adc1<140))){RB1=0;buff[0]=AN0;buff[1]=Dv;send_data(buff);};//24V
if((adc1<40)&&(100<adc0&&adc0<170)){RB1=0;buff[0]=AN1;buff[1]=Bv;send_data(buff);};//phai 2banh12V
if((40<=adc1)&&(adc1<=100)&&(100<adc0&&adc0<170)){RB1=0;buff[0]=AN1;buff[1]=Av;send_data(buff);};//phai 1 banh24V
if((145<=adc1)&&(adc1<=180)&&(100<adc0&&adc0<170)){RB1=0;buff[0]=AN1;buff[1]=Dv;send_data(buff);};//trai 1 banh24V
if((180<adc1)&&(adc1<=255)&&(100<adc0&&adc0<170)){RB1=0;buff[0]=AN1;buff[1]=Cv;send_data(buff);};//trai 2banh12V
if(((0<=adc2)&&(adc2<50))&&((90<adc3)&&(adc3<140))){RB1=0;buff[0]=AN2;buff[1]=Bv;send_data(buff);};
if(((180<=adc2)&&(adc2<=255))&&((90<adc3)&&(adc3<140))){RB1=0;buff[0]=AN2;buff[1]=Cv;send_data(buff);};
if(((0<=adc3)&&(adc3<70))&&((100<adc2)&&(adc2<170))){RB1=0;buff[0]=AN3;buff[1]=Bv;send_data(buff);};
if((160<=adc3)&&(adc3<=255)&&((100<=adc2)&&(adc2<=170)))
{RB1=0;buff[0]=AN3;buff[1]=Cv;send_data(buff);};
}
}
Mạch thu:
Code:
#include <18F4331.h>
#fuses
XT,NOWDT,NOPROTECT,NOLVP,NOPUT,NOBROWNOUT,NOCPD,NODEBUG,NOFCMEN,NOCPB,HPOL_LOW,NOPW
MPIN,STVREN
#use delay(clock=4000000)
#use fast_io(a)
#use fast_io(b)
#use fast_io(c)
#use fast_io(d)
#use fast_io(e)
#bit RX =0xF83.0 // RD0
#bit RL1 =0xF81.4 // RB4
#bit RL2 =0xF81.5 // RB5
#bit RL3 =0xF81.0 // RB0
#bit RL4 =0xF81.2 // RB2
#bit RL5 =0xF83.7 // RD7
#bit RL6 =0xF83.6 // RD6
#bit RL7 =0xF83.5 // RD5
#bit PWM3 =0xF81.1 // RB1
#bit PWM4 =0xF81.3 // RB3
#define AN0 0b10110111
#define AN1 0b01010100
#define AN2 0b01001000
#define AN3 0b10001011
#define Av 0b01011010
#define Bv 0b11001101
#define stop 0b10100101
#define Cv 0b00110010
#define Dv 0b10010110
unsigned char i,j,T0,count;
unsigned char RXREG[2];
int1 get_data(unsigned char *buff);
int1 rx_bit;
//-----------------------------------------------
int1 get_data(unsigned char *buff)
{ unsigned char i,j,T0,count;
i=10; count=0;
T0CON = 0x84; // prescaler =32
while(--i) // wait preamble
{delay_us(1);
while(RX);
TMR0L=0;
delay_us(1);
while(!RX);
T0=TMR0L;
if (T0==0xFF) return 1; // error
if ((T0>=146) && (T0<=166))// 4672-5312 uS it OK.
{count++;
if ((count>=5)|| (i==0)) break;
};
};
if (i==0) return 1; // error
T0CON=0x86; // prescaler = 128
T0=0;
while(!((T0>=146)&&(T0<=166)))//18688 - 21248 uS it OK. syn bit
{delay_us(1);
while(RX); // wait syn
TMR0L=0;
delay_us(1);
while(!RX);
T0=TMR0L;
};
while(RX);//wait start bit go low
delay_us(7355); // delay 7500 uS before sampling
for(j=0;j<2;j++)
{i=8;
buff[j]=0;
while(i--)
{buff[j]=buff[j]<<1;
rx_bit=RX; // sampling
buff[j]=buff[j]|rx_bit;
if (rx_bit==0)while(!RX);
else while(RX);
delay_us(7355); // delay 7500 uS before sampling
};
};
if (rx_bit==0)
if (!RX) return 1; // error (no stop bit found)
else if (!RX) return 1; // error (no stop bit found)
return 0;
}
main()
{ set_tris_B(0x00); PORTB=0xFF;
set_tris_C(0x00); PORTC=0xff;
set_tris_D(0b00000001);PORTD=0b11000001;
set_power_pwm();
while(1)
{ while(get_data(RXREG)){RL1=RL2=RL3=RL4=RL5=RL6=1;RL7=0;dc_len(0);dc_trai(0);}
if(RXREG[0]==AN0)
{if(RXREG[1]==Av){RL7=1;RL1=RL5=0;}; // di toi 24v
if(RXREG[1]==Bv){RL7=0;RL1=RL5=0;}; // toi 12v
if(RXREG[1]==Cv){RL7=0;RL2=RL6=0;}; // lui 12v
if(RXREG[1]==Dv){RL7=1;RL2=RL6=0;}; // di lui 24v
};
if(RXREG[0]==AN1)
{if(RXREG[1]==Bv){RL7=1;RL1=RL6=0;}; //trai 2banh 24V
if(RXREG[1]==Av){RL7=0;RL1=0;}; //trai 1banh 12V
if(RXREG[1]==Dv){RL7=0;RL5=0;}; //phai 1banh 12V
if(RXREG[1]==Cv){RL7=1;RL2=RL5=0;}; //phai 2banh 24V
};
if(RXREG[0]==AN2)
{if(RXREG[1]==Bv){dc_len(150);};
if(RXREG[1]==Cv){RL3=0;dc_len(170);};
};
if(RXREG[0]==AN3)
{if(RXREG[1]==Bv){dc_trai(7);};
if(RXREG[1]==Cv){RL4=0;dc_trai(7);};
};
get_data(RXREG)=1;
}
}
