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

Meausuring Pulse widths (bit banging on 16F84)- Not working.

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



Joined: 06 Sep 2003
Posts: 49

View user's profile Send private message

Meausuring Pulse widths (bit banging on 16F84)- Not working.
PostPosted: Sun Sep 07, 2003 5:54 pm     Reply with quote

Hello,
As a building block for a larger project I'm outputting a positive going 190us pulse from one 16F84 and trying to measure the pulse width on another 16F84. I know that bit banging is not the most accurate way to do this but I'm OK with that, this is just a test program. No matter what I do I can't get the receiving 16F84 to read the pulse width correctly. It is off by orders of magnitude and not the same every time. I've used a scope to make sure the pulse is being transmitted at the correct width. I am using the a crystal that matches the clock speed defined in the program.

There must be something obvious that I am missing.

Here is the code for both the transmitter and receiver. If anyone can see anything obvious I would appreciate your comments. I tried with CCS PCM 3.148 and 3.176.

TRANSMITTER 16F84
#include <16f84.h>
#case
#fuses HS,NOWDT,PUT,NOPROTECT
#use delay(clock=10000000)
#zero_ram

// ----- Definitions
#define statusLED PIN_A0
#define txData PIN_A1

// ----- Function Prototypes

//void transmitZero(void);


// ----- Glogal Variables



void main(void)
{
int i;
output_low(txData); //off
delay_ms(5000); //rest time and stabilize
output_high(txData); //on
delay_us(190);
output_low(txData); //off

while(1)
{
//do nothing
}

} //end main

// ----- Functions


RECEIVER 16F84#include <16f84a.h>
#case
#fuses HS,NOWDT,PUT,NOPROTECT
#use delay(clock=20000000)

#zero_ram

// ----- Definitions
#define LEDStatus PIN_A0
#define rxData PIN_B4
#define TxPC PIN_A2
#define RxPC PIN_A3

#define numSamples 10

// ----- Function Prototypes

//void transmitZero(void);


// ----- Glogal Variables

void main(void)
{
#use rs232(BAUD=57500, XMIT=TxPC,RCV=RxPC) //setup for rs232 Debug.

int i;
int pulseCount;

for(i=1;i<=3;i++)
{
output_low(LEDStatus); //on
delay_ms(50);
output_high(LEDStatus); //off
delay_ms(100);
}

printf("Reset: ");
puts(":>");

pulseCount = 0;

while(!input(rxData)) //no data
{
// do nothing, wait for data
}

while(input(rxData)) && (pulseCount <= 250)) {
pulseCount = pulseCount + 1;
delay_us(1);
}
Shocked
printf("pulseCount %X", pulseCount);
puts(":>");

} //end main


// ----- Functions
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Sep 07, 2003 8:02 pm     Reply with quote

In your loop below, your assumption is that the loop code will
execute almost instantaneously, and the only delay is in the
delay_us(1) statement.
But, if you look at the .LST file, you'll see that it takes many
instructions to execute the loop. This is where the extra
delay is coming from.

With a 10 MHz clock, you'll get 2.5 instruction cycles per usec.
You can count instructions in the .LST file to see how long
your loop code will take. Some instructions, such as GOTO
or Branch instructions (that do the jump) will take 2 instruction
cycles.

while(input(rxData)) && (pulseCount <= 250))
{
pulseCount = pulseCount + 1;
delay_us(1);
}
Freddie



Joined: 06 Sep 2003
Posts: 49

View user's profile Send private message

PostPosted: Tue Sep 09, 2003 1:41 pm     Reply with quote

Yes, I see this now. My assumption that the most significant delay is in the delay_us(1) is in error. I'm using the timers now and it works great.

Thanks PCM Programmer!
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