本帖最后由 AAFREEZE 于 2019-1-4 12:58 编辑
我现在在使用PIC12F1571作为项目的主控MCU,现需要**功能保存按键档位状态。
我使用了MCC已生成memory的文件,但实际调用读写程序没有实现功能,是否我使用上有问题呢?
烦请大神们解答!!谢谢!!
#define Addr 0X0010 //memory地址
#define savevalue 0X00AA //memory测试数据
unsigned int keysaveaddr; //存储地址定义
unsigned int Buf[ERASE_FLASH_BLOCKSIZE]; //定义指向擦除flash块大小的指针
unsigned int Writedata; //写入数据
unsigned int readword; //读取数据
void Lowlight()
{
Highflag = 0;
Lowflag = 1;
if(keyvalue == 1) //低亮档a
{ //输出1:5% 其他:0
light_timer = 0; //5%使用PWM产生,20ms
PWM_EN = 1;
PWMduty = Lowlight1;
output2_SetLow();
output3_SetLow();
output4_SetLow();
//memory 数据存储
Writedata = savevalue;
keysaveaddr = Addr;
FLASH_EraseBlock(keysaveaddr); //memory 擦除
delay(10);
FLASH_WriteWord(keysaveaddr, Buf, Writedata); //memory 写数据
delay(10);
}
else if(keyvalue == 2) //低亮档b
{ //输出2:100% 其他:0
output2_SetHigh();
PWM_EN = 0;
light_timer = 0;
output1_SetLow();
output3_SetLow();
output4_SetLow();
}
else if(keyvalue == 3) //低亮档c
{ //输出3:100% 其他:0
output3_SetHigh();
output1_SetLow();
output2_SetLow();
output4_SetLow();
}
else if(keyvalue == 4) //低亮档d
{ //输出4:100% 其他:0
output4_SetHigh();
output1_SetLow();
output2_SetLow();
output3_SetLow();
}
if(keyvalue == 5) //低亮档e
{ //输出1:爆闪8Hz(占空80%) 其他0
light_timer = 0; //100ms输出high,25ms输出low
//Pwm_timer = 0; //
//T0125ms = 0;
//PWM_EN = 1;
//PWMduty = Lowlight5;
// PWM8Hz = 1;
P8Hz_EN = 1;
output2_SetLow();
output3_SetLow();
output4_SetLow();
}
}
void main(void)
{
// initialize the device
SYSTEM_Initialize();
keyvalue = 1;
keysaveaddr == Addr;
readword = FLASH_ReadWord(keysaveaddr); //memory 读
if(readword == savevalue)
{
output2_SetHigh();
output1_SetLow();
output3_SetLow();
output4_SetLow();
}
else
{
Highlight();
}
|