16F877A and USART

Thread Starter

tigmaster

Joined Jul 15, 2009
3
I have turned to others for help after an intense Googling session. I can't seem to find anything on how to set up USART communications on this particular PIC much less any other for that matter.

My only goal is to establish 1-wire communications with devices such as the ds18b20 which is currently in my possession and my soon to arrive i-buttons.

I truly have no clue where to begin. I have read the data sheets for everything and nothing helps. I just can't seem to comprehend how to send a byte of data much less receive it.

My current setup is MPLAB so C is the language and a PIC16F877A. I am trying to communicate with a ds18b20 at the moment.

Any help would be much appreciated and if someone has an in depth explanation I would love to hear it.
 

Thread Starter

tigmaster

Joined Jul 15, 2009
3
Well, there are tons of things on it if I am programming in assembly or basic. Just nothing in C. I have an example program that works in C that communicates with a ds18b20 but I can't figure it out. It is all commented in Chinese and they somehow defined their own variable titled "uch" but I didn't think you could make classes in C.

Really if there was stuff floating around all over the place I wouldn't be here. I am the last person to directly ask for help. One reason being that people just tell me that I am not looking hard enough. It may be something obvious for someone else but when I turn to forums it is because I have exhausted all searching options.
 

SgtWookie

Joined Jul 17, 2007
22,230
I see.

Here's some sample serial code that came with a free C compiler called cc5x. Haven't tried it myself, but at least it's something to look at in C:
Rich (BB code):
/*
  SERIAL COMMUNICATION (RS232)
  ============================

  Using 9600 baud, one stop bit, no parity.
  Baudrate 9600 baud => 104.167 microsec. per bit
  TMR0 counts each 8th microsec. => 13.02 steps per bit

  ______       _____ _____ _____ _____ _____ _____ _____ _____ _____
        |_____|_____|_____|_____|_____|_____|_____|_____|_____|
         Start bit0  bit1  bit2  bit3  bit4  bit5  bit6  bit7  Stop

  NOTE: RS232 signal levels are different from the CMOS levels
  used by the PIC device. A level converter is required:

        Logic level     CMOS       RS232
        1 (default)     +Vcc       -2.8V to -15V
        0               0 V        +2.8V to +15V
*/

#define _4_MHz    /* 4 MHz system clock */

// optional items
//#define UseTMR0
#define USE_CONST


#pragma bit RS232_out @ PORTB.0
#pragma bit RS232_in  @ PORTB.1

#ifdef _4_MHz
 #define   TimeStartbit    4
 #define   BitTimeIncr     13
#endif

char bitCount, limit;
char serialData;

#ifdef _4_MHz
 #ifdef UseTMR0
   #define delayStart   limit = TMR0;
   #define delayOneBit      \
     limit += BitTimeIncr;  \
     while (limit != TMR0) \
         ;
 #else
   #define delayStart   /* empty */
   #define delayOneBit  {      \
        char ti;               \
        ti = 30;               \
        do ; while( --ti > 0); \
        nop();                 \
     }  /* total: 5 + 30*3 - 1 + 1 + 9 \
           = 104 microsec. at 4 MHz */
 #endif
#endif

void sendData( char dout)
/* sends one byte */
{
    RS232_out = 0;  /* startbit */
    delayStart
    for (bitCount = 9; ; bitCount--)  {
        delayOneBit
        if (bitCount == 0)
            break;
        Carry = 1;  /* incl. stopbit */
        dout = rr( dout);
        RS232_out = Carry;
    }
}


#define NText 12

#ifndef USE_CONST
char text( char i)
{
    skip(i);
   #pragma return[NText] = "Hello world!"
}
#else
const char text[NText] = "Hello world!";
#endif


void main( void)
{
    char i;

    /* NOTE: some devices like the 16F877 have an on-chip analog to
       digital converter with analog input pins that should be
       configured. */
    //ADCON1 = ..; // Refer to the data sheet for init values.

    PORTB = 0x03; // xxxx xx11
    TRISB = 0xFE; // xxxx xx10
    OPTION = 2;   // prescaler divide by 8

    for (i = 0; i < NText; i++)  {
       #ifndef USE_CONST
        sendData( text(i));  /* text string */
       #else
        sendData( text);  /* text string */
       #endif
    }

  WaitNextIO:
    while (RS232_in == 1)
        ;

  Start_RS232:
    /* sampling once per bit */
    TMR0 = 0;
    limit = TimeStartbit;
    while (limit != TMR0)
        ;
    if (RS232_in == 1)
        goto StartBitError;

    bitCount = 8;
    do  {
        limit += BitTimeIncr;
        while (limit != TMR0)
            ;
        Carry = RS232_in;
        serialData = rr( serialData);  /* rotate carry */
    } while (-- bitCount > 0);

    limit += BitTimeIncr;
    while (limit != TMR0)
        ;
    if (RS232_in == 0)
        goto StopBitError;

    /* 'serialData' can be processed here */
    /* NOTE: limited time (40 - 50 microsec.) */
    goto WaitNextIO;

  StopBitError:
    /* RS232 stopbit error,
       waiting while line is low */
    while (RS232_in == 0)
        ;
  StartBitError:
    goto WaitNextIO;
}
 

millwood

Joined Dec 31, 1969
0
Well, there are tons of things on it if I am programming in assembly or basic. Just nothing in C. I have an example program that works in C that communicates with a ds18b20 but I can't figure it out. It is all commented in Chinese and they somehow defined their own variable titled "uch" but I didn't think you could make classes in C.
language doesn't really matter, does it? Basic and C use the same thought process so if you have a piece of code in Basic, it shouldn't be that difficult to get it into C.

here is a google search on picc and ds1820. I am sure you will find something there.

http://www.google.com/search?rlz=1C1GGLS_enUS291US303&sourceid=chrome&ie=UTF-8&q=picc+ds1820
 

millwood

Joined Dec 31, 1969
0
the most difficult part about dealing with mcu is that you have to force yourself to read the datasheet THOROUGHLY. you will not understand most of it in the first 20 or 30 times. But as you start programming it, you will.

My experience is that if you haven't read a datasheet by 50x, you haven't learned about the mcu.

the same goes with ds1820.
 

horsedorf

Joined Jul 15, 2009
5
Howdy there,

I'm reading this and I'm getting a few different things out of it.
It sounds like you have some kind of system that you are trying to put together but are jumbling up all the parts of it, for example, you talk about a usart, and matlab and c, but you also talk about a pic processor and a one-wire part you want to talk to.. I think what would help us help you is for us to have a better understanding of exactly what parts you need help with, which means, give us a notion of the system configuration and what peices of the system are giving you issues.

I'll take a stab at this and make an assumption about what you are trying to do.

you have
1) A pc running matlab which is programmed in C.
2) You wish to use the serial port on the pc to comunicate with the pic processor.
3) You wish to have the pic processor's usart talk to the serial port on the pc.
4) you wish to have the pic processor use an IO pin to talk to the one wire part.

so you then have a few places where you need code.
You need to have some code in matlab that talks to the serial port.
you need some code on the pic that can communicate with matlab via its USART and so you need that whole pile of stuff on the pic that sets up the usart, and then sends data out the usart to matlab. and finally, you need some code that does the one wire communications that also runs on the pic. Further, you wish to write all of the pic code in.. C? is that right?

Horsedorf
 

AlexR

Joined Jan 16, 2008
732
Nice try but wrong on almost every point.

The OP has a Mirochip PIC microcontroller.
He is running Microchip's IDE called MPLAB on his computer.
MPLAB can run various languages including C and is used to write software for the PIC microcontroller.
The PIC has an on-board USART but what the OP intends to do with it is not clear.
The OP also wants to communicate with a ds18b20 chip using the ibutton protocol and is being advised to google 'Ibutton' or 'one wire protocol' for plenty of information and examples.
 

horsedorf

Joined Jul 15, 2009
5
Nice try but wrong on almost every point.

The OP has a Mirochip PIC microcontroller.
He is running Microchip's IDE called MPLAB on his computer.
MPLAB can run various languages including C and is used to write software for the PIC microcontroller.
The PIC has an on-board USART but what the OP intends to do with it is not clear.
The OP also wants to communicate with a ds18b20 chip using the ibutton protocol and is being advised to google 'Ibutton' or 'one wire protocol' for plenty of information and examples.

DOH!! MPLAB, *WHY* did I read that as MATLAB? durrrrr...

(cue Gilda Radner voice) Oh! Nevah mind. :)
 
Top