Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Saving Input state into variable

Status
Not open for further replies.

konradIC13

Member
Hello,

Im having troubles with making this code work on ATmega8, i want to store switch value into variable and later use this variable.
Here is code:
Code:
/*
 * Test.c
 *
 * Created: 2012-07-08 16:30:47
 *  Author: Konrad
 */ 

#define F_CPU 1000000L
#include <avr/io.h>

int main(void)
{
	char a; 
	DDRC &=~ (1<<PC5);		 //PC5 as input
	PORTC |= (1<<PC5);		 //pull up
	DDRC &=~ (1<<PC4);		 //PC4 as input
	PORTC |= (1<<PC4);		 //pull up
	
	DDRC |= (1<<PC3);		 //output
	PORTC |= (1<<PC3);		 //set as 1
    while(1)
    {
		a = (PINC & (1<<PC5));
		b = (PINC & (1<<PC4));

		if(a&&b==1)			 //if both a and b =1
		{
		PORTC |= (1<<PC3);   //set PC3 output as 1
		}		
		else
		{
		PORTC &=~ (1<<PC3);  //set PC3 output as 0
		}
	}	
}

I have switches connected to PC5 and PC4 and led connected to PC3. I want to store values of PC5 switch and PC4 switch in 'a' and 'b' variables and then make them turn led on only when both switches are on (high).

I have problem with storing those values into variable:
Code:
a = (PINC & (1<<PC5));
b = (PINC & (1<<PC4));
Not sure if its right

Also i dont know how to use those values in 'if' function,
Also later
 
Last edited:
First i see a problem at handling of DDRC.
I would set the complete Port with one declaration.
DDRC=0b00001000; //Would set the Port3 as Output the rest as Input
Do the same with the Pullups and Output by setting:
PORTC=0b00111000;

When setting up a Variable by a logical command the Variable not anytime has a value of 1.

So you had to test the result if it is higher than 1.

if(a!=0)
{
do_something();
}
else
{
do_other();
}
 
Amazing, now it works, i just started programming AVR and im playing around with some basics, thanks for help
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top