Rabu, 27 Juni 2012

ATMEGA32 AVR course

Watchdog Timer Concept

The Watchdog Timer is a timer connected to a completely separate RC oscillator within the microcontroller.
If the watchdog timer is enabled, every time it counts up to the program end, the microcontroller reset occurs and program execution starts from the first instruction. The point is to prevent this from happening by using a specific command. The whole idea is based on the fact that every program is executed in several longer or shorter loops.
If instructions which reset the watchdog timer are set at the appropriate program locations, besides commands being regularly executed, then the operation of the watchdog timer will not affect program execution. If for any reason (usually electrical noises in industry), the program counter “gets stuck” on some memory location from which there is no return, the watchdog will not be cleared and the register’s value being constantly incremented will reach the maximum et voila! Reset occurs!



for more details and learn more about microcontroller modules see the following link :
Source:mikroe

Simple Programmer for ATMEGA8

>> ATMEGA 8 used a lot in arduino kits and in this post we will show you how to make a simple programmer for atmega8.

 Main components :

1) components for the programmer.


2) components for the board

Schematic diagram :

 The board circuit diagram :

>> to use this programmer you have to use PonyProg  software which can be downloaded at the following link :

>> after installing the software adjust the connection paramters as following :

>> then connect the cable and choose avr micro and atmega8 as shown :

>> open the hex file as shown :

>> then press command and choose write program flash and the following message will appear :

>> press yes and the writing process started :

===========================================================
DONE



Real Time Clock using ATMega16

Description

The ATMega16 chip has a real-time counter that operates asynchronously when a 32,768hz watch crystal is connected to it, providing a real-time clock. The watch crystal needs to connect to pins TOSC1(pin 28) and TOSC2(pin 29).


 

 Hardware requirements

  • ATMega16
  • 32,768 KHz crystal
  • LCD display
>> The following software (using codevision avr compiler ) uses timer 2 CTC compare match and ther are three switches used to adjust seconds,minutes and hours.
>> Timer 2 use prescalar 1024 and the OCR value will be 32 or (20 hex).

Code Sample

#include <mega16.h>
// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x1b ;PORTA
#endasm
#include <lcd.h>
char s=0,m=0,h=0;
char t,u;
// Timer 2 output compare interrupt service routine
interrupt [TIM2_COMP] void timer2_comp_isr(void)
{
// Place your code here
s=s+1;
    if (s==60)
    {
    s=0;
    m=m+1;
        if (m==60)
        {
        m=0;
        h=h+1;
            if (h==24)
            {
            h=0;
            }
        }
    }
   
}

// External Interrupt 1 service routine
interrupt [2] void int0(void)
    {
    s=s+1;
    if(s==60)
        {
        s=0;
        }
    }
interrupt [3] void int1(void)
    {
    m=m+1;
    if(m==60)
        {
        m=0;
        }
    }
interrupt[19] void int2(void)
    {
    h=h+1;
    if(h==24)
        {
        h=0;
        }
    }
///////////////////////////////
char tenth(char a)
{
t=a/10;
return t;
}
//////////////////////////////
//////////////////////////////
char unit(char b)
{
t=b/10;
u=b-t*10;
return u;
}
////////////////////////////////
// Declare your global variables here
void main(void)
{
// Timer/Counter 2 initialization
// Clock source: TOSC1 pin
// Clock value: PCK2/1024
// Mode: CTC top=OCR2
// OC2 output: Disconnected
ASSR=0x08;
TCCR2=0x0F;
TCNT2=0x00;
OCR2=0x20;
// LCD module initialization
lcd_init(16);
TIFR|=0x80;
// Global enable interrupts
#asm("sei")

while (1)
      {
      // Place your code here
      lcd_gotoxy(0,0);
      t=tenth(h);
      lcd_putchar(t+0x30);
      u=unit(h);
      lcd_putchar(u+0x30);
      lcd_putsf(":");
      t=tenth(m);
      lcd_putchar(t+0x30);
      u=unit(m);
      lcd_putchar(u+0x30);
      lcd_putsf(":");
      t=tenth(s);
      lcd_putchar(t+0x30);
      u=unit(s);
      lcd_putchar(u+0x30);
      };
}


Proteus Simulation

Real Time Clock using ATMega16

>>>> Here is a link containing the project avr and its proteus simulation
Download Here

Download this tutorial in PDF HERE

Speed and Direction Control of a DC motor

>> The pulse width modulation (PWM) mode in timers of ATmega 16 have the main purpose of controlling the speed of a dc motor.
>> This is a simple program where the PWM is used to gradually increase the speed of the dc motor until reach max speed.
>> Also the speed of the dc motor is decreased gradually until the motor stops.

Program Specification

1- The program use ATmega16 MUC.
2- L293D to supply the required current for the motor
3- 4 buttons are uesd to contol dc motor.
      >> sw1 : to speed up dc motor speed until max in clock wise direction.
      >> sw2 : to decrease dc motor speed until it stops in clock wise direction.
      >> sw3 : to speed up dc motor speed until max in counter clock wise direction.
      >> sw4 : to decrease dc motor speed until it stops in counter clock wise direction.

