Arduino i2c with pcf8574 - Page 1
Forum section: electronics / general

author: carbon dude oxide
author_rank: Frequent Contributor
timestamp: April 30, 2013, 09:58:04 pm
content: Hello, i am currently building a project at home which is a nixie tube clock. So far everything has been going swimmingly as it is almost completed. Im now onto programming the arduino to read a time keeping i2c chip and then display the time on each digit which are controled by 4 PCF8574 i2c port expanders. Each port expander has 2 nixie tube drivers wich use a bcd signal to display the number.In order to show the numbers i send an 8-bit bcd to set the outputs eg:To display no 37 i have two seperate 4 bcd's. 3:0011 4:0100 and to send to the port expanders i combine them like so: 00110100 and on my prototype this worked perfectly.Now ive tried to do this again and i have come accros a problem with the 2^0 bit of the bcd as it is not being set correctly on all of the port expanders. When i send a bcd to display the no 1 it displays 1 similarly when i send no 2 it displays 2 and from here onwards any other number i send to the port expander the 2^0 bit Is always 1. It is the same with the 3 other port expanders aswell but apart from this all other outputs on the port expander function correctly.What could be causing this and how would i go about testing and finding out why it is not working and how to fix it?

author: c4757p
author_rank: Super Contributor
timestamp: April 30, 2013, 09:59:04 pm
content: Your code?

author: carbon dude oxide
author_rank: Frequent Contributor
timestamp: May 01, 2013, 06:07:16 am
content: sorry forgot to add itCode:#include <Wire.h>#include <math.h>byte numbers[] = {'0000','0001','0010','0011','0100','0101','0110','0111','1000','1001','1111'};int vara = 1;void setup() {byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;Wire.begin();Serial.begin(9600);// Change these values to what you want to set your clock to.// You probably only want to set your clock once and then remove// the setDateDs1307 call.second = 45;minute = 3;hour = 20;dayOfWeek = 5;dayOfMonth = 17;month = 4;year = 8;setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);}void loop() {byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;delay(5000);getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);int ih = hour/10;int jh = hour-(ih*10);int im = minute/10;int jm = minute-(im*10);int is = second/10;int js = second-(is*10);int testa = is;testa = testa << 4;testa = testa + js;Serial.println(testa);expanderWriteHour(byte(testa));expanderWriteMin(byte(testa));expanderWriteSec(byte(testa));expanderWriteMsec(byte(testa));Serial.println(vara);vara = vara*2;if (vara == 256) { vara = 255;}if (vara > 256) {vara = 1;}}void expanderWriteHour(byte _data ) {Wire.beginTransmission(0x3A);Wire.write(_data);Wire.endTransmission();}void expanderWriteMin(byte _data ) {Wire.beginTransmission(0x39);Wire.write(_data);Wire.endTransmission();}void expanderWriteSec(byte _data ) {Wire.beginTransmission(0x38);Wire.write(_data);Wire.endTransmission();}void expanderWriteMsec(byte _data ) {Wire.beginTransmission(0x3B);Wire.write(_data);Wire.endTransmission();}byte decToBcd(byte val){return ( (val/10*16) + (val%10) );}// Convert binary coded decimal to normal decimal numbersbyte bcdToDec(byte val){return ( (val/16*10) + (val%16) );}void setDateDs1307(byte second, // 0-59byte minute, // 0-59byte hour, // 1-23byte dayOfWeek, // 1-7byte dayOfMonth, // 1-28/29/30/31byte month, // 1-12byte year) // 0-99{Wire.beginTransmission(0x68);Wire.write(0);Wire.write(decToBcd(second)); // 0 to bit 7 starts the clockWire.write(decToBcd(minute));Wire.write(decToBcd(hour)); // If you want 12 hour am/pm you need to set// bit 6 (also need to change readDateDs1307)Wire.write(decToBcd(dayOfWeek));Wire.write(decToBcd(dayOfMonth));Wire.write(decToBcd(month));Wire.write(decToBcd(year));Wire.endTransmission();}// Gets the date and time from the ds1307void getDateDs1307(byte *second,byte *minute,byte *hour,byte *dayOfWeek,byte *dayOfMonth,byte *month,byte *year){// Reset the register pointerWire.beginTransmission(0x68);Wire.write(0);Wire.endTransmission();Wire.requestFrom(0x68, 7);// A few of these need masks because certain bits are control bits*second = bcdToDec(Wire.read() & 0x7f);*minute = bcdToDec(Wire.read());*hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm*dayOfWeek = bcdToDec(Wire.read());*dayOfMonth = bcdToDec(Wire.read());*month = bcdToDec(Wire.read());*year = bcdToDec(Wire.read());}