EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / ATmega32 avr

ATmega32 avr

|

Microcontroller › AVR › ATmega32 avr

  • This topic has 4 replies, 2 voices, and was last updated 8 years, 11 months ago by s.manisrinivas.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • April 19, 2017 at 7:37 am #4644
    s.manisrinivas
    Participant

    after working severals hour I still have trouble doing master receiver and slave transmitter if someone has code … (ATMEGA32)  please provide some suggestions for coding.

    i got this code in internet ,can anybody tell me flow of code .

    I want read adc value from slave transmitter and the value should  display by master in lcd display .

    slave transmitter code………….

    #include <avr/io.h>

    #include <avr/interrupt.h>
     
    #define F_CPU 1000000UL  // 1 MHz
    #include <util/delay.h>
    #include <util/twi.h>
     
    #define SLAVE_ADDRESS (0x01)
     
    uint8_t adc_value; // the value to send
     
    // interrupt routine for TWI message handling
    ISR(TWI_vect)
    {
    // react on TWI status and handle different cases
    uint8_t status = TWSR & 0xFC; // mask-out the prescaler bits
    switch(status)
    {
    case TW_ST_SLA_ACK:   // own SLA+R received, acknoledge sent
    TWDR = adc_value;
    TWCR &= ~((1<<TWSTO) | (1<<TWEA));
    break;
     
    case TW_ST_LAST_DATA: // last byte transmitted ACK received
    TWCR |= (1<<TWEA); // set TWEA to enter slave mode
    break;
    }
    TWCR |= (1<<TWINT);  // set TWINT -> activate TWI hardware
    }
     
    int main(void)
    {
    // TWI setup
    sei(); // enable global interrupt
     
    // set slave address to 0x01, ignore general call
    TWAR = (SLAVE_ADDRESS << 1) | 0x00;
    // TWI-ENable , TWI Interrupt Enable
    TWCR |= (1<<TWEA) | (1<<TWEN) | (1<<TWIE);
     
    // ADC setup
    ADCSRA |= (1<<ADEN);
    ADMUX  |= ( (1<<REFS1) | (1<<REFS0) ); // select internal reference
    ADMUX  |= 3;   // select channel 5 (pin ADC5)
     
    // infinite loop
    for (;;)
    {
    ADCSRA |= (1<<ADSC);        // start single measurement
    while(ADCSRA & (1<<ADSC));  // wait until measurement done
     
    // read result bytes (low and high) and reduce to 8-bits
    adc_value = ADCL;
    adc_value >>= 2;          // drop 2 least significant bits
    adc_value |= (ADCH << 6); // add two most significant bits
    }
    }
     
     
     
     
    master receiver code………….
     
     
    #include <avr/io.h>
    #define F_CPU 1000000UL  // 1 MHz
    #include <util/delay.h>
     
    #include <avr/interrupt.h>
    #include <util/twi.h>
     
    #define SLAVE_ADDRESS (0x01)
     
    uint8_t value; // contains the received value
     
    uint8_t ongoing_transmission = 0;
     
    // interrupt routine for the timer0 overflow interrupt
    ISR(TWI_vect)
    {
    // react on TWI status and handle different cases
    uint8_t status = TWSR & 0xFC; // mask-out the prescaler bits
    switch(status)
    {
    case TW_START:  // start transmitted
    ongoing_transmission = 1;
    // write SLA+R, SLA=0x01
    TWDR = (SLAVE_ADDRESS << 1) | 0x01;
    TWCR &= ~((1<<TWSTA)); // clear TWSTA
    break;
     
    case TW_MR_SLA_ACK: // SLA+R transmitted, ACK received
    TWCR &= ~((1<<TWSTA) | (1<<TWSTO));
    break;
     
    case TW_MR_DATA_ACK: // data received, ACK returned
    ongoing_transmission = 0;
    value = TWDR;
    TWCR |= (1<<TWSTO);  // write stop bit
    TWCR &= ~(1<<TWSTA); // clear start bit
    break;
    }
    TWCR |=   (1<<TWINT);  // hand over to TWI hardware
    }
     
    int main(void)
    {
    // TWI setup
    sei(); // enable global interrupt
    // TWI-ENable , TWI Interrupt Enable
    TWCR |= (1<<TWEA) | (1<<TWEN) | (1<<TWIE);
     
    // LED setup
    DDRB  = 0x3f; // PORTB[0:5] as output
    DDRC  = 0x03; // PORTC[0:1] as output
     
    for (;;) // infinite main loop
    {
    // initiate new transmission if
    //    no transmission is in progress
    if (!ongoing_transmission) TWCR |= (1<<TWSTA);
     
    PORTB = value;    // display number
    PORTC = value>>6; //   as LED pattern
    }
    }

     

    April 20, 2017 at 11:50 am #14571
    Ashutosh Bhatt
    Participant

    what is the problem in your code?

    April 20, 2017 at 12:50 pm #14575
    s.manisrinivas
    Participant

    i would not understand the flow of code and i need atleast pseudo code for this .can you help me

    May 2, 2017 at 11:55 am #14589
    Ashutosh Bhatt
    Participant

    this is the psuedo code with all the necessary comments

    what else you want

    May 2, 2017 at 12:55 pm #14590
    s.manisrinivas
    Participant

    bro ,i need to display the  result also in LCD

  • Author
    Posts
Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • Getting into an LED bulb April 21, 2026
  • understanding of resonance in time domain April 21, 2026
  • Beginner Questions About CNC Machines – G-code, Control Systems & Accuracy April 21, 2026
  • A Must-Watch Video Showing Dangerous Construction of Cheap Lithium-Ion Cells April 21, 2026
  • S1MJ ? April 20, 2026

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2026 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise