Serial comm using pic16f877a....

absf

Joined Dec 29, 2010
1,947
Only US$ 4.60? That is super cheap.

I used this one locally. Costs me US$13.00. And USB cable is optional....

http://www.cytron.com.my/viewProduct.php?pcode=UC00A&name=USB%20to%20UART%20Converter

The driver and sample codes are there for you to download.

It also directly supports the 16F877A in a startup kits SK40C. And the software for UART & LCD interfacing is also there in the doc section for downloading.

http://www.cytron.com.my/viewProduct.php?pcode=SK40C&name=Enhanced%2040%20pins%20PIC%20Start-Up%20Kit

Allen

p/s I have no connection with this company whatsoever.
 

t06afre

Joined May 11, 2009
5,934
Look at the datasheet and TABLE 10-4: BAUD RATES FOR ASYNCHRONOUS MODE (BRGH = 1). You are still using FOSC = 20 MHz, I presume 9600 or 19200 should be OK to use. You must also set up your PC to use this rate to
 

tshuck

Joined Oct 18, 2012
3,534
Look at the datasheet and TABLE 10-4: BAUD RATES FOR ASYNCHRONOUS MODE (BRGH = 1). You are still using FOSC = 20 MHz, I presume 9600 or 19200 should be OK to use. You must also set up your PC to use this rate to
Some of these bridge devices have a predefined baud rate. I have one that only works at 9600. That seems to be the typical rate for these things, and I would suggest starting there...read the device's datasheet to be sure...
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Hi,

I am reading data sheet till now i have written this what to do further??
Rich (BB code):
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000





main(){
    TRISC=0X00;
TX9=0;
TXEN=1;
SYNC=0;
BRGH=0;
SPEN=1
RX9=0;
CREN=0;
FERR=0;
OERR=0;
SPBRG=31;

    while(1){
 

tshuck

Joined Oct 18, 2012
3,534
Rritesh, you have been asked before to comment your code. Please do this at any time you are posting code. If you don't, what you think you are doing may not be what you are actually doing, and that disjunct may not come out for a long time.

Please comment your code.

Also, did you look at the example that came with your compiler?
 

t06afre

Joined May 11, 2009
5,934
Yes comments are needed. Both for your own part. But also for those who try to help you. I think it is time to introduce a new rule for young Rritesh. None comments will result in none help.
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
OK...............................


Rich (BB code):
#include <htc.h> 
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS); #define _XTAL_FREQ 20000000    
  main(){    
 TRISC=0X00; 
TX9=0;//Selects 8-bit transmission 
TXEN=1;//Transmit enabled 
SYNC=0;//Asynchronous mode 
BRGH=0;//Low speed
 SPEN=1//Enables single receive 
RX9=0;//Selects 8-bit reception
 CREN=0;//Disables continuous receive
FERR=0;//No framing error 
OERR=0;//No overrun error
 SPBRG=31;  //baund reg value for 96000 rate/.    
  while(1){
 

tshuck

Joined Oct 18, 2012
3,534
OK...............................


Rich (BB code):
#include <htc.h> 
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS); #define _XTAL_FREQ 20000000    
  main(){    
 TRISC=0X00; 
TX9=0;//Selects 8-bit transmission 
TXEN=1;//Transmit enabled 
SYNC=0;//Asynchronous mode 
BRGH=0;//Low speed
 SPEN=1//Enables single receive 
RX9=0;//Selects 8-bit reception
 CREN=0;//Disables continuous receive
FERR=0;//No framing error 
OERR=0;//No overrun error
 SPBRG=31;  //baund reg value for 96000 rate/.    
  while(1){
Thank you for commenting your code, now we know what you are attempting to do. I would suggest doing this any time you post code, you'll be asked for it anyway :).

However, you have yet to answer if you have looked up the example that came with your compiler. You should also tell us what compiler you are using. It looks like Hi-tech, but I think XC8 can use the same syntax...
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
I am using PIC16F877A with Hi tech C Compiler...
here is that sample.!!
Rich (BB code):
#include <htc.h>
#include <stdio.h>
#include "usart.h"

void 
putch(unsigned char byte) 
{
    /* output one byte */
    while(!TXIF)    /* set when register is empty */
        continue;
    TXREG = byte;
}

unsigned char 
getch() {
    /* retrieve one byte */
    while(!RCIF)    /* set when register is not empty */
        continue;
    return RCREG;    
}

unsigned char
getche(void)
{
    unsigned char c;
    putch(c = getch());
    return c;
}
 

tshuck

Joined Oct 18, 2012
3,534
Okay, so include these function definitions in your code and call them in your main...

So, here's some code that might help you....

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

This will write "Hi" to your terminal window repeatedly, provided proper settings. You can use this to ensure all parts are properly setup. mismatched setting will result in jumbled output on your window.
 

tshuck

Joined Oct 18, 2012
3,534
What will be the circuit diagram...??
I would suggest you look at the links I've posted, understand what they are telling you, draw out what you think might work, then you tell us the circuit diagram. I may be speaking for myself here, but we are not doing your project. We are not invested in your project. Yet, despite this, we answer your questions and attempt to provide you with answers. The least you could do is a little research and put a little bit of effort into it.

I have linked all the information you need to create a schematic. If you can't be bothered to read them, I can't be bothered to draw a schematic for you.

If, on the off chance, you have read them, what about the devices are you unsure of?
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
OK, i have edited the code..is this fine now???
I have installed PIC oshon simulator can't we test in it?

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
    Initialize ();





/*    TRISC=0X00;
TX9=0;
TXEN=1;
SYNC=0;
BRGH=0;
SPEN=1
RX9=0;
CREN=0;
FERR=0;
OERR=0;
SPBRG=31;*/

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












/* Serial initialization */
Initialize (){
    RX_PIN = 1;    
    TX_PIN = 1;          
    SPBRG = DIVIDER;         
    RCSTA = (NINE_BITS|0x90);
    TXSTA = (SPEED|NINE_BITS|0x20);
}
 
Top