立即注册
查看: 724|回复: 2

[资料] 433 ASK收发开发包

已绑定手机
发表于 2021-7-13 00:50:36 | 显示全部楼层 |阅读模式 来自 广东省深圳市
rc 开关是arduino的开发包适合给单片机增加433射频遥控功能,atmega328p 需要用D2口接收,发射不用特定

这里发我的解码代码:
/*
  Simple example for receiving
*/
#include <EEPROM.h>
#include <RCSwitch.h>
#include <string.h>
//for hw-262

int RCLK = 4;
int SCLK = 7;
int DIO = 8;
unsigned char CHAR_MAP[] = { 0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90, 0x8C, 0xBF, 0xC6, 0xA1, 0x86, 0xFF, 0xbf };
char CHAR_INDEX_MAP[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '-' };
char LEDbuf[10];
#define LED1 13
#define LED4 10
#define EEPROM_START_ADDRESS    0     // Start Address in EEPROM
#define EEPROM_SIZE             1024  // EEPROM size
#define LED_BUILTIN 3
typedef struct _eeprom_storage
{
   unsigned int   a;
   unsigned long  b[29];
}   EEPROM_storage;
RCSwitch mySwitch = RCSwitch();
int c=0;
int cr=20;
int sREC = LOW;
int val;        //全局变量
int  inByte=0;
unsigned int x = 0;      //全局变量
unsigned long rcVal[29]={3319553,3319555,3319558,3319557,3319561,3319556,3319554,3319559,3319571,3319560,3319573,3319562,3319563,3319564,3319565,3319566,3319568,3319572,3319569,3319570};
const int buttonPin = A1;     // the number of the pushbutton pin
EEPROM_storage      e2_memory;
int buttonState = 0;         // variable for reading the pushbutton status
void setup() {
  pinMode(buttonPin,  INPUT_PULLUP);
  pinMode(LED1, OUTPUT);
        digitalWrite(LED1, HIGH);
  pinMode(LED4, OUTPUT);
        digitalWrite(LED4, HIGH);
  pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, HIGH);
  pinMode(DIO, OUTPUT);
  pinMode(RCLK, OUTPUT);
  pinMode(SCLK, OUTPUT);
  Serial.begin(9600);
  //delay(5000);
   print_b0("433");
  //print_t3(0);
    read();
   mySwitch.enableReceive(0);
   
}
//3319553 start
//3319555 stop
void loop() {
  if (Serial.available() > 0) {
    inByte = Serial.parseInt();
    print_t0(inByte);
  }
  buttonState = digitalRead(buttonPin);
   if(buttonState == LOW){
      x++;
      if(x==1){
        print_b0("REC");
        digitalWrite(LED1, LOW);
         delay(500);
        sREC=HIGH;
        c=0;
        cr=0;
      }
      if(x==2)
      {
        print_b0("SAVE");
        digitalWrite(LED1, HIGH);
        delay(500);
        digitalWrite(LED1, LOW);
        cr=c;
        save();
        read();
        c=0;
        sREC=LOW;
      }
      delay(100);
   }

    if(sREC==HIGH)
    {
    if (mySwitch.available()) {
    print_t1( mySwitch.getReceivedValue());
    rcVal[c]=mySwitch.getReceivedValue();
    c++;
    print_t3(c);
    mySwitch.resetAvailable();
     flash();
  }
    }else{
    test();  
    }
   
}



void EEPROM_write_block(unsigned char *memory_block, unsigned int start_address, unsigned int block_size)
{
   unsigned char Count = 0;
   for (Count=0; Count<block_size; Count++)
   {  
       EEPROM.write(start_address + Count, memory_block[Count]);
   }
}

void EEPROM_read_block(unsigned char *memory_block, unsigned int start_address, unsigned int block_size)
{
   unsigned char Count = 0;
   for (Count=0; Count<block_size; Count++)
   {
       memory_block[Count]= EEPROM.read(start_address + Count);
       //Serial.println((unsigned int)(memory_block[Count]));   delay(400);
   }
}

void EEPROM_clear_all(unsigned int eeprom_size)
{
   unsigned int Count = 0;
   unsigned char data = 0;
   for (Count=0; Count<eeprom_size; Count++)
   {  
       EEPROM.write(Count, data);
   }
}

// Write an uint value to EEPROM
void EEPROM_write_short(unsigned int Address, unsigned int Data)
{
   unsigned int DataL = Data&0x00FF;
   unsigned int DataH = Data>>8;
   EEPROM.write(Address,   DataL);
   EEPROM.write(Address+1, DataH);
}                     

