16F877A and USART

Discussion in 'Embedded Systems and Microcontrollers' started by tigmaster, Jul 15, 2009.

  1. tigmaster

    Thread Starter New Member

    Jul 15, 2009
    3
    0
    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.
     
  2. millwood

    Guest

    there must be tons of such code floating around in cyber space.
     
  3. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
  4. tigmaster

    Thread Starter New Member

    Jul 15, 2009
    3
    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.

    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.
     
  5. tigmaster

    Thread Starter New Member

    Jul 15, 2009
    3
    0
  6. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    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:
    Code ( (Unknown Language)):
    1. /*
    2.   SERIAL COMMUNICATION (RS232)
    3.   ============================
    4.  
    5.   Using 9600 baud, one stop bit, no parity.
    6.   Baudrate 9600 baud => 104.167 microsec. per bit
    7.   TMR0 counts each 8th microsec. => 13.02 steps per bit
    8.  
    9.   ______       _____ _____ _____ _____ _____ _____ _____ _____ _____
    10.         |_____|_____|_____|_____|_____|_____|_____|_____|_____|
    11.          Start bit0  bit1  bit2  bit3  bit4  bit5  bit6  bit7  Stop
    12.  
    13.   NOTE: RS232 signal levels are different from the CMOS levels
    14.   used by the PIC device. A level converter is required:
    15.  
    16.         Logic level     CMOS       RS232
    17.         1 (default)     +Vcc       -2.8V to -15V
    18.         0               0 V        +2.8V to +15V
    19. */
    20.  
    21. #define _4_MHz    /* 4 MHz system clock */
    22.  
    23. // optional items
    24. //#define UseTMR0
    25. #define USE_CONST
    26.  
    27.  
    28. #pragma bit RS232_out @ PORTB.0
    29. #pragma bit RS232_in  @ PORTB.1
    30.  
    31. #ifdef _4_MHz
    32.  #define   TimeStartbit    4
    33.  #define   BitTimeIncr     13
    34. #endif
    35.  
    36. char bitCount, limit;
    37. char serialData;
    38.  
    39. #ifdef _4_MHz
    40.  #ifdef UseTMR0
    41.    #define delayStart   limit = TMR0;
    42.    #define delayOneBit      \
    43.      limit += BitTimeIncr;  \
    44.      while (limit != TMR0) \
    45.          ;
    46.  #else
    47.    #define delayStart   /* empty */
    48.    #define delayOneBit  {      \
    49.         char ti;               \
    50.         ti = 30;               \
    51.         do ; while( --ti > 0); \
    52.         nop();                 \
    53.      }  /* total: 5 + 30*3 - 1 + 1 + 9 \
    54.            = 104 microsec. at 4 MHz */
    55.  #endif
    56. #endif
    57.  
    58. void sendData( char dout)
    59. /* sends one byte */
    60. {
    61.     RS232_out = 0;  /* startbit */
    62.     delayStart
    63.     for (bitCount = 9; ; bitCount--)  {
    64.         delayOneBit
    65.         if (bitCount == 0)
    66.             break;
    67.         Carry = 1;  /* incl. stopbit */
    68.         dout = rr( dout);
    69.         RS232_out = Carry;
    70.     }
    71. }
    72.  
    73.  
    74. #define NText 12
    75.  
    76. #ifndef USE_CONST
    77. char text( char i)
    78. {
    79.     skip(i);
    80.    #pragma return[NText] = "Hello world!"
    81. }
    82. #else
    83. const char text[NText] = "Hello world!";
    84. #endif
    85.  
    86.  
    87. void main( void)
    88. {
    89.     char i;
    90.  
    91.     /* NOTE: some devices like the 16F877 have an on-chip analog to
    92.        digital converter with analog input pins that should be
    93.        configured. */
    94.     //ADCON1 = ..; // Refer to the data sheet for init values.
    95.  
    96.     PORTB = 0x03; // xxxx xx11
    97.     TRISB = 0xFE; // xxxx xx10
    98.     OPTION = 2;   // prescaler divide by 8
    99.  
    100.     for (i = 0; i < NText; i++)  {
    101.        #ifndef USE_CONST
    102.         sendData( text(i));  /* text string */
    103.        #else
    104.         sendData( text[i]);  /* text string */
    105.        #endif
    106.     }
    107.  
    108.   WaitNextIO:
    109.     while (RS232_in == 1)
    110.         ;
    111.  
    112.   Start_RS232:
    113.     /* sampling once per bit */
    114.     TMR0 = 0;
    115.     limit = TimeStartbit;
    116.     while (limit != TMR0)
    117.         ;
    118.     if (RS232_in == 1)
    119.         goto StartBitError;
    120.  
    121.     bitCount = 8;
    122.     do  {
    123.         limit += BitTimeIncr;
    124.         while (limit != TMR0)
    125.             ;
    126.         Carry = RS232_in;
    127.         serialData = rr( serialData);  /* rotate carry */
    128.     } while (-- bitCount > 0);
    129.  
    130.     limit += BitTimeIncr;
    131.     while (limit != TMR0)
    132.         ;
    133.     if (RS232_in == 0)
    134.         goto StopBitError;
    135.  
    136.     /* 'serialData' can be processed here */
    137.     /* NOTE: limited time (40 - 50 microsec.) */
    138.     goto WaitNextIO;
    139.  
    140.   StopBitError:
    141.     /* RS232 stopbit error,
    142.        waiting while line is low */
    143.     while (RS232_in == 0)
    144.         ;
    145.   StartBitError:
    146.     goto WaitNextIO;
    147. }
    148. [/i]
     
  7. millwood

    Guest

    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
     
  8. millwood

    Guest

    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.
     
  9. horsedorf

    New Member

    Jul 15, 2009
    5
    0
    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
     
  10. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    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.
     
  11. horsedorf

    New Member

    Jul 15, 2009
    5
    0

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

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