英飞凌
直播中

abdkjshd

8年用户 1168经验值
擅长:可编程逻辑
私信 关注

电容感应没有在eeprom中存储数据是怎么回事?

电容感应(滑块)没有在 eeprom 中存储数据?

#include"rtc.h"#include
#include"rtc.h"#define LED_OFF 0
#define LED_ON 1
void HandleError(void);
cy_en_em_eeprom_status_t eepromReturnValue;
void HandleError(void);
CY_ISR_PROTO(SwitchInterrupt);// 声明 ISR 原型
#if defined (__ICCARM__)
#pragma data_alignment = CY_FLASH_SIZEOF_ROW
const uint8_t Em_EEPROM_em_EepromStorage[Em_EEPROM_PHYSICAL_SIZE] = {0u};
#else
const uint8_t Em_EEPROM_em_EepromStorage[Em_EEPROM_PHYSICAL_SIZE]
__ALIGNED(CY_FLASH_SIZEOF_ROW) = {0u};
#endif
#define EEPROM_ADDR 0x000u
#define EEPROM_ADDR2 0x001u
#define EEPROM_ADDR3 0x003u
#define EEPROM_ADDR40x004u
#define EEPROM_ADDR5 0x005u
#define EEPROM_DATA_SIZE 2
uint8_t eepromData1[EEPROM_DATA_SIZE];
uint8_t eepromData2[EEPROM_DATA_SIZE];
uint8_t eepromData3[EEPROM_DATA_SIZE];
uint8_t eepromData4[EEPROM_DATA_SIZE];
uint8_t eepromData5[EEPROM_DATA_SIZE];
uint8_t count=0;
uint8_t count2=0;
uint8_t count3=0;
uint8_t count4=0;
uint8_t count5=0;
int main(void)
{
uint16 sliderval=0;
uint16 sliderval2=0;
uint16 sliderval3=0;
uint16 sliderval4=0;
uint16 sliderval5=0;
uint16 oldsliderval =0xFFFF;
uint16 oldsliderval2 =0xFFFF;
uint16 oldsliderval3 =0xFFFF;
uint16 oldsliderval4 =0xFFFF;
uint16 oldsliderval5 =0xFFFF;
CyGlobalIntEnable; /* 启用全局中断。 */
eepromReturnValue = Em_EEPROM_Init((uint32_t)Em_EEPROM_em_EepromStorage);
if(eepromReturnValue != CY_EM_EEPROM_SUCCESS)
{
HandleError();
}
LED_1_Write(LED_OFF);
LED_2_Write(LED_OFF);
LED_3_Write(LED_OFF);
LED_4_Write(LED_OFF);
LED_5_Write(LED_OFF);
Clock_Start();
PWM_Start();
UART_Start();
CapSense_Start();
CapSense_InitializeAllBaselines();
CapSense_ScanEnabledWidgets();
Em_EEPROM_Read(EEPROM_ADDR, eepromData1[0], EEPROM_DATA_SIZE);
if(eepromData1[0] == 1)
{
LED_1_Write(LED_ON);
}
Em_EEPROM_Read(EEPROM_ADDR2, eepromData2[0], EEPROM_DATA_SIZE);
if(eepromData2[0] == 1)
{
LED_2_Write(LED_ON);
}
Em_EEPROM_Read(EEPROM_ADDR3, eepromData3[0], EEPROM_DATA_SIZE);
if(eepromData3[0] == 1)
{
LED_3_Write(LED_ON);
}
Em_EEPROM_Read(EEPROM_ADDR4, eepromData4[0], EEPROM_DATA_SIZE);
if(eepromData4[0] == 1)
{
LED_4_Write(LED_ON);
}
Em_EEPROM_Read(EEPROM_ADDR5, eepromData5[0], EEPROM_DATA_SIZE);
if(eepromData5[0] == 1)
{
LED_5_Write(LED_ON);
}
for(;;)
{if(!CapSense_IsBusy())
{
sliderval = CapSense_GetCentroidPos(CapSense_LINEARSLIDER0__LS);
sliderval2 = CapSense_GetCentroidPos(CapSense_LINEARSLIDER0__LS);
sliderval3 = CapSense_GetCentroidPos(CapSense_LINEARSLIDER0__LS);
sliderval4 = CapSense_GetCentroidPos(CapSense_LINEARSLIDER0__LS);
sliderval5 = CapSense_GetCentroidPos(CapSense_LINEARSLIDER0__LS);

if(sliderval != 0xFFFF   sliderval != oldsliderval)
{
count = 0;
LED_1_Write(((sliderval>= 20) || (sliderval> 0)) ? led_on : led_off);
Em_EEPROM_Write(EEPROM_ADDR, sliderval, EEPROM_DATA_SIZE);
PWM_WriteCompare(sliderval);
oldsliderval = sliderval;
}

if(sliderval2 != 0xFFFF   sliderval2 != oldsliderval2)
{
count2 = 0;
LED_2_Write(((sliderval2>= 40) || (sliderval2> 20)) ? led_on : led_off);
Em_EEPROM_Write(EEPROM_ADDR2, sliderval2, EEPROM_DATA_SIZE);
PWM_WriteCompare(sliderval2);
oldsliderval2 = sliderval2;
}

if(sliderval3 != 0xFFFF   sliderval3 != oldsliderval3)
{
count3 = 0;
LED_3_Write(((sliderval3>= 60) || (sliderval3> 40)) ? led_on : led_off);
Em_EEPROM_Write(EEPROM_ADDR3, sliderval3, EEPROM_DATA_SIZE);
PWM_WriteCompare(sliderval3);
oldsliderval3 = sliderval3;
}

if(sliderval4 != 0xFFFF   sliderval4 != oldsliderval4)
{
count4 = 0;
LED_4_Write(((sliderval4>= 80) || (sliderval4> 60)) ? led_on : led_off);
Em_EEPROM_Write(EEPROM_ADDR4, sliderval4, EEPROM_DATA_SIZE);
PWM_WriteCompare(sliderval4);
oldsliderval4 = sliderval4;
}

if(sliderval5 != 0xFFFF   sliderval5 != oldsliderval5)
{
count5 = 0;
LED_5_Write(((sliderval5>= 100) || (sliderval5> 80)) ? led_on : led_off);
Em_EEPROM_Write(EEPROM_ADDR5, sliderval5, EEPROM_DATA_SIZE);
PWM_WriteCompare(sliderval5);
oldsliderval5 = sliderval5;
}

CapSense_UpdateEnabledBaselines();
CapSense_ScanEnabledWidgets();
}
}
}
void HandleError(void)
{
__disable_irq(); // 禁用所有中断
while(1u) {}// 无限循环以处理错误
}

