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

📄 loader_f02x.c.bak

📁 C8051F单片机在系统编程和在应用编程实例
💻 BAK
📖 第 1 页 / 共 2 页
字号:
//-----------------------------------------------------------------------------
// loader_F02x.c
//-----------------------------------------------------------------------------
// Copyright 2002 Cygnal Integrated Products, Inc.
// 交互选择性固件刷新程序,针对BTF020开发板做了程序改动;针对一些keil c语言函数做了注释说明
// AUTH: 钟磊
// DATE: 04 sep 07
//
// This program shows an example 'selective code loader' using the 'F02x. It 
// designates the FLASH page at 0x1000 for the code loaded through the UART. 
// 
// Control Function:
//
// The system is controlled via the hardware UART, operating at a baud rate
// determined by the constant <BAUDRATE>, using Timer1 overflows as the baud
// rate source.
//
// Received File Type:
//
// This example receives Intel HEX files which are OMF51 (linker output files)
// passed through the OH51 utility in the 'CYGNAL\IDEfiles\C51\Bin' folder. 
// 
// Note: Because this program writes to FLASH, the MONEN pin should be tied 
//       high.
//
// Target: C8051F02x
// Tool chain: KEIL C51 6.03 / KEIL EVAL C51
//

//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051f020.h>                 // SFR declarations
#include <stdio.h>                     // printf() and getchar()
#include <ctype.h>                     // tolower() and toint()

//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F02x
//-----------------------------------------------------------------------------
sfr16 DP       = 0x82;                 // data pointer
sfr16 TMR3RL   = 0x92;                 // Timer3 reload value
sfr16 TMR3     = 0x94;                 // Timer3 counter
sfr16 ADC0     = 0xbe;                 // ADC0 data
sfr16 ADC0GT   = 0xc4;                 // ADC0 greater than window
sfr16 ADC0LT   = 0xc6;                 // ADC0 less than window
sfr16 RCAP2    = 0xca;                 // Timer2 capture/reload
sfr16 T2       = 0xcc;                 // Timer2
sfr16 RCAP4    = 0xe4;                 // Timer4 capture/reload
sfr16 T4       = 0xf4;                 // Timer4
sfr16 DAC0     = 0xd2;                 // DAC0 data
sfr16 DAC1     = 0xd5;                 // DAC1 data

//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
#define TRUE         1
#define FALSE        0

#define SYSCLK       22118400          // SYSCLK frequency in Hz
#define BAUDRATE     115200            // Baud rate of UART in bps

sbit LED = P2^4;                       // LED='1' means ON
sbit SW2 = P2^0;                       // SW2='0' 表示按键按下

//-----------------------------------------------------------------------------
// Reserved Memory Space
//-----------------------------------------------------------------------------

char reserved_memory_bank[2] _at_ 0x08;// This memory bank is used by the
                                       // functions that will be loaded 
                                       // through the UART.
                                       // The memory bank location and size
                                       // are based on values from the M51 map
                                       // file generated when the loaded code
                                       // is linked.
									   //保留数据区的大小有待刷程序所使用的数据区
									   //大小决定,可以查看M51文件得到

//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------

void main (void);

// Support Subroutines
void print_menu(void);
void erase_flash_page(void);
void receive_code(void);
unsigned char hex2char();

// Initialization Subroutines
void SYSCLK_Init (void);
void PORT_Init (void);
void UART0_Init (void);

//-----------------------------------------------------------------------------
// Global VARIABLES
//-----------------------------------------------------------------------------

#define input_str_len 4                // buffer to hold characters entered               
char input_str[input_str_len];         // at the command prompt

void (*f)();                           // function pointer declaration

bit code_erased = FALSE;               // flag used to indicate that the FLASH
                                       // erase operation is complete
bit f_valid = FALSE;                   // flag to indicate that the FLASH 
                                       // programming operation is complete
									   // 标志先置零

//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------

void main (void) 
{

   WDTCN = 0xde;                       // disable watchdog timer
   WDTCN = 0xad;

   PORT_Init ();                       // initialize crossbar and GPIO
   SYSCLK_Init ();                     // initialize oscillator
   UART0_Init ();                      // initialize UART0
   
   print_menu();                       // print the command menu

   while (1){
         
      printf("\nEnter a command > ");
      gets(input_str, input_str_len);  //gets()读取的字符串,其长度没有限制,编程
	                                   //者要保证字符数组有足够大的空间,存放输入的字符串。

      
      switch ( input_str[0] ){
      
         case '1': erase_flash_page();   
                   printf("\nFlash page 0x1000 has been erased.\n");
                   break;

         case '2': printf("\nReady to receive HEX file...\n");
                   receive_code();
                   break;

         case '3': if(f_valid){
                     f = (void code *) 0x1000;
                     f();
                     printf("\nFinished\n");
                   } else {
                     printf("\n*** No function exists at 0x1000.\n");
                   }
                   break; 

         case '?': print_menu(); 
                   break;    

         default:  printf("\n*** Unknown Command.\n");
                   break;
      }   

   } // end while

} // end main

//-----------------------------------------------------------------------------
// Support Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// print_menu
//-----------------------------------------------------------------------------
//
// This routine uses prints the command menu to the UART.
//
void print_menu(void)
{
  
   printf("\n\nC8051F02x Selective Code Loader Example\n");
   printf("------------------------------------------\n");
   printf("1. Erase the flash page at 0x1000\n");
   printf("2. Receive HEX file\n");
   printf("3. Execute the function at 0x1000\n");
   printf("?. Print Command List\n");
}

//-----------------------------------------------------------------------------
// hex2char
//-----------------------------------------------------------------------------
//
// This routine converts a two byte ascii representation of a char to an
// 8-bit variable;
//
unsigned char hex2char()
{
   
   unsigned char retval;
   char byteH, byteL;
   
   // get a two-byte ASCII representation of a char from the UART
   byteH = _getkey();          //等待一个字符(使用_getkey函数)  
                               //函数_getkey和putchar使用片内串口来完成串行I/O
   byteL = _getkey();          //这些子程序包含在C51库中,每个函数的源文件位于\C51\LIB目录下
   
   // convert to a single 8 bit result
   retval = (char) toint(byteH) * 16;       //toint可重入转换一个十六进制数为一个十进制数

   retval += (char) toint(byteL);
   return retval;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -