浏览代码

First Vin.

Vladimir N. Shilov 5 年之前
父节点
当前提交
eb8668ee6d
共有 5 个文件被更改,包括 127 次插入114 次删除
  1. 2 2
      Makefile
  2. 10 0
      ReadMe.txt
  3. 24 56
      lib/max7219.c
  4. 49 40
      lib/max7219.h
  5. 42 16
      src/main.c

+ 2 - 2
Makefile

@@ -2,7 +2,7 @@
 # IAR Makefile for STM8S #
 ##########################
 
-PROJECT=VAPC
+PROJECT=VAPC2
 
 # Location of build tools and atomthreads sources
 EWSTM8_DIR=C:\IAR\Embedded Workbench 7.3\stm8
@@ -12,7 +12,7 @@ ASM=$(EWSTM8_BIN)/iasmstm8
 LINK=$(EWSTM8_BIN)/ilinkstm8
 HEX=$(EWSTM8_BIN)/ielftool
 FLASHTOOL=C:/MCU/STMicroelectronics/st_toolset/stvp/STVP_CmdLine.exe
-FLASHOPT=-Device=STM8S003F3
+FLASHOPT=-no_log -no_loop -Device=STM8S003F3
 
 DEFINES = -D USE_STDPERIPH_DRIVER
 DEFINES += -D STM8S003

+ 10 - 0
ReadMe.txt

@@ -9,3 +9,13 @@
 ===
 2019.01.23
 Ужал плату до размера 42 на 37 мм.
+---
+2019.07.09
+Какая-то ерунда с распинвкой индикаторов - плата и схема сходяться, а по 
+факту - пришлось методом тыка определять на каком бите какой сегмент...
+
+Хотел зелёный индикатор сверху, а красный снизу - запаял наоборот...
+
+Внешний кварц не запускается.
+
+Запустил SPI и MAX7219.

+ 24 - 56
lib/max7219.c

@@ -12,65 +12,33 @@
 
 #include "max7219.h"
 
-#define SPI_PORT        GPIOC
-#define SPI_SCK         GPIO_PIN_5
-#define SPI_MOSI        GPIO_PIN_6
-#define SPI_PINS        (SPI_SCK|SPI_MOSI)
-#define SPI_LOAD_PORT   GPIOD
-#define SPI_LOAD        GPIO_PIN_3
-
-#define GPIO_SetBits(port, pin)     port->ODR |= (uint8_t)pin
-#define GPIO_ResetBits(port, pin)   GPIOx->ODR &= (uint8_t)(~PortPins)
-
 void MAX7219_Config(void) {
-  /* Set the MOSI,MISO and SCK at high level */
-  SPI_PORT->CR1 |= (uint8_t)SPI_PINS;
-
-  /* Configure LOAD pin to Push-Pull, High, Fast*/
-  SPI_LOAD_PORT->ODR |= SPI_LOAD;
-  SPI_LOAD_PORT->DDR |= SPI_LOAD;
-  SPI_LOAD_PORT->CR1 |= SPI_LOAD;
-  SPI_LOAD_PORT->CR2 &= (uint8_t)(~(SPI_LOAD));
-
-  /* Enable clock for SPI */
-  CLK->PCKENR1 |= 1 << 0x01;
-
-  /* SPI_MODE_MASTER, SPI_FIRSTBIT_MSB, SPI_BAUDRATEPRESCALER_2, SPI_CLOCKPOLARITY_HIGH, SPI_CLOCKPHASE_2EDGE */
-  SPI->CR1 = 0x04 | 0x00 | 0x00 | 0x02 | 0x01;
-
-  /* SPI_DATADIRECTION_1LINE_TX, SPI_NSS_SOFT */
-  SPI->CR2 = 0xC0 | 0x02 | SPI_CR2_SSI;
-
-  /* SPI Enable */
-  SPI->CR1 |= SPI_CR1_SPE;
-
   /* Настройка MAX71219 */
-	MAX7219_WriteData(RegDecodeMode, 0x00);            // все без BCD декодирования
-	MAX7219_WriteData(RegScanLimit, MAX7219_DIGITS);   // сколько цифр используем
-	MAX7219_WriteData(RegIntensity, MAX7219_BRIGHT);   // яркость из 16
-	MAX7219_WriteData(RegPower, MAX7219_ON);           // включили питание
+  MAX7219_WriteData(RegDecodeMode, 0x00);            // все без BCD декодирования
+  MAX7219_WriteData(RegScanLimit, MAX7219_DIGITS);   // сколько цифр используем
+  MAX7219_WriteData(RegIntensity, MAX7219_BRIGHT);   // яркость из 16
+  MAX7219_WriteData(RegPower, MAX7219_ON);           // включили питание
 
 }
 
