This post was last edited by lising on 2019-7-16 17:28
In this experiment, the specified data is written to the specified address of the Data Flash inside CH549, and the data is sent to the P2 port after reading and displayed on the LEDs connected to P22, P23, P24, and P25. At the same time, the read data is sent to the PC via the serial port UART0 and displayed using the serial port debugging assistant.
1. Experimental resources
1. CH549EVT learning development board;
2. Keil v5.28.0.0;
3. CH549 development data summary.rar;
4. WCHISPTool v2.70;
5. CH549EVT other related documents;
6. Serial port debugging assistant SSCOM51 v5.13.1;
2. Experimental Preparation
1. CH549 memory space. CH549 memory space address F000H~F3FFH is Data Flash (EEPROM), supports single byte read (8 bits), single byte write (8 bits), block write (1~64 bytes), block erase (64 bytes) operations, the data remains unchanged after the chip is powered off, and can also be used as Code Flash. From the description, it can be seen that its application is very flexible.
2. Registers related to Data Flash (EEPROM): It can be seen that there are many registers related to Data Flash, but they are still convenient to use.
3. This experiment
The idea of the experiment is to first erase the Data Flash address (0XF000), then write a single byte of data at the address, and finally read the written data and send it to P25 of the P2 port; then erase -> write -> read and send it to the P2 port... After 4 cycles, you can see that the four LEDs of the P2 port are lit and extinguished respectively, forming a running light effect. The read data is sent to the PC through UART0 at the same time and displayed through the serial port assistant. UART0 directly uses the file of the previous experiment, but adjusts the clock, that is, Fsys=24MHz.
The experimental code refers to the official routine and has been trimmed to remove some verification steps, mainly for a more intuitive understanding. The main test code is as follows:
//单字节擦除
void Flash_Erase(UINT16 FLASH_DATA_ADDR)
{
EA = 0; //关闭全局中断,防止Flash操作被打断
SAFE_MOD = 0X55;
SAFE_MOD = 0XAA; //使能安全模式
GLOBAL_CFG |= bDATA_WE; //开启FLASH_DATA写使能
ROM_ADDR = FLASH_DATA_ADDR; //设置地址寄存器写入16位目标地址
ROM_BUF_MOD = 0x80;
ROM_DAT_BUF = 0x00; //设置擦写操作的数据缓冲寄存器为00h;
ROM_CTRL = ROM_CMD_ERASE; //启动擦除
ROM_DAT_BUF = 0X00;
SAFE_MOD = 0X55;
SAFE_MOD = 0XAA; //使能安全模式
GLOBAL_CFG &= ~bDATA_WE; //开启FLASH_DATA写保护
EA = 1; //恢复全局中断
}
//字节编程
void Flash_ProgByte(UINT16 FLASH_DATA_ADDR, UINT8 DATA)
{
EA = 0; //关闭全局中断,防止Flash操作被打断
SAFE_MOD = 0X55;
SAFE_MOD = 0XAA; //使能安全模式
GLOBAL_CFG |= bDATA_WE; //开启FLASH_DATA写使能
ROM_ADDR = FLASH_DATA_ADDR; //设置地址寄存器写入16位目标地址
ROM_BUF_MOD = 0x80;
ROM_DAT_BUF = DATA; //设置擦写操作的数据缓冲寄存器为00h;
ROM_CTRL = ROM_CMD_PROG; //启动编程
SAFE_MOD = 0X55;
SAFE_MOD = 0XAA; //使能安全模式
GLOBAL_CFG &= ~bDATA_WE; //开启FLASH_DATA写保护
EA = 1; //恢复全局中断
}
//读指定地址数据
UINT8 Flash_Read(UINT16 FLASH_DATA_ADDR)
{
UINT8 TEMP;
TEMP = *(PUINT8C)FLASH_DATA_ADDR;
return TEMP;
}
UINT8 LED_TAB[] = {0X1C,0X2C,0X34,0X38};
void delayms(UINT16 ms)
{
UINT16 i;
for(; ms>0; ms--)
for(i=1850; i>0; i--);
}
void GPIO_Config(void)
{
P2_MOD_OC &= ~0x3c;
P2_DIR_PU |= 0x3c;
P2 |= 0X3C;
}
void main()
{
UINT8 i;
UINT16 ID, Read_data;
GPIO_Config();
UART0_Config();
// ID = CHIP_ID; //读芯片ID识别码,CH549为0X49
// printf("\nWCH CH549EVT UART0 TEST\n");
while(1)
{
for(i=0; i<4; i++)
{
Flash_Erase(0xf000);
Flash_ProgByte(0xf000, LED_TAB);
Read_data = Flash_Read(0xf000);
printf("Read_data = %#2X\n",Read_data);
P2 = Read_data;
delayms(500);
}
}
}
IV. Experimental Results
5. Experimental Summary
Through this experiment, we have gained a rough understanding of the Data Flash (EEPROM) of CH549. Through simple experiments, we have realized the storage and reading of data at the specified address, which has created conditions for later advanced experiments.
This content is originally created by lising , a user on the EEWORLD forum. If you want to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source
|