Stuff of Dharmanitech

download Stuff of Dharmanitech

of 34

Transcript of Stuff of Dharmanitech

  • 8/4/2019 Stuff of Dharmanitech

    1/34

    mall Dc Motor Control by PWM Method Using MOSFET H-Bridge with Atmega8

    Microcontroller

    i Friends,

    ere Im discussing DC motor control using the PWM counters of AVR ATmega8 microcontroller. I had used a DC

    motor from an old personal stereo cassette player. The circuit provides speed and direction control of the motor. The

    WM waveforms are used for driving the MOSFET H-bridge as shown in the schematic:

    t a time only one of the two PWM channel is active, driving only two MOSFETS (either Q1-Q4 or Q3-Q2). Other

    MOSFETs remain OFF. Whenever the Direction Control switch is toggled, the PWM channel is also changed, drivin

    he alternative pair of MOSFET, which changes the direction of current flow through motor, resulting in the directionhange in rotation of motor shaft.

    Code:

    /********************************************************/ *********** PWM DC MOTOR CONTROL *************/********************************************************/Controller : ATmega8 (1MHz internal Crystal)

    /Compiler : ICCAVR/Author : CC Dharmani, Chennai(India)/Date : Nov 2008/********************************************************

    include include

    define increaseButton_PRESSED !(PIND & 0x40)define increaseButton_OPEN (PIND & 0x40)define decreaseButton_PRESSED !(PIND & 0x80)define decreaseButton_OPEN (PIND & 0x80)define DIRECTION_FORWARD !(PIND & 0x20)

    define DIRECTION_REVERSE (PIND & 0x20)

  • 8/4/2019 Stuff of Dharmanitech

    2/34

    define STOP_MOTOR TCCR1B = 0x00; TCCR1A = 0x00define START_MOTOR TCCR1B = 0x09define set_FORWARD TCCR1A = 0x81define set_REVERSE TCCR1A = 0x21

    /defining macros for setting minimum and maximum PWM counter values/and step-size for controlling the voltage applied to MOSFETs basedefine COUNTER_LOWER_LIMIT 0x0090define COUNTER_UPPER_LIMIT 0x00f8define STEP_SIZE 0x0008

    oid port_init(void)

    ORTB = 0x00;DRB = 0x06; //PWM pins OC1A & OC1B defined as outputsORTC = 0x00;DRC = 0x00;ORTD = 0xE0; //internal pull-up enabled for three pins connected to switchesDRD = 0x00;

    /TIMER1 initialize - prescale:1/PWM Frequency: 1KHzoid timer1_init(void)

    CCR1B = 0x00; //stopCNT1H = 0xFC; //setupCNT1L = 0x18;CR1A = COUNTER_LOWER_LIMIT;CR1B = COUNTER_LOWER_LIMIT;CR1H = 0x03;CR1L = 0xE8;CCR1A = 0x81; //set forward; OC1A connected, OC1B disconnectedCCR1B = 0x09; //start Timer

    /call this routine to initialize all peripheralsoid init_devices(void)

    /stop errant interrupts until set upLI(); //disable all interruptsort_init();imer1_init();

    CUCR = 0x00;ICR = 0x00;IMSK = 0x00; //timer interrupt sourcesEI(); //re-enable interrupts/all peripherals are now initialized

    /***** FUNCTION FOR SOFTWARE DELAY OF 1 mSEC (appx.) *******

    oid delay_ms(int miliSec) //for 1 Mhz crystal

    nt i,j;

    or(i=0;i

  • 8/4/2019 Stuff of Dharmanitech

    3/34

    oid main(void)

    nsignedint counter = COUNTER_LOWER_LIMIT;nsignedchar dir = 0, dir1 = 0;

    nit_devices();

    hile(1)

    HECK_PB:hile(increaseButton_OPEN && decreaseButton_OPEN)

    //loop here until any push-button is pressedf(DIRECTION_FORWARD) //check for Direction control switch statusir = 0;lseir = 1;f(dir != dir1) //chenge direction if switch position has changed

    TOP_MOTOR;elay_ms(500);f(dir == 0)et_FORWARD;lseet_REVERSE;

    TART_MOTOR;ir1 = dir;

    f(increaseButton_PRESSED) //Speed-increase push-button is pressed

    elay_ms(20); //key debouncing delay after key-pressedf(increaseButton_OPEN) goto CHECK_PB;hile(increaseButton_PRESSED); //wait here till the push-button is kept pressedelay_ms(20); //key debouncing delay after key released

    f(counter >= COUNTER_UPPER_LIMIT) //if speed is already maximum, no actionounter = COUNTER_UPPER_LIMIT;lseounter += STEP_SIZE; //increase speed by a fixed step

    CR1A = counter;CR1B = counter;

    lse//speed-decrease push-button is pressed

    elay_ms(20); //key debouncing delay after key-pressedf(decreaseButton_OPEN) goto CHECK_PB;hile(decreaseButton_PRESSED); //wait here till the push-button is kept pressedelay_ms(20); //key debouncing delay after key released

    f(counter

  • 8/4/2019 Stuff of Dharmanitech

    4/34

    ADC Project - with ATmega32

    ********************************

    i friends,

    m presenting here a small project on how to make use of the ADC of ATmega32. The circuihecks the voltage present at the ADC channel and displays voltage value on the LCD (16x2

    Whenever the push-button is pressed, the LCD displays voltage present on the next ADChannel (total 8 channels, CH0 to CH7). The schematic and the firmware of the project areiven here.

    Code:

  • 8/4/2019 Stuff of Dharmanitech

    5/34

    otal 6 files: 3 program files (ADC_main.c, LCD_routines.c, ADC_routines.c) and 3 header

    les(ADC_main.h, LCD_routines.h, ADC_routines.h)

    /***************************************************************** / **************** ADC_routines.h *********************/*****************************************************************

    ifndef ADC_ROUTINES_H

    define ADC_ENABLE ADCSRA |= (1

  • 8/4/2019 Stuff of Dharmanitech

    6/34

    nt ADC_temp, ADCH_temp;nt ADC_var = 0;

    DC_ENABLE;DC_START_CONVERSION; //do a dummy readout firsthile(!(ADCSRA & 0x10)); // wait for conversion done, ADIF flag activeDCSRA|=(1

  • 8/4/2019 Stuff of Dharmanitech

    7/34

    he LCDParamtr : NoneReturn : None

    **************************************************************************/ oid ADC_displayValue(void)

    t value;

    oat value1;

    alue = ADC_read();

    alue1 = ADC_calculateValue(value);

    CD_DisplayString(2,4,updateDisplay(value1)); //voltage display on 2nd row,

    /4th column

    /***************************************************************** / **************** LCD_routines.c *********************/***************************************************************** ifndef LCD_ROUTINES_H

    / *** LCD Functions declaration *** //oid LCD_init(void);oid LCD_WriteCommand (unsignedchar CMD);oid LCD_WriteData (unsignedchar Data);

    oid LCD_DisplayString_F(char row, char column, constunsignedchar *string);oid LCD_DisplayString(char row, char column, unsignedchar *string);oid LCD_Cursor(char row, char column);oid delay_ms(int miliSec);

    define ENABLE_LCD PORTD |= 0x80define DISABLE_LCD PORTD &= ~0x80

    define SET_LCD_DATA PORTD |= 0x20define SET_LCD_CMD PORTD &= ~0x20

    endif

    /********************************************************/ ************** LCD_routines.c ******************/********************************************************/Controller: ATmega32 (16 MHz)/Compiler: ICCAVR/Author: CC Dharmani, Chennai/Date: Aug 2008/********************************************************

    include include include "LCD_routines.h"

    / *********************************/ *** Initialize the LCD driver ***/ *********************************oid LCD_init(void)

    elay_ms(100); // wait for 100ms

    CD_WriteCommand (0x38); // 8 data linesCD_WriteCommand (0x06); // cursor setting

    CD_WriteCommand (0x0f); // display ONCD_WriteCommand (0x01); // clear LCD memory

  • 8/4/2019 Stuff of Dharmanitech

    8/34

    elay_ms (10); // 10ms delay after clearing LCD

    / **********************************************/ *** Write a command instruction to the LCD ***/ **********************************************oid LCD_WriteCommand (unsignedchar Command)

    ET_LCD_CMD; // Set LCD in command mode

    ORTC = Command; // Load data to port

    NABLE_LCD; // Write data to LCDsm("nop");sm("nop");ISABLE_LCD; // Disable LCD

    elay_ms(1); // wait for 1ms

    / *****************************************/ *** Write one byte of data to the LCD ***/ *****************************************oid LCD_WriteData (unsignedchar Data)

    ET_LCD_DATA; // Set LCD in data mode

    ORTC = Data; // Load data to port

    NABLE_LCD; // Write data to LCDsm("nop");sm("nop");ISABLE_LCD; // Disable LCD

    elay_ms(1); // wait for 1ms

    / *********************************************************************

    / *** Display a string at the specified row and column, from FLASH ****/ *********************************************************************oid LCD_DisplayString_F ( char row, char column , constunsignedchar *string)

    CD_Cursor (row, column);hile (*string)CD_WriteData(*string++);

    / *********************************************************************/ *** Display a string at the specified row and column, from RAM ****/ *********************************************************************oid LCD_DisplayString (char row, char column , unsignedchar *string)

    CD_Cursor (row, column);hile (*string)CD_WriteData(*string++);

    / ***************************************************/ *** Position the LCD cursor at "row", "column". ***/ ***************************************************oid LCD_Cursor (char row, char column)

    witch (row)

    ase 1: LCD_WriteCommand (0x80 + column - 1); break;ase 2: LCD_WriteCommand (0xc0 + column - 1); break;

  • 8/4/2019 Stuff of Dharmanitech

    9/34

    efault: break;

    / ********************************************************/ **** Function for delay of 1 msec (appx.) at 16Mhz *****/ ********************************************************oid delay_ms(int miliSec) //for 16 Mhz crystal

    nt i,j;

    or(i=0;i

  • 8/4/2019 Stuff of Dharmanitech

    10/34

    /stop errant interrupts until set upLI(); //disable all interrupts

    ort_init();DC_init();CD_init();

    CUCR = 0x00;IMSK = 0x00; //timer interrupt sources/ SEI(); //re-enable interrupts

    / MAIN FUNCTION

    oid main(void)

    nsignedchar channel = 0;

    nit_devices();

    CD_DisplayString_F(1,1,"ADC Testing.. ");

    hile(1)

    CD_DisplayString_F(2,1,"CH");CD_WriteData(channel | 0x30); //display channel number

    TART:

    hile(KEY_OPEN) //wait here till key is open

    DMUX &= 0xe0;DMUX |= channel; //select ADC channelDC_displayValue();elay_ms(50);

    elay_ms(20); //key debounce delay when key is pressedf(KEY_OPEN)oto START;

    hile(KEY_PRESSED); //wait here till key is pressedelay_ms(20); //key debounce delay when key is released

    hannel++; //select next channel after key-pressf(channel == 8) //only channel-0 to channel-7 can be selectedhannel = 0;

    /*********************** END **************************

    Ring Detector for Caller ID Unit

    Friends,

    is is an addition to the caller ID circuit of my previous post. For deciding whether call is received call or missed call, u need t

    onitor the ringing signal along with the off-hook detector signal, which is discussed in the last post. If the off-hook detector

    gnal goes low (i.e. receiver lifted) before the ringing signal stops, then the call is labeled as received call, otherwise it is a mi

    all.

    his small circuit converts the ringing signal (sine wave 90vrms) into 5v dc pulses which can be directly fed to microcontroller

    he circuit outputs a pulse for each ring. The delay between two pulses is to be monitered and if it exceeds 4 sec, then ring is

  • 8/4/2019 Stuff of Dharmanitech

    11/34

    ead, hence, the call is missed call.!

    ry it out..

    Make-Yourself ATmega32 Starter's Kit with LCD, I2C, SPI, RTC, ADC interfaces

    Hi friends,ere is my home-made kit of ATmega32 microcontroller interfacing. The ATmega32 controller is rich with

    eatures like onboard 32kB in-System programmable flash, 1 KB EEPROM, 2KB SRAM, 10bit ADC (8 chann

    PI bus inteface, TWI (compatible with I2C bus) interface, an USART, analog comparator, etc.

    hat's why I've selected it to load my kit with all those features. This M32 card is having an LCD inteface with

    ontrast adjustment, an RS232 port for connecting with PC, a connector for 8 analog voltage inputs to measur

    y ADC, a Real Time Clock IC DS1307 from maxim with battery back-up, four general purpose keys, two key

    or generating interrupts and an LED.

    he circuit can be powered by an easily available 12v DC adapter. The voltage regulator IC 7805 used to conv

    he input into regulated 5v supply.

    ee below the kit in powered on condition and the schematic.

  • 8/4/2019 Stuff of Dharmanitech

    12/34

    he PCB is completely home-made, using the etching technique with the Ferric Chloride chemical. If you wan

    now how to make a PCB yourself from your layout, visit following webpage, it's really helpful:

    ttp://electrons.psychogenic.com/modules/arms/art/10/pcb_howto.php

    he schematic and layout of the M32_card shown here are prepared using EAGLE. It's a single layer board fo

    making the home-preparation easy. Check out the bottom layer and the layouts in the pics below:

  • 8/4/2019 Stuff of Dharmanitech

    13/34

    he software for the ATmega32 controller is written using imageCraft AVR compiler. The software contains c

    or LCD interface, use of RTC and communication with PC with RS232 port. Using the Hyper terminal in

    indows, the functionality of the kit can be checked, as I've mentioned in my previous post of RTC_EEPROM

    nterface with ATmega128.

    own below are the pics of RTC circuit in test and related screen shot of the hyper terminal while interacting

    he M32_Card

    he complete source code files including new addition of ADC (check out Here) can be downloaded in zip formrom AVRfreaks forum where I've posted them.

    o me if you have any problem in downloading these files)

    ou can download the datasheets here:

    Tmega32DS1307MAX232

    f any one is interested in making this kit at home, write to me. I'll send u the shcematic and layout files with therogram.

    o write your comments, guys!!!

    ------

    Design Caller ID using DTMF decoder MT8870

    i friends,

    ne more circuit: DTMF decoder interface for designing Caller ID Unit. It's very simple circuit using DTMF decoderMT8870 (or CM8870). As shown in the circuit, u'll receive an interrupt ( if NAND output is connected to INT of the

    microcontroller) whenever u receive a call or make a call and then u can use yr program to read the digits coming ou

    in 11 to 14 of the MT8870. I designed the circuit with additional features like seperating received calls, missed calls

    ialled numbers along with telephone directory.

    o determine whether number is dialled or received, u need to know whether receiver is on-hook or off-hook. followmple circuit will give u that indication:had used microcontroller 8951 with16x2 LCD and Dallas nvRAM for storing numbers and names. The code is in

    ssembly language. It's really easy to make this one with lot of variations.

  • 8/4/2019 Stuff of Dharmanitech

    14/34

    UART FUNCTIONS

    **********************************

    i friends,

    his document provides functions for using UART on AVR controllers for communication with PC RS232 port. Ivested these functions using ATmega32, with little changes it can be adapted to other atmega devices as well.

  • 8/4/2019 Stuff of Dharmanitech

    15/34

    ode:

    /****************************************************************

    /**** UART ROUTINES *******************

    /****************************************************************

    /Controller: ATmega32 (16MHz Crystal)

    /Compiler: ICCAVR

    /Author: CC Dharmani, Chennai

    /****************************************************************

    /***************************************************

    /Function to initialize UART

    /***************************************************

    /UART0 initialize

    / desired baud rate: 19200

    / actual: baud rate:19231 (0.2%)(at 16MHz crystal)

    / char size: 8 bit

    / parity: Disabled

    oid uart0_init(void)

    UCSRB = 0x00; //disable while setting baud rate

    UCSRA = 0x00;

    UCSRC = BIT(URSEL) | 0x06;

    UBRRL = 0x33; //set baud rate lo

    UBRRH = 0x00; //set baud rate hi

    UCSRB = 0x98;

  • 8/4/2019 Stuff of Dharmanitech

    16/34

    /**************************************************

    /Function to receive a single byte

    /*************************************************

    nsigned char receiveByte( void )

    unsigned char data, status;

    while(!(UCSRA & (1

  • 8/4/2019 Stuff of Dharmanitech

    17/34

    oid transmitString_F(const unsigned char* string)

    while (*string)

    transmitByte(*string++);

    /***************************************************

    /Function to transmit a string from RAM

    /***************************************************

    oid transmitString(unsigned char* string)

    while (*string)

    transmitByte(*string++);

    LCD interfacing with ATmega32

    ********************************************

    his document discusses interfacing of an 16x2 intelligent LCD with ATmega32. The schematic is shown here:

  • 8/4/2019 Stuff of Dharmanitech

    18/34

    ode:

    he code is given here. Please note that if you have bought a new ATmega32 IC then you have to disable the JTAG

    nterface which is multiplexed with the PORTC pins. JTAG can be disabled by changing the fuse values whilerogramming, refer to datasheet for further details. If JTAG is not disabled, the LCD will remain blank. Alternativelyou can connect LCD with PORTA, and change the code accordingly to replace PORTC with PORTA.

    fter loading the code into microcontroller, the LCD will display a message Hi, welcome! Have a nice Day. Wheney is pressed the message will change to Atmega32 Board Starters kit. The messages will toggle between this tw

    henever the pushbutton is pressed.

    /********************************************************

  • 8/4/2019 Stuff of Dharmanitech

    19/34

    /*********** PROGRAM FOR LCD INTERFACING *************

    /********************************************************

    /Controller: ATmega32 (Crystal: 16 Mhz)

    /Compiler: ImageCraft ICCAVR

    /Author: CC Dharmani, Chennai

    /********************************************************

    / ************* LCD.h : Header file *************

    oid LCD_init(void);

    oid LCD_WriteCommand (unsigned char CMD);

    oid LCD_WriteData (unsigned char Data);

    oid LCD_DisplayString_F(char row, char column, const unsigned char *string);

    oid LCD_Cursor(char row, char column);

    oid delay_ms(int miliSec);

    define ENABLE_LCD PORTD |= 0x80

    define DISABLE_LCD PORTD &= ~0x80

    define SET_LCD_DATA PORTD |= 0x20

    define SET_LCD_CMD PORTD &= ~0x20

    define pushButton1_PRESSED !(PINB & 0x02)

    define pushButton1_OPEN (PINB & 0x02)

    /******************* lcd.c ****************************

  • 8/4/2019 Stuff of Dharmanitech

    20/34

    include

    include

    include "LCD.h"

    oid port_init(void)

    DRA = 0x00;

    ORTA = 0x00;

    DRB = 0x00;

    ORTB = 0x00;

    DRC = 0xFF;

    ORTC = 0x00;

    DRD = 0xF0;

    ORTD = 0x00;

    /call this routine to initialize all peripherals

    oid init_devices(void)

    LI(); //disable all interrupts

    ort_init();

    CD_init();

    CUCR = 0x00;

    ICR = 0x00;

    IMSK = 0x00; //timer interrupt sources

  • 8/4/2019 Stuff of Dharmanitech

    21/34

    /******************* MAIN FUNCTION *******************

    oid main(void)

    nsigned char PB_Status=0xff,Change_Display=0, Change_Display1=1;

    nit_devices();

    hile(1)

    f(Change_Display != Change_Display1)

    f(Change_Display==0)

    CD_DisplayString_F(1,1," Hi, Welcome! ");

    CD_DisplayString_F(2,1,"Have a nice Day");

    lse

    CD_DisplayString_F(1,1," ATmega32 Board ");

    CD_DisplayString_F(2,1," Starter's Kit ");

    hange_Display1 = Change_Display;

    HECK_PB:

    while(pushButton1_OPEN);// wait here until pushbutton1 is //pressed

    elay_ms(20); // 20ms delay for key debouncing

    // after key-pressed

  • 8/4/2019 Stuff of Dharmanitech

    22/34

    f(pushButton1_OPEN) goto CHECK_PB;

    hile(pushButton1_PRESSED); //wait here till the

    //pushbutton1 is kept pressed

    elay_ms(50); // 50ms delay for key debouncing after

    // key released

    hange_Display = ~Change_Display;

    /*********************************************************

    /*********************** LCD Functions ***************

    /*********************************************************

    / *********************************

    / *** Initialize the LCD driver ***

    / *********************************

    oid LCD_init(void)

    delay_ms(100); // wait for 100ms

    LCD_WriteCommand (0x38); // 8 data lines

    LCD_WriteCommand (0x06); // cursor setting

    LCD_WriteCommand (0x0f); // display ON

    LCD_WriteCommand (0x01); // clear LCD memory

    delay_ms (10); // 10ms delay after clearing LCD

  • 8/4/2019 Stuff of Dharmanitech

    23/34

    / **********************************************

    / *** Write a command instruction to the LCD ***

    / **********************************************

    oid LCD_WriteCommand (unsigned char Command)

    SET_LCD_CMD; // Set LCD in command mode

    PORTC = Command; // Load data to port

    ENABLE_LCD; // Write data to LCD

    asm("nop");

    asm("nop");

    DISABLE_LCD; // Disable LCD

    delay_ms(1); // wait for 1ms

    / *****************************************

    / *** Write one byte of data to the LCD ***

    / *****************************************

    oid LCD_WriteData (unsigned char Data)

    SET_LCD_DATA; // Set LCD in data mode

    PORTC = Data; // Load data to port

  • 8/4/2019 Stuff of Dharmanitech

    24/34

    ENABLE_LCD; // Write data to LCD

    asm("nop");

    asm("nop");

    DISABLE_LCD; // Disable LCD

    delay_ms(1); // wait for 1ms

    / ************************************************************

    / Display a string at the specified row and column, from FLASH

    /*************************************************************

    oid LCD_DisplayString_F (char row, char column ,const unsigned char *string)

    LCD_Cursor (row, column);

    while (*string)

    LCD_WriteData(*string++);

    / ***************************************************

    / *** Position the LCD cursor at "row", "column". ***

    / ***************************************************

    oid LCD_Cursor (char row, char column)

    switch (row)

  • 8/4/2019 Stuff of Dharmanitech

    25/34

    {

    case 1: LCD_WriteCommand (0x80 + column - 1); break;

    case 2: LCD_WriteCommand (0xc0 + column - 1); break;

    default: break;

    }

    / ********************************************************

    / **** Function for delay of 1 msec (appx.) at 16Mhz *****

    / ********************************************************

    oid delay_ms(int miliSec) //for 16 Mhz crystal

    int i,j;

    for(i=0;i

  • 8/4/2019 Stuff of Dharmanitech

    26/34

    chematic:

    Code:

    /***************************************************************** / ******** 4X4 MATRIX KEY-BOARD INTERFACING *******/*****************************************************************

    /Controller: ATmega32 (Crystal: 16 Mhz)/Compiler: ImageCraft ICCAVR/Author: CC Dharmani, Chennai (India)/Date: Aug 2008/********************************************************

    include include

    / *** LCD Functions declaration *** //oid LCD_init(void);oid LCD_WriteCommand (unsignedchar CMD);oid LCD_WriteData (unsignedchar Data);

    oid LCD_DisplayString_F(char row, char column, constunsignedchar *string);oid LCD_Cursor(char row, char column);oid delay_ms(int miliSec);

    define ENABLE_LCD PORTD |= 0x80define DISABLE_LCD PORTD &= ~0x80define SET_LCD_DATA PORTD |= 0x20define SET_LCD_CMD PORTD &= ~0x20define KB_PORT_OUT PORTBdefine KB_PORT_IN PINB

  • 8/4/2019 Stuff of Dharmanitech

    27/34

    oid port_init(void)

    DRA = 0x00;ORTA = 0x00;DRB = 0x0f; //Key-board port, higer nibble - input, lower nibble - outputORTB = 0xff; //pull-up enabled for higher nibbleDRC = 0xff;ORTC = 0x00;DRD = 0xf0;ORTD = 0x00;

    /call this routine to initialize all peripheralsoid init_devices(void)

    /stop errant interrupts until set upLI(); //disable all interrupts

    ort_init();CD_init();

    CUCR = 0x00;IMSK = 0x00; //timer interrupt sources

    /****************** MAIN FUNCTION *******************

    oid main(void)

    nsignedchar upperNibble, keyCode, keyPressed, i;nit_devices();

    CD_DisplayString_F(1,1," WELCOME ");CD_WriteCommand(0xc0); //moving LCD cursor to second row

    hile(1)

    pperNibble = 0xff;

    or(i=0; i

  • 8/4/2019 Stuff of Dharmanitech

    28/34

    reak;ase (0xe7): keyPressed = '3';reak;ase (0xde): keyPressed = '4';reak;ase (0xdd): keyPressed = '5';reak;ase (0xdb): keyPressed = '6';reak;ase (0xd7): keyPressed = '7';reak;ase (0xbe): keyPressed = '8';

    reak;ase (0xbd): keyPressed = '9';reak;ase (0xbb): keyPressed = 'A';reak;ase (0xb7): keyPressed = 'B';reak;ase (0x7e): keyPressed = 'C';reak;ase (0x7d): keyPressed = 'D';reak;ase (0x7b): keyPressed = 'E';reak;

    ase (0x77): keyPressed = 'F';reak;efault : keyPressed = 'X';//end of switch

    CD_WriteData(keyPressed);

    UT:;//end of if//end of for//end of while(1)//end of main()

    /*********************** LCD Functions *****************************

    / *********************************/ *** Initialize the LCD driver ***/ *********************************oid LCD_init(void)

    elay_ms(100); // wait for 100ms

    CD_WriteCommand (0x38); // 8 data linesCD_WriteCommand (0x08); // display offCD_WriteCommand (0x01); // clear LCD memoryelay_ms (10); // 10ms delay after clearing LCD

    CD_WriteCommand (0x06); // cursor settingCD_WriteCommand (0x0f); // display ON

    / **********************************************/ *** Write a command instruction to the LCD ***/ **********************************************oid LCD_WriteCommand (unsignedchar Command)

    ET_LCD_CMD; // Set LCD in command mode

    ORTC = Command; // Load data to port

  • 8/4/2019 Stuff of Dharmanitech

    29/34

    NABLE_LCD; // Write data to LCD

    sm("nop");sm("nop");

    ISABLE_LCD; // Disable LCD

    elay_ms(1); // wait for 1ms

    / *****************************************

    / *** Write one byte of data to the LCD ***/ *****************************************oid LCD_WriteData (unsignedchar Data)

    ET_LCD_DATA; // Set LCD in data mode

    ORTC = Data; // Load data to port

    NABLE_LCD; // Write data to LCD

    sm("nop");sm("nop");

    ISABLE_LCD; // Disable LCD

    elay_ms(1); // wait for 1ms

    / *********************************************************************/ *** Display a string at the specified row and column, from FLASH ****/ *********************************************************************oid LCD_DisplayString_F (char row, char column ,constunsignedchar *string)

    CD_Cursor (row, column);hile (*string)CD_WriteData(*string++);

    / ***************************************************/ *** Position the LCD cursor at "row", "column". ***/ ***************************************************oid LCD_Cursor (char row, char column)

    witch (row)

    ase 1: LCD_WriteCommand (0x80 + column - 1); break;ase 2: LCD_WriteCommand (0xc0 + column - 1); break;efault: break;

    / ********************************************************/ **** Function for delay of 1 msec (appx.) at 16Mhz *****/ ********************************************************oid delay_ms(int miliSec) //for 16 Mhz crystal

    nt i,j;

    or(i=0;i

  • 8/4/2019 Stuff of Dharmanitech

    30/34

    sm("nop");

    / ******** END OF SOURCE CODE FILE MATRIX_KB.C *********

    BLINKING LED

    **************************************

    i friends, this is a beginners project of blinking an LED connected to AVR microcontroller ATmega32, as shown ihe schematic

  • 8/4/2019 Stuff of Dharmanitech

    31/34

    ode:

    ollowing code will make the Led blink every second (appx.) at 16MHz crystal. If you have bought new controller th

    will be set at internal 1MHz osc. (by default). So,if you dont change the fuse values to enable external crystal, theED will blink 16 times slower!!

    //***************************************************

    //************ BLINKING LED ***********************

    //***************************************************

    // Target : ATmega32

    // Crystal: 16.000Mhz

    // Compiler: ICCAVR

    // Author: CC Dharmani, Chennai, India

    //***************************************************

    #include

    #include

    //-----------------------------------------------

    void port_init(void)

    {

    PORTD = 0x00;

    DDRD = 0x10;

    }

    //------------------------------------------------

    void delay_ms(int miliSec) //1 ms delay (appx)for 16MHz crystal

    {

    int i,j;

    for(i=0;i

  • 8/4/2019 Stuff of Dharmanitech

    32/34

    {

    asm("nop");

    asm("nop");

    }

    }

    //------------------------------------------------

    //call this routine to initialize all peripherals

    void init_devices(void)

    {

    //stop errant interrupts until set up

    CLI(); //disable all interrupts

    port_init();

    MCUCR = 0x00;

    GICR = 0x00;

    TIMSK = 0x00; //timer interrupt sources

    //SEI(); //re-enable interrupts

    //all peripherals are now initialized

    }

    //--------------- MAIN FUNCTION -------------

    void main()

    {

    init_devices();

    while(1)

    {

    PORTD |= 0x10;

  • 8/4/2019 Stuff of Dharmanitech

    33/34

    delay_ms(500);

    PORTD &= ~0x10;

    delay_ms(500);

    }

    }

    //********************* END ***********************

    ere is the same code written forAVR-GCC (winAVR) compiler:

    /******* LED BLINKING PROGRAM *********/Controller: ATmega32 (1Mhz or 16Mhz)/Compiler: AVR-GCC (winAVR)/Author: CC Dharmani, Chennai (INDIA)/*************************************

    /#define F_CPU 1000000UL //use this if internal oscillator is useddefine F_CPU 16000000UL //use this when ext 16MHz crystal is used

    include include include

    oid port_init(void)

    PORTD = 0x00;DDRD = 0x10;

    /call this routine to initialize all peripheralsoid init_devices(void)

    //stop errant interrupts until set upcli(); //disable all interruptsport_init();

    MCUCR = 0x00;GICR = 0x00;TIMSK = 0x00; //timer interrupt sources

    / sei(); //re-enable interrupts//all peripherals are now initialized

    nt main()

    init_devices();

    while(1){

    PORTD = 0x10;

  • 8/4/2019 Stuff of Dharmanitech

    34/34

    _delay_ms(500);

    PORTD = 0;_delay_ms(500);

    }

    return(0);

    /---------- END -------------