Setting values in integers

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
while (1) 					//loop while 1
  { 
  ADCSRA |= (1 <<ADSC);			//Starts ADC conversion for single ended mode
  while (ADCSRA & (1 << ADSC) );
 if(ADCH > 140)					
      { 
		TCharge=1;
      } 

      else 
      {  
       	TCharge=0;
      } 
 if (TCharge = 1)					//If TCharge = 1 then PB0 is on and PB1 is off, else PB0 is off and PB1 is on
      { 
	  	PORTB &= ~(1 << 0); 	// Turn off Port Bit 0
		PORTB |= (1 << 1);  	// Turn on Port Bit 1
       
      } 

      else 
      {  
        PORTB |= (1 << 0); 	// Turn on Port Bit 0 
		PORTB &= ~(1 << 1);	// Turn off Port Bit 1
      } 
  } 

If I used int TCharge; above that code, should TCharge not be able to set values in it based on the logic above? When I load that into my Tiny85 it does not work, yet when I directly turn on the ports using the if (ADCH>###) statement it works, but not when I use TCharge. I have never used C before, so its all new to me. [/code]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
if (TCharge = 1)

should probably be

 if (TCharge == 1) 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks, that fixed it, why do I have to use == though instead of = in the if statement?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

why do I have to use == though instead of = in the if statement?

You don't HAVE to, but if you want to do a comparison instead of an assignment then you want to use the comparison operator.

I'd suggest a browse of a C language primer.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If I wanted to see if multiple values were true for a single if statement would I then write:

if (TCharge == 1) | (FAssist == 1) | (BBDetect == 0)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No, you would again go seek a C book either paper or online and use || for the job.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
while (1)                //loop while 1

Love that comment :)

/johan

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Jepael wrote:
No, you would [...] use || for the job.
Perhaps or perhaps not, depending on whether you do or don't want the "short circuit evaluation" effect. In this particular case, it doesn't matter. In other cases, it may.

In any event, the advice to seek out a good C book is well considered. I highly recommend "The C Programming Language" by Kernighan and Ritchie. It should be on every C programmer's desk. You can probably find a good used (second edition) copy of it for not much money at all.

Don Kinzer
ZBasic Microcontrollers
http://www.zbasic.net