Browse Source

Buffer rotation. Led Control functions.

Vladimir N. Shilov 3 years ago
parent
commit
53331e269f
2 changed files with 176 additions and 14 deletions
  1. 5 0
      inc/board.h
  2. 171 14
      src/board.c

+ 5 - 0
inc/board.h

@@ -13,9 +13,14 @@
 /* Variables */
 /* Exported funcions */
 void Board_Init(void);
+void HT1632C_Init(void);
 void HT1632C_Write_bits(uint16_t data, uint16_t nbits);
 void HT1632C_Write_Cmd(uint16_t data, uint16_t nbits);
 void HT1632C_Write_Data(const uint8_t * data, uint8_t addr, uint8_t len);
+void HT1632C_Write_Buffer(void);
+void HT1632C_PWM(uint8_t pwm);
+void HT1632C_Blink(uint8_t state);
+void HT1632C_LedSt(uint8_t state);
 void EXTI4_15_IRQHandler(void);
 void DMA1_Channel2_3_IRQHandler(void);
 void display_test(void);

+ 171 - 14
src/board.c

@@ -105,10 +105,21 @@ void display_test(void) {
   display_Buffer[22] = 0x1e;
   display_Buffer[23] = 0x00;
 
-  HT1632C_Write_Cmd(0x802, 0x800);
-  HT1632C_Write_Cmd(0x806, 0x800);
-
   HT1632C_Write_Data(display_Buffer, 0x0, SPI_BUFFER_SIZE);
+  tdelay_ms(5000);
+  HT1632C_Write_Buffer();
+
+  tdelay_ms(5000);
+  int i;
+  for (i=0; i<16; i++) {
+    HT1632C_PWM(15-i);
+    tdelay_ms(1000);
+  }
+  tdelay_ms(2000);
+  for (i=0; i<16; i++) {
+    HT1632C_PWM(i);
+    tdelay_ms(1000);
+  }
 }
 
 /**
@@ -131,10 +142,10 @@ static void GPIO_Init(void)
   /* Select output mode (01) PP+PU, High Speed
    PA8  - Buzzer (AF2 for TIM1_CH1)
    PA15 - ~CS / SPI_NSS (AF0)
-   PB4  - ~RD / SPI_MISO (AF0) - NOT USED
-   * Select output mode (10) AF+OD, High Speed
+   * Select output mode (10) AF+PP, High Speed
    PB3  - ~WR / SPI_SCK (AF0)
    PB5  - Data / SPI_MOSI (AF0)
+   * Select output mode (10) AF+OD, High Speed
    PB6  - SCL (AF1)
    PB7  - SDA (AF1)
   */
@@ -142,7 +153,7 @@ static void GPIO_Init(void)
   GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER8 | GPIO_MODER_MODER15)) \
                 | (GPIO_MODER_MODER8_0 | GPIO_MODER_MODER15_0);
   GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODER3|GPIO_MODER_MODER5)) \
-                | (GPIO_MODER_MODER3_0|GPIO_MODER_MODER5_0);
+                | (GPIO_MODER_MODER3_1|GPIO_MODER_MODER5_1);
   // Pull-Up
   GPIOA->PUPDR = (GPIOA->PUPDR & ~(GPIO_PUPDR_PUPDR8 | GPIO_PUPDR_PUPDR15)) \
                 | (GPIO_PUPDR_PUPDR8_0 | GPIO_PUPDR_PUPDR15_0);
@@ -211,6 +222,95 @@ static void SPI1_Init(void)
   SPI1->CR2 = (SPI_CR2_DS_2 | SPI_CR2_DS_1 | SPI_CR2_DS_0 | SPI_CR2_TXDMAEN);
 }
 
+/**
+ * @brief Initialization HT1632C
+ */
+void HT1632C_Init(void) {
+  // GPIO pin mode
+  GPIOB->MODER &= ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER5);
+  GPIOB->MODER |= (GPIO_MODER_MODER3_0 | GPIO_MODER_MODER5_0);
+
+  //HT1632C_Write_Cmd(0x802, 0x800);
+  //HT1632C_Write_Cmd(0x806, 0x800);
+  HT1632C_CS_ON;
+  HT1632C_Write_bits(0x802, 0x800); // Turn on system oscillator
+  HT1632C_Write_bits(0x6, 0x100); // Turn on LED duty cycle generator
+  HT1632C_CS_OFF;
+
+  // SPI pin mode
+  GPIOB->MODER &= ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER5);
+  GPIOB->MODER |= (GPIO_MODER_MODER3_1 | GPIO_MODER_MODER5_1);
+}
+
+/**
+ * @brief Set HT1632C PWM Value
+ * @param pwm value in 0-15
+ */
+void HT1632C_PWM(uint8_t pwm) {
+  // check value
+  if (pwm > 15) {
+    pwm = 15;
+  }
+  // align value
+  pwm <<= 1;
+
+  // GPIO pin mode
+  GPIOB->MODER &= ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER5);
+  GPIOB->MODER |= (GPIO_MODER_MODER3_0 | GPIO_MODER_MODER5_0);
+
+  HT1632C_CS_ON;
+  HT1632C_Write_bits((0x940|pwm), 0x800);
+  HT1632C_CS_OFF;
+
+  // SPI pin mode
+  GPIOB->MODER &= ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER5);
+  GPIOB->MODER |= (GPIO_MODER_MODER3_1 | GPIO_MODER_MODER5_1);
+}
+
+/**
+ * @brief Set HT1632C Blink state
+ * @param state 0 for off, any other value for on
+ */
+void HT1632C_BlinkState(uint8_t state) {
+  // GPIO pin mode
+  GPIOB->MODER &= ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER5);
+  GPIOB->MODER |= (GPIO_MODER_MODER3_0 | GPIO_MODER_MODER5_0);
+
+  HT1632C_CS_ON;
+  if (state == 0) {
+    HT1632C_Write_bits(0x810, 0x800); // Blink off
+  } else {
+    HT1632C_Write_bits(0x812, 0x800); // Blink on
+  }
+  HT1632C_CS_OFF;
+
+  // SPI pin mode
+  GPIOB->MODER &= ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER5);
+  GPIOB->MODER |= (GPIO_MODER_MODER3_1 | GPIO_MODER_MODER5_1);
+}
+
+/**
+ * @brief Set HT1632C Led state
+ * @param state 0 for off, any other value for on
+ */
+void HT1632C_LedState(uint8_t state) {
+  // GPIO pin mode
+  GPIOB->MODER &= ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER5);
+  GPIOB->MODER |= (GPIO_MODER_MODER3_0 | GPIO_MODER_MODER5_0);
+
+  HT1632C_CS_ON;
+  if (state == 0) {
+    HT1632C_Write_bits(0x804, 0x800); // Led off
+  } else {
+    HT1632C_Write_bits(0x806, 0x800); // Led on
+  }
+  HT1632C_CS_OFF;
+
+  // SPI pin mode
+  GPIOB->MODER &= ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER5);
+  GPIOB->MODER |= (GPIO_MODER_MODER3_1 | GPIO_MODER_MODER5_1);
+}
+
 /**
  * @brief Write <nbits> bit of data to selected HT1632Cs
  * @param data words to write, nbits of bits (1<<(num-1))
@@ -236,17 +336,11 @@ void HT1632C_Write_bits(const uint16_t data, uint16_t nbits) {
  * @param nbits cmd len in bits (1<<(num-1))
  */
 void HT1632C_Write_Cmd(const uint16_t data, uint16_t nbits) {
-  // GPIO pin mode
-  GPIOB->MODER &= ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER5);
-  GPIOB->MODER |= (GPIO_MODER_MODER3_0 | GPIO_MODER_MODER5_0);
 
-  HT1632C_CS_ON;
-  HT1632C_Write_bits(data, nbits);
-  HT1632C_CS_OFF;
 }
 
 /**
- * @brief Write Data to selected HT1632Cs
+ * @brief Write Data to HT1632C
  * @param data pointer to data array
  * @param addr begin address
  * @param len bytes to write
@@ -273,7 +367,70 @@ void HT1632C_Write_Data(const uint8_t * data, const uint8_t addr, const uint8_t
   // start transfer
   SPI1->CR2 |= (SPI_CR2_TXDMAEN);
   DMA1_Channel3->CCR |= DMA_CCR_EN;
-  // End of transaction in DMA1_Channel2_3_IRQHandler
+  // End of transaction in DMA1_Channel2_3_IRQHandler()
+}
+
+/**
+ * @brief Write all buffer to HT1632C
+ */
+void HT1632C_Write_Buffer(void) {
+static uint8_t spi_buf[SPI_BUFFER_SIZE] = {0};
+
+  /* DMA Source addr: Address of the SPI buffer. */
+  DMA1_Channel3->CMAR = (uint32_t)&spi_buf;
+  /* Set DMA data transfer length (SPI buffer length). */
+  DMA1_Channel3->CNDTR = SPI_BUFFER_SIZE;
+
+  /* Rotate display buffer */
+  uint8_t i, j, p = 0x01;
+  for (i=0; i<8; i++) {
+    if ((display_Buffer[0] & p) != 0) { spi_buf[i] |= 0x01; }
+    if ((display_Buffer[1] & p) != 0) { spi_buf[i] |= 0x02; }
+    if ((display_Buffer[2] & p) != 0) { spi_buf[i] |= 0x04; }
+    if ((display_Buffer[3] & p) != 0) { spi_buf[i] |= 0x08; }
+    if ((display_Buffer[4] & p) != 0) { spi_buf[i] |= 0x10; }
+    if ((display_Buffer[5] & p) != 0) { spi_buf[i] |= 0x20; }
+    if ((display_Buffer[6] & p) != 0) { spi_buf[i] |= 0x40; }
+    if ((display_Buffer[7] & p) != 0) { spi_buf[i] |= 0x80; }
+
+    j = i + 8;
+    if ((display_Buffer[8] & p) != 0)  { spi_buf[j] |= 0x01; }
+    if ((display_Buffer[9] & p) != 0)  { spi_buf[j] |= 0x02; }
+    if ((display_Buffer[10] & p) != 0) { spi_buf[i] |= 0x04; }
+    if ((display_Buffer[11] & p) != 0) { spi_buf[i] |= 0x08; }
+    if ((display_Buffer[12] & p) != 0) { spi_buf[j] |= 0x10; }
+    if ((display_Buffer[13] & p) != 0) { spi_buf[j] |= 0x20; }
+    if ((display_Buffer[14] & p) != 0) { spi_buf[j] |= 0x40; }
+    if ((display_Buffer[15] & p) != 0) { spi_buf[j] |= 0x80; }
+
+    j = i + 16;
+    if ((display_Buffer[16] & p) != 0) { spi_buf[j] |= 0x01; }
+    if ((display_Buffer[17] & p) != 0) { spi_buf[j] |= 0x02; }
+    if ((display_Buffer[18] & p) != 0) { spi_buf[j] |= 0x04; }
+    if ((display_Buffer[19] & p) != 0) { spi_buf[j] |= 0x08; }
+    if ((display_Buffer[20] & p) != 0) { spi_buf[j] |= 0x10; }
+    if ((display_Buffer[21] & p) != 0) { spi_buf[j] |= 0x20; }
+    if ((display_Buffer[22] & p) != 0) { spi_buf[j] |= 0x40; }
+    if ((display_Buffer[23] & p) != 0) { spi_buf[j] |= 0x80; }
+
+    p <<= 1;
+  }
+
+  /* Toggle pins to GPIO mode */
+  GPIOB->MODER &= ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER5);
+  GPIOB->MODER |= (GPIO_MODER_MODER3_0 | GPIO_MODER_MODER5_0);
+
+  HT1632C_CS_ON;
+  HT1632C_Write_bits(0x280, 0x200);
+
+  /* Toggle pins to SPI mode */
+  GPIOB->MODER &= ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER5);
+  GPIOB->MODER |= (GPIO_MODER_MODER3_1 | GPIO_MODER_MODER5_1);
+
+  /* start transfer */
+  SPI1->CR2 |= (SPI_CR2_TXDMAEN);
+  DMA1_Channel3->CCR |= DMA_CCR_EN;
+  /* End of transaction in DMA1_Channel2_3_IRQHandler() */
 }
 
 /**