?? main.c
字號:
#include <stdio.h>
/* 定義P89C51RD2特殊功能寄存器 */
sfr RCAP2L = 0xCA;
sfr RCAP2H = 0xCB;
sfr SCON = 0x98;
sbit RCLK = 0xCD;
sbit TCLK = 0xCC;
sbit TR2 = 0xCA;
/* 定義P89C51RD2保密位 */
#define SECURITY_BIT_1 0x02
#define SECURITY_BIT_2 0x04
#define SECURITY_BIT_3 0x08
#define SECURITY_BIT_1_SET(v) (v & SECURITY_BIT_1)
#define SECURITY_BIT_2_SET(v) (v & SECURITY_BIT_2)
#define SECURITY_BIT_3_SET(v) (v & SECURITY_BIT_3)
/* 定義P89C51RD2的FLASH塊 */
#define BLOCK_0 0x00
#define BLOCK_1 0x20
#define BLOCK_2 0x40
#define BLOCK_3 0x80
#define BLOCK_4 0xc0
#define BLOCK_0x0000_0x1FFF BLOCK_0
#define BLOCK_0x2000_0x3FFF BLOCK_1
#define BLOCK_0x4000_0x7FFF BLOCK_2
#define BLOCK_0x8000_0xBFFF BLOCK_3
#define BLOCK_0xC000_0xFFFF BLOCK_4
/* 外部匯編語言IAP函數說明 */
extern unsigned char iap_read_manufacturer_id(void);
extern void iap_init(unsigned char frequency);
extern unsigned char iap_read_device_id(unsigned char id_number);
extern unsigned char iap_read_security_bits(void);
extern void iap_program_security_bits(unsigned char bits);
extern unsigned char iap_program_data_byte(unsigned char val,
unsigned int addr);
extern unsigned char iap_read_data_byte(unsigned int addr);
extern void iap_erase_block(unsigned char block);
extern void iap_erase_chip(void);
extern unsigned char iap_read_boot_vector(void);
extern unsigned char iap_read_status_byte(void);
extern void iap_erase_boot_vector_status_byte(void);
extern void iap_program_status_byte(unsigned char status_byte);
extern void iap_program_boot_vector(unsigned char boot_vector);
/* 定義指令助記符MOV和RET的機器代碼 */
#define MOV 0x75
#define RET 0x22
unsigned char data foo;
void (*funcat4000)(void) = (void (code *)(void))0x4000;
/**************************** 串行口初始化函數 ***************************
* 功 能:初始化P89C51RD2串行口,定時器T2作為波特率發生器,采用6時鐘
* 模式,采用20MHz晶振時波特率為9600。
*************************************************************************/
void uart_init(void) {
RCLK = 1;
TCLK = 1;
RCAP2H = 255;
RCAP2L = 126;
SCON = 0x52;
TR2 = 1;
}
/********************************* 主函數 *******************************
* 功 能:通過調用匯編語言程序IAP庫文件rx2iaplib.a51中的不同函數
* 來完成各種IAP功能。
************************************************************************/
void main(void) {
unsigned char man_id, id1, id2, boot, status;
uart_init(); /* 初始化串行口 */
iap_init(20); /* IAP庫初始化,用整數指定所用晶振值 */
foo = 0;
man_id = iap_read_manufacturer_id(); /* 讀取器件生產廠家ID,Philips = 0x15 */
id1 = iap_read_device_id(1); /* 讀取器件ID */
id2 = iap_read_device_id(2);
printf("Manufacturer ID = %2.2bxH\nDevice ID 1 = %2.2bxH\nDevice ID 2 = %2.2bxH\n", man_id, id1, id2); /* 輸出ID */
boot = iap_read_boot_vector(); /* 讀取并輸出器件引導向量 */
printf("Boot Vector = %2.2bxH\n", boot);
status = iap_read_status_byte(); /* 讀取并輸出器件狀態字節 */
printf("Status Byte = %2.2bxH\n", status);
printf("Erasing 4000H -> 7FFFH...\n");
iap_erase_block(BLOCK_0x4000_0x7FFF); /* 擦除FLASH存儲器BLOCK_2塊 */
printf("Programming memory...\n");
/* 編程FLASH的一個字節,地址為4000H,內容為MOV指令代碼 */
if(iap_program_data_byte(MOV, 0x4000))
printf("Error programming 4000H\n");
/* 編程FLASH的一個字節,地址為4001H,內容為foo的值 */
if(iap_program_data_byte((unsigned char)&foo, 0x4001))
printf("Error programming 4001H\n");
/* 編程FLASH的一個字節,地址為4002H,內容為FFH */
if (iap_program_data_byte(0xff, 0x4002))
printf("Error programming 4002H\n");
/* 編程FLASH的一個字節,地址為4003H,內容為RET指令代碼 */
if (iap_program_data_byte(RET, 0x4003))
printf("Error programming 4003H\n");
/* 編程保密位1和保密位2 */
printf("Setting security bits...\n");
iap_program_security_bits(SECURITY_BIT_1 | SECURITY_BIT_2);
printf("Calling new code...\n");
funcat4000(); /* 調用位于FLASH地址4000H處的代碼 */
if (foo == 0xff) printf("foo = FFH\n");
printf("Finished.\n");
while(1);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -