keniti akiba
/
i2c_HT16K33_test1_767_1
stm32f767 ht16k33
main.cpp@0:a9f44b553efd, 2022-08-04 (annotated)
- Committer:
- caa45040
- Date:
- Thu Aug 04 10:07:46 2022 +0000
- Revision:
- 0:a9f44b553efd
stm32f767 ht16k33
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
caa45040 | 0:a9f44b553efd | 1 | |
caa45040 | 0:a9f44b553efd | 2 | //i2c_HT16K33_test1_767_1 |
caa45040 | 0:a9f44b553efd | 3 | |
caa45040 | 0:a9f44b553efd | 4 | #include "mbed.h" |
caa45040 | 0:a9f44b553efd | 5 | |
caa45040 | 0:a9f44b553efd | 6 | #include "main.h" |
caa45040 | 0:a9f44b553efd | 7 | |
caa45040 | 0:a9f44b553efd | 8 | #define ADDR (0x70<<1) // address |
caa45040 | 0:a9f44b553efd | 9 | |
caa45040 | 0:a9f44b553efd | 10 | |
caa45040 | 0:a9f44b553efd | 11 | DigitalOut myled(PB_0); |
caa45040 | 0:a9f44b553efd | 12 | |
caa45040 | 0:a9f44b553efd | 13 | |
caa45040 | 0:a9f44b553efd | 14 | //I2C i2c(I2C_SDA, I2C_SCL); //767 |
caa45040 | 0:a9f44b553efd | 15 | //I2C i2c(PB_9, PB_8); //767 |
caa45040 | 0:a9f44b553efd | 16 | |
caa45040 | 0:a9f44b553efd | 17 | |
caa45040 | 0:a9f44b553efd | 18 | I2C_HandleTypeDef hi2c1; |
caa45040 | 0:a9f44b553efd | 19 | |
caa45040 | 0:a9f44b553efd | 20 | |
caa45040 | 0:a9f44b553efd | 21 | |
caa45040 | 0:a9f44b553efd | 22 | |
caa45040 | 0:a9f44b553efd | 23 | |
caa45040 | 0:a9f44b553efd | 24 | |
caa45040 | 0:a9f44b553efd | 25 | void Error_Handler(void) |
caa45040 | 0:a9f44b553efd | 26 | { |
caa45040 | 0:a9f44b553efd | 27 | __disable_irq(); |
caa45040 | 0:a9f44b553efd | 28 | while (1) |
caa45040 | 0:a9f44b553efd | 29 | { |
caa45040 | 0:a9f44b553efd | 30 | } |
caa45040 | 0:a9f44b553efd | 31 | } |
caa45040 | 0:a9f44b553efd | 32 | |
caa45040 | 0:a9f44b553efd | 33 | #ifdef USE_FULL_ASSERT |
caa45040 | 0:a9f44b553efd | 34 | void assert_failed(uint8_t *file, uint32_t line) |
caa45040 | 0:a9f44b553efd | 35 | { |
caa45040 | 0:a9f44b553efd | 36 | } |
caa45040 | 0:a9f44b553efd | 37 | #endif /* USE_FULL_ASSERT */ |
caa45040 | 0:a9f44b553efd | 38 | |
caa45040 | 0:a9f44b553efd | 39 | |
caa45040 | 0:a9f44b553efd | 40 | |
caa45040 | 0:a9f44b553efd | 41 | |
caa45040 | 0:a9f44b553efd | 42 | |
caa45040 | 0:a9f44b553efd | 43 | |
caa45040 | 0:a9f44b553efd | 44 | |
caa45040 | 0:a9f44b553efd | 45 | void SystemClock_Config(void) |
caa45040 | 0:a9f44b553efd | 46 | { |
caa45040 | 0:a9f44b553efd | 47 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
caa45040 | 0:a9f44b553efd | 48 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
caa45040 | 0:a9f44b553efd | 49 | RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0}; |
caa45040 | 0:a9f44b553efd | 50 | |
caa45040 | 0:a9f44b553efd | 51 | /** Configure the main internal regulator output voltage |
caa45040 | 0:a9f44b553efd | 52 | */ |
caa45040 | 0:a9f44b553efd | 53 | __HAL_RCC_PWR_CLK_ENABLE(); |
caa45040 | 0:a9f44b553efd | 54 | __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3); |
caa45040 | 0:a9f44b553efd | 55 | /** Initializes the RCC Oscillators according to the specified parameters |
caa45040 | 0:a9f44b553efd | 56 | * in the RCC_OscInitTypeDef structure. |
caa45040 | 0:a9f44b553efd | 57 | */ |
caa45040 | 0:a9f44b553efd | 58 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; |
caa45040 | 0:a9f44b553efd | 59 | RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
caa45040 | 0:a9f44b553efd | 60 | RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; |
caa45040 | 0:a9f44b553efd | 61 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; |
caa45040 | 0:a9f44b553efd | 62 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) |
caa45040 | 0:a9f44b553efd | 63 | { |
caa45040 | 0:a9f44b553efd | 64 | Error_Handler(); |
caa45040 | 0:a9f44b553efd | 65 | } |
caa45040 | 0:a9f44b553efd | 66 | /** Initializes the CPU, AHB and APB buses clocks |
caa45040 | 0:a9f44b553efd | 67 | */ |
caa45040 | 0:a9f44b553efd | 68 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |
caa45040 | 0:a9f44b553efd | 69 | |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; |
caa45040 | 0:a9f44b553efd | 70 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; |
caa45040 | 0:a9f44b553efd | 71 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
caa45040 | 0:a9f44b553efd | 72 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; |
caa45040 | 0:a9f44b553efd | 73 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
caa45040 | 0:a9f44b553efd | 74 | |
caa45040 | 0:a9f44b553efd | 75 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) |
caa45040 | 0:a9f44b553efd | 76 | { |
caa45040 | 0:a9f44b553efd | 77 | Error_Handler(); |
caa45040 | 0:a9f44b553efd | 78 | } |
caa45040 | 0:a9f44b553efd | 79 | PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2C1; |
caa45040 | 0:a9f44b553efd | 80 | PeriphClkInitStruct.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1; |
caa45040 | 0:a9f44b553efd | 81 | if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) |
caa45040 | 0:a9f44b553efd | 82 | { |
caa45040 | 0:a9f44b553efd | 83 | Error_Handler(); |
caa45040 | 0:a9f44b553efd | 84 | } |
caa45040 | 0:a9f44b553efd | 85 | } |
caa45040 | 0:a9f44b553efd | 86 | |
caa45040 | 0:a9f44b553efd | 87 | |
caa45040 | 0:a9f44b553efd | 88 | |
caa45040 | 0:a9f44b553efd | 89 | |
caa45040 | 0:a9f44b553efd | 90 | static void MX_I2C1_Init(void) |
caa45040 | 0:a9f44b553efd | 91 | { |
caa45040 | 0:a9f44b553efd | 92 | |
caa45040 | 0:a9f44b553efd | 93 | hi2c1.Instance = I2C1; |
caa45040 | 0:a9f44b553efd | 94 | hi2c1.Init.Timing = 0x00303D5B; |
caa45040 | 0:a9f44b553efd | 95 | hi2c1.Init.OwnAddress1 = 0; |
caa45040 | 0:a9f44b553efd | 96 | hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; |
caa45040 | 0:a9f44b553efd | 97 | hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; |
caa45040 | 0:a9f44b553efd | 98 | hi2c1.Init.OwnAddress2 = 0; |
caa45040 | 0:a9f44b553efd | 99 | hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK; |
caa45040 | 0:a9f44b553efd | 100 | hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; |
caa45040 | 0:a9f44b553efd | 101 | hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; |
caa45040 | 0:a9f44b553efd | 102 | if (HAL_I2C_Init(&hi2c1) != HAL_OK) |
caa45040 | 0:a9f44b553efd | 103 | { |
caa45040 | 0:a9f44b553efd | 104 | Error_Handler(); |
caa45040 | 0:a9f44b553efd | 105 | } |
caa45040 | 0:a9f44b553efd | 106 | |
caa45040 | 0:a9f44b553efd | 107 | if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK) |
caa45040 | 0:a9f44b553efd | 108 | { |
caa45040 | 0:a9f44b553efd | 109 | Error_Handler(); |
caa45040 | 0:a9f44b553efd | 110 | } |
caa45040 | 0:a9f44b553efd | 111 | |
caa45040 | 0:a9f44b553efd | 112 | if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK) |
caa45040 | 0:a9f44b553efd | 113 | { |
caa45040 | 0:a9f44b553efd | 114 | Error_Handler(); |
caa45040 | 0:a9f44b553efd | 115 | } |
caa45040 | 0:a9f44b553efd | 116 | |
caa45040 | 0:a9f44b553efd | 117 | } |
caa45040 | 0:a9f44b553efd | 118 | |
caa45040 | 0:a9f44b553efd | 119 | |
caa45040 | 0:a9f44b553efd | 120 | |
caa45040 | 0:a9f44b553efd | 121 | static void MX_GPIO_Init(void) |
caa45040 | 0:a9f44b553efd | 122 | { |
caa45040 | 0:a9f44b553efd | 123 | __HAL_RCC_GPIOB_CLK_ENABLE(); |
caa45040 | 0:a9f44b553efd | 124 | } |
caa45040 | 0:a9f44b553efd | 125 | |
caa45040 | 0:a9f44b553efd | 126 | |
caa45040 | 0:a9f44b553efd | 127 | |
caa45040 | 0:a9f44b553efd | 128 | |
caa45040 | 0:a9f44b553efd | 129 | |
caa45040 | 0:a9f44b553efd | 130 | |
caa45040 | 0:a9f44b553efd | 131 | |
caa45040 | 0:a9f44b553efd | 132 | void HAL_MspInit(void) |
caa45040 | 0:a9f44b553efd | 133 | { |
caa45040 | 0:a9f44b553efd | 134 | |
caa45040 | 0:a9f44b553efd | 135 | __HAL_RCC_PWR_CLK_ENABLE(); |
caa45040 | 0:a9f44b553efd | 136 | __HAL_RCC_SYSCFG_CLK_ENABLE(); |
caa45040 | 0:a9f44b553efd | 137 | |
caa45040 | 0:a9f44b553efd | 138 | |
caa45040 | 0:a9f44b553efd | 139 | } |
caa45040 | 0:a9f44b553efd | 140 | |
caa45040 | 0:a9f44b553efd | 141 | |
caa45040 | 0:a9f44b553efd | 142 | void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c) |
caa45040 | 0:a9f44b553efd | 143 | { |
caa45040 | 0:a9f44b553efd | 144 | GPIO_InitTypeDef GPIO_InitStruct = {0}; |
caa45040 | 0:a9f44b553efd | 145 | if(hi2c->Instance==I2C1) |
caa45040 | 0:a9f44b553efd | 146 | { |
caa45040 | 0:a9f44b553efd | 147 | |
caa45040 | 0:a9f44b553efd | 148 | |
caa45040 | 0:a9f44b553efd | 149 | __HAL_RCC_GPIOB_CLK_ENABLE(); |
caa45040 | 0:a9f44b553efd | 150 | /**I2C1 GPIO Configuration |
caa45040 | 0:a9f44b553efd | 151 | PB8 ------> I2C1_SCL |
caa45040 | 0:a9f44b553efd | 152 | PB9 ------> I2C1_SDA |
caa45040 | 0:a9f44b553efd | 153 | */ |
caa45040 | 0:a9f44b553efd | 154 | GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9; |
caa45040 | 0:a9f44b553efd | 155 | GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; |
caa45040 | 0:a9f44b553efd | 156 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
caa45040 | 0:a9f44b553efd | 157 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
caa45040 | 0:a9f44b553efd | 158 | GPIO_InitStruct.Alternate = GPIO_AF4_I2C1; |
caa45040 | 0:a9f44b553efd | 159 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
caa45040 | 0:a9f44b553efd | 160 | |
caa45040 | 0:a9f44b553efd | 161 | __HAL_RCC_I2C1_CLK_ENABLE(); |
caa45040 | 0:a9f44b553efd | 162 | |
caa45040 | 0:a9f44b553efd | 163 | } |
caa45040 | 0:a9f44b553efd | 164 | |
caa45040 | 0:a9f44b553efd | 165 | } |
caa45040 | 0:a9f44b553efd | 166 | |
caa45040 | 0:a9f44b553efd | 167 | |
caa45040 | 0:a9f44b553efd | 168 | |
caa45040 | 0:a9f44b553efd | 169 | |
caa45040 | 0:a9f44b553efd | 170 | |
caa45040 | 0:a9f44b553efd | 171 | |
caa45040 | 0:a9f44b553efd | 172 | |
caa45040 | 0:a9f44b553efd | 173 | |
caa45040 | 0:a9f44b553efd | 174 | int main() { |
caa45040 | 0:a9f44b553efd | 175 | |
caa45040 | 0:a9f44b553efd | 176 | |
caa45040 | 0:a9f44b553efd | 177 | HAL_Init(); |
caa45040 | 0:a9f44b553efd | 178 | |
caa45040 | 0:a9f44b553efd | 179 | SystemClock_Config(); |
caa45040 | 0:a9f44b553efd | 180 | MX_GPIO_Init(); |
caa45040 | 0:a9f44b553efd | 181 | MX_I2C1_Init(); |
caa45040 | 0:a9f44b553efd | 182 | |
caa45040 | 0:a9f44b553efd | 183 | |
caa45040 | 0:a9f44b553efd | 184 | HAL_MspInit(); |
caa45040 | 0:a9f44b553efd | 185 | |
caa45040 | 0:a9f44b553efd | 186 | HAL_I2C_MspInit(&hi2c1); |
caa45040 | 0:a9f44b553efd | 187 | |
caa45040 | 0:a9f44b553efd | 188 | |
caa45040 | 0:a9f44b553efd | 189 | |
caa45040 | 0:a9f44b553efd | 190 | |
caa45040 | 0:a9f44b553efd | 191 | uint8_t buf[16]; |
caa45040 | 0:a9f44b553efd | 192 | |
caa45040 | 0:a9f44b553efd | 193 | buf[0]=0x21; |
caa45040 | 0:a9f44b553efd | 194 | HAL_I2C_Master_Transmit(&hi2c1, ADDR, buf, 1, 3000);wait_ms(1); |
caa45040 | 0:a9f44b553efd | 195 | |
caa45040 | 0:a9f44b553efd | 196 | buf[0]=0x81; |
caa45040 | 0:a9f44b553efd | 197 | HAL_I2C_Master_Transmit(&hi2c1, ADDR, buf, 1, 3000);wait_ms(1); |
caa45040 | 0:a9f44b553efd | 198 | |
caa45040 | 0:a9f44b553efd | 199 | buf[0]=0xef; |
caa45040 | 0:a9f44b553efd | 200 | HAL_I2C_Master_Transmit(&hi2c1, ADDR, buf, 1, 3000);wait_ms(1); |
caa45040 | 0:a9f44b553efd | 201 | |
caa45040 | 0:a9f44b553efd | 202 | for(int ii=0;ii<16;ii++){ |
caa45040 | 0:a9f44b553efd | 203 | buf[0]=ii; |
caa45040 | 0:a9f44b553efd | 204 | buf[1]=0x00; |
caa45040 | 0:a9f44b553efd | 205 | HAL_I2C_Master_Transmit(&hi2c1, ADDR, buf, 2, 3000);wait_ms(1); |
caa45040 | 0:a9f44b553efd | 206 | }//for |
caa45040 | 0:a9f44b553efd | 207 | |
caa45040 | 0:a9f44b553efd | 208 | while(1) { |
caa45040 | 0:a9f44b553efd | 209 | |
caa45040 | 0:a9f44b553efd | 210 | for(int ii=0;ii<8;ii++){ |
caa45040 | 0:a9f44b553efd | 211 | |
caa45040 | 0:a9f44b553efd | 212 | buf[0]=0; |
caa45040 | 0:a9f44b553efd | 213 | buf[1]= (1<<ii); |
caa45040 | 0:a9f44b553efd | 214 | HAL_I2C_Master_Transmit(&hi2c1, ADDR, buf, 2, 3000);wait_ms(600); |
caa45040 | 0:a9f44b553efd | 215 | |
caa45040 | 0:a9f44b553efd | 216 | //myled = 1; |
caa45040 | 0:a9f44b553efd | 217 | HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET); |
caa45040 | 0:a9f44b553efd | 218 | wait(0.2); |
caa45040 | 0:a9f44b553efd | 219 | //myled = 0; |
caa45040 | 0:a9f44b553efd | 220 | HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET); |
caa45040 | 0:a9f44b553efd | 221 | wait(0.2); |
caa45040 | 0:a9f44b553efd | 222 | |
caa45040 | 0:a9f44b553efd | 223 | |
caa45040 | 0:a9f44b553efd | 224 | }//for |
caa45040 | 0:a9f44b553efd | 225 | }//while |
caa45040 | 0:a9f44b553efd | 226 | |
caa45040 | 0:a9f44b553efd | 227 | |
caa45040 | 0:a9f44b553efd | 228 | |
caa45040 | 0:a9f44b553efd | 229 | }//main |
caa45040 | 0:a9f44b553efd | 230 | |
caa45040 | 0:a9f44b553efd | 231 |