advertisement
mikroC for dsPIC making it simple...
miik ro fo r ds C - C Compiler for Microchip dsPIC microcontrollers
Software UART Library
mikroC for dsPIC provides library which implements software UART. These routines are hardware independent and can be used with any MCU. You can easily communicate with other devices via RS232 protocol – simply use the functions listed below.
Note: This library implements time-based activities, so interrupts need to be disabled when using Soft UART.
Library Routines
Soft_Uart_Init
Soft_Uart_Read
Soft_Uart_Write
Soft_Uart_Init
Prototype
Description
void Soft_Uart_Init(unsigned short *port, unsigned short rx,
unsigned short tx, unsigned short baud_rate, unsigned short inverted);
Initalizes software UART. Parameter port specifies port of MCU on which RX and TX pins are located; parameters rx and tx need to be in range 0–7 and cannot point at the same pin; baud_rate is the desired baud rate. Maximum baud rate depends on dsPIC’s clock and working conditions. Parameter inverted
, if set to non-zero value, indicates inverted logic on output.
Soft_Uart_Init needs to be called before using other functions from Soft UART
Library.
Example
Soft_Uart_Init(&PORTB, 1, 2, 9600, 0); page
Miik ro le tr niik De ve op en to ol -- Bo ks C om piil er
mikroC for dsPIC making it simple...
miik ro fo r ds C - C Compiler for Microchip dsPIC microcontrollers
Soft_Uart_Read
Prototype
Returns
Description
Requires
unsigned short Soft_Uart_Read(unsigned short *received);
Returns a received byte.
Function receives a byte via software UART. Parameter received will be not-zero if the transfer was successful. This is a non-blocking function call, so you should test the received manually (check the example below).
Soft UART must be initialized and communication established before using this function. See
Soft_Uart_Init
.
Example
unsigned short data, rec;
...
do
data = Soft_Uart_Read(rec);
while (!*rec);
/* Now we can work with data: */
if (data) {...}
Soft_Uart_Write
Prototype
Description
Requires
Example
void Soft_Uart_Write(unsigned short data);
Function transmits a byte (data) via UART.
Soft UART must be initialized and communication established before using this function. See
Soft_Uart_Init
.
Be aware that during transmission, software UART is incapable of receiving data – data transfer protocol must be set in such a way to prevent loss of information.
unsigned short some_byte = 0x0A;
...
Soft_Uart_Write(some_byte); page
Miik ro le tr niik D ev lo me t oo s B oo ks C om piil er
mikroC for dsPIC making it simple...
miik ro fo r ds C - C Compiler for Microchip dsPIC microcontrollers
Library Example
The example demonstrates simple data exchange via software UART. When dsPIC
MCU receives data, it immediately sends the same data back. If dsPIC is connected to the PC (see the figure below), you can test the example from mikroC terminal for RS232 communication, menu choice Tools › Terminal.
unsigned short data = 0, ro = 0;
unsigned short *rec;
void main() {
// Init (8 bit, 2400 baud rate, no parity bit..)
Soft_Uart_Init(&PORTB, 1, 2, 2400, 0);
do {
do { data = Soft_Uart_Read(rec); // Receive data
}
while (!*rec);
Soft_Uart_Write(data); // Send data via UART
} while (1);
}//~!
Miik ro le tr niik De ve op en to ol -- Bo ks C om piil er page
advertisement
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Related manuals
advertisement
Table of contents
- 1 mikroC for dsPIC
- 2 Reader's Note
- 3 Table of Contents
- 9 mikroC for dsPIC IDE
- 11 Code Editor
- 14 Code Explorer
- 15 Debugger
- 19 Error Window
- 20 Statistics
- 23 Integrated Tools
- 25 Keyboard Shortcuts
- 29 Building Applications
- 30 Projects
- 32 Source Files
- 35 Compilation
- 36 Error Messages
- 39 mikroC for dsPIC Language Reference
- 40 dsPIC Specifics
- 41 mikroC Specifics
- 46 Lexical Elements
- 48 Tokens
- 49 Constants
- 56 Keywords
- 57 Identifiers
- 58 Punctuators
- 62 Objects and Lvalues
- 64 Scope and Visibility
- 66 Name Spaces
- 67 Duration
- 69 Types
- 70 Fundamental Types
- 75 Derived Types
- 94 Types Conversions
- 97 Declarations
- 108 Functions
- 114 Operators
- 127 Expressions
- 129 Statements
- 139 Preprocessor
- 149 mikroC for dsPIC Libraries
- 150 Built-In Routines
- 156 Library Routines
- 157 ADC Library
- 159 CAN Library
- 176 CANSPI Library
- 190 Compact Flash Library
- 202 EEPROM Library
- 205 SPI Ethernet Library
- 255 Flash Memory Library
- 263 I2C Library
- 268 Keypad Library
- 272 LCD Custom Library (4-bit interface)
- 278 LCD8 Custom Library (8-bit interface)
- 282 Graphic LCD Library
- 293 T6963C Graphic LCD Library
- 305 Manchester Code Library
- 310 MMC Library
- 321 OneWire Library
- 326 PS/2 Library
- 329 PWM Library
- 332 RS-485 Library
- 338 Software I2C Library
- 342 Software SPI Library
- 345 Software UART Library
- 348 Sound Library
- 350 SPI Library
- 357 UART Library
- 363 DSP (Digital Signal Processing) Library
- 375 Util Library
- 381 ANSI C Ctype Library
- 385 ANSI C Math Library
- 391 ANSI C Stdlib Library
- 395 ANSI C String Library
- 401 Conversions Library
- 406 Trigonometry Library
- 407 Sprint Library
- 412 SPI Graphic LCD Library
- 423 Port Expander Library
- 431 Contact Us