pic16f628 UART

Find out how to setup your programmer's software and how to solve many common problems.

Moderator: phalanx

Post Reply
kroepoek85
Posts: 2
Joined: Mon May 14, 2018 3:26 pm

pic16f628 UART

Post by kroepoek85 » Mon May 14, 2018 3:39 pm

I am a really newbie at programming microcontrollers.
Am trying to send data from a pic16f628a to a arduino uno using UART. The problem is I don't receive any data in my serial terminal.

Pin RA7 and RA6 is connected to a 16Mhz crystal, 22 pf to ground on both pins.
Pin RB2 is connected to the arduiono RX pin.

Have searched all over the internet but couldn't find any answer.

Pic16f628 Code:

Code: Select all

#pragma config FOSC = HS  // Oscillator Selection bits (INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF       // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is MCLR)
#pragma config BOREN = ON       // Brown-out Detect Enable bit (BOD enabled)
#pragma config LVP = OFF         // Low-Voltage Programming Enable bit (RB4/PGM pin has PGM function, low-voltage programming enabled)
#pragma config CPD = OFF        // Data EE Memory Code Protection bit (Data memory code protection off)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#define XTAL_FREQ = 16000000;

#include <xc.h>

    void UART_Write(char data)
    {
        while(!TRMT);
        TXREG = data;
    }
    
        
    char UART_TX_Empty()
    {
        return TRMT;
    }
    
    void UART_Write_Text(char *text)
    {
        int i;
        for(i=0;text[i]!='\0';i++)
            UART_Write(text[i]);
    }
    
    void inituart(void)
    {    
        //Set RX/TX pins
        TRISB1 = 1;
        TRISB2 = 1;
        
        //Set BRGH bit
        BRGH = 0;
        
        SPBRG = 25;                                   // Writing SPBRG Register
        SYNC = 0;                                     // Setting Asynchronous Mode, ie UART
        SPEN = 1;                                     // Enables Serial Port
        CREN = 1;                                     // Enables Continuous Reception
        TXEN = 1;                                     // Enables Transmission
    }

void main(void) {

  nRBPU = 0;    //Enables PORTB Internal Pull Up Resistors
  inituart();

  do
  {
    UART_Write_Text("Hello World!");
    __delay_ms(100);
  }while(1);
}
Arduino code:

Code: Select all

void setup() {
        Serial.begin(9600);
}

void loop() {
        while(Serial.available() > 0) {
         Serial.write(Serial.read());
        }
}

User avatar
phalanx
Non-SFE Guru
Posts: 1980
Joined: Sun Nov 30, 2003 8:57 am
Location: Candia, NH

Re: pic16f628 UART

Post by phalanx » Mon May 14, 2018 5:48 pm

In situation like this where you trying to get 2 units to communicate with each other without a known working means of debugging the system, you need to break down your test setup into multiple parts. Instead of having the PIC talk to the Arduino, feed it to the serial port on a PC that you know works. Then see if the PIC code is properly generating serial messages. Once you are sure of that, move on to the Arduino. From my quick scan of your code, I'm not seeing anything readily apparent on the PIC end that would cause it not to transmit. Hook it up to a PC and let us know if it works or not.

-Bill

kroepoek85
Posts: 2
Joined: Mon May 14, 2018 3:26 pm

Re: pic16f628 UART

Post by kroepoek85 » Tue May 15, 2018 1:35 pm

really strange,i used a Parallax USB to Serial before and is wasn't working. With the FTDI basic breakout it is working now.
The only i have now is that the breadboard is really sensitive, when I touch a wire the terminal is flipping, I will figure it out on the internet, maby put caps and resistors.

teprojects1
Posts: 51
Joined: Sat Jan 21, 2017 1:28 am
Location: Lahore
Contact:

Re: pic16f628 UART

Post by teprojects1 » Mon Jun 25, 2018 1:24 am

kroepoek85 wrote:
Tue May 15, 2018 1:35 pm
really strange,i used a Parallax USB to Serial before and is wasn't working. With the FTDI basic breakout it is working now.
The only i have now is that the breadboard is really sensitive, when I touch a wire the terminal is flipping, I will figure it out on the internet, maby put caps and resistors.
Yeah bread boards are not good for embedded, I would suggest you to design your circuit on wero board. Solder the components and you don't need to worry anymore.
If you need any help in Engineering Projects | Arduino Projects you can contact me !! Introduction to PIC16F877a

Post Reply