problem with usart communication pic16f873a

General discussion on mikroC.
Post Reply
Author
Message
Mangus
Posts: 2
Joined: 18 Nov 2015 19:02

problem with usart communication pic16f873a

#1 Post by Mangus » 18 Nov 2015 19:09

having trouble with my code, the communication with USART doesn't work.
I tried posting something before but it failed
It's working fine on proteus, but not on the breadboard. Maybe i'm not starting some register?
ty in advance

Code: Select all

unsigned int i;

void main() {

            PORTB  = 0;
            TRISB  = 0b11110000;
            TRISC  = 0b11000011;
            RCSTA.CREN = 1; // enable reception.
            RCSTA.SPEN = 1; //Serial port enable

            UART1_Init(9600);           // initialize USART module
                                         //  (8 bit, 9600 baud rate, no parity bit...)
    while (1)
      {
            if (PORTB.F4 == 1 ){     //if push button is unpressed
                   UART1_Write(0b11111100);
                   Delay_ms(100);

                         if (UART1_Data_Ready()) {
                         PORTC.F4  = 1;
                         i = UART1_Read();                  // read the received data

                         PORTB = i;
                         Delay_ms(100);      //light LEDs on RB0 - RB3
                         }

             }

            if (PORTB.F4 == 0 ) {                //if push button is pressed
                   UART1_Write(0b00001111);
                   Delay_ms(100);


                         if (UART1_Data_Ready()) {
                         i = UART1_Read();                  // read the received data

                         PORTB = i;
                         Delay_ms(100);    //light LEDs on RB4- RB7
                        }

             }

      }
}

User avatar
Aleksandar.Mitrovic
mikroElektronika team
Posts: 1697
Joined: 11 Mar 2015 12:48

Re: problem with usart communication pic16f873a

#2 Post by Aleksandar.Mitrovic » 19 Nov 2015 14:49

Hi Mangus,

I tested this code on EasyPICv7 board with PIC16F873 MCU:

Code: Select all

char uart_rd;

void main() {

  UART1_Init(9600);               // Initialize UART module at 9600 bps
  Delay_ms(100);                   // Wait for UART module to stabilize
  
  UART1_Write_Text("Start");
  UART1_Write(13);
  UART1_Write(10);

  while (1) {                              // Endless loop
    if (UART1_Data_Ready()) {    // If data is received,
      uart_rd = UART1_Read();    // read the received data,
      UART1_Write(uart_rd);       // and send data via UART
    }
  }
}
Please take this code and test your hardware.

Best regards,
Aleksandar

Post Reply

Return to “mikroC General”