打印
[技术问答]

N76E003,内部Flash怎么写入数据

[复制链接]
5820|5
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
chenqinyao|  楼主 | 2017-7-18 01:54 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 chenqinyao 于 2017-7-18 01:56 编辑

如下程序, 数据手册上的写入流程, 来操作没有任何变化, 读出来的是0xFF, 而不是0x55按BSP包里面的例程序也没任何变化, 写入0x55, 读出来也是0xFF
现在问一下, 写内部Flash要注意什么的吗, 还是还要哪里云设置


#include "N76E003.h"
#include "Function_Define.h"
#include "SFR_Macro.h"

//******************************************************************************
//  This code illustrates how to use IAP to make APROM 201h as a byte of
//  Data Flash when user code is executed in APROM.
//******************************************************************************
#define  PAGE_ERASE_AP    0x22
#define  BYTE_PROGRAM_AP  0x21

/*Data Flash, as part of APROM, is read by MOVC. Data Flash can be defined as
128-element array in “code” area from absolute address 0x0200               */

volatile unsigned char code Data_Flash[128] _at_ 0x0200;

Main (void)
{
    TA = 0xAA;        //CHPCON is TA protected
    TA = 0x55;
    CHPCON |= 0x01;       //IAPEN = 1, enable IAP mode

    TA = 0xAA;        //IAPUEN is TA protected
    TA = 0x55;
    IAPUEN |= 0x01;       //APUEN = 1, enable APROM update

    IAPCN = PAGE_ERASE_AP;    //Erase page 200h~27Fh
    IAPAH = 0x02;
    IAPAL = 0x00;
    IAPFD = 0xFF;
    TA = 0xAA;        //IAPTRG is TA protected
    TA = 0x55;
    IAPTRG |= 0x01;      //write ‘1’ to IAPGO to trigger IAP process

    IAPCN = BYTE_PROGRAM_AP;    // Program 201h with 55h
    IAPAH = 0x02;
    IAPAL = 0x01;
    IAPFD = 0x55;
    TA = 0xAA;
    TA = 0x55;
    IAPTRG |= 0x01;      //write ‘1’ to IAPGO to trigger IAP process

    TA = 0xAA;        //IAPUEN is TA protected
    TA = 0x55;
    IAPUEN &= ~0x01;       //APUEN = 0, disable APROM update

    TA = 0xAA;        //CHPCON is TA protected
    TA = 0x55;
    CHPCON &= ~0x01;       //IAPEN = 0, disable IAP mode

    P0 = Data_Flash[1];     //Read content of address 200h+1

    while(1);
}
上面  Data_Flash[1] 读出来的0xFF, 而不是0x55



沙发
wahahaheihei| | 2017-7-18 14:58 | 只看该作者
提供的有例子吧。

使用特权

评论回复
板凳
wahahaheihei| | 2017-7-18 15:13 | 只看该作者
/*---------------------------------------------------------------------------------------------------------*/
/*                                                                                                         */
/* Copyright(c) 2016 Nuvoton Technology Corp. All rights reserved.                                         */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/

//***********************************************************************************************************
//  Nuvoton Technoledge Corp.
//  Website: http://www.nuvoton.com
//  E-Mail : MicroC-8bit@nuvoton.com
//  Date   : Apr/21/2016
//***********************************************************************************************************

//***********************************************************************************************************
//  File Function: N76E003 APROM program DATAFLASH as EEPROM way
//***********************************************************************************************************
#include <stdio.h>
#include "N76E003.h"
#include "Common.h"
#include "Delay.h"
#include "SFR_Macro.h"
#include "Function_Define.h"

bit BIT_TMP;

//-------------------------------------------------------------------------
UINT8 read_APROM_BYTE(UINT16 code *u16_addr)
{
        UINT8 rdata;
        rdata = *u16_addr>>8;
        return rdata;
}
//-------------------------------------------------------------------------


/*****************************************************************************************************************
write_DATAFLASH_BYTE :
user can copy all this subroutine into project, then call this function in main.
******************************************************************************************************************/               
void write_DATAFLASH_BYTE(UINT16 u16_addr,UINT8 u8_data)
{
        UINT8 looptmp=0,u8_addrl_r;
        unsigned char code *cd_longaddr;
        unsigned char xdata *xd_tmp;
       
//Check page start address
        u8_addrl_r = u16_addr;
        if (u8_addrl_r<0x80)
        {
                u8_addrl_r = 0;
        }
        else
        {
                u8_addrl_r = 0x80;
        }
//Save APROM data to XRAM
        xd_tmp = 0x80;
        cd_longaddr = (u16_addr&0xff00)+u8_addrl_r;       
        while (xd_tmp !=0x100)
        {
                *xd_tmp = *cd_longaddr;
                looptmp++;
                xd_tmp++;
                cd_longaddr++;
        }
// Modify customer data in XRAM
        u8_addrl_r = u16_addr;
        if (u8_addrl_r<0x80)
        {
                xd_tmp = u8_addrl_r+0x80;
        }
        else
        {
                xd_tmp = u8_addrl_r+0;
        }
        *xd_tmp = u8_data;
//Erase APROM DATAFLASH page
                IAPAL = u16_addr;
                IAPAH = u16_addr>>8;
                IAPFD = 0xFF;
          set_IAPEN;
                set_APUEN;
    IAPCN = 0x22;                
                set_IAPGO;
//Save changed RAM data to APROM DATAFLASH
        u8_addrl_r = u16_addr;
        if (u8_addrl_r<0x80)
        {
                u8_addrl_r =0;
        }
        else
        {
                u8_addrl_r = 0x80;
        }
                xd_tmp = 0x80;
          IAPAL = u8_addrl_r;
    IAPAH = u16_addr>>8;
                set_IAPEN;
                set_APUEN;
          IAPCN = 0x21;
                while (xd_tmp !=0xFF)
                {
                        IAPFD = *xd_tmp;
                        set_IAPGO;
                        IAPAL++;
                        xd_tmp++;
                }
                clr_APUEN;
                clr_IAPEN;
}       
/******************************************************************************************************************/               


void main (void)
{
                UINT8 datatemp;
/* -------------------------------------------------------------------------*/
/*  Dataflash use APROM area                                                                                                                         */
/*        APROM 0x3800~0x38FF demo as dataflash                                                                                                     */
/* Please use Memory window key in C:0x3800 to check earse result                                          */             
/* -------------------------------------------------------------------------*/
       
//call write byte
                write_DATAFLASH_BYTE (0x3881,0x55);
                write_DATAFLASH_BYTE (0x3882,0x56);
                write_DATAFLASH_BYTE (0x3855,0xaa);
                write_DATAFLASH_BYTE (0x3856,0x66);
//call read byte
                datatemp = read_APROM_BYTE(0x3882);
    while(1);
}
//-----------------------------------------------------------------------------------------------------------

使用特权

评论回复
地板
yiyigirl2014| | 2017-7-18 18:57 | 只看该作者

使用特权

评论回复
5
稳稳の幸福| | 2017-7-18 19:59 | 只看该作者
手册上应该有说操作哪儿

使用特权

评论回复
6
l4157| | 2017-7-19 10:08 | 只看该作者
KEIL C51 有时会优化掉 TA=0xAA;TA=0x55; ???;这个时序有要求的.我也是刚发现,优化等级为9的时候出了问题,改优化等级为7 就没有 问题了

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

5

主题

23

帖子

2

粉丝