EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / serial communication using PIC18f4550

serial communication using PIC18f4550

|

Microcontroller › PIC › serial communication using PIC18f4550

  • This topic has 4 replies, 5 voices, and was last updated 13 years ago by AJISH ALFRED.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • April 6, 2012 at 6:24 pm #4767
    Mayur Nirmal
    Participant

    hi folks,

    I am trying to communicate with PC using RS232 and PIC18F4550. I am using following circuit diagram as it is and code attached. But I am not getting desired output. I really can’t understand what is the problem. Please help me as early as possible.

     

    The Code I am exactly using is..

     

     

    // Program to depict the configuration of EUSART in PIC18F4550
    // This code receives and then transmits the same data back to the PC .. // ..through PC’s Serial Port
    #include<p18f4550.h>
    #include<usart.h>
     
    // Configuration bits
    /* _CPUDIV_OSC1_PLL2_1L,  // Divide clock by 2
       _FOSC_HS_1H,           // Select High Speed (HS) oscillator
       _WDT_OFF_2H,           // Watchdog Timer off
       MCLRE_ON_3H            // Master Clear on
    */
     
    void tx_data(unsigned char);
     
    unsigned char rx_data(void);
    unsigned char serial_data;
    unsigned int i=0;
     
    #define FREQ 12000000    // Frequency = 12MHz
    #define baud 9600
    #define spbrg_value (((FREQ/64)/baud)-1)    // Refer to the formula for Baud rate calculation in Description tab
     
    void main(void)
    {
        SPBRG=spbrg_value;                                // Fill the SPBRG register to set the Baud Rate
        RCSTAbits.SPEN=1;                                     // To activate Serial port (TX and RX pins)
        TXSTAbits.TXEN=1;                                     // To enable transmission
        RCSTAbits.CREN=1;                                     // To enable continuous reception
        while(1)
        {
            serial_data=rx_data();    // Receive data from PC
            tx_data(serial_data);        // Transmit the same data back to PC
        }
    }
     
     
    void tx_data(unsigned char data1)
    {
        TXREG=data1;                                     // Store data in Transmit register
        while(PIR1bits.TXIF==0);                             // Wait until TXIF gets low
    }
     
     
    unsigned char rx_data(void)
    {
        while(PIR1bits.RCIF==0);                            // Wait until RCIF gets low
        return RCREG;                                   // Retrieve data from reception register
    }
     
     
    Please tell me what is the mistake and what should I do to get the desired output..!!!!
     
    Also refer to the link as I am using it for my project..http://www.engineersgarage.com/embedded/pic-microcontroller-projects/eusart-circuit

    April 17, 2012 at 6:58 am #7470
    faizal
    Participant

    if your output is at the hyperterminal:

    1) check com port baud rate

    2) check if the port you’re trying to connect is being used by other program.

    3) check if your config bits is applied in the project setting

     

    if you have access to oscilloscope:

    1) probe the tx pin if its tranmitting

     

    i have tested above code, and its working.

    if its too hard, consider using the following built in function for mikroc:

     

    char string[20];                                  //put any size here

    int i=0;

    USART_init(9600);                          //set baud rate, built in

    for(i=0;i<sizeof(string);i++)

    {

      USART_Write(string);                //transmit each character one by one

    }

    USART_Write(0x0d);                     //put a carriage return after finishing transmission

    hope this help :)

    May 17, 2012 at 10:50 am #7843
    Anonymous
    Guest

     when i loaded my code, when i enterd any character through keybord the output is not in proper format , its giving like this  —

                                                                                                                                                                                               — .

                                                                                                                                                                                               —

     

    my code is : #include “C:Program FilesPICCProjectsNew FolderpradUART.h”
    #include <stdio.h>
    //#include <18f452.h>
    #use rs232(baud=9600,xmit=pin_c6,rcv=pin_c7)
    #fuses HS,NOWDT,NOPROTECT,NOLVP
    #use delay(clock=2000000)

    void main(void)
    {

    unsigned char k;

       setup_adc_ports(NO_ANALOGS);
       setup_adc(ADC_OFF);
       setup_psp(PSP_DISABLED);
       setup_spi(FALSE);
       setup_wdt(WDT_OFF);
       setup_timer_0(RTCC_INTERNAL);
       setup_timer_1(T1_DISABLED);
       setup_timer_2(T2_DISABLED,0,1);
       setup_oscillator(False);

      while(1)
      {
     k= fgetc();
     fputs(k);
     printf(“41H”);

       // TODO: USER CODE!!
    };
    }

    May 17, 2012 at 10:51 am #7844
    srikanth
    Participant

    when i loaded my code, when i enterd any character through keybord the output is not in proper format , its giving like this  —

                                                                                                                                                                                               — .

                                                                                                                                                                                               —

     

    my code is : #include “C:Program FilesPICCProjectsNew FolderpradUART.h”
    #include <stdio.h>
    //#include <18f452.h>
    #use rs232(baud=9600,xmit=pin_c6,rcv=pin_c7)
    #fuses HS,NOWDT,NOPROTECT,NOLVP
    #use delay(clock=2000000)

    void main(void)
    {

    unsigned char k;

       setup_adc_ports(NO_ANALOGS);
       setup_adc(ADC_OFF);
       setup_psp(PSP_DISABLED);
       setup_spi(FALSE);
       setup_wdt(WDT_OFF);
       setup_timer_0(RTCC_INTERNAL);
       setup_timer_1(T1_DISABLED);
       setup_timer_2(T2_DISABLED,0,1);
       setup_oscillator(False);

      while(1)
      {
     k= fgetc();
     fputs(k);
     printf(“41H”);

       // TODO: USER CODE!!
    };
    }

    May 18, 2012 at 5:58 pm #7852
    AJISH ALFRED
    Participant

     

    Faisal said your code is working. Then there might be some problems in your hardware. Check the following,

     

    Short the wires coming from the pin 2 and 3 of DB9, type something on the hyperterminal and the same should be displayed.

     

    Short pin no. 25 and 26 while, serially connected to the system. Type something on the hyperterminal and you should get the same displayed.

     

    Check whether your crystal is 12mhz itself.

  • Author
    Posts
Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • how to work on pcbs that are thick May 18, 2025
  • Actin group needed for effective PCB software tutorials May 18, 2025
  • Kawai KDP 80 Electronic Piano Dead May 18, 2025
  • Doing consultancy work and the Tax situation? May 18, 2025
  • How to repair this plug in connector where wires came loose May 17, 2025

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise