Thursday, November 17, 2011

Mega16 Development Board : Sample Programs – Source Codes

Posted by Omkar on April 3, 2009

Mega16 Development board comes with sample programs. On this post I will be posting updated versions of sample codes and related documentation. You can download this sample programs, modify and use them free of cost. All programs are written for CodeVisionAVR Compiler, unless otherwise specified.

Following table summarizes the provided sample programs :
(Link for the .RAR file is provided at the end of the post)

Example Number/ Name of the folder

Program demonstrates use of  input/output registers in AVR.
- It will monitor SW1 and SW2 on the kit
- When SW1 is pressed, LEDs will be flashed in one pattern.
When SW2 is pressed, LEDs will be flashed in another pattern- Blinks the LED on PB3 at 31Hz using Timer0.
- Timer0 is configured in CTC mode. Timer resets when compare match  occurs between OCR0 and TCNT0.
- Also output toggle is enabled  in  this mode.  This makes pin OC0 (i.e. PB3 in mega16)  toggle on every compare match.- Blinks the LED on PB0 at 1Hz using Timer1 CTC Interrupt.
- Timer1 is configured to run in CTC mode. Timer resets when compare match occurs between OCR1A and TCNT1. And interrupt is generated.
- State of the PB0 is toggled on every interrupt.- Demonstrates the PWM using Timer0.
- LED connected on PB3 will faded in and faded out continuously using PWM.- Generates sound using the CTC mode of timer 1.
- By changing the OCR1A value, we can generate square waves of different frequencies.
- Output is available on PD5(OC1A) pin.
- Connect this pin to on speaker using external wire to listen the sound.
- Press SW1 or SW2 to start melody.
- Program also prints the frequency being generated on serial interface.- Demonstration of simple edge detector robo-car code- Demonstration of simple line follower- Demo program for the development board. Shows how to use printf, ADC, LCD, I2C, Infrared remote.- Demonstares the use of L293D connected on PD.
- Press SW0 to stop the motors
- Press SW1 to start the motors in one direction
- Press SW2 to start the motors in reverse direction- LED Blinking using RTOS threads. FreeRTOS based. WinAVR compiler.
- Use eclipse IDE and select File> Switch Workspace> Other …, and select this folder.- Toggles all I/O lines of uC at the rate of 1Hz. Helpful for debugging the board.

DOWNLOAD here

Be the first to like this post.This entry was posted on April 3, 2009 at 3:01 pm and is filed under AVR microcontroller. Tagged: ATmega16, AVR Micocontroller, CodeVisionAVR, sample source code. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.


View the original article here

AVRDude Tutorial : Burning hex files using USBasp and AVRdude

AVRDude is excellent program for burning hex code into microcontroller. USBasp is awesome USB based programmer for the AVR. In this tutorial we will see how to use AVRdude for burning hex files into AVR microcontroller using USBasp.

AVRdude is not readily available for windows installation. Windows version of AVRdude is bundled with WinAVR compiler. Before we start : Download WinAVR from here.

Introduction:

In order to program any microcontroller you need the .HEX file. It is nothing but the machine code for the microcontroller. This file is generated by the corresponding assembler software, which converts assembly code into machine code. Assembly can be  produced by third party cross compiler software or can be handwritten.

After you finish the installation of WinAVR, AVRdude executables can be found at :
\bin

Complete documentation of AVRdude can be found at:
\doc\avrdude\avrdude.pdf

Connections:

- Connect the USBasp to PC.
- Connect SPI programming pins of USBasp to the AVR microcontroller. Following figure shows sample schematic diagram, if you have different AVR, then connect MOSI,MISO, SCK, RESET and GND pins of that uC to corresponding pins of USBasp.

USBaspandAVRconnections 

- Give +5V supply to the microcontroller.
- VIMP: If you are burning a fresh microcontroller, close the Slow Serial Clock jumper of USBasp. Since many brand new microcontroller are factory programmed for internal 1MHz oscillator. USBasp uses very high speed serial clock for faster programming. Thus you will have to specifically tell USBasp to use slow serial clock. This setting is done by above mentioned jumper.

NOTE: If you have uC which has internal oscillator enabled and after the programming you are not planning to change its fuse bits back to external clock setting, then you can skip the crystal.

Executing AVRdude:

Fortunately AVRdude is command line tool, so that you can be very sure of what you are doing with your uC Or Unfortunately AVRdude is command line tool, so you will have to spend little time to get familiar with it :)

- Open the command prompt. (Press WinKey + R. Run dialogbox will appear. Type cmd and press enter.)
fig1-commandPrompt

- Navigate to the directory where .hex file is located. For example :

> cd D:\mega16_kitCD\sample code\ex01_simple_input_output\Exe
> d:

fig2-navigate

- To burn the hex file enter following command. Consider for example name of my hex file is io.hex :

> avrdude –c usbasp –p m16 –u –U flash:w:io.hex

You should see something like this :

fig3-pgmdone

Thats all !!! It is as simple as that ! :)

Explanation for command :

avrdude –c usbasp –p m16 –u –U flash:w:io.hex

-c : Indicates the programmer type. Since we are using the USBasp programmer, argument “usbasp” is mentioned.

-p : Processor. We are using ATmega16, hence “m16”. Note ATmega16 has two variants, one is “ATmega16L” (slow speed version) and “ATmega16” normal 16MHz version. However their device signature is same and hence you will have to use “m16” as parameter for both the AVRs. This applies to all AVRs having “L” variants.

-u : Disables the default behavior of reading out the fuses three times before programming, then verifying at the end of programming that the fuses have not changed. Always use this option. Many times it happens that we forget to switch on the AVR’s +5V power supply, then at the end of programming cycle, avrdude detects inconsistent fuses and tries to reprogram them. Since there is no power supply, fuses gets programmed incorrectly and entire microcontroller gets screwed up(means becomes useless). Thus always use this option.

-U  :  memtype:op:filename[:format]

Perform a memory operation. Multiple ‘-U’ options can be speci?ed in order to operate on multiple memories on the same command-line invocation. 

    memtype

The memtype ?eld speci?es the memory type to operate on. 

calibration   One or more bytes of RC oscillator calibration data.
eeprom       The EEPROM of the device.
efuse         The extended fuse byte.
flash          The ?ash ROM of the device.
fuse           The fuse byte in devices that have only a single fuse byte.
hfuse          The high fuse byte.
lfuse           The low fuse byte.
lock            The lock byte.

    op

The op ?eld speci?es what operation to perform:

r        read the speci?ed device memory and write to the speci?ed ?le
w       read the speci?ed ?le and write it to the speci?ed device memory
v       read the speci?ed device memory and the speci?ed ?le and perform a verify operation

    filename

Specify the hex file name. If file is not in current directory specify file name with appropriate path.

    format

Format need not be specified, for hex files, avrdude will automatically detect the format.

The trick to do it quickly : The Batch file :

It is extremely boring to type such a long command every time you program the uC. Therefore to avoid this you can create something called as Batch file. It is a text file which contains series of commands which will be executed by dos command processor. To create batch file follow these steps :

- Open notepad
- Type our avrdude command. i.e. copy paste following line into notepad.
avrdude –c usbasp –p m16 –u –U flash:w:io.hex
- Save the file with filename “burn.bat” and put it into the directory, which has the hex file.

Now whenever you recompile your program and want to burn it, simply double click on burn.bat. This will execute avrdude command that we have typed in it.

Possible errors:


1)couldn’t find usbasp:

found 5 busses
found 5 busses
avrdude: error: could not find USB device "USBasp" with vid=0x16c0 pid=0x5dc

This happens when USBasp is not connected or not recognized by the PC. Try to connect it to different USBport. Make sure that “Self programming” jumper of USBasp is open. Always disconnect AVR from USBasp, before plugging USBasp to the PC.

2)target does not answer

found 5 busses

avrdude: error: programm enable: target doesn’t answer. 1
avrdude: initialization failed, rc=-1
Double check connections and try again, or use -F to override
this check.

avrdude done.  Thank you.

Check the connections of USBasp with uC. Check the  power supply of the uC. Check whether you have connected the crystal and decoupling capacitors. If everything is fine and still you are getting this error, then it indicates that either ur uC is dead or its fuse bits have got screwed up.

I hope this post will help you to get started with AVRdude. Feedback is welcome.

Be the first to like this post.

View the original article here

Xilinx ChipScope Tutorial

Introduction:

Simulation based method is widely used for debugging the FPGA design on computers. Time required for simulating complex design for all possible test cases becomes prohibitively large and simulation approach fails. For rapid testing, such designs can be loaded on to the target FPGAs and tested by applying test inputs and directly observing their outputs. As the complexity of the design under test increases, so does the impracticality of attaching test equipment probes to these devices under test. The ChipScope Pro tools integrate key logic analyzer and other test and measurement hardware components with the target design inside the FPGA. Computer based software tool communicate with these hardware components and provide a designer robust logic analyzer solution.

