Browse Source

VAPC: Configure SPI, out to LED.

Vladimir N. Shilov 10 năm trước cách đây
mục cha
commit
b8db42d441
10 tập tin đã thay đổi với 369 bổ sung48 xóa
  1. 30 22
      Makefile
  2. 6 0
      ReadMe.txt
  3. 14 7
      VAPC-meter.cbp
  4. 36 1
      VAPC-meter.layout
  5. 57 0
      lib/max7219.c
  6. 132 0
      lib/max7219.h
  7. 0 0
      lib/timing_delay.c
  8. 0 0
      lib/timing_delay.h
  9. BIN
      misc/MAX7219-MAX7221.pdf
  10. 94 18
      src/main.c

+ 30 - 22
Makefile

@@ -4,19 +4,29 @@
 
 # Âçÿë èç ïðîåêòà atomthreads è ïåðåèíà÷èë.
 
+PROJECT=VAPC
+
 # Location of build tools and atomthreads sources
 EWSTM8_DIR=C:\IAR\Embedded Workbench 7.0\stm8
-APP_SRC=src
-APP_INC=inc
-PERIPHS_SRC=StdPerphDrv/src
-PERIPHS_INC=StdPerphDrv/inc
 EWSTM8_BIN=C:/IAR/Embedded\ Workbench\ 7.0/stm8/bin
 CC=$(EWSTM8_BIN)/iccstm8
 ASM=$(EWSTM8_BIN)/iasmstm8
 LINK=$(EWSTM8_BIN)/ilinkstm8
-HEX=$(EWSTM8_BIN)/ielftool
+HEX=$(EWSTM8_BIN)/ielftool
+
+APP_SRC = src
+LIB_SRC = lib
+
+# Sources pats
+vpath %.c $(APP_SRC)
+vpath %.c $(LIB_SRC)
+vpath %.c StdPerphDrv/src
 
-PROJECT=VAPC
+# Include paths
+INCLUDES = -I inc
+INCLUDES += -I lib
+INCLUDES += -I StdPerphDrv/inc
+
 
 # CPU part number
 PART = STM8L05X_LD_VL
