mikroC for dsPIC Users Manual

Add to my manuals
431 Pages

advertisement

mikroC for dsPIC Users Manual | Manualzz

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

Was this manual useful for you? Yes No
Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Related manuals

Download PDF

advertisement

Table of contents