CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Odd result for capturing duty cycle with TIMER_1 and #int_RB

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Harry Mueller



Joined: 17 Oct 2005
Posts: 116

View user's profile Send private message

Odd result for capturing duty cycle with TIMER_1 and #int_RB
PostPosted: Thu Apr 13, 2006 12:33 pm     Reply with quote

I'm trying to develop a program that will calculate the duty cycle of an ADXL202E accelerometer on an 16F877 PIN_B7 using #int_RB. Here's the code of the interrupt service routine.
Code:


#int_RB

void accelerometer_isr(void)
{
reading = get_timer1();                     //Read Timer_1
current_portb = input_b();                  //Read value of PORT_B
delta_portb = current_portb ^ last_portb;   //Did PORT_B change?
last_portb = current_portb;                 //Remember PORT_B value
if (delta_portb = 0x80 || 0xc0)             //If PIN_B7 has changed
{
if (bit_test(current_portb, 7))             //If PIN_B7 is high
 {
  period = reading - old_reading;           //Calculate period
  old_reading = reading;
 }
 else
  duty = reading - old_reading;             //Calculate duty
 }
else;
}


When I run the program I consistently get values of around 5200 for the
period, while values for duty vary between 3 values; 2380, 2675 and 4950.

I've checked the output of PIN_B7 with an old analog scope and the value of 2380 seems to be the right one.

Any ideas on what might be happening here?

Thanks, Harry
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 13, 2006 12:42 pm     Reply with quote

Fix the line in bold. Look at both operators in it. Look at the logic.
Quote:
#int_RB

void accelerometer_isr(void)
{
reading = get_timer1(); //Read Timer_1
current_portb = input_b(); //Read value of PORT_B
delta_portb = current_portb ^ last_portb; //Did PORT_B change?
last_portb = current_portb; //Remember PORT_B value
if (delta_portb = 0x80 || 0xc0) //If PIN_B7 has changed
{
if (bit_test(current_portb, 7)) //If PIN_B7 is high
{
period = reading - old_reading; //Calculate period
old_reading = reading;
}
else
duty = reading - old_reading; //Calculate duty
}
else;
}
Harry Mueller



Joined: 17 Oct 2005
Posts: 116

View user's profile Send private message

PostPosted: Thu Apr 13, 2006 1:27 pm     Reply with quote

[quote="PCM programmer"]Fix the line in bold. Look at both operators in it. Look at the logic.
Quote:

if (delta_portb = 0x80 || 0xc0) //If PIN_B7 has changed


I tried changing the line to:
Code:
if ((delta_portb = 0x80) || (delta_portb = 0xc0))


I think that provides the correct logic (my mind seems to rebel at this stuff) but the output hasn't changed.
newguy



Joined: 24 Jun 2004
Posts: 1903

View user's profile Send private message

PostPosted: Thu Apr 13, 2006 1:31 pm     Reply with quote

Tests inside an if statement need two equal signs (==). One equal means "assign this value to the variable." Then the if sees if the variable is a 1 (true) and executes if it is.
Harry Mueller



Joined: 17 Oct 2005
Posts: 116

View user's profile Send private message

PostPosted: Thu Apr 13, 2006 2:06 pm     Reply with quote

Thanks guys, that fixed it. I think I understand the "=" vs "==" better too.

Harry
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group