Serial comm using pic16f877a....

tshuck

Joined Oct 18, 2012
3,534
My previous post was just cleaning up your code, I didn't change anything...

I seem to have missed that line assigning SPEED if HI_SPEED != 1, again, you can't put multiple preprocessor directives on the same line. the preprocessor is not as smart as a compiler is...

Now, that said, try this:
Rich (BB code):
#include <htc.h> 
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);

 #define _XTAL_FREQ 20000000
 #define NINE 0     /* Use 9bit communication? FALSE=8bit */  
 #define BAUD 9600        
 #define RX_PIN TRISB2   
 #define TX_PIN TRISB5
 #define DIVIDER ((int)(_XTAL_FREQ/(16UL * BAUD) -1))
 #if NINE == 1
    #define NINE_BITS 0x40
 #else
    #define NINE_BITS 0 
#endif 
#if HIGH_SPEED == 1
    #define SPEED 0x4
#else
    #define SPEED 0 
#endif 

void Initialize (void);  //<----- YOU FORGOT THE "void's"  


void main ()
{     
    Initialize ();  <---- YOU FORGOT THE";" {     
    while(1)
    {    
     putchar('H');   //print the letter 'H'    
     putchar('i');   //print 'i' 
     putchar('\r');  // return to beginning of line   
     putchar('\n');  //new line     
    }       
}    

void Initialize ()  //<----- YOU FORGOT THE "void"
{
    RX_PIN = 1;        
    TX_PIN = 0;       
    SPBRG = DIVIDER;
    RCSTA = (NINE_BITS|0x90);
    TXSTA = (SPEED|NINE_BITS|0x20);
}
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Problem not solved!!
at putchar...

Make: The target "C:\Users\Abc\Documents\mplab\main.p1" is out of date.
Executing: "C:\Program Files (x86)\HI-TECH Software\PICC\9.83\bin\picc.exe" --pass1 C:\Users\Abc\Documents\mplab\main.c -q --chip=16F877A -P --runtime=default --opt=default -D__DEBUG=1 -g --asmlist "--errformat=Error [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s"
Warning [361] C:\Users\Abc\Documents\mplab\main.c; 29.1 function declared implicit int
Error [314] C:\Users\Abc\Documents\mplab\main.c; 37.1 ";" expected

********** Build failed! **********
 

tshuck

Joined Oct 18, 2012
3,534
Have you even tried to do it yourself?

putchar isn't defined! If you are trying to use the one in usart.h from the Hi-Tech samples, the function you want is "putch".

If you are going to use usart.h, copy the file and its source into the directory your main.c is in, add the files to your project, and do an #include for usart.h, then build your project again.

Use this in your main.c:
Rich (BB code):
#include <htc.h> 
#include "usart.h"
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);

 #define _XTAL_FREQ 20000000
 #define NINE 0     /* Use 9bit communication? FALSE=8bit */  
 #define BAUD 9600        
 #define RX_PIN TRISB2   
 #define TX_PIN TRISB5
 #define DIVIDER ((int)(_XTAL_FREQ/(16UL * BAUD) -1))
 #if NINE == 1
    #define NINE_BITS 0x40
 #else
    #define NINE_BITS 0 
#endif 
#if HIGH_SPEED == 1
    #define SPEED 0x4
#else
    #define SPEED 0 
#endif 

void Initialize ();


void main ()
{     
    Initialize (); 
    while(1)
    {    
		putch('H');   //print the letter 'H'    
		putch('i');   //print 'i' 
		putch('\r');  // return to beginning of line   
		putch('\n');  //new line     
    }       
}    

void Initialize () 
{
    RX_PIN = 1;        
    TX_PIN = 0;       
    SPBRG = DIVIDER;
    RCSTA = (NINE_BITS|0x90);
    TXSTA = (SPEED|NINE_BITS|0x20);
}
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
here is the code........
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
        {
        ch = HSerin();                // wait for a character
         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
    }
unsigned char HSerin()
    {
    while(!RCIF);                    // Wait for a character
    return RCREG;                    // return character
 
    }
 

tshuck

Joined Oct 18, 2012
3,534
HI,

I am using this code with this terminal program for window 7 plaese tell what setting to do iam unable to type in this window...
With the last module you linked, you may not be able to. That was suggested for inter-PIC communication. This is why I didn't want to talk about PICs talking to each other just yet...


That module claims 2.4GHz communication, whether that means Bluetooth, or some other isn't clear. Try powering the device, then attempt to pair with a new Bluetooth device, and try to pair with the module. Then, it should (if it connected properly) assign a COM port, attempt to connect to that COM port in RealTerm. Then you should be able to connect the PIC to the module and receive data in the terminal.
 

tshuck

Joined Oct 18, 2012
3,534
It is not s programmer. It is a communication port.

You can't write on the screen because you are writing to a port. when you are connected to a COM port, you write to the hardware registers, but that's more complicated than you need to know.... So, assume the terminal program will write to a device that it is connected to, e.g. the USB to TTL device...
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
You can't write on the screen because you are writing to a port.
I mean to say terminal window screen how to comm to computer using window 7 and all this pic uC and usb tll i linked in previous post!!
 

tshuck

Joined Oct 18, 2012
3,534
...see post #18, please actually read what I've written, I wrote that for you to understand what to do, yet you continue to ask the same question, this is now the forth time you've asked this question in this thread alone.
 
Top