Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
i2c basics problem
07-10-2014, 02:56 PM, (This post was last modified: 07-10-2014, 04:44 PM by GastonLagaffe.)
#1
i2c basics problem
Salut,

I still have issues with the i1c bus:
I use the adafruit alphanumeric 4-digit display for testing as it is very simple to use. It uses a HT16k33 chip. I use the following code:

Code:
u8 i2cAddr = 0x70;
u16 displaybuffer[8];

void writeDisplay(void) {
   u8 i;
   I2C.start();
   I2C.send((i2cAddr << 1) & 0xFE);
   I2C.send(0x00); // start at address $00

   for (i=0; i<8; i++) {
       I2C.send(displaybuffer[i] & 0xFF);    
       I2C.send(displaybuffer[i] >> 8);    
   }
   I2C.stop();
}

void clearDisplay(void) {
   u8 i;
   for (i=0; i<8; i++) {
       displaybuffer[i] = 0;
   }
}

void setup() {
   u8 i;
   
   pinMode(12, OUTPUT);
   Serial.println("Starting I2C bus");
   I2C.master(I2C_100KHZ);
   I2C.start();
   I2C.send((i2cAddr << 1) & 0xFE);
   I2C.send(0x21);                             // turn on oscillator
   I2C.stop();                                 // Terminate the write sequence}
   
   I2C.start();
   I2C.send((i2cAddr << 1) & 0xFE);
   I2C.send(0x81);                             // turn blink OFF
   I2C.stop();                                 // Terminate the write sequence}
   
   I2C.start();
   I2C.send((i2cAddr << 1) & 0xFE);
   I2C.send(0xEF);                             // set maximum brightness
   I2C.stop();                                 // Terminate the write sequence}

   displaybuffer[0] = 0x0;
   displaybuffer[1] = 0x0;
   displaybuffer[2] = 0x0;
   displaybuffer[3] = 0x0;
   writeDisplay();
   
}

void loop() {
   // put your main code here, to run repeatedly:
   delay(200);
   displaybuffer[3] = 0x0;
   displaybuffer[0] = 0xFFFF;
   writeDisplay();
   delay(200);
   displaybuffer[0] = 0x0;
   displaybuffer[1] = 0xFFFF;
   writeDisplay();
   delay(200);
   displaybuffer[1] = 0x0;
   displaybuffer[2] = 0xFFFF;
   writeDisplay();
   delay(200);
   displaybuffer[2] = 0x0;
   displaybuffer[3] = 0xFFFF;
   writeDisplay();
   delay(200);
}


Connecting the display to an arduino works ok. I use 10kOhm resistors to pull-up SDA and SCL. I connect SCL to pin labeled 01 (pin 34 of chip) and SDA to pin 00 (pin 33 of chip) and 5V to "+" and GND to "-" right next to pin 00. Power seems to be ok as my arduino starts up when I connect to the power bar of the breadboard.
I userd the PCF8574 example as base.

Unfortunately, the display stays dark ...

Any suggestion is welcome,
Mathias
Reply
07-10-2014, 05:59 PM, (This post was last modified: 07-10-2014, 06:21 PM by GastonLagaffe.)
#2
RE: i2c basics problem
Salut,

I checked the acknowledge state of the write calls and found that they fail in setup() but work in loop()
So I moved the initialization calls from setup() to loop() and now the display works.
However, the first run through the loop() returns an error, then the i2c commands are executed ok. I now check an init counter and do the init in loop twice. Doing it setup() twice does not work.

I think this is not the right behavior...

I also found in the wiki that I2C.write is for P8 processors and I2C.send for P32. Is this intentional to have different code for the two architectures? As both calls work on the P8 PIC18F45k50, I assume this is an error in the wiki

but hey! happy it works ...
Ciao, Mathias
Reply
08-10-2014, 11:46 AM, (This post was last modified: 09-10-2014, 02:01 PM by GastonLagaffe.)
#3
RE: i2c basics problem
Salut,

