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 CCS Technical Support

Odd behavior in while loop (I2C comms)

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



Joined: 25 Jan 2005
Posts: 28
Location: Taxahoma

View user's profile Send private message

Odd behavior in while loop (I2C comms)
PostPosted: Mon Mar 14, 2005 8:11 pm     Reply with quote

I'm trying to send the freq to a pic at startup via I2C so that I can have selectable freq. After it gets this freq it then gets the duty cycle (freq stays the same during operation). Here's where it gets weird, I have I2C working with sending one byte ( I can't make it work with multiple bytes but that's another story) The 877a master sends 0x64 to 819 slave to set 650Hz, slave does the following.

Code:

while (freq == 0)  // loop till we recieve the freq
   {
      if (data_available)  //  I2C got the byte
      {
         data_available=false;
         freq = data_byte; // Test example should be 0x64 (100)
      }
   }


It breaks out of the loop and sets the freq at 32KHz which is way off so assume it really didn't get 0x64 so add the following.

Code:

while (freq == 0)  // loop till we recieve the freq
   {
      if (data_available)  //  I2C got the byte
      {
         data_available=false;
         freq = data_byte; // Test example should be 0x64 (100)
         if (freq != 100) // test if it got 0x64 (100)
         {
            freq = 0; // Didn't so continue loop
         }   
      }
   }


Now I hit the button on the master to send 0x64 same as before and it sets the freq to 650Hz, I only hit the button to send 0x64 once with each example so that tells me it really did get 0x64 via I2C properly even with the first example that clearly isn't working. Question Almost seems like it's doing if (freq = 100) instead of if (freq != 100).
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Mar 15, 2005 2:03 am     Reply with quote

It seems to me like your I2C receive routine is not working the way you expect it to. We can't check this because you didn't post your I2C routines....
My guess is you are receiving multiple data bytes before the 0x64 you are expecting. Why???? Without the I2C code for sending and receiving I have no clue.......
picer



Joined: 25 Jan 2005
Posts: 28
Location: Taxahoma

View user's profile Send private message

PostPosted: Tue Mar 15, 2005 8:01 am     Reply with quote

I2C seems to be working without issues with single byte transfers, I can cycle 0-100% duty (pushing other button) and it never misses a beat. My master code right now does not resend if it misses comms it sets a led on on the first error and it never errors out. I changed it last night to do a switch case statement based on recieving 3 different freqs at startup and that works flawlessly so that also says I2C is working fine. This isn't the way I wanted to do it but it does work.

I was doing this in the master code from a ccs example.

Code:

i2c_start();
i2c_write((0x02|(BYTE)(address>>1))&0xfe);
i2c_write(0x02);

changed to this
Code:

i2c_start();
i2c_write(0x02);

and no longer get the extra '0x02' which might be what's causing it and my tight loop for duty cycle is letting me not see this happen since i only have a regular o-scope. I was pretty much using the ccs master example but have to push buttons to make a transfer. Will test further. thanks.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Tue Mar 15, 2005 8:39 am     Reply with quote

As someone said before
Quote:
It seems to me like your I2C receive routine is not working the way you expect it to. We can't check this because you didn't post your I2C routines....
picer



Joined: 25 Jan 2005
Posts: 28
Location: Taxahoma

View user's profile Send private message

PostPosted: Tue Mar 15, 2005 9:43 am     Reply with quote

Mark wrote:
As someone said before
Quote:
It seems to me like your I2C receive routine is not working the way you expect it to. We can't check this because you didn't post your I2C routines....


Understand, my assumption was I2C was doing what I want, it was and wasn't. Looks like I was sending the address twice and the second send my slave thought was data. If I could get my icd2 to debug I would have caught that little error, I tried debug a couple times but either it doesn't display what I think it should or I'm doing it wrong. Either way I'm now talking to two pic slaves sending them seperate start freq and then sending duty cycle to them both and it works. Was trying to get the slaves to work as I want and missed something in the master code, now to make the master code do something useful. I'm used to using gdb (linux) to trace my code so had better figure out how to use the ICD2 debugging since this is only the start. More error checking is in order but it's working.
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