Tuesday, October 9, 2012
PIC Serial Communication Tutorial (UART)
This is a study of the PIC serial communication. First we should study about RS232. It's just a name for a standard that has propagated from generation to generation of computers. The first computers had serial ports that used RS232, and even current computers have serial ports (or at least USB ports that act like RS232 ports). Back in the day, serial information needed to be passed from devices like printers, joysticks, scanners, etc to the computer. The simplest way to do this was to pass a series of 1s and 0s to the computer. Both the computer and the device agreed on a speed of information - 'bits per second'. A computer would pass image data to a printer at 9600 bits per second and the printer would listen for this stream of 1s and 0s expecting a new bit every 1/9600 = 104us (104 micro-seconds, 0.000104 seconds). As long as the computer output bits at the pre-determined speed, the printer could listen.
Zoom forward to today. Electronics have changed a bit. Before they were relatively high power, high voltage devices. The standard that is 'RS232' dictates that a bit ranges from -12V to +12V. Modern electronics do not operate at such high positive and negative voltages. In fact, our PIC runs 0V to 5V. So how do we get our 5V micro to talk the RS232 +/-12V voltages? This problem has been solved by the IC manufacturers of the world. They have made an IC that is generically known as the MAX232 (very close to RS232, no?).
The MAX232 is an IC originally designed by a company called Maxim IC that converts the +/-12V signals of RS232 down to the 0/5V signals that our PIC can understand. It also boosts the voltage of our PIC to the needed +/-12V of the RS232 protocol so that a computer can understand our PIC and vice versa. To get our PIC IC sending serial characters to a computer, we have to send these serial signals through a MAX232 circuit so that the computer receives +/-12V RS232 signals. Don't worry if you're working with a chip labeled 'ICL232' or 'ST232' - these are just generics of the MAX232. Everyone says 'MAX232'. The ICs all function the same and nearly all have the same pinout.
Posted by Wow at 8:29 AM 0 comments
Labels: Embeded Systems Tutorial, PIC
USB to Serial Converter using AVR microcontrollers
This project is based on the AVR microcontrollers, which can be used to make a USB to Serial port converter based on Object
Deveopment's V-USB (Software-USB on AVR), and the CDC (Communication
Device Class) protocol was extended over it
The AVR-CDC
converts USB and RS-232C signals using the AVR micro- controller which
has no on-chip USB interface. . AVR-CDC enables PC to
communicate with the USB device through virtual COM port.
The
basic idea of using CDC protocol over Low-speed USB is based on Kyosuke
Ishikawa's experiment in 2005. To make it stable and practical,
Christian Starkjohann in Object Development helped me modifying his
V-USB stack. Since three endpoints and the bulk transfer on low-speed
device violates the USB standard, I added a tiny patch driver on
Windows' USB stack.
Although this
technology is quite experimental, it may be useful to interface your
original system to PC easily.
Basically the circuit is very simple, but it
requires a certain amount of skills to control. If you need practical or
stable solutions, or you are not familiar with electronics nor
installing drivers, use the dedicated chip from vendors like FTDI.
The
back door to the low-speed bulk transfer is gradually closing on the
newer OS. After enjoying this USB technology, switch to the HID protocol
or to MCU having on-chip USB controller.
CDC-232
CDC-232
creates a virtual COM port on PC that doesn't have real RS- 232C port.
It enables RS-232C communication (without control lines), after
connecting the device and installing the driver.
Virtual COM Port over Software-USB
Usage
Write
the program to AVR, build the circuit, and connect the device to PC's
USB port. Install the driver on Windows. Access the device through
generated virtual COM port from terminal software or your application.
Control lines (DTR, DTS, RTS, CTS) are not used by the host application.
Set the terminal software as "no flow-control".
Windows requests the driver installation again when connected to other USB port. Detect the previously installed driver automatically. Another COM number will be assigned. If you set serial number in AVR (rebuild with modified usbconfig.h), you can get the same COM port at any USB port. However, you cannot connect multiple CDC devices of the same serial number.
Before detaching the device, close the COM port in terminal software or in your application. Otherwise, you cannot connect to the device again because of the broken file handle. Restart the terminal software or your application then. Switch to the fast transfer mode using "lowcdc.vbs" to get the baudrate higher than 9600bps.
Windows requests the driver installation again when connected to other USB port. Detect the previously installed driver automatically. Another COM number will be assigned. If you set serial number in AVR (rebuild with modified usbconfig.h), you can get the same COM port at any USB port. However, you cannot connect multiple CDC devices of the same serial number.
Before detaching the device, close the COM port in terminal software or in your application. Otherwise, you cannot connect to the device again because of the broken file handle. Restart the terminal software or your application then. Switch to the fast transfer mode using "lowcdc.vbs" to get the baudrate higher than 9600bps.
Loop-back test on ATtiny45 version
Schematics
These
schematics are for ATtiny45/85, ATtiny2313/AT90S2313, and
ATmega8/48/88/168. Their firmware are all ISP-programmable. The red LED
drops the USB voltage from 5V to 3.3V, and provides to AVR. The current
is about 10mA, and is not enough to drive other circuit. When connecting
to other MCU, connect Gnd and connect TxD and RxD in crossing way. R4
limits the leak current when the MCU's Vcc is 5V. You can omit if the
Vcc is equal. R5 protects the TxD pin when it shortened to Gnd. You can
omit both R4 and R5 if you connect to the RS- 232C driver like MAX232.
Use crystal oscillator. Although ceramic resonator works well in most
cases, it becomes unstable if the frequency deviation is bigger.
CDC-232 for ATtiny45-20
ATtiny45/85
uses internal RC oscillator and PLL. It is calibrated by USB signal
when connected. UART is implemented by software. It is not enough for
high speed data transfer. If the TxD and the RxD are inverted (rebuild
with -DUART_INVERT option), you can directly connect to RS-232C
line. 1200 - 4800bps, 8N1
CDC-232 for ATtiny2313-20
ATtiny2313/AT90S2313 has 2KB program
memory. Although the baudrate is configured automatically, some
functions are omitted. 600 - 38400bps, 8N1
CDC-232 for ATmega8/48/88-20
ATmega8/48/88's internal UART is configured from the PC. The flow-control (RTS/CTS) is supported.
600 - 38400bps, data 7/8, parity N/E/O, stop 1/2
600 - 38400bps, data 7/8, parity N/E/O, stop 1/2
Schematics
CDC-232 for ATtiny45-20
CDC-232 for ATtiny2313-20
CDC-232 for ATmega8/48/88-20
Driver and code click here for CDC home page
courtesy : microcontrollerprojects00.blogspot.in
tags: Serial Communication, Uart Project, USB to RS 232 converter, USB projects, USB circuits, USB to UART, Embedded projects, AVR, Atmega8, RS 232projects, Atiny
Posted by Wow at 8:22 AM 0 comments
Labels: Microcontroller Projects
Make your own Extra UART For Your PIC
Software UART Library
The mikroC PRO for PIC provides routines for implementing Software UART communication. These routines are hardware independent and can be used with any MCU.
The Software UART Library provides easy communication with other devices via the RS232 protocol.
Important :
The Software UART library
implements time-based activities, so interrupts need to be disabled when using
it. |
Library Routines
Soft_UART_Init
Prototype
|
char Soft_UART_Init(char *port, char rx_pin, char
tx_pin, unsigned long baud_rate, char inverted);
|
Returns
|
|
Description
|
Configures and initializes the
software UART module.
Parameters :
Software UART routines use Delay_Cyc routine. If requested baud rate is
too low then calculated parameter for calling Delay_Cyc exceeds
Delay_Cyc argument range.
If requested baud rate is too high
then rounding error of Delay_Cyc argument corrupts Software UART timings.
|
Requires
|
Nothing.
|
Example
|
This will initialize software UART
and establish the communication at 14400 bps:
char error;
...
error = Soft_UART_Init(&PORTC, 7, 6, 14400, 0); // Initialize Soft UART at 14400 bps
|
Soft_UART_Read
Prototype
|
char Soft_UART_Read(char * error);
|
Returns
|
Byte received via UART.
|
Description
|
The function receives a byte via
software UART.
This is a blocking function call
(waits for start bit). Programmer can unblock it by calling Soft_UART_Break routine.
Parameters :
|
Requires
|
Software UART must be initialized
before using this function. See the Soft_UART_Init routine.
|
Example
|
char data_, error;
...
// wait until data is received
do
data =
Soft_UART_Read(&error);
while (error);
// Now we can work with data:
if (data_) {...}
|
Soft_UART_Write
Prototype
|
void Soft_UART_Write(char udata);
|
Returns
|
Nothing.
|
Description
|
This routine sends one byte via
the Software UART bus.
Parameters :
|
Requires
|
Software UART must be initialized
before using this function. See the Soft_UART_Init routine.
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.
|
Example
|
char some_byte = 0x0A;
...
// Write a byte via Soft UART
Soft_UART_Write(some_byte);
|
Soft_UART_Break
Prototype
|
void Soft_UART_Break();
|
Returns
|
Nothing.
|
Description
|
Soft_UART_Read is blocking routine and it can
block the program flow. Calling this routine from the interrupt will unblock
the program execution. This mechanism is similar to WDT.
Note :
Interrupts should be disabled before using Software UART routines again (see
note at the top of this page).
|
Requires
|
Nothing.
|
Example
|
char data1, error,
counter = 0;
void interrupt() {
if (INTCON.T0IF)
{
if
(counter >= 20) {
Soft_UART_Break();
counter =
0; // reset counter
}
else
counter++; //
increment counter
INTCON.T0IF =
0; // Clear Timer0
overflow interrupt flag
}
}
void main() {
OPTION_REG =
0x04; // TMR0 prescaler
set to 1:32
...
if
(Soft_UART_Init(&PORTC, 7, 6, 9600, 0) == 0)
Soft_UART_Write(0x55);
...
// try
Soft_UART_Read with blocking prevention mechanism
INTCON.GIE = 1; // Global interrupt enable
INTCON.T0IE =
1; // Enable Timer0
overflow interrupt
data1 =
Soft_UART_Read(&error);
INTCON.GIE =
0; // Global interrupt
disable
}
|
Example Program use two UART
/* This program inputs from on chip UART and output to Soft
UART*/
char i, error, byte_read='1',uart_rd='1'; // Auxiliary variables
void main(){
TRISB = 0x00; // Set PORTB as
output (error signalization)
PORTB = 0;
UART1_Init(9600); // No error
error =
Soft_UART_Init(&PORTC,1 , 0, 9600, 0); // Initialize Soft UART at 14400 bps
if (error > 0) {
PORTB =
error; //
Signalize Init error
while(1) ; // Stop program
}
Delay_ms(100);
UART1_Write_Text("Enter Text Here (0 to exit)");
do {
if
(UART1_Data_Ready()) {
UART1_Write(10);
UART1_Write(13);
uart_rd =
UART1_Read();
Soft_UART_Write(uart_rd);
Delay_ms(100);
}
}while(uart_rd!='0');
}
tags: extra UART, circuit for extra UART, sample program for UART, UART circuit, PIC Tutorial, MikroC PIC Tutorial, Extra UART, 2 UART, Additional UART, Embedded Tutorial, Embedded
Posted by Wow at 8:12 AM 0 comments
Labels: PIC
Using RB0 External Interrupt of PIC 16F877A
This is a demonstration for the RB0 external interrupt of PIC 16F877A, below are features of the external interrupt that we are using.
An input ( depending on how we set it up ) on the RB0 triggers the interrupt, either the signal goes from 0 to 1, called the rising edge, or as the signal goes from 1 to 0, called the falling edge. This sets the interrupt flag, and if the interrupt is enabled ( and we are not already in an interrupt ) the microcontroller goes to the interrupt subroutine.
The use of the RB0/Int to should be clear that the use of RB0/Int function of a picmicro requires the setting of the:
GIE(7): set to 1 to enable global interrupts
INTE(4): set to 1 to enable interrupts on pin RB0
PEIE(6): to disable other periferal interrupts
and the set of the INTEDG bit(6) of the OPTION_REG register simply to select the rising or falling edge, of the signal.
When switch is pressed RB0 goes from 0 to 1 causing an interrupt
which calls the interrupt() routine. The interrupt routine increment the value
of PORD by one.
Code
void interrupt(void)
{
PORTD++; //Increment PORTD value by one
//delay_ms(500);
INTCON.INTF = 0; // clear the interrupt flag
}
void main(void)
{
TRISB = 0x01;
TRISD = 0x00;
INTCON.GIE = 1; //Enable Global Interrupt
INTCON.INTE = 1; //Enable RB0/INT external
Interrupt
INTCON.PEIE = 0; //Disable all unmasked
peripheral interrupt
OPTION_REG.INTEDG = 1; //Interrupt on rising edge
PORTD=0;
do
{
} while(1);
}
tags: PIC, PIC 16F877A, PIC 16F877A circuits, PIC 16F877A external interupt, PIC 16F877A projects, MikroC PIC Tutorial, pic interrupt, PIC Microcontroller Interrupt, MikroC, Microcontroller
Posted by Wow at 8:05 AM 0 comments
Labels: Embeded Systems Tutorial
Subscribe to:
Posts (Atom)