// Read an uint value from EEPROM
unsigned int EEPROM_read_short(unsigned int Address)
{
   unsigned int DataL = EEPROM.read(Address);
   unsigned int DataH = EEPROM.read(Address+1);
   return((DataH<<8) + DataL);
}
void print_t0(int s){
      Serial.write("t0.txt=\"");
    //Serial.print("Received ");
    Serial.print(s);
    Serial.write(" \"");
    Serial.write(0XFF);         //结束符
    Serial.write(0XFF);
    Serial.write(0XFF);
}
void print_t1(unsigned long s){
      Serial.write("t1.txt=\"");
    //Serial.print("Received ");
    Serial.print(s,HEX);
    Serial.write(" \"");
    Serial.write(0XFF);         //结束符
    Serial.write(0XFF);
    Serial.write(0XFF);
    //ledshow(s.String);
}
void print_t3(long s){
      Serial.write("t3.txt=\"");
    //Serial.print("Received ");
    Serial.print(s);
    Serial.write(" \"");
    Serial.write(0XFF);         //Count
    Serial.write(0XFF);
    Serial.write(0XFF);
}

void print_b0(String s){
      Serial.write("b0.txt=\"");
    //Serial.print("Received ");
    Serial.print(s);
    Serial.write(" \"");
    Serial.write(0XFF);         //REC
    Serial.write(0XFF);
    Serial.write(0XFF);
}

void save(){
    e2_memory.a=cr;
    for(int i=0;i<=cr;i++)
    {
    e2_memory.b=rcVal;
    }
   
    EEPROM_write_block((unsigned char*)&e2_memory, EEPROM_START_ADDRESS, sizeof(EEPROM_storage));
}

void read(){
     EEPROM_read_block((unsigned char*)&e2_memory, EEPROM_START_ADDRESS, sizeof(EEPROM_storage));
   if(e2_memory.a>100)e2_memory.a=29;
       cr=e2_memory.a;
   for (int i=0;i<cr;i++){
   // Serial.println(e2_memory.b);
    rcVal=e2_memory.b;
   }
}

void test(){
          digitalWrite(LED1, HIGH);
    ledshow(LEDbuf);
  if (mySwitch.available()) {
    //String stringOne =  String(mySwitch.getReceivedValue(), HEX);
    sprintf(LEDbuf, "%d", mySwitch.getReceivedValue());
    print_t1( mySwitch.getReceivedValue());
   //    ledshow(buf);
    flash();
    if(rcVal[c]==mySwitch.getReceivedValue()){
      print_b0("OK");
          flash();
         c++;
    }else{
       print_b0("BAD");
      
    }
    if(c>=cr)
    {
    c=0;
     print_b0("PASS");
     flash_pass();
    }
    print_t3(c);
    mySwitch.resetAvailable();
  }
}

void flash(){
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED on (HIGH is the voltage level)
  delay(10);                       // wait for a second
  digitalWrite(LED_BUILTIN, HIGH);    // turn the LED off by making the voltage LOW
  delay(10);                       // wait for a second
  digitalWrite(LED4, LOW);   // turn the LED on (HIGH is the voltage level)
  delay(10);                       // wait for a second
  digitalWrite(LED4, HIGH);    // turn the LED off by making the voltage LOW
  delay(10);                       // wait for a second
}
void flash_pass(){
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED on (HIGH is the voltage level)
  delay(100);                       // wait for a second
  digitalWrite(LED_BUILTIN, HIGH);    // turn the LED off by making the voltage LOW
  delay(100);                       // wait for a second
}

void ledout(unsigned char chr)
{
  unsigned char i;
  for (i = 8; i >= 1; i--)
  {
    if (chr & 0x80) digitalWrite(DIO, 1); else digitalWrite(DIO, 0);
    chr <<= 1;
    digitalWrite(SCLK, 0);
    digitalWrite(SCLK, 1);
  }
}

void ledshow(const char* str)
{
  int len = strlen(str);
  int hasDot = 0;
  for (int i = len - 1, m = 0; i >= 0; i--) {
    char chr = str;
    if (chr == '.') {
      hasDot = 1;
      continue;
    }
    int n = 0;
    for (; n < 17; n++) {
      if (chr == CHAR_INDEX_MAP[n])
        break;
    }
    if (n != 17) {
      unsigned char chr1 = CHAR_MAP[n];
      if (hasDot)
        chr1 &= 0x7f; // 所有字符的最高位(左数第一位)都是1,代表小数点为暗
              // 因此只要将最高位改为0小数点就可以点亮啦
      ledout(chr1); // 显示字符
      ledout((int)ceil(pow(2, m))); // 位置
      digitalWrite(RCLK, 0);
      digitalWrite(RCLK, 1);
    }

    m++;
    hasDot = 0;
  }
}
已绑定手机
发表于 2021-8-19 21:48:10 | 显示全部楼层 来自 广东省深圳市
没人气呢,支持一下
已绑定手机
发表于 2021-11-11 08:33:31 | 显示全部楼层 来自 广东省江门市
1111111111111111111
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

合作/建议

TEL: 19168984579

工作时间:
周一到周五 9:00-11:30 13:30-19:30
  • 扫一扫关注公众号
  • 扫一扫打开小程序
Copyright © 2013-2024 一牛网 版权所有 All Rights Reserved. 帮助中心|隐私声明|联系我们|手机版|粤ICP备13053961号|营业执照|EDI证
在本版发帖搜索
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表