Microcontroller › AVR › ATmega32 avr
- This topic has 4 replies, 2 voices, and was last updated 7 years, 9 months ago by
s.manisrinivas.
Viewing 5 posts - 1 through 5 (of 5 total)
-
AuthorPosts
-
April 19, 2017 at 7:37 am #4644
s.manisrinivas
Participantafter 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 handlingISR(TWI_vect){// react on TWI status and handle different casesuint8_t status = TWSR & 0xFC; // mask-out the prescaler bitsswitch(status){case TW_ST_SLA_ACK: // own SLA+R received, acknoledge sentTWDR = adc_value;TWCR &= ~((1<<TWSTO) | (1<<TWEA));break;case TW_ST_LAST_DATA: // last byte transmitted ACK receivedTWCR |= (1<<TWEA); // set TWEA to enter slave modebreak;}TWCR |= (1<<TWINT); // set TWINT -> activate TWI hardware}int main(void){// TWI setupsei(); // enable global interrupt// set slave address to 0x01, ignore general callTWAR = (SLAVE_ADDRESS << 1) | 0x00;// TWI-ENable , TWI Interrupt EnableTWCR |= (1<<TWEA) | (1<<TWEN) | (1<<TWIE);// ADC setupADCSRA |= (1<<ADEN);ADMUX |= ( (1<<REFS1) | (1<<REFS0) ); // select internal referenceADMUX |= 3; // select channel 5 (pin ADC5)// infinite loopfor (;{ADCSRA |= (1<<ADSC); // start single measurementwhile(ADCSRA & (1<<ADSC)); // wait until measurement done// read result bytes (low and high) and reduce to 8-bitsadc_value = ADCL;adc_value >>= 2; // drop 2 least significant bitsadc_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 valueuint8_t ongoing_transmission = 0;// interrupt routine for the timer0 overflow interruptISR(TWI_vect){// react on TWI status and handle different casesuint8_t status = TWSR & 0xFC; // mask-out the prescaler bitsswitch(status){case TW_START: // start transmittedongoing_transmission = 1;// write SLA+R, SLA=0x01TWDR = (SLAVE_ADDRESS << 1) | 0x01;TWCR &= ~((1<<TWSTA)); // clear TWSTAbreak;case TW_MR_SLA_ACK: // SLA+R transmitted, ACK receivedTWCR &= ~((1<<TWSTA) | (1<<TWSTO));break;case TW_MR_DATA_ACK: // data received, ACK returnedongoing_transmission = 0;value = TWDR;TWCR |= (1<<TWSTO); // write stop bitTWCR &= ~(1<<TWSTA); // clear start bitbreak;}TWCR |= (1<<TWINT); // hand over to TWI hardware}int main(void){// TWI setupsei(); // enable global interrupt// TWI-ENable , TWI Interrupt EnableTWCR |= (1<<TWEA) | (1<<TWEN) | (1<<TWIE);// LED setupDDRB = 0x3f; // PORTB[0:5] as outputDDRC = 0x03; // PORTC[0:1] as outputfor (;// infinite main loop
{// initiate new transmission if// no transmission is in progressif (!ongoing_transmission) TWCR |= (1<<TWSTA);PORTB = value; // display numberPORTC = value>>6; // as LED pattern}}April 20, 2017 at 11:50 am #14571Ashutosh Bhatt
Participantwhat is the problem in your code?
April 20, 2017 at 12:50 pm #14575s.manisrinivas
Participanti 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 #14589Ashutosh Bhatt
Participantthis is the psuedo code with all the necessary comments
what else you want
May 2, 2017 at 12:55 pm #14590s.manisrinivas
Participantbro ,i need to display the result also in LCD
-
AuthorPosts
Viewing 5 posts - 1 through 5 (of 5 total)
- You must be logged in to reply to this topic.