#define HC165_CE_0 PBout(0) #define HC165_CE_1 PBout(8) #define HC165_CE_2 PEout(4) #define HC165_CE_3 PEout(8) #define HC165_CE_4 PEout(12) 如何简化这些定义
时间: 2023-09-11 18:05:56 浏览: 94
可以使用宏定义和参数来简化这些定义,如下所示:
```
#define HC165_CE(n, pin) PEout((n)*4 + (pin)) // n为芯片编号,pin为引脚编号
```
使用方式为:
```
HC165_CE(0, 0) = 1; // 使第0个芯片的第0个引脚为高电平
HC165_CE(2, 1) = 0; // 使第2个芯片的第1个引脚为低电平
```
相关问题
#define HC165_CE_0 PBout(0) #define HC165_PL_0 PAout(11) #define HC165_CP_0 PAout(12) #define HC165_Q7_0 PBin(1) #define HC165_CE_1 PBout(8) #define HC165_PL_1 PBout(6) #define HC165_CP_1 PBout(7) #define HC165_Q7_1 PBin(9) 如何做成宏定义数组
可以将HC165_CE、HC165_PL、HC165_CP、HC165_Q7定义为宏定义数组,如下所示:
```
#define HC165_CE {PBout(0), PBout(8)}
#define HC165_PL {PAout(11), PBout(6)}
#define HC165_CP {PAout(12), PBout(7)}
#define HC165_Q7 {PBin(1), PBin(9)}
```
然后在readVoltage函数中通过传入参数来确定使用哪一组HC165引脚,如下所示:
```
uint8_t readVoltage(uint8_t group) {
uint8_t i = 0;
uint8_t temp = 0;
HC165_PL[group] = 0;
HC165_PL[group] = 1;
if(HC165_Q7[group] == 1)
temp |= 0x01;
for(i = 0; i < 7; i ++)
{
temp = temp << 1;
HC165_CP[group] = 0;
delayUs(1);
HC165_CP[group] = 1;
if(HC165_Q7[group] == 1)
temp |= 0x01;
}
return temp;
}
```
这样可以大大简化代码,提高代码的可读性和可维护性。同时,如果需要添加更多的HC165引脚组,只需要在宏定义数组中添加对应的引脚即可。
#define HC165_CE {PBout(0),PBout(8),PEout(4),PEout(8),PEout(12)} #define HC165_PL {PAout(11),PBout(6),PEout(2),PEout(6),PEout(10)} #define HC165_CP {PAout(12),PBout(7),PEout(3),PEout(11),PEout(11)} #define HC165_Q7 {PBin(1),PBin(9),PEin(5),PEin(9),PEin(13)} uint8_t readVoltage(int group) { uint8_t i = 0; uint8_t temp = 0; HC165_CE[group] = 0; HC165_PL[group] = 0; HC165_PL[group] = 1; if(HC165_Q7[group] == 1) temp |= 0x01; for(i = 0; i < 7; i ++) { temp = temp << 1; HC165_CP[group] = 0; delayUs(1); HC165_CP[group] = 1; if(HC165_Q7[group] == 1) temp |= 0x01; } return temp; } 请帮我改成纯C语言代码
以下是改成纯 C 语言代码后的代码:
```c
#define HC165_CE_0 GPIOB->ODR &= ~(1 << 0)
#define HC165_CE_1 GPIOB->ODR &= ~(1 << 8)
#define HC165_CE_2 GPIOE->ODR &= ~(1 << 4)
#define HC165_CE_3 GPIOE->ODR &= ~(1 << 8)
#define HC165_CE_4 GPIOE->ODR &= ~(1 << 12)
#define HC165_PL_0 GPIOA->ODR &= ~(1 << 11)
#define HC165_PL_1 GPIOB->ODR &= ~(1 << 6)
#define HC165_PL_2 GPIOE->ODR &= ~(1 << 2)
#define HC165_PL_3 GPIOE->ODR &= ~(1 << 6)
#define HC165_PL_4 GPIOE->ODR &= ~(1 << 10)
#define HC165_CP_0 GPIOA->ODR &= ~(1 << 12)
#define HC165_CP_1 GPIOB->ODR &= ~(1 << 7)
#define HC165_CP_2 GPIOE->ODR &= ~(1 << 3)
#define HC165_CP_3 GPIOE->ODR &= ~(1 << 11)
#define HC165_CP_4 GPIOE->ODR &= ~(1 << 15)
#define HC165_Q7_0 GPIOB->IDR & (1 << 1)
#define HC165_Q7_1 GPIOB->IDR & (1 << 9)
#define HC165_Q7_2 GPIOE->IDR & (1 << 5)
#define HC165_Q7_3 GPIOE->IDR & (1 << 9)
#define HC165_Q7_4 GPIOE->IDR & (1 << 13)
uint8_t readVoltage(int group)
{
uint8_t i = 0;
uint8_t temp = 0;
switch (group) {
case 0:
HC165_CE_0 = 0;
HC165_PL_0 = 0;
HC165_PL_0 = 1;
if (HC165_Q7_0 == 1)
temp |= 0x01;
for (i = 0; i < 7; i++) {
temp = temp << 1;
HC165_CP_0 = 0;
delayUs(1);
HC165_CP_0 = 1;
if (HC165_Q7_0 == 1)
temp |= 0x01;
}
break;
case 1:
HC165_CE_1 = 0;
HC165_PL_1 = 0;
HC165_PL_1 = 1;
if (HC165_Q7_1 == 1)
temp |= 0x01;
for (i = 0; i < 7; i++) {
temp = temp << 1;
HC165_CP_1 = 0;
delayUs(1);
HC165_CP_1 = 1;
if (HC165_Q7_1 == 1)
temp |= 0x01;
}
break;
case 2:
HC165_CE_2 = 0;
HC165_PL_2 = 0;
HC165_PL_2 = 1;
if (HC165_Q7_2 == 1)
temp |= 0x01;
for (i = 0; i < 7; i++) {
temp = temp << 1;
HC165_CP_2 = 0;
delayUs(1);
HC165_CP_2 = 1;
if (HC165_Q7_2 == 1)
temp |= 0x01;
}
break;
case 3:
HC165_CE_3 = 0;
HC165_PL_3 = 0;
HC165_PL_3 = 1;
if (HC165_Q7_3 == 1)
temp |= 0x01;
for (i = 0; i < 7; i++) {
temp = temp << 1;
HC165_CP_3 = 0;
delayUs(1);
HC165_CP_3 = 1;
if (HC165_Q7_3 == 1)
temp |= 0x01;
}
break;
case 4:
HC165_CE_4 = 0;
HC165_PL_4 = 0;
HC165_PL_4 = 1;
if (HC165_Q7_4 == 1)
temp |= 0x01;
for (i = 0; i < 7; i++) {
temp = temp << 1;
HC165_CP_4 = 0;
delayUs(1);
HC165_CP_4 = 1;
if (HC165_Q7_4 == 1)
temp |= 0x01;
}
break;
}
return temp;
}
```
注意,为了让代码更清晰易懂,我将宏定义改成了对应的 C 语言语句。同时,由于寄存器操作需要使用 STM32 的相关库函数,因此我将它们改成了对应的寄存器操作语句。
阅读全文