-void MAX7219_WriteData(max7219_reg_t reg, uint8_t data)
-{
-    /*!< Wait wait until the completion of the transfer. */
-    while ((SPI->SR & 0x80) != RESET);
-    /* Down LOAD pin */
-    SPI_LOAD_PORT->ODR &= (uint8_t)(~SPI_LOAD);
-
-    /*!< Wait until the transmit buffer is empty */
-    while ((SPI->SR & 0x02) == RESET);
-    /* Send the register where the data will be stored */
-    SPI->DR = reg; /* Write in the DR register the data to be sent*/
-
-    /*!< Wait until the transmit buffer is empty */
-    while ((SPI->SR & 0x02) != RESET);
-    /* Send the data to be stored */
-    SPI->DR = data; /* Write in the DR register the data to be sent*/
-
-    /*!< Wait wait until the completion of the transfer. */
-    while ((SPI->SR & 0x80) != RESET);
-    /* Up LOAD pin */
-    SPI_LOAD_PORT->ODR |= SPI_LOAD;
+void MAX7219_WriteData(max7219_reg_t reg, uint8_t data) {
+  /*!< Wait wait until the completion of the transfer. */
+  while ((SPI->SR & 0x80) != RESET);
+  /* Down LOAD pin */
+  SPI_LOAD_PORT->ODR &= ~(SPI_LOAD);
+
+  /*!< Wait until the transmit buffer is empty */
+  while ((SPI->SR & 0x02) == RESET);
+  /* Send the register where the data will be stored */
+  SPI->DR = reg; /* Write in the DR register the data to be sent*/
+
+  /*!< Wait until the transmit buffer is empty */
+  while ((SPI->SR & 0x02) == RESET);
+  /* Send the data to be stored */
+  SPI->DR = data; /* Write in the DR register the data to be sent*/
+
+  /*!< Wait wait until the completion of the transfer. */
+  while ((SPI->SR & 0x80) != RESET);
+  /* Up LOAD pin */
+  SPI_LOAD_PORT->ODR |= SPI_LOAD;
 }

+ 49 - 40
lib/max7219.h

@@ -19,9 +19,16 @@
 #include "stm8s.h"
 
 /* Exported defines ----------------------------------------------------------*/
-#define MAX7219_ON			0x01
-#define MAX7219_OFF			0x00
-#define MAX7219_BRIGHT	0x08
+#define SPI_PORT        GPIOC
+#define SPI_SCK         GPIO_PIN_5
+#define SPI_MOSI        GPIO_PIN_6
+#define SPI_PINS        (SPI_SCK|SPI_MOSI)
+#define SPI_LOAD_PORT   GPIOD
+#define SPI_LOAD        GPIO_PIN_3
+
+#define MAX7219_ON      0x01
+#define MAX7219_OFF     0x00
+#define MAX7219_BRIGHT  0x08
 
 // used LED digits - 1
 #define MAX7219_DIGITS  7
@@ -46,47 +53,49 @@ typedef enum _max7219_reg {
 
 // соответсвие бит сегментам
 typedef enum _max7219_seg {
-  SegA    = 4,
-  SegB    = 0,
-  SegC    = 1,
-  SegD    = 7,
-  SegE    = 3,
-  SegF    = 2,
-  SegG    = 5,
-  SegDP   = 6,
+  SegA  = (uint8_t)(1 << 2),
+  SegB  = (uint8_t)(1 << 6),
+  SegC  = (uint8_t)(1 << 5),
+  SegD  = (uint8_t)(1 << 7),
+  SegE  = (uint8_t)(1 << 3),
+  SegF  = (uint8_t)(1 << 4),
+  SegG  = (uint8_t)(1 << 1),
+  SegDP = (uint8_t)(1 << 0),
 } max7219_seg_t;
 
 // symbols без кодирования
 typedef enum _max7219_sym {
-  Sym_0       = 0x9F,
-  Sym_1       = 0x03,
-  Sym_2       = 0xB9,
-  Sym_3       = 0xB3,
-  Sym_4       = 0x27,
-  Sym_5       = 0xB6,
-  Sym_6       = 0xBe,
-  Sym_7       = 0x13,
-  Sym_8       = 0xBF,
-  Sym_9       = 0xB7,
-  Sym_A       = 0x3F,
-  Sym_b       = 0xAE,
-  Sym_c       = 0xA8,
-  Sym_C       = 0x9C,
-  Sym_d       = 0xAB,
-  Sym_E       = 0xBC,
-  Sym_F       = 0x3C,
-  Sym_h       = 0x2E,
-  Sym_H       = 0x2F,
-  Sym_o     	= 0xAA,
-  Sym_P       = 0x3D,
-  Sym_t       = 0xAC,
-  Sym_u       = 0x8A,
-  Sym_U       = 0x8F,
-  Sym_Gradus	= 0x35,
-  Sym_Minus	  = 0x20,
-  Sym_BLANK	  = 0x00,
-  Sym_FULL	  = 0xFF,
-  Sym_Dot     = 0x40
+  Sym_0       = SegA | SegB | SegC | SegD | SegE | SegF,
+  Sym_1       = SegB | SegC,
+  Sym_2       = SegA | SegB | SegD | SegE | SegG,
+  Sym_3       = SegA | SegB | SegC | SegD | SegG,
+  Sym_4       = SegB | SegC | SegF | SegG,
+  Sym_5       = SegA | SegC | SegD | SegF | SegG,
+  Sym_6       = SegA | SegC | SegD | SegE | SegF | SegG,
+  Sym_7       = SegA | SegB | SegC,
+  Sym_8       = SegA | SegB | SegC | SegD | SegE | SegF | SegG,
+  Sym_9       = SegA | SegB | SegC | SegD | SegF | SegG,
+  Sym_A       = SegA | SegB | SegC | SegE | SegF | SegG,
+  Sym_b       = SegC | SegD | SegE | SegF | SegG,
+  Sym_c       = SegD | SegE | SegG,
+  Sym_C       = SegA | SegD | SegE | SegF,
+  Sym_d       = SegB | SegC | SegD | SegE | SegG,
+  Sym_E       = SegA | SegD | SegE | SegF | SegG,
+  Sym_F       = SegA | SegE | SegF | SegG,
+  Sym_i       = SegC,
+  Sym_h       = SegC | SegE | SegF | SegG,
+  Sym_H       = SegB | SegC | SegE | SegF | SegG,
+  Sym_n       = SegC | SegE | SegG,
+  Sym_o       = SegC | SegD | SegE | SegG,
+  Sym_P       = SegA | SegB | SegE | SegF | SegG,
+  Sym_t       = SegD | SegE | SegF | SegG,
+  Sym_u       = SegC | SegD | SegE,
+  Sym_U       = SegB | SegC | SegD | SegE | SegF,
+  Sym_Gradus  = SegA | SegB | SegF | SegG,
+  Sym_Minus   = SegG,
+  Sym_BLANK   = 0x00,
+  Sym_FULL    = 0xFF,
+  Sym_Dot     = SegDP
 } max7219_sym_t;
 
 /* Exported constants --------------------------------------------------------*/

+ 42 - 16
src/main.c

@@ -26,7 +26,7 @@ const static max7219_reg_t digitPosition[8] = {
   RegDigit0, RegDigit1, RegDigit2, RegDigit3,
   RegDigit4, RegDigit5, RegDigit6, RegDigit7
 };
-const static uint8_t digitValue[16] = {
+const static max7219_sym_t digitValue[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
 };
@@ -41,14 +41,24 @@ void main(void)
   boardInit();
 
   MAX7219_Config();
-  uint8_t i;
-  for (i=1; i<9; i++) {
-    MAX7219_WriteData(digitPosition[i], digitValue[i]);
-  }
+
+  MAX7219_WriteData(digitPosition[0], Sym_BLANK);
+  MAX7219_WriteData(digitPosition[1], Sym_BLANK);
+  MAX7219_WriteData(digitPosition[2], Sym_BLANK);
+  MAX7219_WriteData(digitPosition[3], Sym_BLANK);
+  MAX7219_WriteData(digitPosition[4], Sym_BLANK);
+  MAX7219_WriteData(digitPosition[5], Sym_BLANK);
+  MAX7219_WriteData(digitPosition[6], Sym_BLANK);
+  MAX7219_WriteData(digitPosition[7], Sym_BLANK);
 
   /* Infinite loop */
   while (1)
   {
+    if (CLK_GetSYSCLKSource() == CLK_SOURCE_HSE) {
+      MAX7219_WriteData(digitPosition[3], Sym_E);
+    } else {
+      MAX7219_WriteData(digitPosition[3], Sym_i);
+    }
   }
 
 }
@@ -64,25 +74,41 @@ static void boardInit(void) {
 
   ErrorStatus status = ERROR;
 
-  CLK_DeInit();
-
-  /* Configure the Fcpu to DIV1*/
-  CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);
-
-  /* Configure the HSI prescaler to the optimal value */
-  CLK_SYSCLKConfig(CLK_PRESCALER_HSIDIV1);
+  /* Initialization of the clock to 16MHz */
+  CLK->CKDIVR = 0x00;
 
-  /* Output Fcpu on CLK_CCO pin */
-  CLK_CCOConfig(CLK_OUTPUT_CPU);
+  /* Disable clock of unused peripherial */
+  CLK->PCKENR1 = (uint8_t)(~CLK_PCKENR1_UART1);
+  CLK->PCKENR2 = (uint8_t)(~(CLK_PCKENR2_CAN | CLK_PCKENR2_ADC | CLK_PCKENR2_AWU));
 
   /* Configure the system clock to use HSE clock source and to run at Crystal Mhz */
   status = CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSE, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE);
-
   if (status == ERROR) {
     /* Configure the system clock to use HSI clock source and to run at 16Mhz */
-    status = CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE);
     // set FLAG for
   }
