1. Guinness1759

    Thread Starter Member

    Dec 10, 2010
    64
    0
    Hi,
    I have PIC16F1827 and I'm using MPLAB IDE which uses Hi-Tech C for my c compiler. I'm having trouble locating simple code to enable serial port and send data out on serial port. I'm not receiving any serial data. If anyone has some concise code examples let me know. I went to the examples in Hi-Tech C and they are really long. I'ved used serial commands before with Freescale and it was only 4 or 5 commands, so I don't see why it needs to be so complicated. Also for some reason the serial portion of the PIC datasheet is very confusing. I was surprised since everything else seemed to be easy to follow.
     
  2. madhusudan.pigeonsembs

    New Member

    Mar 17, 2011
    23
    2
  3. cheezewizz

    Active Member

    Apr 16, 2009
    82
    10
    He's using HiTech C and you've just pointed him towards a different compiler's site... Have a look in the samples for Hitech C they would've been installed along with the compiler and necessary libraries.
     
    Guinness1759 likes this.
  4. Guinness1759

    Thread Starter Member

    Dec 10, 2010
    64
    0
    I'm getting this error (bold) when running the following code

    Code ( (Unknown Language)):
    1.  
    2. #include    <sci.h> // Serial Header file
    3.  
    4.  
    5. sci_Init(9600,SCI_EIGHT);
    6. sci_PutByte(0xaa);  /* send 0xaa when device is ready */
    7.  
    8. I left the irrelevant code out to save you time scanning.
    9.  

    Error [500] ; 0. undefined symbols: _sci_PutByte(high-tech-new.obj) _sci_Init(high-tech-new.obj)

    sci.h looks las follows

    Code ( (Unknown Language)):
    1.  
    2. #define FOSC    (4000000L)
    3. #define SCI_EIGHT   (0)
    4. #define SCI_NINE    (1)
    5.  
    6. unsigned char   sci_Init(unsigned long int, unsigned char);
    7. void            sci_PutByte(unsigned char);
    8. unsigned char   sci_GetByte(void);
    9. void            sci_PutNinth(unsigned char);
    10. unsigned char   sci_GetNinth(void);
    11. unsigned char   sci_GetFERR(void);
    12. unsigned char   sci_CheckOERR(void);
    13.  
    14.  
    This is the example.txt they have for serial data.

    Code ( (Unknown Language)):
    1.  
    2. The sci functions and macros to implement asynchronous
    3. communication on the Serial Communication Interface (SCI)
    4. module.
    5.  
    6. CONTENTS
    7. ########
    8.  
    9. 1 Setting Up
    10. 2 Asynchronous Function Definitions
    11. 3 Examples
    12. 4 Using Interrupts
    13.  
    14.  
    15. 1) Setting Up
    16. #############
    17.  
    18. The file sci.h should be #included into your source
    19. files. This file contains a macro which specifies
    20. the Fosc frequency. This affects the baud rate
    21. calculations and should be adjusted to suit your
    22. application.
    23.  
    24.  
    25. 2) Asynchronous Function and Macro Definitions
    26. ##############################################
    27.  
    28. unsigned char
    29. sci_Init(unsigned long int baud, unsigned char ninebits)
    30. ~~~~~~~~
    31.  
    32. This function is used to set up the appropriate registers
    33. associated with the sci module. Specify the desired
    34. baud rate. If this is possible using the current
    35. value of Fosc, the value specified will be used - see
    36. the PIC manual for details on baud rate selection.
    37. If ninebits is true, 9-bit data values will be used for
    38. both transmission and reception. The function returns
    39. true if the desired baud rate could not be achieved;
    40. false otherwise.
    41.  
    42. SCI_EIGHT and SCI_NINE
    43. ~~~~~~~~~     ~~~~~~~~~
    44.  
    45. These macros can beused with sci_Init() to indicate
    46. eight- and nine-bit communication, respectively.
    47.  
    48. void
    49. sci_PutByte(unsigned char byte)
    50. ~~~~~~~~~~~
    51.  
    52. This function is used to send an 8-bit quantity to
    53. the SCI. The function first waits until TXIF is
    54. set then loads the transmit register.
    55.  
    56. sci_PutNinth(bitnine)
    57. ~~~~~~~~~~~~
    58.  
    59. This macro is used to send the ninth bit to
    60. the SCI when in nine-bit data mode. It should be
    61. called before calling sci_PutByte().
    62.  
    63. unsigned char
    64. sci_GetByte(void)
    65. ~~~~~~~~~~~
    66.  
    67. This function waits until the receive register is not
    68. empty and returns the received 8-bit data.
    69.  
    70. unsigned char
    71. sci_GetNinth(void)
    72. ~~~~~~~~~~~~
    73.  
    74. This function waits until the receive register is not
    75. empty and returns the received ninth-bit when in nine-
    76. bit mode. It should be called before calling
    77. sci_GetByte()as the ninth bit is lost after calling
    78. this function.
    79.  
    80. unsigned char
    81. sci_GetFERR(void)
    82. ~~~~~~~~~~~
    83.  
    84. This function waits until the receive register is not
    85. empty and returns the received frame error status bit.
    86. It should be called before calling sci_GetByte() as
    87. frame error information is lost after calling this
    88. function.
    89.  
    90. unsigned char
    91. sci_CheckOERR(void)
    92. ~~~~~~~~~~~~~
    93.  
    94. This function checks for an overrun error and resets the
    95. receiver, by toggling the CREN bit, and returns true if
    96. this has occured. The function returns false if no error
    97. occured. If an overrun error occurs, the receiver is
    98. completely disabled.
    99.  
    100.  
    101. 3) Examples
    102. ###########
    103.  
    104. // 8-bit mode at 9600 baud using polling
    105.  
    106. sci_Init(9600,SCI_EIGHT);
    107. sci_PutByte(0xaa);  /* send 0xaa when device is ready */
    108. data = sci_GetByte();   /* read data when device is ready */
    109.  
    110. // 9-bit mode at 19200 baud using polling
    111.  
    112. sci_Init(19200,SCI_NINE);
    113. sci_PutNinth(0x00); /* ninth bit is zero */
    114. sci_PutByte(0xff);  /* and data is 0xff */
    115.  
    116. if(sci_getNinth())
    117.     ; /* bit nine was true */
    118. data = sci_GetByte();   /* get 8-bit value */
    119. if(sci_CheckOERR())
    120.     ; /* an overrun error occured */
    121.  
    122.  
    123. 4) Using Interrupts
    124. ###################
    125.  
    126. To use interrupts with the SCI, there are several things
    127. which must be attended to.
    128.  
    129. Firstly there must be an interrupt service routine which
    130. can process the interrupts when they occur. There can be
    131. only one interrupt routine associated with the PIC
    132. processor and so this routine must be able to service
    133. any interrupt which occurs, not just those associated
    134. with the SCI. The following example shows an ISR which
    135. handles reception and transmission. The RCIF, TXIF,
    136. RCIE and TXIE bits can be used to ascertain what caused
    137. the interrupt. In the example, if the receive register
    138. is full (RCIF), a character is read from the SCI and
    139. echoed back to the source. If the transmitter is
    140. empty and the transmitter interrupts are enabled then
    141. another byte from a message is transmitted. If all
    142. the message has been sent, the interrupts are disabled
    143. to prevent further transmission. If any other interrupts
    144. are enabled, the code to handle these will also have to
    145. appear in this ISR.
    146.  
    147. unsigned char byte, i;
    148.  
    149. void interrupt isr(void)
    150. {
    151.     if(RCIF)
    152.     {
    153.         byte = sci_GetByte();
    154.         sci_PutByte(byte);  /* echo char */
    155.     }
    156.     if(TXIF && TXIE)
    157.     {
    158.         sci_PutByte(message[i]);
    159.         if(message[++i] == '\0')
    160.             TXIE = 0;   /* finished */
    161.     }
    162. }
    163.  
    164. The interrupts can be enabled by setting the appropriate
    165. bits in the INTCON register. To use the SCI, enable the
    166. PEIE bit. If other interrupts are to be used, set the
    167. bits corresponding to these interrupts in this register.
    168. The global interrupt enable bit must also be set. The
    169. TXIE and RCIE bits can then be used to mask and unmask
    170. the interrupts at various points in your code.
    171.  
    172. sci_Init(9600, SCI_EIGHT);
    173.  
    174. PIR1 = 0;   /* clear any pending interrupts */
    175. PEIE = 1;   /* enable perhipheral interrupts */
    176. GIE  = 1;   /* global interrupts enabled */
    177.  
    178. /* perform other setup */
    179.  
    180. RCIE = 1;   /* unmask receiver interrupts... */
    181.  
    182. /* an interrupt could now come from the SCI receiver
    183. at any time. */
    184.  
    185. /* process data read in, if any */
    186.  
    187. RCIE = 0;   /* mask receive interrupts */
    188.  
    189. /* no more interrupts can come from the receiver */
    190.  
    191. See the PIC appropriate manual for further details
    192. on the use of PIC interrupts.
    193. [/i]


    I'm getting the error, any suggestions? It looks like the sci_GetByte function is defined in sci.h, but there is no actual function.
     
    Last edited: May 27, 2011
  5. Guinness1759

    Thread Starter Member

    Dec 10, 2010
    64
    0
    bump.....................
     
  6. Guinness1759

    Thread Starter Member

    Dec 10, 2010
    64
    0
    bump, $25 in paypal if anyone can help me solve this problem.
     
  7. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Read the data sheet for your pic. It will give you all that you need. Then look at examples in the "C:\Program Files\HI-TECH Software\PICC\9.xx\samples"
    If you use USB to RS232 converter or a serial port. You will need a TTL to RS232 level converter. A typical circuit for this purpose. Is the max232 in some variant.
     
    Guinness1759 likes this.
  8. Guinness1759

    Thread Starter Member

    Dec 10, 2010
    64
    0
    Thanks I think I'm getting closer, looks like the problem was I was using the serial example code and not the usart example code. Ustart is way simpler and it looks like it is what I need.
     
  9. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    You must by all means feel free to post your code. Then we can have a look at it. And give you advice
     
Loading...