In this tutorial we will use simple UP counter design and test it using ChipScope.

You can download the PDF file of this tutorial from here.

NOTE: For more detailed information on various settings and parameters of ChipScope Pro, refer to ‘ChipScope Pro 10.1 Software and Cores User Guide’.

Couter Design:

Create a new project in the Xilinx ISE and paste the following counter code in top module (cntr.v):

module cntr(
input rst,
input clk,
output [3:0] count
);

reg [25:0] cnt;
assign count[3:0] = cnt[25:22];

always @(posedge clk) begin
  if(rst)
    cnt <= 0;
  else
    cnt <= cnt + 1;
end
endmodule

To assign the constraints to the design, create new file named ‘cntr.ucf’ and add it to ISE project. Paste following constrains in ‘cntr.ucf’ file. These constraints are applicable for XUP-V2P Development board.

NET "count<0>" LOC = AC4;
NET "count<1>" LOC = AC3;
NET "count<2>" LOC = AA6;
NET "count<3>" LOC = AA5;
NET "clk" LOC = AJ15;
NET "rst" LOC = AG5;

These constraints connect the ‘rst’ signal to ENTER button on the development board. Output nets are connected to on board LEDs.

Configuring the logic analyzer core:

In order to test the counter design we have to configure and insert the logic analyzer core in our design. Follow these steps:

· In the ‘Sources’ view right click on the top module (cntr.v) and select ‘New Source’.

clip_image002

· In the ‘New Source Wizard’ window, select ‘ChipScope Definition and Connection File’ and specify the filename as ‘debug’. Click ‘Next’.

clip_image004

· Now we have to associate this debug.cdc file with our desired top module. Thus select ‘cntr’ from list of the modules. If there are multiple modules shown, select the one which you wish to test. Click ‘Next’ and then click ‘Finish’.

clip_image006

Note that ‘debug.cdc’ file has been added to your ‘Sources’ list and is listed below the selected top module (cntr).

clip_image008

· Double click on ‘debug.cdc’ to launch the ChipScope Pro Core Inserter application. This application will integrate the logic analyzer core into our counter design. Do not alter any settings on the first screen. Click ‘Next’.

clip_image010

· To observe any signal, we have to specify the trigger. Logic analyzer core will start capturing the desired signal upon activation of trigger signal. In this example we want to monitor the counter’s counting action as soon as ‘rst’ signal is deactivated. So we will create two trigger ports. One port will be ‘rst’ signal and another port will be counter’s eight least significant bits.

Set ‘Number of trigger ports’ to 2.

In ‘TRIG0’ frame set ‘Trigger Width’ as 1 (since ‘rst’ is one bit signal).

In ‘TRIG1’ frame set ‘Trigger Width’ as 8 (as we want to observe counter’s 8 least significant bits).

clip_image012

Click ‘Next’.

· Now in this window we will specify capture parameters. We want to use our trigger ports as data ports which will be recorded by logic analyzer. We also want to sample data on rising clock edge.

In ‘Sample On’ list select ‘Rising’.

Set Number of samples to be recorded by changing ‘Data Depth’ to 1024 samples. This will record 1024 samples from the trigger event. You can at the most record 16K samples.

Select both check boxes in ‘Trigger Ports Used As Data’ frame.

clip_image014

Click ‘Next’.

· Now we will specify which signal(s) to be used as Clock and Trigger.

Click on ‘Modify Connections’.

clip_image016

· Select the ‘Clock signals’ Pane, then select ‘clk_BUFG’ signal from the left hand side list and then click on ‘Make Connection’. This will add ‘clk’ signal as the clock signal for logic analyzer.

clip_image018

· Now select ‘Trigger/Data signals’ pane. Select ‘TP0’ and connect ‘rst_IBUF’ signal to CH0 channel.

clip_image020

· Similarly click on ‘TP1’ pane and add connect counter’s lowe eight bits to eight channels.

clip_image022

Click ‘OK’ once you finish making connections.

· Now in the main window click on ‘Return to Project Navigator’. It will ask for saving the project, click ‘Yes’.

clip_image024

Now we are ready to compile the entire counter design along with the logic analyzer core.