>>>>>>> This code can be used in controlling lift (elevator ) motor.

Code Sample

#include <mega16.h>
unsigned int d=4369,x,y;
char flag;   // flag =1 left direction , flag =0 right direction
// Timer 1 overflow interrupt service routine
interrupt [9] void timer1_ovf_isr(void)
{
// Place your code here
if (flag ==1)
    {
    if (x==0)
        {                     
        TIMSK &=0b11111011;     // disable interrupt
        }
    else
        {
        OCR1A=OCR1A-d;
        x=OCR1A;
        }
       
    }
if(flag==0)
    {
    if (y==0)
        {                     
        TIMSK &=0b11111011;     // disable interrupt
        }
    else
        {
        OCR1B=OCR1B-d;
        y=OCR1B;
        }
    }   
   
}


void main(void)
{
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Ph. & fr. cor. PWM top=ICR1
// OC1A output: Non-Inv.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x10;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0xFF;
ICR1L=0xFF;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Global enable interrupts
#asm("sei")

while (1)
      {
      // Place your code here
      if (PINA.0==1)
         {
          OCR1A=0xffff;
          x=OCR1A;
          TCCR1A |= 0b11000000 ; // inv mode for OC1A
          TCCR1B |= 0b00000010 ; // start timer one with 1 mhz
          flag =1;      
          TIMSK |=0b00000100;
          while (PINA.1==0)
          {}
          OCR1A=0xffff;
          x=OCR1A;
          TCCR1A &= 0b10111111 ; // non inv mode for OC1A  
          TIMSK |=0b00000100;
         
         }
      if (PINA.2==1)
         {
         OCR1B=0xffff;
         y=OCR1B;
         TCCR1A |= 0b00110000 ; // inv mode for OC1B
         TCCR1B |= 0b00000010 ; // start timer one with 1 mhz
         flag =0;
         TIMSK |= 0b00000100;    // enable interrupt
         while (PINA.3==0)
         {}
         OCR1B=0xffff;
         y=OCR1B;
         TCCR1A &= 0b11100000 ; // non inv mode for OC1B
         TIMSK |= 0b00000100;    // enable interrupt
         
         }
      };
}

Proteus Simulation

>>>> Here is a link including the project file using codevision avr compiler and proteus simulation
Download Here

Download this tutorial in PDF Here

Selasa, 26 Juni 2012

STK 500 troubleshooting problems

 Problem 

A problem occured while executing the command. make sure that you are using the correct programming method.current mode is ISP ..............


 SOLUTION  ( 1 ) 

Step 1:

>> Go to the tab HW settings in the connection dialog and set the clock generator to max (3.6 MHZ) as shown in the following image

Step 2:

>> Go to the main tab and inprogramming mode and target settings press on settings.

>> Select the ISP frequency to be 115.2 KHZ and then press write.

SOLUTION  (2)

Disconnect all connections to all ports.

Testing UART of Atmega16

Here is a simple program to test the UART communication of the atmega 16

>> The UART in atmega 16 can send frames ranges from 5 bits to 9 bits
>> In this program we use the assynchronous mode
>> The frames used 8 and 9 bits.

UART  transmitter code


// USART initialization
// Communication Parameters: 8 Data, 1 Stop, Even Parity
// USART Receiver: Off
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x08;
UCSRC=0xA6;
UBRRH=0x00;
UBRRL=0x19;

while (1)
      {
      // wait for UDR register to be empty (bit 5 in UCSRA register)
      while((UCSRA & 0b00100000)==0)
      {}
      UDR = PINA;
       };



UART receiver code


// USART initialization
// Communication Parameters: 8 Data, 1 Stop, Even Parity
// USART Receiver: On
// USART Transmitter: Off
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x10;
UCSRC=0xA6;
UBRRH=0x00;
UBRRL=0x19;

while (1)
      {
      // wait for reception flag to be raised (bit 7 in UCSRA)
      while ((UCSRA & 0b10000000)==0)
      {}
      // check for errors
      if (((UCSRA&0b00010000)|(UCSRA&0b00001000)|(UCSRA&0b00000100))==0)
      {
      PORTC=UDR;
      }
      else
      {
      PORTD.7=1;  // indication for error
      }
      };


>>>> Here is a link including codevision project for UART tx and rx using frames of 8 bits and 9 bits and their proteus simulation :

Testing SPI communication

Here is a simple code written in c to test the SPI communication using atmega16 the avr code vision compiler is used here.

>> In SPI communication : one uc is programmed as a master (initiates communication) and the other uc is programmed as a slave.

Here is the master C code

// SPI initialization
// SPI Type: Master
// SPI Clock Rate: 1000.000 kHz
// SPI Clock Phase: Cycle Half
// SPI Clock Polarity: Low
// SPI Data Order: MSB First
SPCR=0x50;
SPSR=0x00;

while (1)
      {
      // Place your code here
      PORTB.4=0;
      SPDR=PINA;
      while(SPSR.7==0)
      {}
      status=SPSR;
      PORTC=SPDR;
      };

