Implementing Discrete Fourier Transform in Atmega32 to make an audio spectrum analyzer


"All waveforms, no matter what you scribble or observe in the universe, are actually just the sum of simple sinusoids of different frequencies."

Hi,
I am just refreshing the basics of fourier transform. I am not an expert. Now I did a small audio spectrum analyzer(0 - 10KHz) on a 16x2 character lcd using an atmega32 microcontroller. Since I am refreshing from the basics, so I started with simple DFT. Also, I believe I should learn to walk before running. So I am not straight away going towads the FFT, which is nothing but the fastest and a bit complicated algorithm to find DFT.(I will try it later, as soon as possible)
        DFT is too slow compared to FFT. My lcd spectum analyzer doesn't need a great speed like that of an FFT, now if it is capable of providing a speed of around 30 frame/second, then it is more than enough for visualizing the audio spectrum on an LCD. But any way, in my case I can roughly achieve around 100 frames/second(any way it is too high refresh rate for a 16x2 lcd, not recommended also :-)). My audio sampling rate is 20KHz for 32 point DFT. Since the transform result is symmetric, I need to use only the first half, ie the first 16 results. So, it means, it can display upto 10KHz spectrum. So the resolution is 10KHz/16 = 625Hz.
         I have tried to improve the speed of DFT computation. If it is an N point DFT, it needs to find (N^2)/2 sin and cos values. For a 32 point DFT, it needs to find 512 sine and cosine. Before finding the sine and cosine, we need to find the angle(degree) which takes some processor time, so I implemented a lookup table for that. Next two tables are for sine and cosine. I didn't used any float or double since it takes more processing time in 8 bit avr, instead I implemented the sine and cosine lookups as 16bit intiger, by multiplying the real sine and cosine values by 10000. Then after finding the transform, finally I need to divide each result by 10000. This eliminates the need of using float or double and makes it more faster. Now I can calculate 120  32-point DFT operation/sec which is more than enough for my small spectrum analyzer.

Generating AUDIO ECHO using Atmega32 microcontroller


(video demo of echo generation using atmega32)
Introduction:
Hi,
While I was studying at 10th standard, I used to play with small electronic circuits mostly based on transistor bc547 - bc557 pair. At that time, I just asked myself, I can amplify audio signals using few transistor combinations, but how can I make an echo effect which I used to hear in almost all loudspeaker announcements? I can't imagine what I can do with few transistors and resistors to make such an effect! I have no answer at that time because it was beyond my limitation..
  But now I can do this very easily by a simple digital signal processing using a microcontroller. It's concept is very simple, ie we need to apply a proper delayed feedback in digital samples with in a circular buffer. I did this using an atmega32 microcontroller and it worked fine. This is simple but really an interesting project. Not only an echo, but we can do a lot of fun with this type of small DSP experiments if we have considerably large RAM in the mcu...

An attempt to mount an SD/MMC connected at parallel port of PC (a linux device driver)

Introduction:

Hi, for the last few days, I was engaged on a mad project, ie I was trying to access as SD card directly via parallel port of my desktop PC with linux. At first, I successfully did the SD/MMC initialization and tested it by reading and writing few sectors. Later I got a simple demo block driver code (kernel module) from net. Then I modified it and used my SD card code along with it and made a driver for the parallel port - SD/MMC card. Then after connecting the card to parallel port and inserting the module, a new device file is generated (/dev/sbd). After that I successfully mounted it and accessed it similar to that of a usb pendrive or any other external memory device. I have tested it with 64MB MMC, 512MB SD and a 1GB MMC and all worked fine.. But the only drawback is the speed of data transfer is around 200kbps only..:-( It takes 1 minute for transferring 1.5MB file to or from the card. Also the entire system may get hanged while the data is transferring...
But any way, it worked, that's enough for me..... :-)

Running PYTHON (pymite-09) on an Arduino MEGA 2560

Hi, I got a new toy from farnell. It is an Arduino mega 2560 board
in.element14.com/atmel


 

Now it is the first time I am using an arduino board. Arduino mega 2560 is really a great product. The chip got a flash of 256KB, RAM of 8KB and EEPROM of 4KB. Also, the data sheet of Atmega2560 says that we can extend the RAM (by external) upto 64KB. Another most important feature of this stuff is that it have 54 Digital I/O pins (of which 14 provide PWM output). It is clocked with a 16MHz crystal osc. Also the board contains another atmega16 (just above the crystal) which is pre-programmed as a USB to UART converter which enables a serial communitation between the atmega2560 and  PC via USB. The product is shipped with a bootloader inside which make it easy to program it via the same USB-UART channel. So the channel got two functions, ie programming the chip and serial communication with PC. From the arduino home page we can download the arduino IDE for appropriate OS.
      I heard arduino makes people lazy. ;-) But now I understood it is true. Because it is so easy to program an arduino using the arduino IDE. It have an arduino language (it is c++) with a great library functions those are very much user friendly and I heard people saying, "if it is arduino, any one can program it". Now I believe statement is almost right because in arduino IDE, we doesn't need to know any thing about the processor, it's registers and even its real port address also... Every thing is grouped and numbered in the board not according to the real PORTA, PORTB etc but according to it's functioning like PWM, ANALOG, DIGITAL, COMMUNICATION etc.