· In the ISE, select top level module ‘cntr’ and in the ‘Processes’ pane double click on ‘Analyze Design Using ChipScope’. This will start the process to synthesize combined unit consisting of design under test (in this case counter) and the ChipScope cores.

clip_image026

Debugging the design using ChipScope Analyzer tool:

Once the synthesis gets over, ISE will launch the Analyzer tool. Make sure that FPGA board is connected to PC.

· Once the analyzer tool is running, click on ‘Initialize JTAG Chain’ clip_image028 icon located at the top right corner of the window. This will initialize the JTAG chain and identify the devices found in the chain. A dialog box will appear showing the devices discovered. Click ‘OK’.

clip_image030

· Now select the FPGA device from the JTAG chain, right click and then select ‘Configure’ to specify the configuration bit stream file.

clip_image032

· Select the bit stream file ‘cntr.bit’ from the bit stream folder. Then click ‘OK’.

clip_image034

· IMPORTANT: After clicking ‘OK’, tool will load the bit stream file into FPGA and check the availability of debugging cores. If debugging core is found tool will show ‘INFO: Found 1 Core Unit in the JTAG device Chain.’ Message in status window.

If you see ‘Found 0 Core …’ message instead, then either you have selected wrong bit stream file or something has gone wrong in one of the previous steps and debugging core has not been inserted properly into the design.

If everything is fine then you will see options for Logic Analyzer core inserted in our design. Now double click on the ‘Trigger Setup’ element to launch trigger setup window. And for trigger port 0 (i.e. ‘rst’ signal) specify the trigger Value 0.

clip_image036

This will make logic analyzer to trigger as soon as ‘rst’ become zero and record 1024 samples on successive clock edges. Note that trigger signals are sampled on rising clock edge.

Double click on ‘Waveform’ element to see the waveform.

· Now everything is ready. To apply the settings and ARM the trigger click on clip_image038 button. After that press the ‘Down’ button on the development board to release the ‘rst’ signal. This will trigger the logic analyzer. Once 1024 samples are recorded, this data will be transferred to PC and will be displayed in the waveform window.

clip_image040

Final data is uploaded on PC and displayed in waveform window.

clip_image042

NOTE: To see the names of the trigger ports, you can import the ‘debug.cdc’ file in analyzer tool. Click on File>Import and then select ‘debug.cdc’

NOTE: For more detailed information on various settings and parameters of ChipScope Pro, refer to ‘ChipScope Pro 10.1 Software and Cores User Guide’.

Be the first to like this post.

View the original article here

USBasp Drivers for Windows 7

Posted by Omkar on June 25, 2010

I have uploaded USBasp drivers for windows 7 on megaupload. Drivers can be downloaded from the following link:

http://www.megaupload.com/?d=N7RML15L

Instructions:

1) Download and extract contents into some folder.
2) Connect USBasp to PC. Go to Device Manager, locate “USPasp”. Right click and select “Update Driver” .
3) Browse for extracted folder and proceed as usual.
4) “Windows Security”  warning will appear. Select “Install this driver software anyway”  and proceed.

Thanks to Mr. Nandan: http://elecrom.wordpress.com/2009/04/19/usbasp-tutorial-usbasp-driver-installation/#comment-2149

Be the first to like this post.This entry was posted on June 25, 2010 at 2:23 pm and is filed under AVR microcontroller. Tagged: AVR, Drivers, USBasp, Windows 7. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.


View the original article here

Products

Posted by Omkar on February 17, 2010

Following products are available for sell.


Low cost Development board for AVR ATmega16 / ATmega32 microcontrollers. Starting from Rs. 890/- only.

Main Features :
- ZIF Socket for microcontroller
- Serial port interface (MAX232)
- Two motor drivers (L293D)
- support for I2C EEPROM
- socket for connecting character LCD

Click Here for more information

IMG_0865-m16-0

Efficient and easy to use infrared sensors for robotics and other general applications. Cost Rs. 80/- only.

Main features:
- Compact design
- High precision multi-turn potentiometer for sensitivity adjustment.
- Output indicator LED, which helps easy calibration without the need of microcontroller.

IR Sensor

USBasp is Universal USB based programmer for AVR microcontrollers ingeniously designed by Thomas Fischl.
For Indian hobbyists, low cost compact PCB has been developed and available for sale at Rs. 350/- only.

IMG_0871-USBasp

Drop me a mail at elecrom [a t] gmail [d o t] com to get the payment and shipment information.

Be the first to like this post.This entry was posted on February 17, 2010 at 8:44 am and is filed under AVR microcontroller, Electronics, My Products, Robotics. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.


