Commits

Tero Koskinen  committed 9058a18

Controlling 4-digit 7-segment display with Arduino MEGA 2560.

  • Participants

Comments (0)

Files changed (3)

+MCU=atmega2560
+OBJCOPY=avr-objcopy
+BIN_FORMAT=ihex
+PORT=/dev/ttyACM0
+PART=m328p
+BAUD?=115200
+AVRDUDE=sudo avrdude
+
+display.hex: display.adb
+	avr-gnatmake -p -XMCU=$(MCU) -Pdisplay.gpr
+	$(OBJCOPY) -O $(BIN_FORMAT) -R .eeprom display.elf display.hex
+
+upload-mega2560: display.hex
+	$(AVRDUDE) -c stk500v2 -p m2560 -P $(PORT) -b $(BAUD) -U flash:w:display.hex
+
+clean:
+	avr-gnatclean -XMCU=$(MCU) -Pdisplay.gpr
+	rm -f display.hex display.elf main.map
+with Interfaces;
+with AVR;
+with AVR.MCU;
+with AVR.Wait;
+
+use AVR;
+use Interfaces;
+
+procedure Display is
+
+   -- pin mapping
+   --
+   -- 52 = B1 = pin 11 = F
+   -- 51 = B2 = pin 13 = C
+   -- 50 = B3 = pin 16 = B
+   -- 13 = B7 = pin 5  = E
+   -- 12 = B6 = pin 3  = D
+   -- 11 = B5 = pin 14 = A
+   -- 10 = B4 = pin 15 = G
+   --
+   -- A5 = F5 = pin 8 = DIG 4
+   -- A6 = F6 = pin 6 = DIG 3
+   -- A7 = F7 = pin 2 = DIG 2
+   -- A8 = K0 = pin 1 = DIG 1
+
+   procedure Wait_Some_USecs is new AVR.Wait.Generic_Wait_USecs
+     (Crystal_Hertz => 16_000_000, Micro_Seconds => 2_000);
+
+   type Digit_Led is (D1, D2, D3, D4);
+
+   procedure Enable_Led (L : Digit_Led) is
+   begin
+      case L is
+         when D1 =>
+            MCU.PORTF_Bits (5) := False;
+            MCU.PORTF_Bits (6) := False;
+            MCU.PORTF_Bits (7) := False;
+            MCU.PORTK_Bits (0) := True;
+         when D2 =>
+            MCU.PORTF_Bits (5) := False;
+            MCU.PORTF_Bits (6) := False;
+            MCU.PORTF_Bits (7) := True;
+            MCU.PORTK_Bits (0) := False;
+         when D3 =>
+            MCU.PORTF_Bits (5) := False;
+            MCU.PORTF_Bits (6) := True;
+            MCU.PORTF_Bits (7) := False;
+            MCU.PORTK_Bits (0) := False;
+         when D4 =>
+            MCU.PORTF_Bits (5) := True;
+            MCU.PORTF_Bits (6) := False;
+            MCU.PORTF_Bits (7) := False;
+            MCU.PORTK_Bits (0) := False;
+      end case;
+   end Enable_Led;
+
+   type Digit_Type is new Unsigned_8 range 0 .. 9;
+   type Digit_Array is array (Digit_Type) of Unsigned_8;
+
+   Segment_Digits : Digit_Array :=
+           -- EDAGBCFx
+      (0 => 2#00010000#,
+       1 => 2#11110010#,
+       2 => 2#00000110#,
+       3 => 2#10000010#,
+       4 => 2#11100000#,
+       5 => 2#10001000#,
+       6 => 2#00001000#,
+       7 => 2#11010010#,
+       8 => 2#00000000#,
+       9 => 2#11000000#);
+
+   procedure Show_Digit (D : Digit_Type) is
+   begin
+      MCU.PORTB := Segment_Digits (D);
+   end Show_Digit;
+
+begin
+   MCU.DDRB_Bits := (others => DD_Output);
+
+   MCU.DDRF_Bits (5) := DD_Output;
+   MCU.DDRF_Bits (6) := DD_Output;
+   MCU.DDRF_Bits (7) := DD_Output;
+   MCU.DDRK_Bits (0) := DD_Output;
+
+   MCU.PORTB_Bits := (others => True);
+   MCU.PORTF_Bits := (others => False);
+   MCU.PORTK_Bits := (others => False);
+
+   MCU.PORTB_Bits (0) := False;
+   MCU.PORTB_Bits (1) := False;
+   MCU.PORTK_Bits (0) := True;
+   MCU.PORTF_Bits (7) := True;
+
+   loop
+      Enable_Led (D4);
+      Show_Digit (1);
+      Wait_Some_USecs;
+      MCU.PORTB := 0;
+
+      Enable_Led (D3);
+      Show_Digit (2);
+      Wait_Some_USecs;
+      MCU.PORTB := 0;
+
+      Enable_Led (D2);
+      Show_Digit (3);
+      Wait_Some_USecs;
+      MCU.PORTB := 0;
+
+      Enable_Led (D1);
+      Show_Digit (4);
+      Wait_Some_USecs;
+      MCU.PORTB := 0;
+   end loop;
+end Display;
+
+project Display extends "avr_app" is
+	for Main use ("display.adb");
+	for Object_Dir use "objects";
+	for Exec_Dir use ".";
+
+	for Source_Dirs use (".");
+end Display;