Tuesday, October 9, 2012

High power LED torch

This is a simple and very old circuit idea, even though i am publishing here for those who haven't seen this before and for beginners in electronics circuits. It' s nothing but an ordinary LED of 1.5 V circuit realized using salvaged components!!

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.

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.

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


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



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
  • 2 - error, requested baud rate is too low
  • 1 - error, requested baud rate is too high
  • 0 - successful initialization
Description
Configures and initializes the software UART module.
Parameters :
  • port: port to be used.
  • rx_pin: sets rx_pin to be used.
  • tx_pin: sets tx_pin to be used.
  • baud_rate: baud rate to be set. Maximum baud rate depends on the MCU’s clock and working conditions.
  • inverted: inverted output flag. When set to a non-zero value, inverted logic on output is used.
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 :
  • error: Error flag. Error code is returned through this variable.
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 :
  • udata: data to be sent.
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

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

Monday, September 17, 2012

DFT and IDFT using MATLAB



AIM:
To find DFT and IDFT of a sequence.
APPARATUS REQUIRED:
Hard ware: IBM PC or compatible
Soft ware : Matlab 6.5 or higher
THEORY:
In mathematics, the discrete Fourier transform (DFT) is a specific kind of discrete transform, used in Fourier analysis. It transforms one function into another, which is called the frequency domain representation, or simply the DFT, of the original function (which is often a function in the time domain). The DFT requires an input function that is discrete. Such inputs are often created by sampling a continuous function, such as a person's voice. The discrete input function must also have a limited (finite) duration, such as one period of a periodic sequence or a windowed segment of a longer sequence. Unlike the discrete-time Fourier transform (DTFT), the DFT only evaluates enough frequency components to reconstruct the finite segment that was analyzed. The inverse DFT cannot reproduce the entire time domain, unless the input happens to be periodic. Therefore it is often said that the DFT is a transform for Fourier analysis of finite-domain discrete-time functions.
FFT algorithms are so commonly employed to compute DFTs that the term "FFT" is often used to mean "DFT" in colloquial settings. Formally, there is a clear distinction: "DFT" refers to a mathematical transformation or function, regardless of how it is computed, whereas "FFT" refers to a specific family of algorithms for computing DFTs. The terminology is further blurred by the (now rare) synonym finite Fourier transform for the DFT, which apparently predates the term "fast Fourier transform"
The discrete Fourier transform (DFT) is given by:
The inverse discrete Fourier transform (IDFT) is given by:

PROGRAM:
clc;
clear all;
close all;
x=input('enter the sequence');
x1=fft(x);
subplot(2,2,1);
stem(imag(x1));
title('imaginary fft');
disp('fft');
disp(x1);
subplot(2,2,2);
stem(real(x1));
title('real fft');
y=ifft(x1);
 subplot(2,2,3);
stem(imag(y));
title('imaginary ifft');
disp('ifft');
disp(y);
subplot(2,2,4);
stem(real(y));
title('real ifft');


Enter the sequence     [1   2   3   4]



Tags: MATLAB basics, MATLAB programs, MATLAB signal generation, Learn MATLAB, IDFT, DFT, IDFT using MATLAB, DFT using MATLAB, IDFT programs, DFT programs, IDFT and DFT programs