+ Post New Thread
Page 1 of 4 1 2 3 ... LastLast
Results 1 to 20 of 67
  1. #1
    Member level 1
    Points: 266, Level: 3

    Join Date
    May 2012
    Posts
    41
    Helped
    0 / 0
    Points
    266
    Level
    3

    uart driver for pic16f877a

    Hi all,

    I have been working on the uart module of pic16f877a micro controller. I have gone through the registers used and I am aware of the configuration to use 8 bit communication.I am using interrupts for Tx and Rx.But I am facing problem in transmitting and receiving the data bytes as the RCREG buffer is not gettin updated, Please help me with the logic for transmission and reception.The initial conditions for uart configuration is done.I am tryin to transmit the recieved data through the code snippet below. Please help me with it.

    /*ISR*/
    void interrupt uart_rx(void)
    {
    if(RCIE && RCIF)
    {
    ClearUartRxIntFlag;
    UART_TX(RCREG);
    }
    }
    /*Tx function definition */
    void UART_TX(unsigned int TX_BYTE)
    {
    TXREG = TX_BYTE;
    while(!TRMT);
    }



    Thank you

    •   Alt15th May 2012, 12:48

      advertising

        
       

  2. #2
    Super Moderator
    Points: 69,545, Level: 64
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    11,367
    Helped
    3762 / 3762
    Points
    69,545
    Level
    64

    Re: uart driver for pic16f877a

    1. RCIE will not change during the interrupt and must be set for the interrupt to occur so you can remove it from the check, just use "if(RCIF)". You should use a single '&' anyway!
    2. I'm almost certain the RCIF bit is cleared by reading RCREG (it is on similar PICs) so you don't need to clear it in your program.
    3. Check the transmit register is empty before you put new data into it, not afterwards so swap the last two lines.

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.


    1 members found this post helpful.

  3. #3
    Member level 1
    Points: 266, Level: 3

    Join Date
    May 2012
    Posts
    41
    Helped
    0 / 0
    Points
    266
    Level
    3

    Re: uart driver for pic16f877a

    Hi Betwixt,

    I tried with the changes that you told.But still the RCREG is not getting updated. In my program I have initially enabled RCIE so that the interrupt happens during reception. But when I run the program there is no change in the RCIF.It is always reset. This indicates that transfer to RCREG is not happening with this code.And my TXREG is getting updated with random values though m sending the data through TX_BYTE. And according to data sheet TRMT is the bit which tells if my TSR(transmit shift register)is full or empty. TRMT=1(TSR IS EMPTY).Data is always transmitted from TXREG to TSR so swapping those two lines as you said won't work i feel.Please help me to collect data to RCREG which is not happening. Please suggest the logic if my code is wrong.


    Thankyou



  4. #4
    Member level 4
    Points: 1,326, Level: 8

    Join Date
    Sep 2010
    Location
    Thirukalukalukundra, Kanchipuram, Tamilnadu, India
    Posts
    77
    Helped
    3 / 3
    Points
    1,326
    Level
    8

    Re: uart driver for pic16f877a

    I think, changing few lines of your code would make your communication successful.
    Code:
    /*ISR*/
    void interrupt uart_rx(void)
    {
    while(!RCIF)
    continue;
    ClearUartRxIntFlag;
    UART_TX(RCREG);
    }


    ---------- Post added at 12:38 ---------- Previous post was at 12:38 ----------

    I think, changing few lines of your code would make your communication successful.
    Code:
    /*ISR*/
    void interrupt uart_rx(void)
    {
    while(!RCIF)
    continue;
    ClearUartRxIntFlag;
    UART_TX(RCREG);
    }



  5. #5
    Member level 4
    Points: 1,324, Level: 8

    Join Date
    Sep 2007
    Posts
    74
    Helped
    20 / 20
    Points
    1,324
    Level
    8

    Re: uart driver for pic16f877a

    can you add your registers configuration?



  6. #6
    Super Moderator
    Points: 69,545, Level: 64
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    11,367
    Helped
    3762 / 3762
    Points
    69,545
    Level
    64

    Re: uart driver for pic16f877a

    Actually, you shouldn't be transmitting random values because the transmit routine should only be called when a byte has been received in RCREG.
    I suspect the problem is to do with other registers not being set up properly, can you show us the routine for configuring the USART, the clock speed you are using and if possible the contents of the receive and transmit status registers and the SPBRG register.

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



  7. #7
    Member level 1
    Points: 266, Level: 3

    Join Date
    May 2012
    Posts
    41
    Helped
    0 / 0
    Points
    266
    Level
    3

    Re: uart driver for pic16f877a

    Hi all,

    I am using 4MHz clock.Contents of TXSTA=0x24 and RCSTA=0x90.SPBRG=0x19(9.6KHz baudrate) and BRGH=1(HIGH SPEED).Apart from the ISR and Tx function which I had posted in my first thread I have configued TXSTA and RCSTA to the values above and I have enabled the GIE,PEIE and RCIE. I don't know why TXREG is taking random values and the reason why RCREG is not getting updated. Please help.


    Thankyou

    ---------- Post added at 14:22 ---------- Previous post was at 14:19 ----------

    If possible please share the code for USART communication to happen successfully.



    Thankyou

    ---------- Post added at 14:22 ---------- Previous post was at 14:22 ----------

    If possible please share the code for USART communication to happen successfully.



    Thankyou



  8. #8
    Super Moderator
    Points: 69,545, Level: 64
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    11,367
    Helped
    3762 / 3762
    Points
    69,545
    Level
    64

    Re: uart driver for pic16f877a

    The registers look fine and banking should be controlled by the compiler so I can't see anything wrong with the code. My preference would be to use 'return' rather than 'continue' though. I assume 'ClearUartRxIntFlag; is a macro as it is missing function brackets. Is it built-in to your compiler or something you have written yourself. Sorry if that's a silly question, I normally program in assembly language and when I use 'C' it's with a different compiler to yours.

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



    •   Alt16th May 2012, 10:52

      advertising

        
       

  9. #9
    Member level 1
    Points: 266, Level: 3

    Join Date
    May 2012
    Posts
    41
    Helped
    0 / 0
    Points
    266
    Level
    3

    Re: uart driver for pic16f877a

    Hi Betwixt/Gopintj/yuvko ,

    I have made the changes in the code as you people told.And ClearUartRxIntFlag is a macro to which I have assigned values. However the code still is'nt working fine though I have made the changes.Please can you people suggest me the right way to program so that the communication is possible. Please hlp me. As I need to develop the prototype within this week:( I am using Hitech C Compiler and MPLAB version8.30


    Thankyou



  10. #10
    Member level 4
    Points: 1,326, Level: 8

    Join Date
    Sep 2010
    Location
    Thirukalukalukundra, Kanchipuram, Tamilnadu, India
    Posts
    77
    Helped
    3 / 3
    Points
    1,326
    Level
    8

    Re: uart driver for pic16f877a

    Have you polled the TXIF bit while you transmit the received byte? Let me show you how i perform Transmission and Reception operations by a simple coding.

    let c be a character variable that holds a character as 'a'. Now, i am going to transmit this character.

    Code:
    main()
    {
    unsigned char c, r;
    ------
    tx(c);                             \\Function to transmit a character.
    DelayMs(100);                  \\A simple Delay Function.
    r=rx();                            \\Function to receive a character.
    lcd(r);                            \\Lcd Display of the received char.
    }
    
    void tx(unsigned char ch)    \\Body of the function to perform Tx operation
    {
         TXEN=1;
         TXREG=ch;
         tx_wait();
    }
    
    unsigned char rx()          \\Body of the function to perform Rx operation
    {
            unsigned char ch;
    	CREN=1;
    	rx_wait();
    	letter2=RCREG;
    	return ch;
    }
    
    void tx_wait()
    {
    	while(!TXIF)
    	continue;
    	TXIF=0;
    }
    
    void rx_wait()
    {
    	int i;
    	while(OERR){
    	for(i=0;i<2000;i++)
    	{i=i+1;}
    	CREN=0;
    	OERR=0;
    	}
    	CREN=1;
    	while(!RCIF)
    	continue;
    	RCIF=0;
    }
    This is how i programmed for UART Data Transmission and Reception. And i got the correct output.

    Are you using a Simulator like PROTEUS or Hardware?



    •   Alt16th May 2012, 15:47

      advertising

        
       

  11. #11
    Member level 1
    Points: 266, Level: 3

    Join Date
    May 2012
    Posts
    41
    Helped
    0 / 0
    Points
    266
    Level
    3

    Re: uart driver for pic16f877a

    Hi,

    Thanks for the code. I will try this logic also.I am not using any simulator. I am using Pic16f877a h/w. In the code which you posted,RCREG contents is moved to variable 'letter2'.But where is that variable defined?And which hardware did you use for displaying the results?If this works then I need to develop communication with the hyperterminal.That is the end result which is required. Hope you guys will support me with it too. :)Will get back with the results of this execution.


    Thankyou

    ---------- Post added at 11:42 ---------- Previous post was at 10:00 ----------

    Hi,

    I tried the logic which you told. I changed unsigned char to unsigned int but when I execute I am gettin error which says"illegal character(0x5C)". I have not used characters in my implementation and still it is giving this error. May I know the reason for this error?The program is not getting built due to this error. Please help


    Thankyou



  12. #12
    Member level 4
    Points: 1,324, Level: 8

    Join Date
    Sep 2007
    Posts
    74
    Helped
    20 / 20
    Points
    1,324
    Level
    8

    Re: uart driver for pic16f877a

    hi,
    have you set TRISC bit 6,7 ?



  13. #13
    Member level 1
    Points: 266, Level: 3

    Join Date
    May 2012
    Posts
    41
    Helped
    0 / 0
    Points
    266
    Level
    3

    Re: uart driver for pic16f877a

    Hi,

    I have set TRISC7(RX) and have reset TRISC6(TX) so that whatever is received gets transmitted. But the communication is not happening.



  14. #14
    Member level 4
    Points: 1,324, Level: 8

    Join Date
    Sep 2007
    Posts
    74
    Helped
    20 / 20
    Points
    1,324
    Level
    8

    Re: uart driver for pic16f877a

    as far as i remember, the datasheet say you should set both of them



  15. #15
    Advanced Member level 3
    Points: 4,828, Level: 16
    papunblg's Avatar
    Join Date
    Oct 2010
    Location
    Kolkata India
    Posts
    726
    Helped
    172 / 172
    Points
    4,828
    Level
    16
    Blog Entries
    9

    Re: uart driver for pic16f877a

    Publish the circuit diagram
    Rajsekhar Nag
    -------------..--------
    Knowledge Grows When Shared



  16. #16
    Member level 1
    Points: 266, Level: 3

    Join Date
    May 2012
    Posts
    41
    Helped
    0 / 0
    Points
    266
    Level
    3

    Re: uart driver for pic16f877a

    Hi,

    I have set the TRISC6 and TRISC7 bits as in data sheet but still I am not getting the output..Please help!!I ll publish the circuit diagram later. Right now I dont have access to add image. Please bear. I want to know how to establish communication with the hyperterminal.The data which we send through hyperterminal gets updated in RCREG of pic16f microcontroller is it?Please give me a brief idea as to how exactly the uart communication takes place with hyperterminal.


    Thankyou



    •   Alt21st May 2012, 07:48

      advertising

        
       

  17. #17
    Super Moderator
    Points: 69,545, Level: 64
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    11,367
    Helped
    3762 / 3762
    Points
    69,545
    Level
    64

    Re: uart driver for pic16f877a

    To use Hyperterminal through a serial port you MUST use a level converter such as the MAX232 or MAX3232 (or similar parts) because the voltage levels between the PIC and serial port are not only different but inverted. A '1' from the PIC is a '0' at the PC. This does not mean you can invert the bits in software before sending them! The start and stop bits are produced by the USART circuit inside the PIC and those would still be upside down.

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



  18. #18
    Member level 1
    Points: 266, Level: 3

    Join Date
    May 2012
    Posts
    41
    Helped
    0 / 0
    Points
    266
    Level
    3

    Re: uart driver for pic16f877a

    Hi,
    What exactly is MAX232 level shifter?And how should I implement it for the communication to happen successfully?What are the hardware connections to be made with the pic microcontroller and also what are the inputs and outputs to verify my communication is happening correctly?I m sorry if its too many questions but I am not clear as to how I should use it. Please help.


    Thankyou



  19. #19
    Super Moderator
    Points: 69,545, Level: 64
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    11,367
    Helped
    3762 / 3762
    Points
    69,545
    Level
    64

    Re: uart driver for pic16f877a

    It's an IC made by MAXIM but there are other manufacturers too. Basically, it has three circuits inside it, one converts 0-5V levels to +/- 12V, one does the opposite, converting +/- 12V to 0-5V and the third generates the + and - 12 volts levels from the IC's 5V supply pin.

    For more information, look at the data sheet for the MAX232, it is a very common IC. The MAX3232 is the same IC but it works on 3.3V supply instead of 5V.

    All you do is connect the PIC TX to the data in pin on the MAX232, the PIC RX pin to the data out pin on the MAX232 and it will produce and accept the RS232 (+12 to -12V) voltage levels your PC uses at it's serial port. You also have to fit some capacitors between pins on the MAX232 to allow it to generate the 12V signals from a 5V supply. It's really very simple, the schematics are in the data sheet, No software is needed, it's a hardware solution.

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



  20. #20
    Member level 1
    Points: 266, Level: 3

    Join Date
    May 2012
    Posts
    41
    Helped
    0 / 0
    Points
    266
    Level
    3

    Re: uart driver for pic16f877a

    Hi,

    Is it necessary to make use of MAX-232 level converter for communication with the hyperterminal or will the RS-232 cable take care of it?



--[[ ]]--