在 pic 微控制器中使用端口作为变量
using a port as a variable in pic microcontroller
我使用 PIC18f45k22,我正在尝试制作一个热电偶库。该库要求将 LATCH 分配为参数变量。这是创建的代码。
#define B4_LATCH LATB4
unsigned short Thermo1;
unsigned short thermal (unsigned char* Latch) {
unsigned short thermocouple;
Latch = 0;
thermocouple = spiRead(0)<<8; //Read first byte
thermocouple |= spiRead(0); //add second byte with first one
thermocouple = (thermocouple>>4)*0.5;
Latch = 1;
return thermocouple;
}
void main (){
while (1){
thermo1 = thermal (B4_LATCH);
printf ("%u" , thermo1);
}
但是,我使用了相同的代码,但没有在函数中使用它,而且它起作用了。
你可以使用间接实现。如果您查看 PIC18 头文件或 MPLAB 中的任何 PIC 头文件,您会看到每个寄存器都分配有一个内存地址,并且它们还使用 volatile 限定符定义,如下所示:
volatile unsigned char LATB __at(0xF8A)
因此,端口可以使用指针访问,而位字段不能使用指针访问,因为 C lang 的性质。因此,对端口位进行一般修改访问的一种简便方法是使用宏。所以让我们先定义位设置和位清除宏:
#define mBitClear(var, n) var &= ~(1 << n)
#define mBitSet(var, n) var |= 1 << n
现在我们有了通用的 C 风格位操作宏,我们可以将任何端口和位号作为变量传递,只要端口确实存在并且位号不超过 7,因为端口是 8 位的宽的。但请注意,某些端口的位可能最少。现在让我们将这些宏应用到您的函数中:
#define B4_LATCH 4 // Define the bit number
unsigned short Thermo1;
unsigned short thermal (volatile unsigned char* Port, const unsigned char Latch) {
unsigned short thermocouple;
mBitClear(*Port, Latch); // Clear the bit
thermocouple = spiRead(0)<<8; //Read first byte
thermocouple |= spiRead(0); //add second byte with first one
thermocouple = (thermocouple>>4)*0.5;
mBitSet(*Port, Latch); // Set the bit
return thermocouple;
}
void main (){
while (1){
thermo1 = thermal (&LATB, B4_LATCH); //< Note the indirection symbol (&) it is important
printf ("%u" , thermo1);
}
}
您可以使用此方法传递任何有效端口甚至具有有效位数的变量。有什么看不懂的就问我吧。
我使用 PIC18f45k22,我正在尝试制作一个热电偶库。该库要求将 LATCH 分配为参数变量。这是创建的代码。
#define B4_LATCH LATB4
unsigned short Thermo1;
unsigned short thermal (unsigned char* Latch) {
unsigned short thermocouple;
Latch = 0;
thermocouple = spiRead(0)<<8; //Read first byte
thermocouple |= spiRead(0); //add second byte with first one
thermocouple = (thermocouple>>4)*0.5;
Latch = 1;
return thermocouple;
}
void main (){
while (1){
thermo1 = thermal (B4_LATCH);
printf ("%u" , thermo1);
}
但是,我使用了相同的代码,但没有在函数中使用它,而且它起作用了。
你可以使用间接实现。如果您查看 PIC18 头文件或 MPLAB 中的任何 PIC 头文件,您会看到每个寄存器都分配有一个内存地址,并且它们还使用 volatile 限定符定义,如下所示:
volatile unsigned char LATB __at(0xF8A)
因此,端口可以使用指针访问,而位字段不能使用指针访问,因为 C lang 的性质。因此,对端口位进行一般修改访问的一种简便方法是使用宏。所以让我们先定义位设置和位清除宏:
#define mBitClear(var, n) var &= ~(1 << n)
#define mBitSet(var, n) var |= 1 << n
现在我们有了通用的 C 风格位操作宏,我们可以将任何端口和位号作为变量传递,只要端口确实存在并且位号不超过 7,因为端口是 8 位的宽的。但请注意,某些端口的位可能最少。现在让我们将这些宏应用到您的函数中:
#define B4_LATCH 4 // Define the bit number
unsigned short Thermo1;
unsigned short thermal (volatile unsigned char* Port, const unsigned char Latch) {
unsigned short thermocouple;
mBitClear(*Port, Latch); // Clear the bit
thermocouple = spiRead(0)<<8; //Read first byte
thermocouple |= spiRead(0); //add second byte with first one
thermocouple = (thermocouple>>4)*0.5;
mBitSet(*Port, Latch); // Set the bit
return thermocouple;
}
void main (){
while (1){
thermo1 = thermal (&LATB, B4_LATCH); //< Note the indirection symbol (&) it is important
printf ("%u" , thermo1);
}
}
您可以使用此方法传递任何有效端口甚至具有有效位数的变量。有什么看不懂的就问我吧。