I’m trying to incorporate a PIC chip into my next Arduino project so that I can send serial data back and forth between the two. I’m using the PIC16F690 that comes with the PICkit2 programmer/Low Pin Count demo board from Microchip.
The first step is trying to figure out how to get serial data out of the PIC. Right now I’m just using the Arduino as a USB serial interface. It should just pass any serial data it gets over to the serial monitor inside the IDE. The following code is for that. Does this look correct?
// Arduino code
// This code will try to pass any received serial data to the serial logger built into the
// Arduino IDE. It out puts the binary for 'a' which we will try to match with real
// serial data from the PIC chip's output.
int ledPin = 13; // built in led
int incomingByte = 'a';
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH); // turn on the LED so we know when the program starts
Serial.print("Good to go!\n"); // Let the serial monitor know when the program is
// running as well...
Serial.print(incomingByte, BIN); // Print 'a' in binary format. Hopefully our PIC's
// serial data will match this.
}
void loop()
{
if (Serial.available() > 0) {
while (Serial.available() > 0) {
incomingByte = Serial.read();
Serial.print(incomingByte, BIN);
}
Serial.println();
}
}
So this should be able to pass any serial data received on the RX pin to the serial monitor.
The next step is setting up the PIC chip. For some reason, I can’t get results that make any sense out of the PIC. Here’s my code written with the CCS C compiler for the midrange PIC devices:
// PIC code
#include <16F690.h>
#fuses INTRC_IO, NOWDT, NOBROWNOUT, PUT, NOMCLR
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C1, rcv=PIN_C2, bits=8) // Set LED 2 (and the corresponding output) to the transmit pin
void main(void)
{
while(1){
output_c(0b00000001); // turn on LED 0 on the board
delay_ms(1000); // wait 1 second
output_c(0b00000000); // turn the LED off
delay_ms(1000); // wait 1 second
putc('a'); // Send the character 'a' to the serial out.
// This should be '01100001' or '1100001'
// when it gets to the serial monitor
}
}
The output I get in the serial monitor is as follows:
Good to go!
1100001 11
1011000
1011000
1011000
1011000
and so on. The “11” at the end of the second line is noise from where I plug the PIC into the arduino. I remove it when I’m programming the arduino because I’m scared it’ll fry the PIC somehow.
So the question is: Why isn’t the output on the lines after the first two correct? The incoming serial data should be the same.
Do I need a hex inverter on the PIC chips output for the serial data to be correct? Inverting the output in the PIC code didn’t seem to work. Or does it have to do with some difference between CMOS and TTL that I’m missing?
Any help on this would be great. I’ll write up a little tutorial for it if I get it working.
Thanks!
jamis