回帖(1)

张亮

2024-5-30 16:06:07
以下是一些建议和可能的原因:

1. 确保您已经正确初始化了EEPROM。在您的代码中,您需要调用初始化函数,例如`Cy_Em_EEPROM_Init()`。

2. 确保您已经正确编写了存储数据到EEPROM的函数。您需要使用`Cy_Em_EEPROM_Write()`函数来存储数据。

3. 检查您的中断服务例程(ISR)。在您的代码中,您声明了一个名为`SwitchInterrupt`的ISR原型,但没有提供实现。确保您已经正确实现了ISR,并在其中调用了存储数据到EEPROM的函数。

4. 确保您的滑块(电容感应)硬件连接正确,并且能够触发中断。

5. 检查您的代码中是否有任何语法错误或缺少的头文件。

6. 如果您仍然无法解决问题,请提供更完整的代码片段,以便更好地了解问题所在。

以下是一个简化的示例,展示了如何使用EEPROM存储数据:

```c
#include "cy_em_eeprom.h"
#include "rtc.h"

#define LED_OFF 0
#define LED_ON 1

void HandleError(void);
cy_en_em_eeprom_status_t eepromReturnValue;

void HandleError(void)
{
    // Handle error here
}

CY_ISR_PROTO(SwitchInterrupt);

#if defined (__ICCARM__)
#pragma data_alignment = CY_FLASH_SIZEOF_ROW
const uint8_t Em_EEPROM_em_EepromStorage[Em_EEPROM_PHYSICAL_SIZE] = {0u};
#else
const uint8_t Em_EEPROM_em_EepromStorage[Em_EEPROM_PHYSICAL_SIZE] = {0u};
#endif

void SwitchInterrupt(void)
{
    // Read the value from the capacitive sensor (slider)
    uint8_t sliderValue = ReadSliderValue();

    // Write the value to EEPROM
    eepromReturnValue = Cy_Em_EEPROM_Write(0, sizeof(sliderValue), &sliderValue);
    if (eepromReturnValue != CY_EM_EEPROM_SUCCESS)
    {
        HandleError();
    }
}

int main(void)
{
    // Initialize the EEPROM
    Cy_Em_EEPROM_Init(Em_EEPROM_em_EepromStorage, Em_EEPROM_PHYSICAL_SIZE);

    // Configure and enable the interrupt for the capacitive sensor (slider)
    // ...

    while (1)
    {
        // Your main loop code here
    }
}
```

请注意,这只是一个示例,您需要根据您的硬件和需求进行调整。
举报

更多回帖

发帖
×
20
完善资料,
赚取积分