Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PROBLEM IN TAKING MULTIPLE INPUTS IN ATmega32

Status
Not open for further replies.

anoop kr

Member level 4
Joined
Aug 6, 2010
Messages
69
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
kerala
Activity points
1,885
Hai i am doing a project of security system now.For that i have to take multiple inputs from different sensors.The sensors i am using are LDR , GAS sensor , PIR motion sensor and Temperature sensor(LM35). the LM35 is connecting to the PORTA [PA.0 pin].And all the other sensors are connecting to the PORTB [PB.0 ,PB.1 ,PB.2 respectively].

Initially i just dont connect the sensors and instead of that i just check the status of the port PORTA which use as the input and the PORTB which is now use as output.these are only just done for checking that whether the programe is working correctly.After i burn the hexcode into the MCU and wired it according to the code it shows malfunctions.i mean some false triggering is happening.so that without any input to the port pins the LEDs are glowing , sometimes one LED is glowing continuously.I am using Atmega32 controller.I am giving the code i had written..Please help mee..:-(


#include<avr/io.h>
#include<util/delay.h>


void main()
{
int read;

DDRA=0x00;
PORTA=0x00;
DDRB=0xFF;
PORTB=0x00;

while(1)
{
read=PINA;
switch(read)
{

case 0x01:
dis1();
break;
case 0x02:
dis2();
break;
case 0x04:
dis4();
break;

default :
disp();
break;

}
}
}

void disp()
{
PORTB=0x00;
}

void dis1()
{
PORTB=0x01;
_delay_ms(50);
}

void dis2()
{
PORTB=0x02;
_delay_ms(50);
}

void dis4()
{
PORTB=0x04;
_delay_ms(50);
}
 

you have a big mistake!
"read=PINA.0" returns the digital value of a pin "1 or 0".
output of sensors such as LM35 are analog not digital,you must first convert them to digital using microcontroller internal ADC,and then make decisions upon their values in your program,
ADC channels are connected to PORTA in ATMEGA32,you must first initialize ADC in your program and then use it,
refer to analog to digital conversion part in datasheet
 

PINA.0 is used to reads specific bits of the port in codevisionAVR, the program above seems to be from winAVR (gcc)

read=PINA; will read the value of the port to the variable input.
I don't agree with the case method in this because you are scanning for only one single triggered pin ,but these could be two alarms at the same time.
you are scanning for values 1,2,4 but if 1 and 2 are both 1 you will not be able to detect it because it will be value 3, the same happens with any other combination.
You should use

Code:
if(read&(0x1)) dis1();
if(read&(0x2)) dis2();
if(read&(0x4)) dis3();

so that all the alarms are triggered individually if the input changes.

I also don't agree with the way you use your leds, you are assigning a value to the entire port which means that you will turn off any led that is already on.
You should use logic "OR" to light the led and logic "AND" to turn them off

Code:
PORTD|=0x1;       // to light led 1
PORTD|=0x2;       // to light led 1
PORTD|=0x4;       // to light led 3

PORTD&=(~0x1);    // turn off led 1
PORTD&=(~0x2);    // turn off led 2
PORTD&=(~0x4);    // turn off led 3

//or
PORTD&=(~0x7);    // turn off leds 1,2,3 at once

The above will only work if your sensors have a digital output, if not you need to use the a/d converter to read any analog voltage and react depending on the value

hamidmoallemi is corrected, LM35 output is an analog voltage so you need to use the a/d converter to read it.

Alex.
 

All sensors except PIR sensor are analog type. If you want to read the values then you have to connect it to PORTA which has a/d inputs
 

hai nandhu..i am happy about your reply...I want to say that i make this code working in PROTEUS software correctly.And according to your aspect its true that i wrote this code in Winavr gcc compiler.And i want to say that by reading the port value whole we can control the input seperately.By this way we get the byte value and not the value of a single port pin.Also we can control the output of the controller means controlling the output LEDs seperately by passing this byte value to the port.We can turn on or off the LEDs individually or by mass...And finally i want say that i correct the problem that i had..And i want to say sorry to you that i am using PIR sensor module not the PIR sensor only.Its output is TTL compatable.Thank you.....
 

And i want to say that by reading the port value whole we can control the input seperately.By this way we get the byte value and not the value of a single port pin.

Using the case statement you are scanning for alarm from 1 source only,
if 2 alarms happen at the same time then you will not be able to detect that.
Try it in proteus, enable any 2 of the sensor pins and watch the output, all leds will be off,
even if one sensor gives high and a led lights as soon as the second sensor goes high all leds will go off.

Also we can control the output of the controller means controlling the output LEDs separately by passing this byte value to the port.We can turn on or off the LEDs individually or by mass

This is not the case, you can turn them off in mass but you can only light one of them, when you try to light on a second one all the others will go off,
you can't turn on any 2 of them (or all three) with your code so i wouldn't call that individual control of the leds.

Alex
 

hai nandhu..you are right..by that way i can't turn on LEDs by mass.only one will glow at a time.but i done this only for checking the code that whether its working or not.At first it was not working when the code is burn to controller.means it was malfunctioning occasionally. ie when no input is applied the LED will glow sometimes.After that i wrote another code and it work perfectly.Anyway thanks for your helps..expect more cooperations from you.....
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top