Serial comm using pic16f877a....

tshuck

Joined Oct 18, 2012
3,534
Rich (BB code):
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000
 
 
unsigned char  HSerin(void);
void HSerout(unsigned char ch), HSerinit(void);
 
void main(void)                        // program entry
    {
    int index = 0;
    unsigned char ch;
    ADCON1 = 0x6;                    // Analogue off                    
    HSerinit();
       __delay_ms(150);
    while(1)                        // endless Loop
        {
       
         HSerout(ch);                // Echo back
 
        }
    }
 
void HSerinit()
    {
    TRISC = 0xC0;                    // should ideally be set
    SPBRG = 129;                    // 20Mhz xtal 9600 BAUD
    TXSTA = 0x24;                    // TXEN and BRGH
    RCSTA = 0x90;                    // SPEN and CREN
    }
 
void HSerout(unsigned char ch)
    {
    while(!TXIF);                    // Wait for module to finish
    TXREG = ch;                        // ready to send
    }
I was thinking to transmit the dta from uc to PC for this will this will work OK??
TXREG = 'a';
You would need to put 'a' into the TXREG, but other than that, yes...

I am not familiar with RealTerm, but it seems some other people have had issues doing this with it... perhaps you could try TeraTerm?
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Rich (BB code):
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000
 
 
unsigned char  HSerin(void);
void HSerout(unsigned char ch), HSerinit(void);
 
void main(void)                        // program entry
    {
    int index = 0;
    unsigned char ch;
    ADCON1 = 0x6;                    // Analogue off                    
    HSerinit();
       __delay_ms(150);
    while(1)                        // endless Loop
        {
        
         HSerout(ch);                // Echo back
 
        }
    }
 
void HSerinit()
    {
    TRISC = 0xC0;                    // should ideally be set
    SPBRG = 129;                    // 20Mhz xtal 9600 BAUD
    TXSTA = 0x24;                    // TXEN and BRGH
    RCSTA = 0x90;                    // SPEN and CREN
    }
 
void HSerout(unsigned char ch)
    {
    while(!TXIF);                    // Wait for module to finish
    TXREG = 'a';                        // ready to send
    }
 

tshuck

Joined Oct 18, 2012
3,534
Try:
Rich (BB code):
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000
 
void  HSerinit();
 
void main(void)                        // program entry
    {
    int index = 0;
    unsigned char ch;
    ADCON1 = 0x6;                    // Analogue off                    
    HSerinit();
       __delay_ms(150);
    while(1)                        // endless Loop
        {
        
         while(!TXIF);                    // Wait for module to finish
         TXREG = 'a';                // print 'a'
 
        }
    }
void HSerinit()
{
    TRISC = 0x80;                    // TX was an input!
    SPBRG = 129;                    // 20Mhz xtal 9600 BAUD
    TXSTA = 0x24;                    // TXEN and BRGH
    RCSTA = 0x90;                    // SPEN and CREN
}
 

tshuck

Joined Oct 18, 2012
3,534
OK, then how to have circuit i mean what part i show to u??
actually i am using this board for many time for testing and using like led display controller....
everything work fine on board!
If that LED is connected, try turning that on when the program starts...

Do you have a MCLR resistor? I don't see one....
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Do you have a MCLR resistor? I don't see one....
yes i have 4.7k with switch to reset also all work fine!!

If that LED is connected, try turning that on when the program starts...
Is this fine??

Rich (BB code):
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000
 
void  HSerinit();
 
void main(void)                        // program entry
    {
TRISB=0X00;
PORTB=0X00;
    int index = 0;
    unsigned char ch;
    ADCON1 = 0x6;                    // Analogue off                    
    HSerinit();
       __delay_ms(150);
    while(1)                        // endless Loop
        {
RB7=1;
        
         while(!TXIF);                    // Wait for module to finish
         TXREG = 'a';  
RB7=0;             
 
        }
    }
void HSerinit()
{
    TRISC = 0x80;                    // TX was an input!
    SPBRG = 129;                    // 20Mhz xtal 9600 BAUD
    TXSTA = 0x24;                    // TXEN and BRGH
    RCSTA = 0x90;                    // SPEN and CREN
}
 
Top