+
+  /* Init GPIO */
+  /* Set the MOSI,MISO and SCK at high level */
+  SPI_PORT->ODR |= SPI_MOSI;
+  SPI_PORT->DDR |= SPI_PINS;
+  SPI_PORT->CR1 |= SPI_PINS;
+  SPI_PORT->CR2 |= SPI_PINS;
+
+  /* Configure LOAD pin to Push-Pull, High, Fast*/
+  SPI_LOAD_PORT->ODR |= SPI_LOAD;
+  SPI_LOAD_PORT->DDR |= SPI_LOAD;
+  SPI_LOAD_PORT->CR1 |= SPI_LOAD;
+  SPI_LOAD_PORT->CR2 |= SPI_LOAD;
+
+  /* Init SPI */
+  /* SPI_MODE_MASTER, SPI_FIRSTBIT_MSB, SPI_BAUDRATEPRESCALER_2, SPI_CLOCKPOLARITY_LOW, SPI_CLOCKPHASE_1EDGE */
+  SPI->CR1 = 0x04;
+  /* SPI_DATADIRECTION_1LINE_TX, SPI_NSS_SOFT */
+  SPI->CR2 = 0xC0 | 0x02 | 0x01;
+  /* SPI Enable */
+  SPI->CR1 |= SPI_CR1_SPE;
+
 }
 
 #ifdef USE_FULL_ASSERT