The arduino IDE got a lot of working examples, what we need to do is, we need to select the board first and then set the serial port and later we can open any example and then simply press 'upload'. It is ready!!!!!!!! ;-)
So no need to explain it more because it is so simple..

Running Python (pymite) on Arduino mega 2560
Appart from the arduino IDE, I would like to use the board seperately to try assembly programming and the normal C programming which I normally used to do using avr-gcc. 1 year ago I heard some one ported python for 8 bit devices but I couldn't use it since I didn't have a chip with enough RAM and flash. But since now I have this arduino mega 2560, I just downloaded pymite (p14p) and I could see a port for arduino mega there. It is nothing but the same "python" programming language written for microcontrollers. It have many limitations but still its interesting to see the python running on this 8 bit chip. I just build it for arduino mega 2560 and was successful (a small trouble was there in building, but any way it's okay).

Drawing geometric figures on a PAL TV using ATmega32 (128x64 resolution)


Photos of my TV screen:





Introduction:
    I am interested to draw lines, square, rectangle, circle etc on my TV screen. At first I was confused where to start. While thinking about it, a pencil and an eraser came to my mind. If we have a good pencil, eraser and a paper, then we can draw on it according to our own logic. If we use the pencil with compass and scale, we can draw circle, line, box etc on the paper. This is the basic idea I implemented in this small hobby project.

AVR based monochrome signal generation for a PAL TV


Introduction:
       I have learned some thing about TV in one of my B.tech semester but I forgot most of them. Now I refreshed a few basics and tried to implement a monochrome PAL TV signal generator using an AVR micrcontroller. I was using PIC earlier but later I jumped to AVR because I loves the USBASP programmer, the free avr-gcc compiler and the user friendly architecture of AVR microcontroller .Also at any time, if I feel little bit lazy, they I can try arduino also..;-)

An attempt to show grayscale images on an LED dot matrix display with software PWM

Introduction:
      This is just a time pass hobby project, I am trying to display some pictures (JPEG/PNG/BMP etc) on my 8x8 led dotmatrix display.  You can see photos of my 8x8 led dotmatrix display showing some grayscale pictures. The main thing which you may notice is that the brightness of each led depends on the picture information respective to the scaled(8x8) pixel of the original picture.
The original pictures are drawn using GIMP image editor in linux and saved as png. The scaled pictue is sent from the PC to PIC via UART.
 Any way, this will be the worst monochrome display showing pictures:-)
 
Photos: 
 Here you could see the led display showing few pictures opened in gimp editor... You could compare the pictures and observe the PWM effect on the led matrix.


Multitasking in AVR (A demo to run 7 tasks on an atmega32)

Hi,
Here I am introducing a simple task switching on an AVR as a demo... I did this just to get familiarized with the AVR assembly language programming.. Now I believe I learned it to an extent. Also I loves assembly language because it offers a great deal of power to use all of the features of the processor.

Introduction:
     Switching multiple tasks on a same CPU is the one of the major function of an operating system. What I did now is a time sharing multitasking (time multiplexing) on an AVR. Here an atmega32 is configured to use Round-Robin Multitasking. Round-Robin allows quasi-parallel execution of several tasks. Tasks are not really executed concurrently but are time-sliced (the available CPU time is divided into time slices). 
         Here, in my code below, it consist of 7 independent tasks and those are switched from one to another on a timer interrupt. 'May be' this could be considered as a simple & very very basic RTOS demo. Here I am mainly focusing on the core part ie the "task switching". 

Scheduling algorithm used: Round-robin (RR)
(one of the simplest scheduling algorithm)

Video demo:

Each led on the demo represents a task. Task1 (right most led, WHITE) is a software PWM task. All other tasks (2 to 7 from right to left) are toggling tasks. If you concentrate on a single LED, U may notice that the particular LED is toggling with a constant delay and is independent of others.

Working: 
 Here, total RAM (2KB) is divided among 7 tasks almost equally in such a way that each one get around 300 bytes of RAM space... This 300 bytes is considered

TV remote controller 160KHz High Quality Stereo MMC WAV player using ATMEGA32



