#include #include #include "ht1632c.h" #include "font1.h" #define DS1307_I2C_ADDRESS 0x68 // Dual 3216 LED Matrix // If you have only one set these to: // X_MAX=31 // Y_MAX=15 // CHIP_MAX=4 #define X_MAX 63 // 0 based X #define Y_MAX 15 // 0 based Y #define CHIP_MAX 4*2 // Number of HT1632C Chips // 4 each board * 2 boards // possible values for a pixel; #define BLACK 0 #define GREEN 1 #define RED 2 #define ORANGE 3 #define ASSERT(condition) //nothing /* * Set these constants to the values of the pins connected to the SureElectronics Module */ static const byte ht1632_data = 53; // Data pin (pin 7) static const byte ht1632_clk = 52; // Data pin (pin 2) static const byte ht1632_wrclk = 51; // Write clock pin (pin 5) static const byte ht1632_cs = 50; // Chip Select (pin 1) // The should also be a common GND (pins . // The module with all LEDs like draws about 200mA, // which makes it PROBABLY powerable via Arduino +5V /* * we keep a copy of the display controller contents so that we can * know which bits are on without having to (slowly) read the device. * Note that we only use the low four bits of the shadow ram, since * we're shadowing 4-bit memory. This makes things faster, but we * COULD do something with the other half of our bytes ! */ byte ht1632_shadowram[64][CHIP_MAX] = {0}; /* * we use buffer to put the char fonts */ char buffer[8][8]; //************************************************************************************************** //Function Name: decToBcd //Function Feature: Convert normal decimal numbers to binary coded decimal //Input Argument: void //Output Argument: void //************************************************************************************************** byte decToBcd(byte val) { return ( (val/10*16) + (val%10) ); } //************************************************************************************************** //Function Name: bcdToDec //Function Feature: Convert binary coded decimal to normal decimal numbers //Input Argument: void //Output Argument: void //************************************************************************************************** byte bcdToDec(byte val) { return ( (val/16*10) + (val%16) ); } //************************************************************************************************** //Function Name: setDateDs1307 //Function Feature: set date/time to DS1307 IC //Input Argument: void //Output Argument: void //************************************************************************************************** void setDateDs1307(byte second, // 0-59 byte minute, // 0-59 byte hour, // 1-23 byte dayOfWeek, // 1-7 byte dayOfMonth, // 1-28/29/30/31 byte month, // 1-12 byte year) // 0-99 { Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.send(0); Wire.send(decToBcd(second)); // 0 to bit 7 starts the clock Wire.send(decToBcd(minute)); Wire.send(decToBcd(hour)); // If you want 12 hour am/pm you need to set // bit 6 (also need to change readDateDs1307) Wire.send(decToBcd(dayOfWeek)); Wire.send(decToBcd(dayOfMonth)); Wire.send(decToBcd(month)); Wire.send(decToBcd(year)); Wire.endTransmission(); } //************************************************************************************************** //Function Name: getDateDs1307 //Function Feature: get date/time from DS1307 IC //Input Argument: void //Output Argument: void //************************************************************************************************** void getDateDs1307(byte *second, byte *minute, byte *hour, byte *dayOfWeek, byte *dayOfMonth, byte *month, byte *year) { // Reset the register pointer Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.send(0); Wire.endTransmission(); Wire.requestFrom(DS1307_I2C_ADDRESS, 7); // A few of these need masks because certain bits are control bits *second = bcdToDec(Wire.receive() & 0x7f); *minute = bcdToDec(Wire.receive()); *hour = bcdToDec(Wire.receive() & 0x3f); // Need to change this if 12 hour am/pm *dayOfWeek = bcdToDec(Wire.receive()); *dayOfMonth = bcdToDec(Wire.receive()); *month = bcdToDec(Wire.receive()); *year = bcdToDec(Wire.receive()); } //************************************************************************************************** //Function Name: ds1307_initialize //Function Feature: initialize DS1307 IC //Input Argument: void //Output Argument: void //************************************************************************************************** void ds1307_initialize() { Wire.begin(); Serial.begin(9600); } //************************************************************************************************** //Function Name: OutputCLK_Pulse //Function Feature: enable CLK_74164 pin to output a clock pulse //Input Argument: void //Output Argument: void //************************************************************************************************** void OutputCLK_Pulse(void) //Output a clock pulse { digitalWrite(ht1632_clk, HIGH); digitalWrite(ht1632_clk, LOW); } //************************************************************************************************** //Function Name: ht1632_chipselect //Function Feature: enable HT1632C //Input Argument: select: HT1632C to be selected // If select=0, select none. // If s<0, select all. //Output Argument: void //************************************************************************************************** void ht1632_chipselect(int select) { unsigned char tmp = 0; if (select < 0) { // Enable all HT1632C digitalWrite(ht1632_cs, LOW); for (tmp=0; tmp>= 1; } } //************************************************************************************************** //Function Name: ht1632_sendcmd //Function Feature: Send a command to the ht1632 chip. // Select 1 0 0 c7 c6 c5 c4 c3 c2 c1 c0 xx Free //Input Argument: chipNo: the chip you want to send data // command: consists of a 3-bit "CMD" ID, an 8bit command, and // one "don't care bit". //Output Argument: void //************************************************************************************************** static void ht1632_sendcmd (byte chipNo, byte command) { ht1632_chipselect(chipNo); ht1632_writebits(HT1632_ID_CMD, 1<<2); // send 3 bits of id: COMMMAND ht1632_writebits(command, 1<<7); // send the actual command ht1632_writebits(0, 1); /* one extra dont-care bit in commands. */ ht1632_chipselect(0); } //************************************************************************************************** //Function Name: ht1632_senddata //Function Feature: send a nibble (4 bits) of data to a particular memory location of the // ht1632. The command has 3 bit ID, 7 bits of address, and 4 bits of data. // Select 1 0 1 A6 A5 A4 A3 A2 A1 A0 D0 D1 D2 D3 Free // Note that the address is sent MSB first, while the data is sent LSB first! // This means that somewhere a bit reversal will have to be done to get // zero-based addressing of words and dots within words. //Input Argument: chipNo: the chip you want to send data // address: chip address to write // data: data to write to chip memory //Output Argument: void //************************************************************************************************** static void ht1632_senddata (byte chipNo, byte address, byte data) { ht1632_chipselect(chipNo); ht1632_writebits(HT1632_ID_WR, 1<<2); // send ID: WRITE to RAM ht1632_writebits(address, 1<<6); // Send address ht1632_writebits(data, 1<<3); // send 4 bits of data ht1632_chipselect(0); } //************************************************************************************************** //Function Name: ht1632_clear //Function Feature: clear display //Input Argument: void //Output Argument: void //************************************************************************************************** void ht1632_clear() { char i; for (int j=1;j<=CHIP_MAX;j++) { ht1632_chipselect(j); ht1632_writebits(HT1632_ID_WR, 1<<2); // send ID: WRITE to RAM ht1632_writebits(0, 1<<6); // Send address for (i=0; i<64/2; i++) // Clear entire display ht1632_writebits(0, 1<<7); // send 8 bits of data ht1632_chipselect(0); for (i=0; i<64; i++) ht1632_shadowram[i][j-1] = 0; } } //************************************************************************************************** //Function Name: xyToIndex //Function Feature: get the value of x,y //Input Argument: x: X coordinate // y: Y coordinate //Output Argument: address of xy //************************************************************************************************** byte xyToIndex(byte x, byte y) { byte nChip, addr; if (x>=32) { nChip = 3 + x/16 + (y>7?2:0); } else { nChip = 1 + x/16 + (y>7?2:0); } x = x % 16; y = y % 8; addr = (x<<1) + (y>>2); return addr; } //************************************************************************************************** //Function Name: calcBit //Function Feature: calculate the bitval of y //Input Argument: y: Y coordinate //Output Argument: bitval //************************************************************************************************** #define calcBit(y) (8>>(y&3)) //************************************************************************************************** //Function Name: get_pixel //Function Feature: get the value of x,y //Input Argument: x: X coordinate // y: Y coordinate //Output Argument: color setted on x,y coordinates //************************************************************************************************** int get_pixel(byte x, byte y) { byte addr, bitval, nChip; if (x>=32) { nChip = 3 + x/16 + (y>7?2:0); } else { nChip = 1 + x/16 + (y>7?2:0); } addr = xyToIndex(x,y); bitval = calcBit(y); if ((ht1632_shadowram[addr][nChip-1] & bitval) && (ht1632_shadowram[addr+32][nChip-1] & bitval)) { return ORANGE; } else if (ht1632_shadowram[addr][nChip-1] & bitval) { return GREEN; } else if (ht1632_shadowram[addr+32][nChip-1] & bitval) { return RED; } else { return 0; } } //************************************************************************************************** //Function Name: ht1632_plot //Function Feature: plot a dot on x,y //Input Argument: x: X coordinate // y: Y coordinate // color: BLACK(clean), GREEN, RED, ORANGE //Output Argument: void //************************************************************************************************** void ht1632_plot (byte x, byte y, byte color) { byte nChip, addr, bitval; if (x<0 || x>X_MAX || y<0 || y>Y_MAX) return; if (color != BLACK && color != GREEN && color != RED && color != ORANGE) return; if (x>=32) { nChip = 3 + x/16 + (y>7?2:0); } else { nChip = 1 + x/16 + (y>7?2:0); } addr = xyToIndex(x,y); bitval = calcBit(y); switch (color) { case BLACK: if (get_pixel(x,y) != BLACK) { // compare with memory to only set if pixel is other color // clear the bit in both planes; ht1632_shadowram[addr][nChip-1] &= ~bitval; ht1632_senddata(nChip, addr, ht1632_shadowram[addr][nChip-1]); addr = addr + 32; ht1632_shadowram[addr][nChip-1] &= ~bitval; ht1632_senddata(nChip, addr, ht1632_shadowram[addr][nChip-1]); } break; case GREEN: if (get_pixel(x,y) != GREEN) { // compare with memory to only set if pixel is other color // set the bit in the green plane and clear the bit in the red plane; ht1632_shadowram[addr][nChip-1] |= bitval; ht1632_senddata(nChip, addr, ht1632_shadowram[addr][nChip-1]); addr = addr + 32; ht1632_shadowram[addr][nChip-1] &= ~bitval; ht1632_senddata(nChip, addr, ht1632_shadowram[addr][nChip-1]); } break; case RED: if (get_pixel(x,y) != RED) { // compare with memory to only set if pixel is other color // clear the bit in green plane and set the bit in the red plane; ht1632_shadowram[addr][nChip-1] &= ~bitval; ht1632_senddata(nChip, addr, ht1632_shadowram[addr][nChip-1]); addr = addr + 32; ht1632_shadowram[addr][nChip-1] |= bitval; ht1632_senddata(nChip, addr, ht1632_shadowram[addr][nChip-1]); } break; case ORANGE: if (get_pixel(x,y) != ORANGE) { // compare with memory to only set if pixel is other color // set the bit in both the green and red planes; ht1632_shadowram[addr][nChip-1] |= bitval; ht1632_senddata(nChip, addr, ht1632_shadowram[addr][nChip-1]); addr = addr + 32; ht1632_shadowram[addr][nChip-1] |= bitval; ht1632_senddata(nChip, addr, ht1632_shadowram[addr][nChip-1]); } break; } } //************************************************************************************************** //Function Name: set_buffer //Function Feature: set buffer variable to char //Input Argument: chr: char to set on buffer //Output Argument: void //************************************************************************************************** void set_buffer(char chr){ for(int i=0; i