Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 363 | johboh | 1 | /********************************************************************* |
| 2 | * |
||
| 3 | * UART access routines for C18 and C30 |
||
| 4 | * |
||
| 5 | ********************************************************************* |
||
| 6 | * FileName: UART.c |
||
| 7 | * Dependencies: UART.h |
||
| 8 | * Processor: PIC18, PIC24F/H, dsPIC30F, dsPIC33F |
||
| 9 | * Complier: Microchip C18 v3.03 or higher |
||
| 10 | * Complier: Microchip C30 v2.01 or higher |
||
| 11 | * Company: Microchip Technology, Inc. |
||
| 12 | * |
||
| 13 | * Software License Agreement |
||
| 14 | * |
||
| 15 | * This software is owned by Microchip Technology Inc. ("Microchip") |
||
| 16 | * and is supplied to you for use exclusively as described in the |
||
| 17 | * associated software agreement. This software is protected by |
||
| 18 | * software and other intellectual property laws. Any use in |
||
| 19 | * violation of the software license may subject the user to criminal |
||
| 20 | * sanctions as well as civil liability. Copyright 2006 Microchip |
||
| 21 | * Technology Inc. All rights reserved. |
||
| 22 | * |
||
| 23 | * This software is provided "AS IS." MICROCHIP DISCLAIMS ALL |
||
| 24 | * WARRANTIES, EXPRESS, IMPLIED, STATUTORY OR OTHERWISE, NOT LIMITED |
||
| 25 | * TO MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND |
||
| 26 | * INFRINGEMENT. Microchip shall in no event be liable for special, |
||
| 27 | * incidental, or consequential damages. |
||
| 28 | * |
||
| 29 | * |
||
| 30 | * Author Date Comment |
||
| 31 | *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||
| 32 | * Howard Schlunder 4/04/06 Copied from dsPIC30 libraries |
||
| 33 | * Howard Schlunder 6/16/06 Added PIC18 |
||
| 34 | ********************************************************************/ |
||
| 35 | #define THIS_IS_UART |
||
| 36 | |||
| 37 | #include "..\Include\Compiler.h" |
||
| 38 | #include "..\Include\StackTsk.h" |
||
| 39 | #include "..\Include\UART.h" |
||
| 40 | |||
| 41 | #if 1//defined(STACK_USE_UART) |
||
| 42 | |||
| 43 | BYTE ReadStringUART(BYTE *Dest, BYTE BufferLen) |
||
| 44 | { |
||
| 45 | BYTE c; |
||
| 46 | BYTE count = 0; |
||
| 47 | |||
| 48 | while(BufferLen--) |
||
| 49 | { |
||
| 50 | *Dest = '\0'; |
||
| 51 | |||
| 52 | while(!DataRdyUART()); |
||
| 53 | c = ReadUART(); |
||
| 54 | |||
| 55 | if(c == '\r' || c == '\n') |
||
| 56 | break; |
||
| 57 | |||
| 58 | count++; |
||
| 59 | *Dest++ = c; |
||
| 60 | } |
||
| 61 | |||
| 62 | return count; |
||
| 63 | } |
||
| 64 | |||
| 65 | |||
| 66 | #if defined(__dsPIC33F__) || defined(__dsPIC30F__) || defined(__PIC24H__) || defined(__PIC24F__) |
||
| 67 | |||
| 68 | |||
| 69 | /*************************************************************************** |
||
| 70 | * Function Name : putsUART2 * |
||
| 71 | * Description : This function puts the data string to be transmitted * |
||
| 72 | * into the transmit buffer (till NULL character) * |
||
| 73 | * Parameters : unsigned int * address of the string buffer to be * |
||
| 74 | * transmitted * |
||
| 75 | * Return Value : None * |
||
| 76 | ***************************************************************************/ |
||
| 77 | |||
| 78 | void putsUART2(unsigned int *buffer) |
||
| 79 | { |
||
| 80 | char * temp_ptr = (char *) buffer; |
||
| 81 | |||
| 82 | /* transmit till NULL character is encountered */ |
||
| 83 | |||
| 84 | if(U2MODEbits.PDSEL == 3) /* check if TX is 8bits or 9bits */ |
||
| 85 | { |
||
| 86 | while(*buffer != '\0') |
||
| 87 | { |
||
| 88 | while(U2STAbits.UTXBF); /* wait if the buffer is full */ |
||
| 89 | U2TXREG = *buffer++; /* transfer data word to TX reg */ |
||
| 90 | } |
||
| 91 | } |
||
| 92 | else |
||
| 93 | { |
||
| 94 | while(*temp_ptr != '\0') |
||
| 95 | { |
||
| 96 | while(U2STAbits.UTXBF); /* wait if the buffer is full */ |
||
| 97 | U2TXREG = *temp_ptr++; /* transfer data byte to TX reg */ |
||
| 98 | } |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | |||
| 103 | /****************************************************************************** |
||
| 104 | * Function Name : getsUART2 * |
||
| 105 | * Description : This function gets a string of data of specified length * |
||
| 106 | * if available in the UxRXREG buffer into the buffer * |
||
| 107 | * specified. * |
||
| 108 | * Parameters : unsigned int length the length expected * |
||
| 109 | * unsigned int *buffer the received data to be * |
||
| 110 | * recorded to this array * |
||
| 111 | * unsigned int uart_data_wait timeout value * |
||
| 112 | * Return Value : unsigned int number of data bytes yet to be received * |
||
| 113 | ******************************************************************************/ |
||
| 114 | |||
| 115 | unsigned int getsUART2(unsigned int length,unsigned int *buffer, |
||
| 116 | unsigned int uart_data_wait) |
||
| 117 | |||
| 118 | { |
||
| 119 | unsigned int wait = 0; |
||
| 120 | char *temp_ptr = (char *) buffer; |
||
| 121 | |||
| 122 | while(length) /* read till length is 0 */ |
||
| 123 | { |
||
| 124 | while(!DataRdyUART2()) |
||
| 125 | { |
||
| 126 | if(wait < uart_data_wait) |
||
| 127 | wait++ ; /*wait for more data */ |
||
| 128 | else |
||
| 129 | return(length); /*Time out- Return words/bytes to be read */ |
||
| 130 | } |
||
| 131 | wait=0; |
||
| 132 | if(U2MODEbits.PDSEL == 3) /* check if TX/RX is 8bits or 9bits */ |
||
| 133 | *buffer++ = U2RXREG; /* data word from HW buffer to SW buffer */ |
||
| 134 | else |
||
| 135 | *temp_ptr++ = U2RXREG & 0xFF; /* data byte from HW buffer to SW buffer */ |
||
| 136 | |||
| 137 | length--; |
||
| 138 | } |
||
| 139 | |||
| 140 | return(length); /* number of data yet to be received i.e.,0 */ |
||
| 141 | } |
||
| 142 | |||
| 143 | |||
| 144 | /********************************************************************* |
||
| 145 | * Function Name : DataRdyUart2 * |
||
| 146 | * Description : This function checks whether there is any data * |
||
| 147 | * that can be read from the input buffer, by * |
||
| 148 | * checking URXDA bit * |
||
| 149 | * Parameters : None * |
||
| 150 | * Return Value : char if any data available in buffer * |
||
| 151 | *********************************************************************/ |
||
| 152 | |||
| 153 | char DataRdyUART2(void) |
||
| 154 | { |
||
| 155 | return(U2STAbits.URXDA); |
||
| 156 | } |
||
| 157 | |||
| 158 | |||
| 159 | /************************************************************************* |
||
| 160 | * Function Name : BusyUART2 * |
||
| 161 | * Description : This returns status whether the transmission * |
||
| 162 | * is in progress or not, by checking Status bit TRMT * |
||
| 163 | * Parameters : None * |
||
| 164 | * Return Value : char info whether transmission is in progress * |
||
| 165 | *************************************************************************/ |
||
| 166 | |||
| 167 | char BusyUART2(void) |
||
| 168 | { |
||
| 169 | return(!U2STAbits.TRMT); |
||
| 170 | } |
||
| 171 | |||
| 172 | |||
| 173 | /*************************************************************************** |
||
| 174 | * Function Name : ReadUART2 * |
||
| 175 | * Description : This function returns the contents of UxRXREG buffer * |
||
| 176 | * Parameters : None * |
||
| 177 | * Return Value : unsigned int value from UxRXREG receive buffer * |
||
| 178 | ***************************************************************************/ |
||
| 179 | |||
| 180 | unsigned int ReadUART2(void) |
||
| 181 | { |
||
| 182 | if(U2MODEbits.PDSEL == 3) |
||
| 183 | return (U2RXREG); |
||
| 184 | else |
||
| 185 | return (U2RXREG & 0xFF); |
||
| 186 | } |
||
| 187 | |||
| 188 | |||
| 189 | /********************************************************************* |
||
| 190 | * Function Name : WriteUART2 * |
||
| 191 | * Description : This function writes data into the UxTXREG, * |
||
| 192 | * Parameters : unsigned int data the data to be written * |
||
| 193 | * Return Value : None * |
||
| 194 | *********************************************************************/ |
||
| 195 | |||
| 196 | void WriteUART2(unsigned int data) |
||
| 197 | { |
||
| 198 | if(U2MODEbits.PDSEL == 3) |
||
| 199 | U2TXREG = data; |
||
| 200 | else |
||
| 201 | U2TXREG = data & 0xFF; |
||
| 202 | } |
||
| 203 | |||
| 204 | |||
| 205 | #elif defined(__18CXX) |
||
| 206 | // |
||
| 207 | // PIC18 |
||
| 208 | // |
||
| 209 | |||
| 210 | |||
| 211 | char BusyUSART(void) |
||
| 212 | { |
||
| 213 | return !TXSTAbits.TRMT; |
||
| 214 | } |
||
| 215 | |||
| 216 | void CloseUSART(void) |
||
| 217 | { |
||
| 218 | RCSTA &= 0b01001111; // Disable the receiver |
||
| 219 | TXSTAbits.TXEN = 0; // and transmitter |
||
| 220 | |||
| 221 | PIE1 &= 0b11001111; // Disable both interrupts |
||
| 222 | } |
||
| 223 | |||
| 224 | char DataRdyUSART(void) |
||
| 225 | { |
||
| 226 | if(RCSTAbits.OERR) |
||
| 227 | { |
||
| 228 | RCSTAbits.CREN = 0; |
||
| 229 | RCSTAbits.CREN = 1; |
||
| 230 | } |
||
| 231 | return PIR1bits.RCIF; |
||
| 232 | } |
||
| 233 | |||
| 234 | char ReadUSART(void) |
||
| 235 | { |
||
| 236 | return RCREG; // Return the received data |
||
| 237 | } |
||
| 238 | |||
| 239 | void WriteUSART(char data) |
||
| 240 | { |
||
| 241 | TXREG = data; // Write the data byte to the USART |
||
| 242 | } |
||
| 243 | |||
| 244 | void getsUSART(char *buffer, unsigned char len) |
||
| 245 | { |
||
| 246 | char i; // Length counter |
||
| 247 | unsigned char data; |
||
| 248 | |||
| 249 | for(i=0;i<len;i++) // Only retrieve len characters |
||
| 250 | { |
||
| 251 | while(!DataRdyUSART());// Wait for data to be received |
||
| 252 | |||
| 253 | data = getcUART(); // Get a character from the USART |
||
| 254 | // and save in the string |
||
| 255 | *buffer = data; |
||
| 256 | buffer++; // Increment the string pointer |
||
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | void putsUSART( char *data) |
||
| 261 | { |
||
| 262 | do |
||
| 263 | { // Transmit a byte |
||
| 264 | while(BusyUSART()); |
||
| 265 | putcUART(*data); |
||
| 266 | } while( *data++ ); |
||
| 267 | } |
||
| 268 | |||
| 269 | void putrsUSART(const rom char *data) |
||
| 270 | { |
||
| 271 | do |
||
| 272 | { // Transmit a byte |
||
| 273 | while(BusyUSART()); |
||
| 274 | putcUART(*data); |
||
| 275 | } while( *data++ ); |
||
| 276 | } |
||
| 277 | |||
| 278 | #endif |
||
| 279 | |||
| 280 | |||
| 281 | #endif //STACK_USE_UART |