STC15F104W uses 315/433 MHz super regenerative module to send/receive data

Publisher:jiaohe1Latest update time:2022-08-02 Source: csdnKeywords:STC15F104W Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Schematic diagram

315/433 MHz super regenerative module used in the experiment

insert image description here
insert image description here

Transmitter schematic diagram

Schematic diagram of receiving end

insert image description here

2. Transmitter Code

Send data using NEC protocol


#include

#include


#define FOSC 6000000L //IRC frequency


sfr P3M1 = 0xB1;

sfr P3M0 = 0xB2;


sbit SEND = P3 ^ 4; //data sending pin

sbit btn = P3 ^ 2; //Send key


#ifndef fly

#define uchar unsigned char

#endif

#ifndef uint

#define uint unsigned int

#endif

uchar tx_buff[4]; //send buffer


void Delay9ms() //@6.000MHz

{

unsigned char i, j;


i = 53;

j = 132;


do {

while(--j);

} while(--i);

}

void Delay4ms() //@6.000MHz

{

unsigned char i, j;


i = 24;

j = 85;


do {

while(--j);

} while(--i);

}

void Delay560us() //@6.000MHz

{

unsigned char i, j;


i = 4;

j = 65;


do {

while(--j);

} while(--i);

}

void Delay20us() //@6.000MHz

{

unsigned char i;


_nop_();

_nop_();

i = 27;


while(--i);

}



//Send logic 1, logic 1 is 2.25ms, pulse time is 560us

void send_logic_1()

{

SEND = 1;

Delay560us();

SEND = 0;

Delay560us();

Delay560us();

Delay560us();

}

//Send logic 0, logic 0 is 1.12ms, pulse time is 560us

void send_logic_0()

{

SEND = 1;

Delay560us();

SEND = 0;

Delay560us();

}

//Send data of specified length

void send(uchar *dat, uint len)

{

fly i, j, temp;

//Boot code

SEND = 1;

Delay9ms(); //9ms high level

SEND = 0;

Delay4ms(); //4ms low level


//Data code

for(j = 0; j < len; j++){

temp = *that;

for(i = 0; i < 8; i++) {

if((temp & 0x01) == 0x01) {

send_logic_1();

} else {

send_logic_0();

}


temp = temp >> 1;

}

that++;

}

//End code

SEND = 1;

Delay4ms(); //4ms high level

SEND = 0;

}


void main()

{

INT0 = 1;

IT0 = 1; //Set the interrupt type of INT0 (1: falling edge only 0: rising edge and falling edge)

EX0 = 1; // Enable INT0 interrupt

EA = 1;


P3M1 &= 0xEF; //P3.4 push-pull output

P3M0 &= 0xFF;

tx_buff[0] = 0x40;

tx_buff[1] = ~tx_buff[0]; //取反

tx_buff[2] = 0x56;

tx_buff[3] = ~tx_buff[2]; //取反

while(1) {

}

}


//Interrupt service routine

void ex_int0() interrupt 0 //INT0 interrupt entry

{

if(btn == 0) {

Delay20us();


if(btn == 0) {

Delay20us();

send(tx_buff, 4);

}


while(!btn); //wait for button to be pressed

}

}


3. Receiver Code

Receive data using NEC protocol


#include

#include


#ifndef DARK

#define FOSC 6000000L //IRC frequency

#endif


#ifndef fly

#define uchar unsigned char

#endif

#ifndef uint

#define uint unsigned int

#endif


sfr P3M1 = 0xB1;

sfr P3M0 = 0xB2;

sbit RECEIVE = P3 ^ 2; //Data receiving pin, INT0

bit  exint0;

bit flag = 0; //receive flag

sbit LED = P3 ^ 3; //LED


uchar recv[4]; //Receive buffer


void Delay2ms() //@6.000MHz

{

unsigned char i, j;


i = 12;

j = 169;


do {

while(--j);

} while(--i);

}


void Delay200us() //@6.000MHz

{

unsigned char i, j;


i = 2;

j = 39;


do {

while(--j);

} while(--i);

}


void Init_Int0(void)

{

INT0 = 1;

IT0 = 0; //Set the interrupt type of INT0 (1: falling edge only 0: rising edge and falling edge)

EX0 = 1; // Enable INT0 interrupt

}


void main()

{

Heat_Int0();

EA = 1;


P3M1 &= 0xF7; //P3.3 push-pull output

P3M0 &= 0xFF;

flag = 0;


while(1) {

if(flag) {

flag = 0;

LED = !LED; //Receive data and invert IO output

}

}

}


//Interrupt service routine

void External_INT0() interrupt 0 //INT0 interrupt entry

{

flying i, j, N;

uint k;

exint0 = INT0; //Save the status of INT0 port, INT0=0 (falling edge); INT0=1 (rising edge)

EX0 = 0; //Disable interrupt


flag = 0;

Delay2ms();


if(RECEIVE == 0) {

EX0 = 1;

flag = 0;

return;

}


k = 100;


//Confirm that the RECEIVE signal appears

while(RECEIVE && k) { //Wait for RECEIVE to become a low level and skip the 9ms leading high level signal.

Delay200us();

k--;

}


k = 100;


while(!RECEIVE && k) { //Wait for RECEIVE to become high level and skip the 4ms leading low level signal.

Delay200us();

k--;

}


for(i = 0; i < 4; i++) { //4 groups of data

for(j = 0; j < 8; j++) { //Each set of data has 8 bits

k = 100;


while(RECEIVE && k) { //Wait for RECEIVE to become low level

Delay200us();

k--;

}


k = 100;

N = 0;


while(!RECEIVE && k) { //Calculate the RECEIVE low level duration

Delay200us();

N++;

k--;


if(N >= 30) {

EX0 = 1;

flag = 0;

return; //0.14ms count is too long and automatically exits.

}

} //Low level counting completed


recv[i] = recv[i] >> 1; //Fill the highest bit of data with "0"


if(N >= 8) {

recv[i] = recv[i] | 0x80; //Fill the highest bit of data with "1"

}


N = 0;


}

}

if((recv[0] == ~recv[1]) && (recv[2] == ~recv[3])){ //Data verification successful

flag = 1;

}

else{

flag = 0;

}

EX0 = 1; //Open interrupt


}


4. Receive output waveform

insert image description here

Keywords:STC15F104W Reference address:STC15F104W uses 315/433 MHz super regenerative module to send/receive data

Previous article:STC15 series MCU SPI usage tutorial (I)
Next article:STC15F104W PWM dimming

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号