本帖最后由 xbyu520 于 2014-4-25 16:34 编辑
这是我严格按照f340手册写的简单程序,写完执行后,看到闪灯。用官方Flash Programming Utility读code 0x0100 到0x0104数据。不是0x99 哪里问题呢??困扰几天了
#include<c8051f340.h>
// Peripheral specific initialization functions,
// Called from the Init_Device() function
void PCA_Init()
{
PCA0MD &= ~0x40;
PCA0MD = 0x00;
}
void Oscillator_Init()
{
OSCICN = 0x83;
}
void Reset_Sources_Init()
{
int i = 0;
VDM0CN = 0x80;
for (i = 0; i < 350; i++); // Wait 100us for initialization
}
void Port_IO_Init()
{
P2MDOUT = 0x10;
XBR1 = 0x40;
}
// Initialization function for device,
// Call Init_Device() from your main program
void Init_Device(void)
{
PCA_Init();
Oscillator_Init();
Port_IO_Init();
Reset_Sources_Init();
}
unsigned char i;
unsigned int *pwrite;
unsigned char j;
unsigned char k;
sbit LED=P2^4;
/*²Á³ýflashº¯Êý*/
void EraserFlash()
{
EA=0; //"禁止中断,建议这么做"手册原话,一个字没改
FLKEY=0xA5; //写入第一个关键码
FLKEY=0xF1; //写入第二个关键码
FLSCL=0x80;
PSCTL=0x03; //将pswe psee 置1
pwrite=0x0100;
*pwrite=0xff; //写入一个字节,相当于擦除整个扇区
PSCTL =0; //清除pswe psee
}
/*写字节函数*/
void Write_flash()
{
EA=0; //"禁止中断,建议这么做"手册原话,一个字没改
PFE0CN = 0; // Clear the FLBWE bit (register PFE0CN) to select single-byte
PSCTL=0x01; //set the PSWE bit,clear the PSEE bit
for(i=0;i<5;i++)
{
FLKEY=0xA5; //送入第一个关键码
FLKEY=0xF1; //送入第二个关键码
*pwrite=0x99;
pwrite++;
}
PSCTL = 0; // Clear the PSWE bit.½ûÖ¹flashд
EA=1; //使能中断
}
void main()
{
Init_Device();
EraserFlash();
Write_flash();
while(1)
{
for(j=0;j<255;j++)
for(k=0;k<255;k++);
LED=~LED;
}
} |