⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 这是《Keil Cx51 V7.0单片机高级语言编程与uVision2应用实践》教材各章中列出的全部程序例子。
💻 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 + -