Here is the slave C code

// SPI initialization
// SPI Type: Slave
// SPI Clock Rate: 1000.000 kHz
// SPI Clock Phase: Cycle Half
// SPI Clock Polarity: Low
// SPI Data Order: MSB First
SPCR=0x40;
SPSR=0x00;
while (1)
      {
      // Place your code here
      SPDR=PINA;
      while(SPSR.7==0)
      {}
      status=SPSR;
      PORTC=SPDR;
      };



Proteus simulation




Here is the link of the master and slave codevision projects and proteus simulation :
Two types of AVR programmers can be built :

Serial port programmer(reference: At-Prog) is shown here. Main advantage of using serial port is you can have cable length upto 2m. You can use PonyProg2000 software to program your AVR using this programmer. Serial port generates +- 12V on its pins, thus diodes and zener diode is used to clamp +12V to +5.0V(4.7V due to zener + 0.3V due to bat85) and -12V to -0.3V (due to lower Bat85). You can substitute Bat85 with 1n4148, however it is not recommended because, this diode will clamp +12V and -12V to +5.4V and -0.7V which are not desirable, as per datasheet of microcontroller. Its not that micro will burn if you use 1N4148 diodes, but why to take risk !!.


NOTE: 

The ponyprog2000 software is found in the following link:

CIRCUIT DIAGRAM :


LIST OF COMPONENTS :

1)Resistors1K,1/4W4
2)Capacitors27pF,disc2
1µF , electrolytic2
3)Crystal4Mhz OR 8Mhz1
4)Voltage regulator78051
5)ConnectorD9 Female 1
6)DiodeBat858
7)Zener Diode 4.7V1
8)Grid PCB3″ x 3″
9)Connecting wires--

CONFIGURING AND USING PONYPROG2000 :

  1. All steps are same as described in parallel port programmer section, However during third step do the following settings : select “serial”, from list select “SI prog I/O”, select “COM1″( usually serial port is at COM1, if your computer have more than one serial ports multiple options will be active. Select appropriate, if you know, else do trial&error), check “Invert Reset”. Click on OK.

COMMENTS :

• Circuit diagrams shown above are generic and suitable for any AVR µC which supports ISP (almost all AVR, you can refer to www.atmel.com)
• Connect pins (e.g. MISO,MOSI, etc…) of AVR to appropriate points shown in circuit diagram.
• You can built programmer for more than one AVR devices by using different IC sockets for each device and doing appropriate connections of all above signal lines ( MISO,MOSI,SCK,RESET,X1,X2) and Vcc,Gnd of each AVR.
• Prefer using ZIF socket with IC base(refer: How to use ZIF socket in circuit without re-drilling holes on PCBs). Though It is bit costly, it proves extremely helpful.


NOTE:

you can use a usb to RS232 cable if you dont have a RS232 port in your pc
Two types of AVR programmers can be built :

PARALLEL PORT 

Following circuit diagram shows ultra simple AVR microcontroller (SOURCE: unkown). It requires very few components and its very simple to build on general purpose matrix PCB. You can use PonyProg2000 software to program your AVR using this programmer.


NOTE: 

The ponyprog2000 software is found in the following link:

CIRCUIT DIAGRAM :

LIST OF COMPONENTS :

1)Resistors220Ohms ,1/4W4
2)Capacitors27pF,disc2
1µF , electrolytic2
3)Crystal4Mhz OR 8Mhz1
4)Voltage regulator78051
5)ConnectorD25 Male1
6)Grid PCB3″ x 3″
7)Connecting wires--

• As programmer circuit is very simple, u can use substitutes if particular component is not available at your place.
• You will not get 3”x 3” GPCB ready made, you have to cut standard sized PCB. 3” x 3” is just rough estimate


CONFIGURING AND USING PONYPROG2000 :

  1. Open PonyProg2000
  2. Select menu : Setup>Interface Setup
  3. Select “parallel”, from drop down box select “Avr isp i/o”, select “LPT1″. Click OK.
     4.Now select menu Setup>Calibration. A dialog box will appear , click YES.
     5.Now PonyProg2000 is configured and ready to use.
     6.To burn your .Hex file into micro, select File>Open Program(FLASH) file.
     7.Select appropriate microcontroller from the list of devices at top-right. e.g >
      8.Connect your programmer ckt to PC, then place your micro into programmer ckt and turn on its power supply.
      9.Now click on the encircled icon :
        10.When device is programmed you will get “Program succesful” message.

Getting started with Atmel microcontroller

Here is a simple course to learn how to start using atmel microcontrollers.
We will use atmega16 microcontroller.
Getting started with Atmel microcontroller


Here is a link for atmega16 datasheet :
datasheet

Course contents

1- Introduction.
2-Memories in uc.
3-I/O ports.
4-External and Internal interrupts.
5-Timers.
6-clock sources and sleep modes.
7-reset sources and watch dog timer.
8-communication using SPI or USART.
9-dealing with analog signals.

Here is a link for lectures (presentations)

Programs used in this course are :

NOTE :

 You need to know basics of C language ( you can see this link : Summary of C language )