(updated the complete source code + makefile + hex + asm + .out files on bitbucket repository) 
link is provided below the source code
Introduction:
This is my first AVR based hobby project and the most successful one compared to my all previous stuff. I am 100% satisfied with this work.. Few months ago, I tried to make a wav player using a PIC16F877A. It worked anyway, but the audio quality was not so good for higher sampling rate because that chip doens't have enough ram and thus I couldn't implement a good data buffer. But when I bought an atmega32 microcontroller, the first thing came to my mind is to make a good wav player...Now, I have completed my work and the audio quality is really amazing...

 NOW I can say that, my wav player IS ABLE TO PLAY 8 BIT MONO/STEREO with maximum bitrate of 1300kbps for mono and 1600kbps for stereo ... ie it can play an 8 bit mono wav of sampling frequency upto 160KHz and stereo upto 96KHz  without any noise or trouble!!!!! (at OSC 16.450MHz).

LINUX DEVICE DRIVER FOR A 16x2 LCD MODULE CONNECTED AT PARALLEL PORT

This is a small parallel port char driver for printing text on a 16x2 lcd module connected at parallel port of a PC. I did this as a part of learning linux kernel-module programming. May be this could be considered as a hello world device driver.

INTRODUCTION:

 As we know, everything in Linux is treated as a file, even hardware devices like serial ports, hard disks, and scanners. In order to access these devices, a special file called a device node has to be present. All device nodes are stored in the /dev directory. Here, my 16x2 lcd connected to parallel port is also treated as a file and is accessed via a device node....
    Now, after connecting the 16x2 lcd to parallel port (as in my circuit diagram), and then inserting the driver module, a message "DRIVER INSERTED" is displayed on the 16x2 LCD. Later, if we make a character special file (node) with major number as 61, and if we write any string to it (echo HELLO > file), then it will be displayed on the 16x2 LCD. Now, if we write a long string to it, then it is displayed on the lcd by scrolling it from bottom to top until the string is displayed completely ...



My own AVR ISP programmer using PIC16f877a and python!

Introduction: 

(don't skip to read the note below)
I recently purchased few AVR microcontrollers. I don't know much about AVR since I am using it for first time. Any way, I have some experience on working with PIC and MSP430. To program AVR using USB, I came to know that USBASP is the best and cheap choice. But any way, I thought of making my own ISP programmer (both PC software and burning hardware) for AVR using my fav PIC (PIC16f877A) microcontroller just for getting familiarised with the process of loading the binary into the AVR flash.
      From PC side, coding is done on python. In short words, the python script reads the hex file (intel hex format) and make some ascii conversion on it and then send it to the PIC16F877A line by line. Now by using some commands for isp programming, the PIC communicate with AVR and transfer the data to it via SPI. I have tested my programmer on atmega8, atmega16 and atmega32.

Note:
 I did this just for learning some thing about the burning process, please don't consider this seriously because this may have many bugs , I don't know and I didn't tested much.. Also I am not interested to continue this because it will be time wasting as there are already many cheap and efficient avr programmers available now. So please don't complain about bugs and don't use this for any important purpose ;-)


 Intel hex format:

Intel hex format is well explained in wikipedia. I learned about it from there. Here is the link towards the wikipedia page. An intel hex file contains the binary information about the flash address and data. It is a text file format. Each line of intel hex file consist of 6 parts. See below picture.

Python maps mouse movements to an led matrix display controlled by a PIC


Introduction:

This is a small "toy paint" on an LED dot matrix display which is controlled by a mouse connected to a PC... We have seen the MS PAINT in windows. This is a small 'TOY PAINT' for my small 8x10 LED dot matrix display :-) Here, using a PC mouse, I could draw and erase picture on a small 80 pixel display made by LEDs. At first I thought of making a mouse controlled robotic arm but due to lack of few mechanical parts, I just postponed that and now made this toy paint as a part of familiarizing  two python modules named pygame and pyserial. The heart of the display is a PIC16F877A micro controller. Now, from the PC side, every thing is done on python. Now, if I am using an RF module in between the PIC and USB to UART converter, I can do the same process remotely... The same idea could be applied in robotics for smooth control of activities remotely using a computer mouse...

MULTITASKING IN MSP430

Task switching is one of the main function of an operating system. We could 'feel' a computer is doing multiple tasks at a time. The OS is actually switching the tasks one by one in a circular manner and executing each one for a small period of time and we feel it is doing all tasks at a time.... 

A small demo of multitasking in MSP430G2231 microcontroller:

     Here, three independent tasks are to be switched on every timer interrupt.  The CPU registers used in previous task is to be stored some where and the register values of the task to which it is switched is to be retained as before. Also, the switched task should run from where it is paused earlier.
