kf8a100ftn芯片的iic中断模式读取stcn75芯片代码
时间: 2024-02-10 11:03:55 浏览: 186
以下是基于 kf8a100ftn 芯片的 IIC 中断模式读取 STCN75 芯片的代码示例:
```c
#include <stdio.h>
#include "kf8a100ftn.h"
#define STCN75_ADDRESS 0x90
volatile int iic_flag = 0;
volatile int iic_data = 0;
void IIC_ISR() {
int status;
status = KF8A100FTN->IIC.IICSR;
if (status & 0x08) { // IIC complete
iic_data = KF8A100FTN->IIC.IICDR;
iic_flag = 1;
}
}
void STCN75_Read(int address, int *data) {
int iic_data_low, iic_data_high;
iic_flag = 0;
// Send start bit
KF8A100FTN->IIC.IICCR = 0x90;
KF8A100FTN->IIC.IICDR = STCN75_ADDRESS | 0x01;
// Wait for IIC to complete
while (!iic_flag);
iic_data_low = iic_data;
iic_flag = 0;
// Send register address
KF8A100FTN->IIC.IICDR = address;
// Wait for IIC to complete
while (!iic_flag);
iic_flag = 0;
// Send repeated start bit
KF8A100FTN->IIC.IICCR = 0x98;
KF8A100FTN->IIC.IICDR = STCN75_ADDRESS;
// Wait for IIC to complete
while (!iic_flag);
iic_data_high = iic_data;
iic_flag = 0;
// Receive data
KF8A100FTN->IIC.IICCR = 0x88;
// Wait for IIC to complete
while (!iic_flag);
*data = (iic_data_high << 8) | iic_data_low;
// Send stop bit
KF8A100FTN->IIC.IICCR = 0x80;
}
int main() {
int temperature;
KF8A100FTN->IIC.IICSR = 0x00; // Disable IIC
KF8A100FTN->IIC.IICCR = 0x80; // Enable IIC, master mode, no interrupt
KF8A100FTN->IIC.IICMR = 0x0A; // IIC clock divider = 10
KF8A100FTN->IIC.IICIER = 0x01; // Enable IIC interrupt
KF8A100FTN->SYS.PINTEN = 0x08; // Enable external interrupt 3
KF8A100FTN->SYS.PINTMD = 0x08; // External interrupt 3 is falling edge triggered
KF8A100FTN->SYS.IPRST = 0x08; // Clear external interrupt 3 flag
KF8A100FTN->SYS.IPRMD = 0x00; // Enable external interrupt 3
__asm__("sei"); // Enable interrupts globally
// Read temperature from STCN75
STCN75_Read(0x00, &temperature);
printf("Temperature: %d\n", temperature);
return 0;
}
```
这段代码使用了 kf8a100ftn 芯片的 IIC 接口模拟了一个 IIC 主机,通过 IIC 总线与 STCN75 芯片进行通信,读取了 STCN75 芯片的温度数据。在代码中使用了外部中断来触发 IIC 中断,使用了全局变量 iic_flag 和 iic_data 来实现 IIC 中断数据的传递。
阅读全文