• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Arduino - Internet of Things (continued #3)

    Blog entry posted in 'Electronics and Other Ramblings...', June 09, 2014.

    As I mentioned previously (http://www.electro-tech-online.com/blog-entries/arduino-internet-of-things-continued-2.748/), I gave-up on the using VirtualWire on the PIC-side and decided to instead use the wireless code I had developed for the basic OOK wireless devices. First step was to create a serial connection between PIC and Arduino to exchange basic data. That went pretty smoothly. Next step was to connect the two using the simple coding/decoding scheme presented on the OOK wireless post (still through a direct wired connection). The code used was posted below for reference. So far it has worked really good.

    Next on the list is to connect through the wireless interface ...

    PIC Code

    Code:

    // Author: languer (©2014)
    // Preprocessor
    #include <12F683.h>
    #device *=16 ADC=10
    #fuses INTRC_IO,NOWDT,NOPUT,NOPROTECT,NOCPD,NOMCLR
    #use delay(clock=8000000)

    // Global Definitions
    #byte INTCON = 0x0B
    #bit GIE = INTCON.7
    #bit PEIE = INTCON.6
    #bit TOIE = INTCON.5
    #bit INTE = INTCON.4
    #bit GPIE = INTCON.3
    #bit T0IF = INTCON.2
    #bit INTF = INTCON.1
    #bit GPIF = INTCON.0

    #define GP0 PIN_A0
    #define GP1 PIN_A1
    #define GP2 PIN_A2
    #define GP3 PIN_A3
    #define GP4 PIN_A4
    #define GP5 PIN_A5

    #define TRISA1 0b11111110
    #define IO_TX232 GP0
    #define IO_RX232 GP1

    #use rs232 (baud=9600, xmit=IO_TX232, rcv=IO_RX232, parity=N, bits=8, stream=ARDUINO, FORCE_SW)

    #define JUNK 0xAA
    #define SYNC1 0x2D
    #define SYNC2 0xD4
    #define MSG_SIZE 4

    // Global Variables
    int8 dataMsg;

    // Function Prototypes
    void init();
    void xmt_data(int8 msg);
    int8 crc8(int8 crc, int8 crc_data);

    // Main Program
    main()
    {
    int8 cnt=0;

    init();

    while (TRUE)
    {
    for (cnt=0;cnt<10;cnt++)
    {
    xmt_data(cnt);
    }
    delay_ms(500);
    }
    }

    /*Function - Initialize Hardware*/
    void init()
    {
    set_tris_a(TRISA1); // set TRISA IOs
    setup_comparator(NC_NC_NC_NC); // disable COMPARATORS
    setup_adc(ADC_OFF); // disable ADC
    setup_adc_ports(NO_ANALOGS); // disable all ANALOG INPUTS
    delay_ms(2500);
    }

    /*Function - Transmit Msg*/
    void xmt_data(int8 msg)
    {
    int8 i = 0;
    int8 crcMsg = 0;
    dataMsg = SYNC1;
    dataMsg = SYNC2;
    dataMsg = msg;

    // calculate message CRC
    crcMsg = 0;
    for (i=0;i<(MSG_SIZE-1);i++)
    {
    crcMsg = crc8(crcMsg, dataMsg);
    }
    dataMsg = crcMsg;

    // transmit data
    fputc(JUNK,ARDUINO);
    fputc(JUNK,ARDUINO);
    fputc(JUNK,ARDUINO);
    for (i=0;i<(MSG_SIZE);i++)
    {
    fputc(dataMsg,ARDUINO);
    }
    }

    /*Function - Dattalo's CRC-8 Function (ROM=39 / RAM=4 / 184_Tcy / 23.0_us) */
    int8 crc8(int8 crc, int8 crc_data)
    {
    int8 i;
    i = (crc_data ^ crc) & 0xff;
    crc = 0;
    if(i & 1)
    crc ^= 0x5e;
    if(i & 2)
    crc ^= 0xbc;
    if(i & 4)
    crc ^= 0x61;
    if(i & 8)
    crc ^= 0xc2;
    if(i & 0x10)
    crc ^= 0x9d;
    if(i & 0x20)
    crc ^= 0x23;
    if(i & 0x40)
    crc ^= 0x46;
    if(i & 0x80)
    crc ^= 0x8c;
    return(crc);
    }



    Arduino Code
    Code:
    // Author:  languer (©2014)
    // definition of message structure
    #define JUNK = 0xAA
    #define SYNC1 0x2D
    #define SYNC2 0xD4
    #define MSG_SIZE 4
    #define ioMcuPwr 7 // pwr pin for PIC
    byte dataMsg;
    byte data = 0;

    void setup()
    {
    // remove power to PIC MCU
    pinMode(ioMcuPwr, OUTPUT);
    digitalWrite(ioMcuPwr, LOW);
    // delay on start-up
    delay(1500);
    // initialize serial:
    Serial.begin(9600);
    Serial.println("Arduino-to-PIC Serial Interface");
    // apply power to PIC MCU
    digitalWrite(ioMcuPwr, HIGH);
    }

    void loop()
    {
    data = receiveMsg();
    Serial.println(data, HEX);
    }

    /* function to receive message */
    byte receiveMsg()
    {
    byte msg = 0;
    byte crcMsg = 0;
    boolean returnMsgFlag = false; // flag for string completion
    byte msgState = 0;
    byte msgCnt = 0;

    while (~returnMsgFlag)
    {
    if (Serial.available() > 0)
    {
    // get new byte:
    msg = Serial.read();
    switch (msgState)
    {
    case 0: // SYNC1
    {
    if (msg == SYNC1)
    {
    dataMsg = msg;
    msgState++;
    msgCnt++;
    }
    break;
    }
    case 1: // SYNC2
    {
    if (msg == SYNC2)
    {
    dataMsg = msg;
    msgState++;
    msgCnt++;
    }
    else
    {
    msgState = 0;
    msgCnt = 0;
    }
    break;
    }
    case 2: // MSG
    {
    dataMsg = msg;
    msgState++;
    msgCnt++;
    break;
    }
    case 3: // CRC
    {
    dataMsg = msg;
    crcMsg = 0;
    for (byte i=0;i<(MSG_SIZE-1);i++)
    {
    crcMsg = crc8(crcMsg, dataMsg);
    }
    if (msg == crcMsg)
    {
    msg = dataMsg;
    returnMsgFlag = true;
    return msg;
    }
    else
    {
    msgState = 0;
    msgCnt = 0;
    returnMsgFlag = false;
    }
    break;
    }
    default:
    {
    msgState = 0;
    msgCnt = 0;
    returnMsgFlag = false;
    }
    }
    }
    }
    }

    // CRC-8 FUNCTION (Dattalo's CRC-8 Function)
    byte crc8(byte crc, byte crc_data)
    {
    byte i;
    i = (crc_data ^ crc) & 0xff;
    crc = 0;
    if(i & 1)
    crc ^= 0x5e;
    if(i & 2)
    crc ^= 0xbc;
    if(i & 4)
    crc ^= 0x61;
    if(i & 8)
    crc ^= 0xc2;
    if(i & 0x10)
    crc ^= 0x9d;
    if(i & 0x20)
    crc ^= 0x23;
    if(i & 0x40)
    crc ^= 0x46;
    if(i & 0x80)
    crc ^= 0x8c;
    return(crc);
    }

    Comments
 

EE World Online Articles

Loading

 
Top