@@ -32,6 +42,9 @@ DATA_MODEL = small
 
 # Application object files
 APP_OBJECTS = $(notdir $(patsubst %.c,%.o,$(wildcard $(APP_SRC)/*.c)))
+
+# Librarys object files
+LIB_OBJECTS = $(notdir $(patsubst %.c,%.o,$(wildcard $(LIB_SRC)/*.c)))
 
 # STM8S Peripheral driver object files
 PERIPH_OBJECTS = stm8l15x_adc.o
@@ -65,25 +78,19 @@ PERIPH_OBJECTS += stm8l15x_usart.o
 
 
 # Collection of built objects (excluding test applications)
-ALL_OBJECTS = $(APP_OBJECTS) $(PERIPH_OBJECTS)
+ALL_OBJECTS = $(APP_OBJECTS) $(LIB_OBJECTS) $(PERIPH_OBJECTS)
 BUILT_OBJECTS = $(patsubst %,$(OUT_DIR)/%,$(ALL_OBJECTS))
 
 # Target application filenames (.elf) for each test object
-PROJ_ELFS = $(PROJECT).elf
-PROJ_S19S = $(PROJECT).s19
-PROJ_IHEX = $(PROJECT).hex
+PROJ_ELF = $(PROJECT).elf
+PROJ_S19 = $(PROJECT).s19
+PROJ_HEX = $(PROJECT).hex
 
 # Search build/output directory for dependencies
-vpath %.c $(APP_SRC)
-vpath %.c $(PERIPHS_SRC)
 vpath %.o .\$(OBJ_DIR)
 vpath %.elf .\$(OUT_DIR)
 vpath %.hex .\$(OUT_DIR)
 
-# Include paths
-INCLUDES = -I $(APP_INC)
-INCLUDES += -I $(PERIPHS_INC)
-
 # Compiler/Assembler flags
 CFLAGS = -e -Oh
 CFLAGS += --code_model $(CODE_MODEL) --data_model $(DATA_MODEL)
@@ -96,9 +103,9 @@ DBG_CFLAGS = -e -Ol --no_cse --no_unroll --no_inline --no_code_motion --no_tbaa
 DBG_CFLAGS += --no_cross_call --debug --code_model $(CODE_MODEL) --data_model $(DATA_MODEL)
 DBG_CFLAGS += --dlib_config "$(EWSTM8_DIR)\lib\$(LIB_MODEL)" -D $(PART)
 DBG_CFLAGS += -lC $(LIST_DIR) -lB $(LIST_DIR)
-DBG_CFLAGS += --diag_suppress Pa050 
+DBG_CFLAGS += --diag_suppress Pa050
 
-ASMFLAGS = -M'<>' -ld $(OUT_DIR)\list --diag_suppress Pa050 
+ASMFLAGS = -M'<>' -ld $(OUT_DIR)\list --diag_suppress Pa050
 ASMFLAGS += --code_model small --data_model small
 
 DBG_ASMFLAGS = -M'<>' -r -ld $(OUT_DIR)\list --diag_suppress Pa050
@@ -126,7 +133,8 @@ DBG_LINKFLAGS += --entry __iar_program_start
 #################
 
 # All tests
-all: $(OUT_DIR) $(PROJ_S19S) $(PROJ_IHEX)
+all: $(OUT_DIR) $(PROJ_S19) $(PROJ_HEX)
+Release: all
 
 # Make build/output directory
 $(OUT_DIR):
@@ -135,17 +143,17 @@ $(OUT_DIR):
 	@mkdir $(LIST_DIR)
 
 # Test HEX files (one application build for each test)
-$(PROJ_S19S): %.s19: $(PROJECT).elf
+$(PROJ_S19): %.s19: $(PROJECT).elf
 	@echo
 	@echo Building $@
 	@$(HEX) $(OUT_DIR)/$(notdir $<) $(OUT_DIR)/$@ --srec --silent
 
-$(PROJ_IHEX): %.hex: $(PROJECT).elf
+$(PROJ_HEX): %.hex: $(PROJECT).elf
 	@echo Building $@
 	@$(HEX) $(OUT_DIR)/$(notdir $<) $(OUT_DIR)/$@ --ihex --silent
 
 # Test ELF files (one application build for each test)
-$(PROJ_ELFS): %.elf: $(PERIPH_OBJECTS) $(APP_OBJECTS)
+$(PROJ_ELF): %.elf: $(ALL_OBJECTS)
 	@echo
 	@echo Linking $@
 	@$(LINK) $(OBJ_DIR)/*.o $(LINKFLAGS) -o $(OUT_DIR)/$@

+ 6 - 0
ReadMe.txt

@@ -43,3 +43,9 @@ SVN-
 2015.05.14
 
 добавил скелет проекта, makefile, C::B проект.
+
+---
+2015.05.15
+
+настройка SPI.
+должно вывести 0-7 на индикаторы.

+ 14 - 7
VAPC-meter.cbp

@@ -6,13 +6,13 @@
 		<Option makefile_is_custom="1" />
 		<Option pch_mode="2" />
 		<Option compiler="null" />
-		<Option virtualFolders="StdPerphDrv\;StdPerphDrv\sources\;StdPerphDrv\headers\;Timing delay\;" />
+		<Option virtualFolders="MAX7219\;StdPerphDrv\;StdPerphDrv\sources\;StdPerphDrv\headers\;Timing delay\;" />
 		<Build>
 			<Target title="Release">
 				<Option output="bin/Release/VAPC-meter" prefix_auto="1" extension_auto="1" />
 				<Option object_output="obj/Release/" />
 				<Option type="1" />
-				<Option compiler="null" />
+				<Option compiler="gcc" />
 			</Target>
 		</Build>
 		<Unit filename="Makefile" />
@@ -218,7 +218,18 @@
 		</Unit>
 		<Unit filename="inc/stm8l15x_conf.h" />
 		<Unit filename="inc/stm8l15x_it.h" />
-		<Unit filename="inc/timing_delay.h">
+		<Unit filename="lib/max7219.c">
+			<Option compilerVar="CC" />
+			<Option virtualFolder="MAX7219/" />
+		</Unit>
+		<Unit filename="lib/max7219.h">
+			<Option virtualFolder="MAX7219/" />
+		</Unit>
+		<Unit filename="lib/timing_delay.c">
+			<Option compilerVar="CC" />
+			<Option virtualFolder="Timing delay/" />
+		</Unit>
+		<Unit filename="lib/timing_delay.h">
 			<Option virtualFolder="Timing delay/" />
 		</Unit>
 		<Unit filename="src/main.c">
@@ -227,10 +238,6 @@
 		<Unit filename="src/stm8l15x_it.c">
 			<Option compilerVar="CC" />
 		</Unit>
-		<Unit filename="src/timing_delay.c">
-			<Option compilerVar="CC" />
-			<Option virtualFolder="Timing delay/" />
-		</Unit>
 		<Extensions>
 			<code_completion />
 			<envvars />

+ 36 - 1
VAPC-meter.layout

@@ -1,9 +1,44 @@
 <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
 <CodeBlocks_layout_file>
 	<ActiveTarget name="Release" />
+	<File name="StdPerphDrv\inc\stm8l15x_clk.h" open="0" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
+		<Cursor>
+			<Cursor1 position="11542" topLine="255" />
+		</Cursor>
+	</File>
+	<File name="StdPerphDrv\src\stm8l15x_clk.c" open="1" top="0" tabpos="7" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
+		<Cursor>
+			<Cursor1 position="21509" topLine="515" />
+		</Cursor>
+	</File>
+	<File name="lib\timing_delay.c" open="1" top="0" tabpos="6" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
+		<Cursor>
+			<Cursor1 position="3396" topLine="54" />
+		</Cursor>
+	</File>
+	<File name="lib\max7219.c" open="1" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
+		<Cursor>
+			<Cursor1 position="813" topLine="0" />
+		</Cursor>
+	</File>
+	<File name="lib\max7219.h" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
+		<Cursor>
+			<Cursor1 position="2218" topLine="50" />
+		</Cursor>
+	</File>
 	<File name="src\main.c" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="1935" topLine="15" />
+			<Cursor1 position="1495" topLine="58" />
+		</Cursor>
+	</File>
+	<File name="Makefile" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
+		<Cursor>
+			<Cursor1 position="4622" topLine="0" />
+		</Cursor>
+	</File>
+	<File name="lib\timing_delay.h" open="1" top="0" tabpos="5" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
+		<Cursor>
+			<Cursor1 position="933" topLine="0" />
 		</Cursor>
 	</File>
 </CodeBlocks_layout_file>

+ 57 - 0
lib/max7219.c

@@ -0,0 +1,57 @@
+/* MAX7219 Interaction Code
+ * ---------------------------
+ * For more information see
+ * http://www.adnbr.co.uk/articles/max7219-and-7-segment-displays
+ * ----------------------------------------------------------------------------
+ * "THE BEER-WARE LICENSE" (Revision 42):
+ * <shilow@ukr.net> wrote this file.  As long as you retain this notice you
+ * can do whatever you want with this stuff. If we meet some day, and you think
+ * this stuff is worth it, you can buy me a beer in return.   Shilov V.N.
+ * ----------------------------------------------------------------------------
+ */
+
+#include "max7219.h"
+
+#define SPI_PORT        GPIOB
+#define SPI_NSS         GPIO_Pin_4
+#define SPI_SCK         GPIO_Pin_5
+#define SPI_MOSI        GPIO_Pin_6
+#define SPI_PINS        (SPI_NSS|SPI_SCK|SPI_MOSI)
+
+void MAX7219_Init(void) {
+    SPI_DeInit(SPI1);
+    /* Enable clock for SPI */
+    CLK_PeripheralClockConfig(CLK_Peripheral_SPI1, ENABLE);
+    /* Set the MOSI,MISO and SCK at high level */
+    GPIO_ExternalPullUpConfig(SPI_PORT, SPI_PINS, ENABLE);
+//    GPIO_Init(SPI_PORT, SPI_PINS, GPIO_Mode_Out_PP_High_Fast);
+    /* Init SPI */
+    SPI_Init(SPI1, SPI_FirstBit_MSB, SPI_BaudRatePrescaler_2, SPI_Mode_Master,
+             SPI_CPOL_High, SPI_CPHA_2Edge, SPI_Direction_1Line_Tx, SPI_NSS_Hard, 0x00);
+    /* SPI Enable */
+    SPI_Cmd(SPI1, ENABLE);
+
+    /* Настройка MAX71219 */
+	MAX7219_WriteData(DecodeMode, 0x00);            // все без BCD декодирования
+	MAX7219_WriteData(ScanLimit, MAX7219_DIGITS);   // сколько цифр используем
+	MAX7219_WriteData(Intensity, MAX7219_BRIGHT);   // яркость из 16
+	MAX7219_WriteData(Power,MAX7219_ON);            // включили питание
+
+}
+
+void MAX7219_WriteData(max7219_reg_t reg, uint8_t data)
+{
+//    SPI_NSSInternalSoftwareCmd(ENABLE);
+
+    /*!< Wait until the transmit buffer is empty */
+    while (SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET) {}
+    /* Send the register where the data will be stored */
+    SPI_SendData(SPI1, reg);
+
+    /*!< Wait until the transmit buffer is empty */
+    while (SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET) {}
+    /* Send the data to be stored */
+    SPI_SendData(SPI1, data);
+
+//    SPI_NSSInternalSoftwareCmd(DISABLE);
+}

+ 132 - 0
lib/max7219.h

@@ -0,0 +1,132 @@
+/* MAX7219 Header file
+ * ---------------------------
+ * For more information see
+ * http://www.adnbr.co.uk/articles/max7219-and-7-segment-displays
+ * ----------------------------------------------------------------------------
+ * "THE BEER-WARE LICENSE" (Revision 42):
+ * <shilow@ukr.net> wrote this file.  As long as you retain this notice you
+ * can do whatever you want with this stuff. If we meet some day, and you think
+ * this stuff is worth it, you can buy me a beer in return.   Shilov V.N.
+ * ----------------------------------------------------------------------------
+ */
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#pragma once
+#ifndef __MAX7219_H
+#define __MAX7219_H
+
+/* Includes ------------------------------------------------------------------*/
+#include "stm8l15x.h"
+
+/* Exported defines ----------------------------------------------------------*/
+// соответсвие бит сегментам
+#define SEG_A			5
+#define SEG_B			3
+#define SEG_C			4
+#define SEG_D			1
+#define SEG_E			0
+#define SEG_F			6
+#define SEG_G			7
+#define SEG_DP			2
+
+// symbols
+// Для BCD
+#define MAX7219_CHAR_BLANK	0x0F
+#define MAX7219_CHAR_FULL	0x88
+// без кодирования
+#define SYM_0		0x7B
+#define SYM_1		0x18
+#define SYM_2		0xAB
+#define SYM_3		0xBA
+#define SYM_4		0xD8
+#define SYM_5		0xF2
+#define SYM_6		0xF3
+#define SYM_7		0x38
+#define SYM_8		0xFB
+#define SYM_9		0xFA
+#define SYM_A		0xF9
+#define SYM_b		0xD3
+#define SYM_c		0x83
+#define SYM_C		0x63
+#define SYM_d		0x9B
+#define SYM_E		0xE3
+#define SYM_F		0xE1
+#define SYM_P		0xE9
+#define SYM_Gradus	0xE8
+#define SYM_LGradus	0x93
+#define SYM_Temp	0xC3
+#define SYM_Minus	0x80
+#define SYM_BLANK	0x00
+#define SYM_FULL	0xFF
+
+#define MAX7219_ON			0x01
+#define MAX7219_OFF			0x00
+#define MAX7219_BRIGHT		0x08
+
+// used LED digits - 1
+#define MAX7219_DIGITS  7
+
+/* Exported types ------------------------------------------------------------*/
+typedef enum {
+    Digit0      = 0x07,
+    Digit1      = 0x06,
+    Digit2      = 0x04,
+    Digit3      = 0x01,
+    Digit4      = 0x03,
+    Digit5      = 0x02,
+    Digit6      = 0x00,
+    Digit7      = 0x05,
+    DecodeMode  = 0x09,
+    Intensity   = 0x0A,
+    ScanLimit   = 0x0B,
+    Power       = 0x0C,
+    Test        = 0x0F
+} max7219_reg_t;
+
+typedef enum {
+    SegA    = 5,
+    SegB    = 3,
+    SegC    = 4,
+    SegD    = 1,
+    SegE    = 0,
+    SegF    = 6,
+    SegG    = 7,
+    SegDP   = 2,
+} max7219_seg_t;
+
+typedef enum {
+    Sym_0        = 0x7B,
+    Sym_1        = 0x18,
+    Sym_2        = 0xAB,
+    Sym_3        = 0xBA,
+    Sym_4        = 0xD8,
+    Sym_5        = 0xF2,
+    Sym_6        = 0xF3,
+    Sym_7        = 0x38,
+    Sym_8        = 0xFB,
+    Sym_9        = 0xFA,
+    Sym_A        = 0xF9,
+    Sym_b        = 0xD3,
+    Sym_c        = 0x83,
+    Sym_C        = 0x63,
+    Sym_d        = 0x9B,
+    Sym_E        = 0xE3,
+    Sym_F        = 0xE1,
+    Sym_P        = 0xE9,
+    Sym_Gradus	 = 0xE8,
+    Sym_LGradus	 = 0x93,
+    Sym_Temp	 = 0xC3,
+    Sym_Minus	 = 0x80,
+    Sym_Plus	 = 0x98, // C1 ?
+    Sym_BLANK	 = 0x00,
+    Sym_FULL	 = 0xFF
+} max7219_sym_t;
+
+/* Exported constants --------------------------------------------------------*/
+/* Exported macro ------------------------------------------------------------*/
+/* Exported variables --------------------------------------------------------*/
+/* Exported functions --------------------------------------------------------*/
+void MAX7219_Init(void);
+void MAX7219_WriteData(max7219_reg_t reg, uint8_t data);
+
+#endif /* __MAX7219_H */

+ 0 - 0
src/timing_delay.c → lib/timing_delay.c


+ 0 - 0
inc/timing_delay.h → lib/timing_delay.h


BIN
misc/MAX7219-MAX7221.pdf


+ 94 - 18
src/main.c

@@ -1,32 +1,26 @@
 /**
   ******************************************************************************
-  * @file    Project/STM8L15x_StdPeriph_Template/main.c
-  * @author  MCD Application Team
-  * @version V1.6.1
-  * @date    30-September-2014
+  * @file    VAPC-meter
+  * @author  Vladimir N. Shilov <shilow@ukr.net>
+  * @version V0.0.1
+  * @date    2015.05.15
   * @brief   Main program body
   ******************************************************************************
   * @attention
   *
-  * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
-  *
-  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
-  * You may not use this file except in compliance with the License.
-  * You may obtain a copy of the License at:
-  *
-  *        http://www.st.com/software_license_agreement_liberty_v2
-  *
-  * Unless required by applicable law or agreed to in writing, software 
-  * distributed under the License is distributed on an "AS IS" BASIS, 
-  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-  * See the License for the specific language governing permissions and
-  * limitations under the License.
+  * ----------------------------------------------------------------------------
+  * "THE BEER-WARE LICENSE" (Revision 42):
+  * <shilow@ukr.net> wrote this file.  As long as you retain this notice you
+  * can do whatever you want with this stuff. If we meet some day, and you think
+  * this stuff is worth it, you can buy me a beer in return.   Shilov V.N.
+  * ----------------------------------------------------------------------------
   *
   ******************************************************************************
   */
 
 /* Includes ------------------------------------------------------------------*/
 #include "stm8l15x.h"
+#include "max7219.h"
 
 /** @addtogroup STM8L15x_StdPeriph_Template
   * @{
@@ -35,8 +29,43 @@
 /* Private typedef -----------------------------------------------------------*/
 /* Private define ------------------------------------------------------------*/
 /* Private macro -------------------------------------------------------------*/
+/* Private constant ----------------------------------------------------------*/
+// перевод числа 0-7 в номер индикатора
+static const max7219_reg_t dig[8] = {
+	Digit0,
+	Digit1,
+	Digit2,
+	Digit3,
+	Digit4,
+	Digit5,
+	Digit6,
+	Digit7
+};
+
+// перевод значения 0x00 - 0x0F в код индикатора
+static const max7219_sym_t num[16] = {
+	Sym_0,
+	Sym_1,
+	Sym_2,
+	Sym_3,
+	Sym_4,
+	Sym_5,
+	Sym_6,
+	Sym_7,
+	Sym_8,
+	Sym_9,
+	Sym_A,
+	Sym_b,
+	Sym_C,
+	Sym_d,
+	Sym_E,
+	Sym_F
+};
+
 /* Private variables ---------------------------------------------------------*/
 /* Private function prototypes -----------------------------------------------*/
+static void GPIO_Config(void);
+static void CLK_Config(void);
 
 /* Private functions ---------------------------------------------------------*/
 
@@ -47,12 +76,59 @@
   */
 void main(void)
 {
+  /* Clock configuration -----------------------------------------*/
+  CLK_Config();
+
+  /* GPIO Configuration  -----------------------------------------*/
+  GPIO_Config();
+
+  /* MAX7219 Configuration */
+  MAX7219_Init();
+
+  uint8_t i;
+  for (i=0;i<8;i++) {
+    MAX7219_WriteData(dig[i], num[i]);
+  }
+
   /* Infinite loop */
   while (1)
   {
   }
 }
 
+/**
+  * @brief  Configure GPIO for button available on the VAPC board
+  * @param  None
+  * @retval None
+  */
+static void GPIO_Config(void)
+{
+  /* Configure GPIO used to drive KEY button */
+//    STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_EXTI);
+
+    /* Enable general interrupts */
+    enableInterrupts();
+}
+
+/**
+  * @brief  Configure system clock to run at Maximum clock speed
+  * @param  None
+  * @retval None
+  */
+static void CLK_Config(void)
+{
+  CLK_DeInit();
+
+  /* Configure the clock source */
+  CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSI);
+
+  /* Configure the System clock frequency to run at 16Mhz */
+  CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);
+
+  CLK_HSICmd(ENABLE);
+}
+
+
 #ifdef  USE_FULL_ASSERT
 
 /**
@@ -63,7 +139,7 @@ void main(void)
   * @retval None
   */
 void assert_failed(uint8_t* file, uint32_t line)
-{ 
+{
   /* User can add his own implementation to report the file name and line number,
      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */