Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] pic16f873 UART problem

Status
Not open for further replies.

tejashs

Member level 1
Joined
Sep 4, 2011
Messages
38
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,288
Activity points
1,513
hello,
i have recently started learning PIC microcontrollers. i have done a few programs on PIC 16F84 and 16F72 and that is all the experiance i got with PICs. now i was trying to use serial communication using PIC 16F873A. and having a lot of trouble with reiever part even for low baud rates. the trouble being reciever part dosent seem to be working properly.
here is my code
Code:
#include <htc.h>

__CONFIG(0x3ffa);

void init()
{

    TRISC = 0xc0;
    SPBRG = 12; //  (4,000,000/4800baud/64)-1   for setting 4800baud

    TXSTA = 0x20;
    RCSTA = 0x90;

    TXIE = 0; // disable TX interrupt
    RCIE = 0; // disable RX interrupt
}

void main()
{
    unsigned int i;
    unsigned char temp = 0;

    init();

    while (1)
    {
        while(!TXIF);
        TXREG = 'U';
        while(!TRMT);

        for (i = 0; i < 3000; i++);
        
        while (!RCIF);
        temp = RCREG;
        for (i = 0; i < 3000; i++);

        while (!TXIF);
        TXREG = temp;
        while (!TRMT);
        //while(!TXIF);
        for (i = 0; i < 6000; i++);
    }
}

The output i expected is the it should display 'U' in terminal first then echo the character i type in the terminal but that is not happening.
I am getting a continuous stream of 'U's non stop. I am unable to figure out the problem... i think i did all the steps given in the datasheet can someone help me out in this.
 

Try this code:

Code:
#include <htc.h>

[COLOR="#FF0000"]__CONFIG(0x3f3a);[/COLOR]

void init()
{

[COLOR="#FF0000"]    TRISC6 = 0;
    TRISC7 = 1;[/COLOR]
    SPBRG = 12; //  (4,000,000/4800baud/64)-1   for setting 4800baud

    TXSTA = 0x20;
    RCSTA = 0x90;

    TXIE = 0; // disable TX interrupt
    RCIE = 0; // disable RX interrupt
}

void main()
{
    unsigned int i;
    unsigned char temp = 0;
	[COLOR="#FF0000"][/COLOR]
	
    init();

[COLOR="#FF0000"]
        TXREG = 'U';
        while(1) {
	        while(!RCIF);
	        while(!TRMT);
	        TXREG = RCREG;
	} 
    [/COLOR]
}
 
Last edited:

same problem whit the above program....
it keeps displaying 'U's eventhough it is outside loop....

---------- Post added at 01:14 ---------- Previous post was at 00:10 ----------

well i just tested a simple program of led toggle, this is not working with PIC16F873 whereas if i simply replace it with PIC16F72 it is working, in the same board.....
i am totally at a loss...
can someone guide me so that i can debug the situation

Code:
#include

__CONFIG(0x3ffa);

void main()
{
TRISC = 0x00;
while(1)
{
_delay(1000000);
PORTC = 0;
_delay(1000000);
PORTC = 0xff;

}
}
 
Last edited:

Just a thought but shouldn't you be clearing you interrupt flags? Otherwise they will always be true??
 

try with fuse bits 0x3F3A

---------- Post added at 21:38 ---------- Previous post was at 21:33 ----------

In your fuse bits, LVP is enabled... (assuming that you are using HVP)
This will make some problem if you didn't ground the PGM PIN while the mcu start working ....


So, if you are not using Low voltage programming, then use the above config bits, If you are using LVP, then ground the RB3 pin using a 10K...
Then it may work, I think....
Try it ...


Because in the code I provided. there is no way the while(1) can exit, other than a reset occur,....
 
Last edited:
could the IC have been damaged because of wrong configuration bits.....
because even by using this
__CONFIG ( FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF & LVP_OFF & CPD_OFF & WRT_OFF & DEBUG_OFF & CP_OFF);
it dosent seem to be working....
 

Are you sure your PIC burner is taking fuse bits from the hex file?
Did you noticed the fuse bits loaded in the programmer?

Any way, just try by grounding the RB3 pin using a 10K resistor and use my code above (modified now). Also don't forget to pull up the MCLR pin also...
Still if it is printing continuous U, then it may be the WDT resetting the PIC...(due to the fuse bit problem)...

Also check if there is an option to provide the fuse bits manually in the programmer...

---------- Post added at 11:27 ---------- Previous post was at 11:00 ----------

Still you can check if it is a watchdog problem by usign clear watchdog instruction when the pic waits in while loops.... But I think there will be the option of fuse bits in the programmer itself..
So which programmer are u using ?

---------- Post added at 12:13 ---------- Previous post was at 11:27 ----------

if it is the watchdog reset problem,
then try below code, I assume it will work,but any way I didn't tried and always I used to disable WDT in fuse bits.
CLRWDT() wil clear the watchdog timer and prevent overflow and thus prevent reset...

Code:
#include <htc.h>

__CONFIG(0x3f3a);

void init()
{

    TRISC6 = 0;
    TRISC7 = 1;
    SPBRG = 12; //  (4,000,000/4800baud/64)-1   for setting 4800baud

    TXSTA = 0x20;
    RCSTA = 0x90;

    TXIE = 0; // disable TX interrupt
    RCIE = 0; // disable RX interrupt
}

void main()
{
    unsigned int i;
    unsigned char temp = 0;
	
	
    init();

        [COLOR="#FF0000"] CLRWDT();[/COLOR]
        TXREG = 'U';
        while(1) {
	        while(!RCIF) {[COLOR="#FF0000"]CLRWDT();[/COLOR]}
	        while(!TRMT);
	        TXREG = RCREG;
	} 
    
}

If the above code works properly, then it means the WDT is not disabled..
 
Last edited:

thank you.. i changed the config bits and i got a new pic and its working fine....
i had a grounding problem in my pc so that might have damaged the controller ..
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top