Arduino to PIC Communication

Program Download:
PIC Project
PIC_Arduino_Comm.X.zip

Arduino Project
Arduino_Com.ino

The Software
           There are two firmware programs that are used for this project:

    -The Transmitter Code
    -The Receiver Code



The compiler used for the transmitter side of this project is the C18 Compiler Provided Free From Micropchip. The first part of the code intializes the PIC 18LF4520's hardware serial communication module for 9600 baud. Then after that, a forever while loop tells the serial module to write a command depending upon whether the button connected to RD0 is pressed or not.

PIC 18LF4520 Transmitter Code

------------« Begin Code »------------
#include <p18f4520.h>
#include <usart.h>
#include <delays.h>
#include <timers.h>
#include <pwm.h>

#pragma     config OSC = HS
#pragma     config WDT = OFF
#pragma     config LVP = OFF

void main(void) {

    TRISA = 0x00;
    TRISD = 0x01;

    // configure USART
    OpenUSART( USART_TX_INT_OFF &
    USART_RX_INT_OFF &
    USART_ASYNCH_MODE &
    USART_EIGHT_BIT &
    USART_CONT_RX &
    USART_BRGH_LOW,
    64 );

    while(1){
        if(PORTDbits.RD0 == 0){
            PORTA = 0x01;
            WriteUSART( 0x40 );
        }
        else{
            PORTA = 0x00;
            WriteUSART( 0x55 );
        }
        Delay10KTCYx(25);
    }
}
------------« End Code »------------

           The Arduino UNO is acting as a receiver and its job is to receive the command and act accordingly. Initially the Arduino Uno sets up its hardware serial communication module for 9600 baud, then it goes into a forever while loop, parsing commands as they are received in specific intervals.

Arduino UNO Receiver Code

------------« Begin Code »------------
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
unsigned int inByte = 0;

void setup() {
  lcd.begin(16, 2);
  Serial.begin(9600);
  lcd.print(" PyroElectro.com");
  lcd.setCursor(0, 1);
  lcd.print("Count: ");
}

void loop() {
  while (1) {               
    if (Serial.available() > 0) {      
      inByte = Serial.read();           
      if (inByte == 28) {
        lcd.setCursor(7, 1);
        lcd.print(millis());       
      } 
    }
      
    delay(10);     
  }   
}
------------« End Code »------------

           The firmware for each device looks different because different compilers are used to build the downloadable hex. However, the same basic ideas are at work here--we're using their internal hardware modules to either send or receive commands, a very basic input/output type of system.



;