View previous topic :: View next topic |
Author |
Message |
Harry Mueller
Joined: 17 Oct 2005 Posts: 116
|
Odd result for capturing duty cycle with TIMER_1 and #int_RB |
Posted: Thu Apr 13, 2006 12:33 pm |
|
|
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
|
|
Posted: Thu Apr 13, 2006 12:42 pm |
|
|
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
|
|
Posted: Thu Apr 13, 2006 1:27 pm |
|
|
[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: 1909
|
|
Posted: Thu Apr 13, 2006 1:31 pm |
|
|
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
|
|
Posted: Thu Apr 13, 2006 2:06 pm |
|
|
Thanks guys, that fixed it. I think I understand the "=" vs "==" better too.
Harry |
|
|
|