0

I'm using an Atmega168 I'm attempting to use I2C with my PCA9685 Servo driver.

I'm using this I2C library: https://github.com/g4lvanix/I2C-master-lib

I'm attempting to start the I2C connection with my PCA9685 (Address: 0x41).

For some reason the I2C Library is bouncing back an error because the acknowledge bit isn't being sent. What is wrong here? My SDA and SCL pins are hooked up to 10k pull up resistors, and they are connected to the PCA9685 correctly. Yet it still isn't working. Could it be my PCA9685 chip? I also know the address is 0x41 because I manually bridged an address connection to assign that address.

Here is my code:

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/power.h>
#include "i2c.h"

#define SERVO_MIN 1000
#define SERVO_MAX 2000
#define SERVO_MID 1500

#define PCA9685_ADDR 0x40

#define PCA9685_MODE1 0x0

#define LED0_ON_L 0x6
#define LED0_ON_H 0x7
#define LED0_OFF_L 0x8
#define LED0_OFF_H 0x9

#define LED      PB0
#define LED_DDR  DDRB
#define LED_PORT PORTB

#define DELAYTIME 200

#define setBit(sfr, bit)     (_SFR_BYTE(sfr) |= (1 << bit))
#define clearBit(sfr, bit)   (_SFR_BYTE(sfr) &= ~(1 << bit))
#define toggleBit(sfr, bit)  (_SFR_BYTE(sfr) ^= (1 << bit))


void setupController();
void setServo(uint8_t id, uint8_t start, uint8_t stop);
int main(void)
{
  setBit(LED_DDR, LED);
  clock_prescale_set(clock_div_1);
  i2c_init();
  uint8_t err = i2c_start(0x41);

  while(err == 1) {
    setBit(LED_PORT, LED);
    uint8_t err = i2c_start(0x41);

  }
  clearBit(LED_PORT, LED);
  while(1) {

}
  return 0;
}

Thank you in advance!

Wezley
  • 413
  • 1
  • 3
  • 19
  • 1
    Without intending to push you towards *using* Arduino *in your application* you might see if you could temporarily get some Arduino PCA9685 example (such as Adafruit's) to *momentarily* run in order to validate your hadware connections - you might also in the process discover something overlooked. – Chris Stratton Sep 13 '17 at 03:54
  • Unfortunately I don't have an arduino to work with at the moment. I'm almost positive everything is hooked up correctly. I'm assuming my I2C code is fine? I'll try with another I2C device to see if something on my micro controller isn't working right. – Wezley Sep 13 '17 at 04:03
  • I mean run an Arduino test program on your present ATmega168 MCU to see what you can learn from it, then revert to using your custom code. You don't even need the bootloader for this, you can let the Arduino download fail after making a hex file and flash that by whatever method you are currently using. That said, get some cheap Arduino clones especially of the miniature breadboardable variety - even if you never intend to touch their software stack, they are about the cheapest ATmega development boards around. – Chris Stratton Sep 13 '17 at 04:08
  • If you aren't sure if the error is caused by hardware or software, it is better to ask at https://electronics.stackexchange.com/ instead. Provide a simple schematic if possible. – Lundin Sep 13 '17 at 09:47

0 Answers0