?? flash.c
字號:
/* AVR FLASH.H */
#ifndef FLASH
#define FLASH
#include <stdio.h>
#include <iom128V.h>
#define SPM_PAGESIZE 256
/*M128的一個Flash頁為256字節(128字)*/
#define DATA_BUFFER_SIZE SPM_PAGESIZE
/*定義接收緩沖區長度*/
char data[DATA_BUFFER_SIZE];
long address=0;
/*擦除(code=0x03)和寫入(code=0x05)一個Flash頁*/
void boot_page_ew(long p_address,char code)
{
asm("mov r30,r16\n"
"mov r31,r17\n"
"out 0x3b,r18\n"); /*將頁地址放入Z寄存器和RAMPZ的Bit0中*/
SPMCSR=code; /*寄存器SPMCSR中為操作碼*/
asm("spm\n"); /*對指定Flash頁進行操作 */
}
/*填充Flash緩沖頁中的一個字*/
void boot_page_fill(unsigned int address,int data)
{
asm("mov r30,r16\n"
"mov r31,r17\n" /*Z寄存器中為填沖頁內地址*/
"mov r0,r18\n"
"mov r1,r19\n"); /*R0R1中為一個指令字*/
SPMCSR=0x01;
asm("spm\n");
}
/*等待一個Flash頁的寫完成 */
void wait_page_rw_ok(void)
{
while(SPMCSR&0x40)
{
while(SPMCSR&0x01);
SPMCSR=0x11;
asm("spm\n");
}
}
/*更新一個Flash頁的完整處理*/
void main()
{
void write_one_page(void)
{
int i;
boot_page_ew(address,0x03); /*擦除一個Flash頁*/
wait_page_rw_ok(); /*等待擦除完成*/
for(i=0;i {
boot_page_fill(i, data[i]+(data[i+1]<<8));
}
boot_page_ew(address,0x05); /*將緩沖頁數據寫入一個Flash頁*/
wait_page_rw_ok(); /*等待寫入完成*/
}
}
#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -