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

I2C and Serial at the same time on the 16F88

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



Joined: 28 May 2006
Posts: 56

View user's profile Send private message

I2C and Serial at the same time on the 16F88
PostPosted: Thu Mar 06, 2008 11:14 pm     Reply with quote

Hello,
I've created an 8 channel servo controller that operates on a shared serial bus. I have two of these boards. I want to control these boards from a Wii Nunchuck. I am trying to create a board that will read the nunchuck data via I2C and format it correctly for the Servo controller's serial bus. Looking at the pinout of the 16F88 I was under the impression that I would be able to use the serial pins (and hardware UART) as well as the I2C pins at the same time. I've already created a PCB based on this assumption. When I run the code listed below, and have FORCE_HW in the #USE I2C line I get 255 returned from all channels of the nunchuck. This leads me to believe a HW problem with the nunchuck (much of this code was copied from a project I did on an 16F876a that worked fine. Thanks again PCM!). If I omit the FORCE_HW then I get 46. I don't understand the reason for the difference. I've tried adding FORCE_SW to the #USE RS232 line, no change. So, is this an issue of trying to use two pieces of HW at the same time, or do I have another problem?


Code:
#include <16F88.h>

//#build(reset=0x1, interrupt=0x5)          // Necessary for Bootloader
//#ORG 0x0F00,0x0FFF {}                     // Necessary for Bootloader

#fuses NOWDT,NOPROTECT,NOBROWNOUT,NOLVP, NOMCLR, INTRC

#use delay(clock=8000000)
#use rs232(stream=Jech_Bus, baud=19200, xmit=PIN_B5, rcv=PIN_B2)
#use i2c(Master, sda=PIN_B1, scl=PIN_B4,FORCE_HW)

#define NUNCHUCK_I2C_WRITE_ADDR 0xA4
#define NUNCHUCK_I2C_READ_ADDR  0xA5

int8 nunchuk_decode_byte (char x);
void nunchuck_read_data(int8 *buffer);
void nunchuck_init(void);

void main(){
   int8 buffer[6] = {0};
   printf("16F88 Nunchuck Test\n\r");
   
   nunchuck_init();
   delay_ms(100);
   
   while(TRUE){
      delay_ms(100);
      nunchuck_read_data(buffer);
      printf("X:%U\n\r",buffer[0]);
   }
}

//the following nunchuck routines are from PCM Programmer on the CCS forums..Thanks!
int8 nunchuk_decode_byte (char x)
{
  x = (x ^ 0x17) + 0x17;
  return x;
}


void nunchuck_init(void)
{
i2c_start();
i2c_write(NUNCHUCK_I2C_WRITE_ADDR);
i2c_write(0x40);
i2c_write(0x00);
i2c_stop();
}

//---------------------------------
void nunchuck_read_data(int8 *buffer)
{
i2c_start();
i2c_write(NUNCHUCK_I2C_WRITE_ADDR);
i2c_write(0);
i2c_stop();

delay_us(150);   // This delay is necessary.

i2c_start();
i2c_write(NUNCHUCK_I2C_READ_ADDR);
buffer[0] = nunchuk_decode_byte(i2c_read());
buffer[1] = nunchuk_decode_byte(i2c_read());
buffer[2] = nunchuk_decode_byte(i2c_read());
buffer[3] = nunchuk_decode_byte(i2c_read());
buffer[4] = nunchuk_decode_byte(i2c_read());
buffer[5] = nunchuk_decode_byte(i2c_read(0));

i2c_stop();

}
Matro
Guest







#use_i2c initiliazation
PostPosted: Fri Mar 07, 2008 2:21 am     Reply with quote

Your "#use_i2c" configures the 16F88 i2c as Master while it's only capable to be a Slave.
Maybe here is the problem. ;-)
Bye.
Ttelmah
Guest







PostPosted: Fri Mar 07, 2008 3:34 am     Reply with quote

It ought to work, if he gets rid of the 'FORCE_HW' keyword. As you say, the 16F88, has a hardware slave, but requires a software master. 'FORCE_HW', forces code for the I2C hardware to be generated, which this chip doesn't have for master operation. Really ought to give an error message.....

Best Wishes
kd5uzz



Joined: 28 May 2006
Posts: 56

View user's profile Send private message

PostPosted: Fri Mar 07, 2008 9:06 am     Reply with quote

When I remove the FORCE_HW (and remove the nunchuck_decode bit) I get 0's.
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