Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample #1

Open
Xodor opened this issue Feb 12, 2014 · 4 comments
Open

Sample #1

Xodor opened this issue Feb 12, 2014 · 4 comments

Comments

@Xodor
Copy link

Xodor commented Feb 12, 2014

Hi Erik,

is it possible to provide a small sample, where this lib is included ?

Klaus

@eriksl
Copy link
Owner

eriksl commented Feb 13, 2014

is it possible to provide a small sample, where this lib is included ?

Have a look here: http://github.com/eriksl/attiny861_1

What are you making?

@Xodor
Copy link
Author

Xodor commented Feb 13, 2014

Hi Erik,

 

i want to control a led, and learn about avr programming.

the address of my slave should be 0x55 and the commands are:

0x01 : led on

0x02 : led off

 

But nothing is working.

 

In the link at bottom, you can find my testproject

 

If you can help me get this working, i would be very glad.

 

Joern.

 

https://anonfiles.com/file/922e51f666e2e01cf6dd1bed79b44080

@Xodor
Copy link
Author

Xodor commented Feb 13, 2014

Hi Erik,

i post it here.

#include <avr/interrupt.h>
#include <avr/io.h>
#include "usitwislave/usitwislave.h"

#define LED_OFF PORTB &= ~(1<<PB3)
#define LED_ON PORTB |= 1<<PB3

static void build_reply(uint8_t volatile *output_buffer_length, volatile uint8_t *output_buffer,
        uint8_t reply_length, const uint8_t *reply_string)
{
    uint8_t ix;

    output_buffer[0] = reply_length;

    for(ix = 0; ix < reply_length; ix++)
    {
        output_buffer[1 + ix] = reply_string[ix];
   }

    *output_buffer_length = reply_length + 1;
}

static void twi_callback(uint8_t buffer_size, volatile uint8_t input_buffer_length, const volatile uint8_t *input_buffer,
                        uint8_t volatile *output_buffer_length, volatile uint8_t *output_buffer)
{

    if (input_buffer_length > 0)
    {
        if (input_buffer[0] == 0x01)
        {
            LED_ON;
        }

        if (input_buffer[0] == 0x02)
        {
            LED_OFF;
        }

    }

    return(build_reply(output_buffer_length, output_buffer, 0, 0));
}

int main(void)
{
    DDRB |= 1<<PB3;

    usi_twi_slave(0x55, 0, twi_callback, 0);

    return 0;
}

@eriksl
Copy link
Owner

eriksl commented Feb 14, 2014

the address of my slave should be 0x55 and the commands are:
0x01 : led on
0x02 : led off
But nothing is working.

The first thing that comes to my mind is wrong i2c address. The adress
you supply to the library is the "plain" address. I2c shifts this
address by one bit for the read/write bit. So often you must use the
adress << 1. Or exactly the other way around address >> 1.

Otherwise are you sure that the electrical part is correct, pull up
resistors? You can hook up a transistor + led to the scl and sda lines
to see what happens (I did...). These lines are normally high, so you
should use an inverting circuit (led parallel over collector and
emitter, of course a resistor between + and the collector/led.

I've found out the internal pull up resistors of the attiny861 are not
always working. What microcontroller are you using? If you're using a
attiny861, you should also configure what pins are used for i2c.

Are you sure you program is even run? How about a second led that
flashes when the program runs (add this code and add a led):

#include <avr/delay.h>

DDRB = _BV(3) | _BV(4);

main()
{
PINB = _BV(4); // this toggles the led
_delays_ms(100);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants