Close or Esc Key

Arduino Projects   |   Raspberry Pi   |   Electronic Circuits   |   AVR   |   PIC   |   8051   |   Electronic Projects

ATmega32 avr

3 posts / 0 new
Last post
s.manisrinivas
s.manisrinivas's picture
Offline
Last seen: 10 hours 21 min ago
Joined: 16/09/2015 - 12:05
ATmega32 avr

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
}
}

 

Ashutosh Bhatt
Ashutosh Bhatt's picture
Offline
Last seen: 4 days 5 hours ago
Joined: 21/12/2010 - 15:59
what is the problem in your

what is the problem in your code?

s.manisrinivas
s.manisrinivas's picture
Offline
Last seen: 10 hours 21 min ago
Joined: 16/09/2015 - 12:05
i would not understand the

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