// This program will setup Counters 0 - 2 and then poll the counters every 2 sec // // // by Mark // Verison Histroy // Ver Date Descriptsion // 1.00 9/13/23 #define PRM_VERSION "1.00" #include #include "LS7866_Registers.h" // Include LS7866 Register Def's // Define which counters will be used #define CNTR_0 0 //#define CNTR_1 1 //#define CNTR_2 2 #define BRD_INT_PIN 2 // INT0 is Pin2 , INT1 is Pin3 #define CNTR_SIZE 4 // number of bytes to configure counters #define LED_OFF 0 #define LED_RED 1 #define LED_GRN 2 #define LED_FLASH_INTERVAL 2000 // 2 Sec //#define LED_FLASH_INTERVAL 500 // .5 Sec #define LED_FLASH_ON_TIME 50 // 50mS const char fmtStartPromt[] = "APP LS7866-01 Ver %s\r\nI2C Master Address:%d Clock:%06ld\r\n"; const int ledPin1 = 8; const int ledPin2 = 9; const int SyncPin = 13; char msgBuff[64]; int I2cMstrAddr = 7; int LFlag = 0; int LFlagCntr = 0; unsigned long mstrClock = 400000l; unsigned long tsPollingInterval = 0; /* * Function setLed * Desc configures drive currnt in TriColor LED * Input Led Color State * Output Led off, Led Green or Led Red */ int setLed(int pState){ switch(pState){ default: case LED_OFF: digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, LOW); break; case LED_RED: digitalWrite(ledPin1, HIGH); digitalWrite(ledPin2, LOW); break; case LED_GRN: digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, HIGH); break; } return pState; } /* * Function LS7866_Read * Desc Overloaded function for reading unsigned long value from LS7866 Registers * Input slaveAddress has I2C slave address * Input regAddr has address of LS7866 register to read from * Input numBytes has number of bytes to read * Output pbValue has assembled register value */ void LS7866_Read(byte slaveAddress, byte regAddr, unsigned long *pbValue, byte numBytes){ unsigned long value = 0; byte * valPtr = (byte *)&value + numBytes-1; // configure byte pointer to MSB position // Write to Slave and set Register Addr we need to read from Wire.beginTransmission(slaveAddress); // transmit to device Wire.write(regAddr); // Set Addr of Data we want Wire.endTransmission(0); // Send Transmission do not send Stop // Send read Request to Slave Wire.requestFrom((int)slaveAddress, (int)numBytes, true); // Request from Slave NumBytes and terminate with Stop // Read data out of Wire Buffer // Data is read from device in Msb to Lsb format while(Wire.available()) *valPtr-- = (byte)Wire.read(); // load memory with data // Passback Value *pbValue = value; } /* * Function LS7866_Read * Desc Overloaded function for reading 8 bit values from LS7866 registers * Input slaveAddress has I2C slave address * Input regAddr has address of LS7866 register to read from * Output pbValue has register value */ void LS7866_Read(byte slaveAddress, byte regAddr, byte *pbValue){ // Write to Slave and set Register Addr we need to read from Wire.beginTransmission(slaveAddress); // transmit to device Wire.write(regAddr); // Set Addr of Data we want to read Wire.endTransmission(0); // Send Transmission do not send Stop // Send Request to Slave Wire.requestFrom((int)slaveAddress, 1, true); // Request from Slave NumBytes and terminate with Stop // Read data out of Wire Buffer // Data is read from device in Msb to Lsb format while(Wire.available()) *pbValue = (byte)Wire.read(); // receive byte } /* * Function LS7866_Write * Desc Overloaded function for writing unsigned long value to LS7866 Registers like IDR0 and IDR1. * Input slaveAddress has I2C slave address * Input regAddr has address of LS7866 register to read from * Input value has data to write * Input numBytes has number of bytes to write * Output Value has register value */ int LS7866_Write(byte slaveAddress, byte regAddr, unsigned long value, byte numBytes){ int error = 0; byte * valPtr = (byte*)&value + (numBytes - 1); Wire.beginTransmission(slaveAddress); // transmit to device Wire.write(regAddr); // Load Addr we want to write to // Load Data MSB to LSB int Wire Buffer for (byte i = 0; i tsPollingInterval){ tsPollingInterval = millis() + LED_FLASH_INTERVAL; // save next polling time stamp // Poll defined counters #ifdef CNTR_0 CounterPoll(0, LS7866_I2C_FIXED_ADDR + 0, CNTR_SIZE); #endif #ifdef CNTR_1 CounterPoll(1, LS7866_I2C_FIXED_ADDR + 1, CNTR_SIZE); #endif #ifdef CNTR_2 CounterPoll(2, LS7866_I2C_FIXED_ADDR + 2, CNTR_SIZE); #endif //Serial.print("\r\n"); // Add Break line } }