Below is my asm code for running three independent tasks in an MSP430G2231 microcontroller. (just a demo, stack depth is very limited due to the limited RAM)

DS89C430 development with linux (SDCC compiler and pyserial based hex loader )

I got an 8051 mini board containing DS89C430 mcu from a junk.. Then I started collecting some information about it's development in linux. I came to know that the burning of DS89C430 is very easy since it have a built in UART bootloader.. So I had written a small python code (based on pyserial) to make it more easier.....


INTRODUCTION:

The DS89C430, DS89C440, and DS89C450 offer the highest performance available in 8051-compatible microcontrollers. They feature newly designed processor cores that execute instructions up to 12 times faster than the original 8051 at the same crystal speed. Typical applications will experience a speed improvement up to 10x. At 1 million instructions per second (MIPS) per megahertz, the microcontrollers achieve 33 MIPS performance from a maximum 33MHz clock rate. Almost all programs written for the original 8051 will be working in the DS89C430, similar to that of 8051 , with improved execution speed. The reverse may not be possible because it have many extra features which is not there in the traditional 8051 microcontroller......

Bulk file name encoder using python

Python is really an interesting and user friendly scripting language. It is easy to write codes in python compared to C and some other high level languages..
     It will be much difficult to rename or encode all the file names manually to a particular pattern if the number of files in a folder are too large... For example, If we want to rename all files (for example 1000 files) in a folder to just numbers ,say, 1,2,3 etc with in seconds, this could be easily done using python.
     Below code could be used for the purpose. Also, we could easily rename it back to the original file names using the same script (just double on the rename.py).  A backup file containing the original file names is created in the same folder so that we could rename the numbered files back to its original file names at any time... The renaming to numbers and the reverse could be done just by clicking the same  "rename.py" script copied in the folder containing the files to be bulk renamed and this is very simple and easy process...

MSP430 assembly language programs

I got a simple and user friendly msp430 assembler for linux. It is the "naken430asm", developed by Michael Khon. It is free to download from his website http://www.mikekohn.net

 Here are few example programs which I had done as a part of learning msp430 assembly language programming..
(microcontroller used -> MSP430G2231)
(assembler  used         ->naken430asm)


To familiarize the assembly language programming, I did some basic codes and tested it in the launchpad. Those codes are below...

C and corresponding Assembly (MSP430G2231)

 Microcontroller programming become much simple if we use high level language like C. But it doesn't means that we could ignore the assembly language. In some situations, we might me forced to use a bit of assembly codes. Assembly language is a symbolic representation of a processor's native code. Using machine code allows the programmer to control precisely what the processor does. It offers a great deal of power to use all of the features of the processor. The resulting program is normally very fast and very compact. Using assembly programs, timings, for example, could be calculated very precisely.
     Here, I am just posting some basic C codes and their corresponding assembly (generated by msp430-gcc). By comparing the compiler generated assembly and the source C file, I hope, we will get a good idea of how the compiler effectively utilize the stack, various addressing modes, functions, global variables, local variables, pointers etc etc.
    For getting the disassembled view of the final hex code, we could use the msp430-objdump command. Disassembled view is the ideal one and it will contain each and every instruction from the reset address to the end of the program.

MMC WAV PLAYER USING PIC16F877A






Video of my PIC16F877A based MMC wav player 
 (video updated on 06/09/2011 (improved audio quality)):

Later i tried to access an MMC with FAT16 file system using the same PIC16F877A and then successfully played an 8 bit mono wav file of byterates from 8000 to 48000. Here, it reads the first sector of MMC (boot record) to obtain some important information like no of reserved sectors, start of partition, no of t Per FAT ,maximum Root Directory Entries ,Bytes per Sector etc. Using those information, it is possible to calculate the starting sector address of FAT1, FAT2, root directory, data area etc....But due to the limited RAM in PIC16F877A, it is required to read a complete sector multiple number of times to access the entire 512 bytes....

Python-Basic Operators


Python language supports following type of operations:
[Arithematic, Comparison, Logical, Assignment, Conditional]

Arithematic operators are:

+ ,  - ,  * ,  / ,  % , ** (exponent) , // (division but digits after decimal in quotient is removed)

Comparison Operators: 
  • ' == ' Checks if the value of two operands are equal or not, if equal then condition becomes true.
  • ' != ' Checks if the value of two operands are equal or not, if not equal then condition becomes true.
  • ' <> ' is similar to ' != '
  • ' > '  checks if the value of left operand is greater than right operand and, if yes then condition become true. Similarly there are comparison operators '<' , '>=', '<=' as in C.