Saturday, December 7, 2013

Example Energia code for controlling 433MHz RF transmitter with a microcontroller

// Example Energia code for controlling 433MHz RF transmitter
// I tested this code on MSP430G2553 microcontroller on MSP-EXP430G2 LaunchPad board
// This code should be able to run on Arduino with minor modifications

// Creative Commons Attribution-ShareAlike 3.0 license
// by Roman Staszewski
// December 2013

#define TX_DATA_PIN   2

#define SHORT_PULSE (345)  // time in us. This may need to be fine tuned for a different RF system
#define LONG_PULSE  (1070) // time in us. This may need to be fine tuned for a different RF system
#define BLANK_DELAY (9400) // time in us. This may need to be fine tuned for a different RF system

#define NUM_CHAR 27  // number of characters in the code for the devices I want to control

const int TxPin =  TX_DATA_PIN;      // The number of the TX data pin on my MCU device that is connected to my RF TX module. This assignment may be different on another MCU

char test_code_ch_on[NUM_CHAR+1] = "011101111111100111111010111";  //Channel ON code
char test_code_ch_off[NUM_CHAR+1] = "101101111111100111111010111";  //Channel OFF code

unsigned long time;
unsigned long time2;

void setup() {
  // Initialize TXPin
  pinMode(TxPin, OUTPUT);
  digitalWrite(TxPin, LOW);
}

void loop()
{
  // toggle switch every three seconds
  time = millis();  // get current time in milliseconds
  do {
  send_code(test_code_ch_on);  // transmit code
  time2 = millis();  // get current time in milliseconds
  } while ((time2 - time) < 3000);

  time = millis(); // get current time in milliseconds
  do {
  send_code(test_code_ch_off);  // transmit code
  time2 = millis();  // get current time in milliseconds
  } while ((time2 - time) < 3000);
}

// This function sends either encoded 0 or 1 pulse to the digital output pin connected to data pin of the TX module
// my TX module has polarity inversion, so I'm writing HIGH to TxPin instead of LOW below (and vice versa)
void transmit_bit(int single_bit)
{
  if(single_bit == LOW) {   // send encoded '0'
    digitalWrite(TxPin, HIGH);  
    delayMicroseconds(LONG_PULSE);                
    digitalWrite(TxPin, LOW);
    delayMicroseconds(SHORT_PULSE);  
  }
  else {                 // send encoded '1'
    digitalWrite(TxPin, HIGH);
    delayMicroseconds(SHORT_PULSE);                
    digitalWrite(TxPin, LOW);
    delayMicroseconds(LONG_PULSE);
  }
}

// This function provides a time gap between sending codes
void transmit_blank()
{
  digitalWrite(TxPin, LOW);
  delayMicroseconds (BLANK_DELAY);
}

// This function sends an arbitrary code the the TX
void send_code(char* code)
{
  for (int i=0; i< NUM_CHAR; i++) {
    if(code[i] == '1')
      transmit_bit(HIGH);
    else if(code[i] == '0')
      transmit_bit(LOW);
  }

  transmit_blank(); // transmit time gap at the end
}

3 comments:

  1. Hello!!
    Have you built your RX software??
    Have you used the VirtualWire library? If yes, which version?

    ReplyDelete
  2. I have not built RX side. In my case, the RX is a commercial product - Stanley wireless remote plugs. I'm not using any VirtualWire library on TX side, just controlling the digital pin directly.

    See here for the description of the project.
    http://tinkeringandsuch.blogspot.com/2013/12/turning-dumb-wireless-wall-plugs-into.html

    ReplyDelete
  3. Hi Roman,

    I am fairly new to using TI MCU's. Can you please help me with my current project. Here is the link to all the details.

    http://forum.43oh.com/topic/9944-rf-wireless-lcd-temp-monitor-using-tm4c123-msp432/

    ReplyDelete