View the original article here

CodeVisionAVR Tip: How to display formatted numbers on LCD

Generally at some point of time we all need to display something more than “hello world” onto LCD. CodeVisionAVR(cvavr) provides functions like lcd_putsf() and lcd_puts() for displaying strings on LCD. However these function cannot be used directly to display something like : “Vin = 10V” or may be “Value = 0000A01F”. In short these functions do not provide the functionality of formatted output, as provided by printf() function. How to implement this functionality is objective of this post. Fortunately there is very simple way to get this thing done …

Understanding lcd_puts() function:

This is standard library function provided by cvavr for printing string stored in RAM on LCD display. Syntax of this function is :

void lcd_puts(char *str)

str is the pointer to the buffer containing null terminated string. (All strings in C are always NULL terminated). This function will print everything contained in the given string buffer. So if we can somehow get the formatted string ready made, we can simply pass it to this function and get things printed on LCD.

The sprintf() function:

This is standard C library function. Its prototype is provided in stdio.h header file. When I say standard C library function, then it means that functionality of this function will be provided by ANY C compiler (supporting standard C library) existing in the world. It can be WinAVR, IAR C compiler, Imagecraft C compiler, RealView compiler for ARM, TI’s C compiler for DSPs, etc. So you can use the technique described here for any C compiler with suitable modifications.

Syntax of sprintf() function is as follows :

void sprintf(char *str, char flash *fmtstr [ , arg1, arg2, ...])

This function will place the formatted text into given string buffer (str). So when you write :

i = 10;
sprintf(strbuff,"Value of i = %d (dec) or %X (hex)",i,i);

Then after executing this statement strbuff will contain following string:

“Value of i = 10 (dec) or A (hex)”

Now you can print this string on LCD by passing strbuff to lcd_puts() function :

lcd_puts(strbuff);

Thats it !!

You should refer documentation of the cvavr for knowing the formatting features provided by sprintf() function. Since embedded systems have limited resources, cvavr provides different implementations of this function. In these implementations certain formatting features have been removed to reduce program size. To change the implementation of sprintf(), go to Menu Project>Configure and select tab C Compiler. In this tab look at the (s)printf features drop down list and select the desired functionality. By deafult this setting will be set to int, width. For this implementation, following conversion type characters are supported: ‘c’, ‘s’, ‘p’, ‘i’, ‘d’, ‘u’, ‘x’, ‘X’, ‘%’, no width or precision specifiers are supported, only the ‘+’ and ‘ ‘ flags are supported, no input size modifiers are supported.

For more details on other options, refer cvavr help. The more features are selected, larger is the code size generated for the printf and sprintf functions.

NOTE:
- Remember to choose size of string buffer array (i.e. char *str) appropriately. If the size is less than expected final formatted string, sprintf() function will continue to write sequentially in memory, till all characters are written. This will result in application crash or will corrupt values of other variables in SRAM.

- Generally LCDs can accommodate very small number of characters. So make sure that you have enough space to print the desired string on LCD. If there is no sufficient space left for printing further, then clear the LCD using lcd_clear() function.

Summary:

To print formatted output on LCD, use the code similar to the one shown below :

char strbuff[20];
—–
—–
sprintf(strbuff, “Value of x = %d”, x);
lcd_puts(strbuff);
—–
—–

Be the first to like this post.

View the original article here

100% Flash utilization !!

Posted by Omkar on May 7, 2009

Sometimes we met with interesting coincidences while working on projects. One such coincident happened just few moments ago … interesting and hence sharing with you all.

I was working on one small project and was frustrated as the code was not working as expected … so I have added couple of lines of debugging code and recompiled the code in CodeVisionAVR … boom … I got an error : program size exceeds the flash ! Well I was wondering what can be done ? I didn’t want to optimize the program for size, as it will hamper its speed performance. So I went to project options,  checked the optimization settings of my code. Optimization was set to Speed and effort was LOW. I changed effort level to HIGH (just did something, I didn’t expect that code size will reduce or something) and compiled the code again … and too my surprise compiler managed to produce a code which filled entire flash of the uC, not a single byte less not a single byte more !!! :) Have a look ..

100percentflash

Hehe … Howz that :)

Be the first to like this post.This entry was posted on May 7, 2009 at 4:31 pm and is filed under AVR microcontroller. Tagged: AVR Micocontroller, coincidence, flash utilization. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.


View the original article here