I have cleaned out the code and it now displays all 127 ascii characters on the display. I finally fixed the code to initialize the display. With this, it now initializes the display in setup().
Here the complete code for you to enjoy (I have put it into the wiki as well)
Code:
/*-----------------------------------------------------

Author: Dr. Mathias Wilhelm - http://www.mathias-wilhelm.de/arduino
Date: 2014-10-09, Version 1.03
Description:
Drive a alphanumeric 4-digit led display with HT16k33 chip from adafruit
display can be found at adafruit: http://www.adafruit.com/products/1911
Code based on the Demo code from adafruit led backpack kit
This code is only tested with the PIC18F45k50
Connection:
Display | PIC18F45k50
vi2c +
VCC +
GND -
SDA 00 (pin 33 of chip)
SCL 01 (pin 34 of chip)
License:
This code is free software; you can redistribute it and/or
modify it under the terms of the CC BY-NC-SA 3.0 license.
-----------------------------------------------------*/
const u16 alphafonttable[] = {
0b0000000000000001,
0b0000000000000010,
0b0000000000000100,
0b0000000000001000,
0b0000000000010000,
0b0000000000100000,
0b0000000001000000,
0b0000000010000000,
0b0000000100000000,
0b0000001000000000,
0b0000010000000000,
0b0000100000000000,
0b0001000000000000,
0b0010000000000000,
0b0100000000000000,
0b1000000000000000,
0b0000000000000000,
0b0000000000000000,
0b0000000000000000,
0b0000000000000000,
0b0000000000000000,
0b0000000000000000,
0b0000000000000000,
0b0000000000000000,
0b0001001011001001,
0b0001010111000000,
0b0001001011111001,
0b0000000011100011,
0b0000010100110000,
0b0001001011001000,
0b0011101000000000,
0b0001011100000000,
0b0000000000000000, //
0b0000000000000110, // !
0b0000001000100000, // "
0b0001001011001110, // #
0b0001001011101101, // $
0b0000110000100100, // %
0b0010001101011101, // &
0b0000010000000000, // '
0b0010010000000000, // (
0b0000100100000000, // )
0b0011111111000000, // *
0b0001001011000000, // +
0b0000100000000000, // ,
0b0000000011000000, // -
0b0000000000000000, // .
0b0000110000000000, // /
0b0000110000111111, // 0
0b0000000000000110, // 1
0b0000000011011011, // 2
0b0000000010001111, // 3
0b0000000011100110, // 4
0b0010000001101001, // 5
0b0000000011111101, // 6
0b0000000000000111, // 7
0b0000000011111111, // 8
0b0000000011101111, // 9
0b0001001000000000, // :
0b0000101000000000, // ;
0b0010010000000000, // <
0b0000000011001000, // =
0b0000100100000000, // >
0b0001000010000011, // ?
0b0000001010111011, // @
0b0000000011110111, // A
0b0001001010001111, // B
0b0000000000111001, // C
0b0001001000001111, // D
0b0000000011111001, // E
0b0000000001110001, // F
0b0000000010111101, // G
0b0000000011110110, // H
0b0001001000000000, // I
0b0000000000011110, // J
0b0010010001110000, // K
0b0000000000111000, // L
0b0000010100110110, // M
0b0010000100110110, // N
0b0000000000111111, // O
0b0000000011110011, // P
0b0010000000111111, // Q
0b0010000011110011, // R
0b0000000011101101, // S
0b0001001000000001, // T
0b0000000000111110, // U
0b0000110000110000, // V
0b0010100000110110, // W
0b0010110100000000, // X
0b0001010100000000, // Y
0b0000110000001001, // Z
0b0000000000111001, // [
0b0010000100000000, //
0b0000000000001111, // ]
0b0000110000000011, // ^
0b0000000000001000, // _
0b0000000100000000, // `
0b0001000001011000, // a
0b0010000001111000, // b
0b0000000011011000, // c
0b0000100010001110, // d
0b0000100001011000, // e
0b0000000001110001, // f
0b0000010010001110, // g
0b0001000001110000, // h
0b0001000000000000, // i
0b0000000000001110, // j
0b0011011000000000, // k
0b0000000000110000, // l
0b0001000011010100, // m
0b0001000001010000, // n
0b0000000011011100, // o
0b0000000101110000, // p
0b0000010010000110, // q
0b0000000001010000, // r
0b0010000010001000, // s
0b0000000001111000, // t
0b0000000000011100, // u
0b0010000000000100, // v
0b0010100000010100, // w
0b0010100011000000, // x
0b0010000000001100, // y
0b0000100001001000, // z
0b0000100101001001, // {
0b0001001000000000, // |
0b0010010010001001, // }
0b0000010100100000, // ~
0b0011111111111111,
};

boolean debug = true;
u8 i2cAddr = (0x70 << 1) & 0xFE;
u16 displaybuffer[8];
u8 initDone = 0;

boolean initDisplay(void){
boolean status = true;
if (debug) Serial.println("\n\rStarting I2C bus");
if (debug) Serial.println("turn on oscillator");
I2C.start();
status &= I2C.write(i2cAddr);
status &= I2C.send(0x21);
I2C.stop(); // Terminate the write sequence}

if (debug) Serial.println("turn blink off");
I2C.start();
status &= I2C.write(i2cAddr);
status &= I2C.write(0x81);
I2C.stop(); // Terminate the write sequence}

if (debug) Serial.println("set maximum brightness");
I2C.start();
status &= I2C.write(i2cAddr);
status &= I2C.write(0xEF);
I2C.stop(); // Terminate the write sequence}
return status;
}

boolean writeDisplay(void) {
u8 i;
boolean status = true;
//i2cAddr = (0x70 << 1) & 0xFE;
//i2cAddr = 0x70;
I2C.start();
status &= I2C.write(i2cAddr);
status &= I2C.write(0x00);
for (i=0; i<8; i++) {
status &= I2C.write(displaybuffer[i] & 0xFF);
status &= I2C.write(displaybuffer[i] >> 8);
}
I2C.stop();
return status;
}

void clearDisplay(void) {
u8 i;
for (i=0; i<8; i++) {
displaybuffer[i] = 0;
}
}

void setup() {
u8 i;

I2C.master(I2C_100KHZ);

pinMode(12, OUTPUT);
if (debug) Serial.begin(9600);
if (debug) delay(1000);

displaybuffer[0] = 0x0;
displaybuffer[1] = 0x0;
displaybuffer[2] = 0x0;
displaybuffer[3] = 0x0;

if (!initDisplay()){
if (debug) Serial.println("init display failed");
}
if (debug) Serial.println("Setup done");
}

void loop() {
int i;

if (debug) Serial.println("start loop");
displaybuffer[3] = 0x0;
displaybuffer[0] = 0xFFFF;
if (!writeDisplay()){
if (debug) Serial.println("write display failed");
}
delay(200);
displaybuffer[0] = 0x0;
displaybuffer[1] = 0xFFFF;
if (!writeDisplay()){
if (debug) Serial.println("write display failed");
}
delay(200);
displaybuffer[1] = 0x0;
displaybuffer[2] = 0xFFFF;
if (!writeDisplay()){
if (debug) Serial.println("write display failed");
}
delay(200);
displaybuffer[2] = 0x0;
displaybuffer[3] = 0xFFFF;
if (!writeDisplay()){
if (debug) Serial.println("write display failed");
}
delay(200);
for (i=0; i<124; i++){
displaybuffer[0] = alphafonttable[i];
displaybuffer[1] = alphafonttable[i+1];
displaybuffer[2] = alphafonttable[i+2];
displaybuffer[3] = alphafonttable[i+3];
writeDisplay();
delay(200);
}

}

Ciao, Mathias
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)