STM32基于MX的CH455硬件IIC驱动 ch455官方提供的示例代码是51单片机的io模拟iic地址如下CH4XXEVT.ZIP - 南京沁恒微电子股份有限公司但是处于效率原因还是希望使用单片机本身的硬件IIC来与ch455通信。首先我们先了解ch455时序STM32CubeMX部分配置下面是ch455的驱动这里我使用的是三位数码管所以代码主要数据是三位数码管的代码。Core\Inc\ch455.h/** ****************************************************************************** * file : ch455.h * brief : Header for ch455.c file. * CH455 Seven-Segment Display Driver ****************************************************************************** */ #ifndef __CH455_H #define __CH455_H #ifdef __cplusplus extern C { #endif /* Includes */ #include main.h // 设置系统参数命令 #define CH455_BIT_ENABLE 0x01 // 开启/关闭位 #define CH455_BIT_SLEEP 0x04 // 睡眠控制位 #define CH455_BIT_7SEG 0x08 // 7段控制位 #define CH455_BIT_INTENS1 0x10 // 1级亮度 #define CH455_BIT_INTENS2 0x20 // 2级亮度 #define CH455_BIT_INTENS3 0x30 // 3级亮度 #define CH455_BIT_INTENS4 0x40 // 4级亮度 #define CH455_BIT_INTENS5 0x50 // 5级亮度 #define CH455_BIT_INTENS6 0x60 // 6级亮度 #define CH455_BIT_INTENS7 0x70 // 7级亮度 #define CH455_BIT_INTENS8 0x00 // 8级亮度 #define CH455_BIT_KOFF 0x80 // 关闭键盘 #define CH455_SYSCMD 0x4800 // 操作命令,需另加8位数据 // 加载字数据命令 #define CH455_DIG0 0x6800 // 数码管位0显示,需另加8位数据 #define CH455_DIG1 0x6A00 // 数码管位1显示,需另加8位数据 #define CH455_DIG2 0x6C00 // 数码管位2显示,需另加8位数据 #define CH455_DIG3 0x6E00 // 数码管位3显示,需另加8位数据 #define BCD_decode_DP 0x0080 #define BCD_decode_NG 0x0040 /* Seven-Segment Display Character Macros */ #define CH455_CHAR_0 0x00 #define CH455_CHAR_1 0x01 #define CH455_CHAR_2 0x02 #define CH455_CHAR_3 0x03 #define CH455_CHAR_4 0x04 #define CH455_CHAR_5 0x05 #define CH455_CHAR_6 0x06 #define CH455_CHAR_7 0x07 #define CH455_CHAR_8 0x08 #define CH455_CHAR_9 0x09 #define CH455_CHAR_A 0x0A #define CH455_CHAR_B 0x0B #define CH455_CHAR_C 0x0C #define CH455_CHAR_D 0x0D #define CH455_CHAR_E 0x0E #define CH455_CHAR_F 0x0F #define CH455_CHAR_G 0x10 #define CH455_CHAR_H 0x11 #define CH455_CHAR_I 0x12 #define CH455_CHAR_J 0x13 #define CH455_CHAR_K 0x14 #define CH455_CHAR_L 0x15 #define CH455_CHAR_M 0x16 #define CH455_CHAR_N 0x17 #define CH455_CHAR_O 0x18 #define CH455_CHAR_P 0x19 #define CH455_CHAR_Q 0x1A #define CH455_CHAR_R 0x1B #define CH455_CHAR_S 0x1C #define CH455_CHAR_T 0x1D #define CH455_CHAR_U 0x1E #define CH455_CHAR_V 0x1F #define CH455_CHAR_W 0x20 #define CH455_CHAR_X 0x21 #define CH455_CHAR_Y 0x22 #define CH455_CHAR_Z 0x23 #define CH455_CHAR_SPACE 0x24 #define CH455_CHAR_DASH 0x25 /* Exported functions prototypes */ void CH455G_Init(uint8_t intens); void CH455G_SetDigit(uint8_t position, uint8_t value); void CH455G_SetDigits(uint8_t d0, uint8_t d1, uint8_t d2); void CH455G_SetNumber(uint16_t number); void CH455G_SetAllSegments(void); void CH455G_SetRaw(uint8_t position, uint8_t data); #ifdef __cplusplus } #endif #endif /* __CH455_H */Core\Src\ch455.c/** ****************************************************************************** * file : ch455.c * brief : CH455 Seven-Segment Display Driver * Common Cathode 3-Digit Display * DIG0Left, DIG1Middle, DIG2Right * SEG0A, SEG1B, SEG2C, SEG3D, SEG4E, SEG5F, SEG6DP ****************************************************************************** */ #include ch455.h #include main.h #include i2c.h /* Common Cathode 7-Segment Display Code Table */ /* Segments: ASEG0, BSEG1, CSEG2, DSEG3, ESEG4, FSEG5, GSEG6, DPSEG7 */ static const uint8_t ch455_seg_code[] { 0x3F, /* 0: ABCDEF */ 0x06, /* 1: BC */ 0x5B, /* 2: ABGED */ 0x4F, /* 3: ABGCD */ 0x66, /* 4: FGBC */ 0x6D, /* 5: AFGCD */ 0x7D, /* 6: AFGCDE */ 0x07, /* 7: ABC */ 0x7F, /* 8: All segments (A-G) */ 0x6F, /* 9: ABCDFG */ 0x77, /* A: ABCEFG */ 0x7C, /* b: FGCDE */ 0x39, /* C: ADEF */ 0x5E, /* d: BCDEG */ 0x79, /* E: ADEFG */ 0x71, /* F: AEFG */ 0x3D, /* G: ABCDEF */ 0x76, /* H: BCEFG */ 0x06, /* I: BC (same as 1) */ 0x1F, /* J: ABCDG */ 0x76, /* K: Same as H */ 0x38, /* L: ADEF */ 0x55, /* M: ABCEG (double-height) */ 0x74, /* N: ABCEFG */ 0x3F, /* O: ABCDEF (same as 0) */ 0x73, /* P: ABCEFG */ 0x67, /* Q: ABCDFG */ 0x50, /* R: AEFG */ 0x6D, /* S: Same as 5 */ 0x78, /* T: EFG */ 0x3E, /* U: ABCDE */ 0x1C, /* V: ABCDE (slant) */ 0x3E, /* W: Same as U */ 0x6E, /* X: Same as H */ 0x6F, /* Y: ABCDFG (same as 9) */ 0x5B, /* Z: Same as 2 */ 0x00, /* Space */ 0x40, /* - (Dash) */ }; /** * brief Write command to CH455 * param cmd: Command byte * retval HAL status */ static HAL_StatusTypeDef CH455G_Write(uint16_t cmd) { uint8_t data1 0; uint8_t data2 0; data1 (uint8_t)(cmd 8); data2 (uint8_t)(cmd 0x00ff); return HAL_I2C_Master_Transmit(hi2c1, data1, data2, 1, 1000); // __NOP(); } /** * brief Initialize CH455 * param intens: CH455_BIT_INTENS1-8 * retval None */ void CH455G_Init(uint8_t intens) { if ((intens ! CH455_BIT_INTENS1) (intens ! CH455_BIT_INTENS2) (intens ! CH455_BIT_INTENS3) (intens ! CH455_BIT_INTENS4) (intens ! CH455_BIT_INTENS5) (intens ! CH455_BIT_INTENS6) (intens ! CH455_BIT_INTENS7) (intens ! CH455_BIT_INTENS8)) { CH455G_Write(CH455_SYSCMD | CH455_BIT_ENABLE | CH455_BIT_INTENS4); } else { CH455G_Write(CH455_SYSCMD | CH455_BIT_ENABLE | intens); } /* Clear all digits */ CH455G_SetDigits(CH455_CHAR_SPACE, CH455_CHAR_SPACE, CH455_CHAR_SPACE); } /** * brief Set a single digit * param position: 0Left, 1Middle, 2Right * param value: 0-150-F, 16Space, 17Dash * retval None */ void CH455G_SetDigit(uint8_t position, uint8_t value) { if (position 2) return; if (value sizeof(ch455_seg_code) - 1) value CH455_CHAR_SPACE; /* Default to space */ switch (position) { case 0: /* Leftmost digit */ CH455G_Write(CH455_DIG0 | ch455_seg_code[value]); break; case 1: /* Middle digit */ CH455G_Write(CH455_DIG1 | ch455_seg_code[value]); break; case 2: /* Rightmost digit */ CH455G_Write(CH455_DIG2 | ch455_seg_code[value]); break; default: break; } } /** * brief Set all three digits at once * param d0: Left digit (0-17) * param d1: Middle digit (0-17) * param d2: Right digit (0-17) * retval None */ void CH455G_SetDigits(uint8_t d0, uint8_t d1, uint8_t d2) { CH455G_SetDigit(0, d0); CH455G_SetDigit(1, d1); CH455G_SetDigit(2, d2); } /** * brief Display a number (0-999) * param number: Number to display (0-999) * retval None */ void CH455G_SetNumber(uint16_t number) { if (number 999) number 999; uint8_t d0 (number / 100) % 10; /* Hundreds */ uint8_t d1 (number / 10) % 10; /* Tens */ uint8_t d2 number % 10; /* Ones */ CH455G_SetDigits(d0, d1, d2); } /** * brief Turn on all segments of all digits (test mode) * param None * retval None */ void CH455G_SetAllSegments(void) { /* 0x7F All segments decimal point */ CH455G_Write(CH455_DIG0 | 0xFF); CH455G_Write(CH455_DIG1 | 0xFF); CH455G_Write(CH455_DIG2 | 0xFF); } /** * brief Set raw segment data directly * param position: 0Left, 1Middle, 2Right * param data: 8-bit raw segment data (bit7DP, bit6G, bit5F, bit4E, bit3D, bit2C, bit1B, bit0A) * retval None */ void CH455G_SetRaw(uint8_t position, uint8_t data) { if (position 2) return; switch (position) { case 0: CH455G_Write(CH455_DIG0 | data); break; case 1: CH455G_Write(CH455_DIG1 | data); break; case 2: CH455G_Write(CH455_DIG2 | data); break; default: break; } }使用方法距离如下CH455G_SetRaw(0, 0x20); CH455G_SetNumber(123); CH455G_SetDigits(CH455_CHAR_SPACE, CH455_CHAR_SPACE, CH455_CHAR_SPACE);这个ch455并不是标准的iic器件所以没有iic地址使用的是命令码数据的方式通信。