alphawatch / alphawatch.c

Diff from to

alphawatch.c

 /**
- * \file Seven segment driver
+ * \file Alpha numeric watch
+ *
+ * Based on the HDSP-2110 display.
  *
  */
 
 }
 
 
+#define ADDR_0	0xF0
+#define ADDR_1	0xF1
+#define ADDR_2	0xF4
+#define ADDR_3	0xF5
+#define ADDR_4	0xF6
 
-/*  4
- * 5 3
- *  7
- * 0 2
- *  1 
- *
- * 6 is not used since D6 drives the onboard LED.
- */
-static const uint8_t digits[] = {
-	1 << 3 | 1 << 1 | 1 << 0 | 1 << 5 | 1 << 4 | 1 << 2, // 0
-	1 << 3 | 1 << 2, // 1
-	1 << 4 | 1 << 1 | 1 << 7 | 1 << 0 | 1 << 3, // 2
-	1 << 4 | 1 << 2 | 1 << 7 | 1 << 3 | 1 << 1, // 3
-	1 << 5 | 1 << 7 | 1 << 2 | 1 << 3, // 4
-	1 << 4 | 1 << 5 | 1 << 7 | 1 << 2 | 1 << 1, // 5
-	1 << 4 | 1 << 0 | 1 << 7 | 1 << 2 | 1 << 5 | 1 << 1, // 6
-	1 << 4 | 1 << 2 | 1 << 3, // 7
-	1 << 3 | 1 << 1 | 1 << 0 | 1 << 5 | 1 << 4 | 1 << 2 | 1 << 7, // 8
-	1 << 5 | 1 << 7 | 1 << 2 | 1 << 3 | 1 << 4, // 9
-};
+#define DATA_0	0xB0
+#define DATA_1	0xB1
+#define DATA_2	0xB2
+#define DATA_3	0xB3
+#define DATA_4	0xD0
+#define DATA_5	0xD1
+#define DATA_6	0xD2
+#define DATA_7	0xD3
 
-static const uint8_t chases[] = {
-	1 << 4, // chase
-	1 << 5, // chase
-	1 << 0, // chase
-	1 << 1, // chase
-	1 << 2, // chase
-	1 << 3, // chase
-};
+#define CE	0xF7
+#define RESET	0xB6
 
 
 
-
-/** "Frame buffer" of output LEDs.
- * Since we can't drive all LEDs simultaneously, instead draw them from
- * this frame buffer.
- */
-static uint8_t outputs[4];
-
-/** Brightness goes from 0 to 15 */
-static uint8_t brightness = 15;
-
-
-/** PWM the output */
-static void
-draw(void)
-{
-	uint8_t mask = 1;
-	if (brightness >= 16)
-		brightness = 15;
-
-	for (uint8_t i = 0 ; i < 7 ; i++, mask <<= 1)
-	{
-		// skip output 6
-		if (i == 6)
-			mask <<= 1;
-
-		DDRC = outputs[0] & mask;
-		DDRD = outputs[1] & mask;
-		DDRB = outputs[2] & mask;
-		DDRF = outputs[3] & mask;
-		_delay_us(brightness);
-
-		DDRB = 0;
-		DDRC = 0;
-		DDRD = 0;
-		DDRF = 0;
-
-		if (brightness < 15)
-			_delay_us(15 - brightness);
-	}
-
-}
-
-
 static void
-report_count(void)
+lcd_write(
+	uint8_t addr,
+	uint8_t data
+)
 {
-	for (uint16_t min = 0 ; min < 512 ; min++)
-	{
-		const uint8_t count = eeprom_read_byte((void*) min);
-		if (count == 0)
-			continue;
-		char buf[8];
-		uint8_t off = 0;
-		buf[off++] = hexdigit(min >> 8);
-		buf[off++] = hexdigit(min >> 4);
-		buf[off++] = hexdigit(min >> 0);
-		buf[off++] = '=';
-		buf[off++] = hexdigit(count >> 4);
-		buf[off++] = hexdigit(count >> 0);
-		buf[off++] = '\r';
-		buf[off++] = '\n';
-		usb_serial_write(buf, off);
-	}
+	out(ADDR_0, addr & 1); addr >>= 1;
+	out(ADDR_1, addr & 1); addr >>= 1;
+	out(ADDR_2, addr & 1); addr >>= 1;
+	out(ADDR_3, addr & 1); addr >>= 1;
+	out(ADDR_4, addr & 1); addr >>= 1;
+
+	out(DATA_0, data & 1); data >>= 1;
+	out(DATA_1, data & 1); data >>= 1;
+	out(DATA_2, data & 1); data >>= 1;
+	out(DATA_3, data & 1); data >>= 1;
+	out(DATA_4, data & 1); data >>= 1;
+	out(DATA_5, data & 1); data >>= 1;
+	out(DATA_6, data & 1); data >>= 1;
+	out(DATA_7, data & 1); data >>= 1;
+
+	//_delay_us(100);
+	out(CE, 0);
+	_delay_us(10);
+	out(CE, 1);
 }
 
-
-static void
-zero_count(void)
-{
-	for (uint16_t min = 0 ; min < 512 ; min++)
-	{
-		eeprom_write_byte((void*) min, 0);
-	}
-}
-
-
 int
 main(void)
 {
 	ddr(LED, 1);
 	out(LED, 1);
 
-	// Timer1 is used for psudeo random numbers
-	TCCR1B = 0
-		| 0 << CS12
-		| 0 << CS12
-		| 1 << CS10
-		;
+	ddr(DATA_0, 1);
+	ddr(DATA_1, 1);
+	ddr(DATA_2, 1);
+	ddr(DATA_3, 1);
+	ddr(DATA_4, 1);
+	ddr(DATA_5, 1);
+	ddr(DATA_6, 1);
+	ddr(DATA_7, 1);
+
+	ddr(ADDR_0, 1);
+	ddr(ADDR_1, 1);
+	ddr(ADDR_2, 1);
+	ddr(ADDR_3, 1);
+	ddr(ADDR_4, 1);
+
+	ddr(CE, 1);
+	ddr(RESET, 1);
+
+	out(CE, 1);
+	out(RESET, 1);
+
+	_delay_ms(100);
+	out(RESET, 0);
+	_delay_ms(10);
+	out(RESET, 1);
+
+#define CHAR_RAM 0x18
+
+	// We should be good to go
+	lcd_write(0x20, 0x00); // normal operation
+
+	lcd_write(CHAR_RAM | 0, 'A');
+	lcd_write(CHAR_RAM | 1, 'B');
+	lcd_write(CHAR_RAM | 2, 'C');
+	lcd_write(CHAR_RAM | 3, 'D');
+	lcd_write(CHAR_RAM | 4, 'E');
+	lcd_write(CHAR_RAM | 5, 'F');
+	lcd_write(CHAR_RAM | 6, 'G');
+	lcd_write(CHAR_RAM | 7, 'H');
 
         // Timer 0 is used for a 64 Hz control loop timer.
         // Clk/1024 == 15.625 KHz, count up to 244
 	// "AT command", which can still be buffered.
 	usb_serial_flush_input();
 
-	send_str(PSTR("seven segment\r\n"));
-	DDRC = 1 << 7;
-	PORTC = 0;
-
-	outputs[0] = digits[0];
-	outputs[1] = digits[1];
-	outputs[2] = digits[2];
-	outputs[3] = digits[3];
-
-	for (uint16_t i = 0 ; i < 64 ; )
-	{
-		draw();
-		if (bit_is_clear(TIFR0, OCF0A))
-			continue;
-		sbi(TIFR0, OCF0A);
-		i++;
-	}
-
-	uint8_t chase = 0;
-	uint16_t ticks = 0;
-	uint16_t ms = 0;
-	uint16_t sec = TCNT1;
-
-	uint16_t total_count = 0;
-	uint16_t minute_count = 0;
-	uint16_t minute_current = 0;
-
-	for ( ; minute_current < 512 ; minute_current++)
-	{
-		const uint8_t count = eeprom_read_byte((void*) minute_current);
-		if (count == 0)
-			break;
-	}
+	send_str(PSTR("alphawatch\r\n"));
+	uint32_t ticks = 0;
 	
 	while (1)
 	{
 		int c = usb_serial_getchar();
 		if (c != -1)
 		{
-			if (c == '+')
-			{
-				if (brightness != 16)
-					brightness++;
-			} else
-			if (c == '-')
-			{
-				if (brightness != 0)
-					brightness --;
-			}
-			else
-			if (c == ' ')
-			{
-				total_count++;
-				minute_count++;
-			}
-			else
-			if (c == '?')
-			{
-				report_count();
-			} else
-			if (c == '!')
-			{
-				zero_count();
-				minute_count = total_count = 0;
-			}
+			lcd_write(CHAR_RAM | 5, c);
+			usb_serial_putchar(c);
 		}
 
-		if (bit_is_clear(TIFR0, OCF0A))
+		if (bit_is_set(TIFR0, OCF0A))
 		{
-			draw();
-			continue;
+			sbi(TIFR0, OCF0A); // reset the bit
+			// do periodic stuff;
 		}
 
-		sbi(TIFR0, OCF0A); // reset the bit
 		ticks++;
-		if (++ms == 200)
-		{
-			ms = 0;
-			if (++sec == 60)
-			{
-				// Write minute_count
-				eeprom_write_byte(minute_current++, minute_count);
-				sec = 0;
-				minute_count = 0;
-				chase = 1;
-			}
-		}
-
-		if (chase)
-		{
-			if (ticks % 16 != 0)
-				continue;
-			outputs[0] = chases[(chase+0) % 6];
-			outputs[1] = chases[(chase+1) % 6];
-			outputs[2] = chases[(chase+2) % 6];
-			outputs[3] = chases[(chase+3) % 6];
-			if (chase++ == 6*2)
-				chase = 0;
-		
-			continue;
-		}
-
-		uint16_t o = total_count;
-		outputs[3] = digits[o % 10]; o /= 10;
-		outputs[2] = digits[o % 10]; o /= 10;
-		outputs[1] = digits[o % 10]; o /= 10;
-		outputs[0] = digits[o % 10]; o /= 10;
+		lcd_write(CHAR_RAM | 7, hexdigit(ticks >> 0x00));
+		lcd_write(CHAR_RAM | 6, hexdigit(ticks >> 0x04));
+		lcd_write(CHAR_RAM | 5, hexdigit(ticks >> 0x08));
+		lcd_write(CHAR_RAM | 4, hexdigit(ticks >> 0x0C));
+		lcd_write(CHAR_RAM | 3, hexdigit(ticks >> 0x10));
+		lcd_write(CHAR_RAM | 2, hexdigit(ticks >> 0x14));
+		lcd_write(CHAR_RAM | 1, hexdigit(ticks >> 0x18));
+		lcd_write(CHAR_RAM | 0, hexdigit(ticks >> 0x1C));
 	}
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.