📄 example.c
字号:
/****************************************************************************
*
* (c) ATMEL-Wireless and Microcontrollers 2001
*
*
*****************************************************************************/
/*C**************************************************************************
* NAME: example_api.c
*----------------------------------------------------------------------------
* CREATED_BY: Jean-Sebastien Berthy && Rapha雔 L'etendu
* CREATION_DATE: 1/03/2000
* AUTHOR: $Jean-Sebastien Berthy && Rapha雔 L'etendu $
* REVISION $Revision: 1.4 $
* DATE: $Date: 2001/09/10 11:11:30 $
*----------------------------------------------------------------------------
* This file provides a simple example for using Flah_Api library on T89C51RD2
* This example prints messages through the UART
* in the following configuration : 9600 Bauds with T89C51RD2 at 11MHz X1
*****************************************************************************/
/*_____ I N C L U D E - F I L E S __________________________________________*/
#include "config.h"
#include "stdio.h"
/*_____ G L O B A L S ______________________________________________________*/
/*_____ P R I V A T E - F U N C T I O N S - D E C L A R A T I O N __________*/
void uart_init (void);
void tx_data (Uchar );
/*_____ L O C A L S ________________________________________________________*/
/*F**************************************************************************
* NAME: uart_init
*----------------------------------------------------------------------------
* AUTHOR: Jean-Sebatien BERTHY
*****************************************************************************/
void uart_init (void)
{
SCON = 0x50;
TMOD = TMOD | 0x20 ; /* Timer1 in mode 2 & not gated */
TH1 = 0xFD; /* 9600 bauds at 11.059200 MHZ */
TL1 = 0xFD;
PCON = PCON & 0X80;
TCON |= 0x40;
TI=1;
}
/*F**************************************************************************
* NAME: bin_to_asc.
* PARAMS:
* to_convert: byte to parse in ascii
*----------------------------------------------------------------------------
* PURPOSE: This function return the ascii value
*****************************************************************************/
Uchar bin_to_ascii (Uchar to_convert)
{
Uchar convert;
if ((to_convert >= 0) &&
(to_convert <= 9) )
{
convert = to_convert + 0x30;
}
else
{
convert = to_convert + 0x37;
}
return (convert);
}
/*F**************************************************************************
* NAME: tx_data
*----------------------------------------------------------------------------
* PARAMS:
* data_to_tx: byte to transmit on the line
*----------------------------------------------------------------------------
* PURPOSE: This function send on UART the value
*****************************************************************************/
void tx_data (Uchar data_to_tx)
{
while(!TI);
TI = 0;
SBUF = data_to_tx;
}
/*F**************************************************************************
* NAME: send_byte_ascii
*----------------------------------------------------------------------------
* PARAMS:
* byte_to_tx: byte to send on UART in ascii
*----------------------------------------------------------------------------
* PURPOSE:
*****************************************************************************/
void send_byte_ascii(Uchar byte_to_tx)
{
Uchar data_to_tx;
data_to_tx = bin_to_ascii( byte_to_tx >> 4);
tx_data(data_to_tx);
data_to_tx = bin_to_ascii( byte_to_tx & 0x0F);
tx_data(data_to_tx);
}
/*F**************************************************************************
* NAME: send_cr_lf
*----------------------------------------------------------------------------
* PARAMS:
*----------------------------------------------------------------------------
* PURPOSE:
*****************************************************************************/
void send_cr_lf(void)
{
tx_data(0x0D);
tx_data(0x0A);
}
/*F**************************************************************************
* NAME: main
*----------------------------------------------------------------------------
*
*****************************************************************************/
Uchar xdata tab[256];
void main (void)
{
Uchar value;
Uchar cpt;
Uint16 add_flash;
uart_init();
printf("Example for Flash API\n");
/* ----- Special read ---------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
// Read bootloader version
printf("Bootloader version : ");
value = __api_rd_bootloader_version();
send_byte_ascii(value);
send_cr_lf();
// Read device id1
printf("Device id1........ : ");
value = __api_rd_device_id1();
send_byte_ascii(value);
send_cr_lf();
// Read device id2
printf("Device id2........ : ");
value = __api_rd_device_id2();
send_byte_ascii(value);
send_cr_lf();
// Read device id3
printf("Device id3........ : ");
value = __api_rd_device_id3();
send_byte_ascii(value);
send_cr_lf();
// Read Manufacturer
printf("Manufacturer id... : ");
value = __api_rd_manufacturer();
send_byte_ascii(value);
send_cr_lf();
/* ----- HSB ------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
// Read HSB
printf("Hardware byte: ");
value = __api_rd_HSB();
send_byte_ascii(value);
send_cr_lf();
/* ----- BSB ------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
// Read BSB
printf("read BSB : ");
value = __api_rd_BSB();
send_byte_ascii(value);
send_cr_lf();
// Write BSB = 0x55
printf("Write BSB = 0xAB \n");
__api_wr_BSB(0xAB);
// Read BSB
printf("read BSB : ");
value = __api_rd_BSB();
send_byte_ascii(value);
send_cr_lf();
// Write BSB = 0x00
printf("Write BSB = 0x00 \n");
__api_wr_BSB(0x00);
// Read BSB
printf("read BSB : ");
value = __api_rd_BSB();
send_byte_ascii(value);
send_cr_lf();
/* ----- SBV ------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
// Read SBV
printf("read SBV : ");
value = __api_rd_SBV();
send_byte_ascii(value);
send_cr_lf();
// Write SBV = 0x55
printf("Write SBV = 0x55 \n");
__api_wr_SBV(0x55);
// Read SBV
printf("read SBV : ");
value = __api_rd_SBV();
send_byte_ascii(value);
send_cr_lf();
// Erase SBV
printf("Erased SBV : \n");
__api_erase_SBV();
// Read SBV
printf("read SBV : ");
value = __api_rd_SBV();
send_byte_ascii(value);
send_cr_lf();
/* ----- SSB ------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
// Read SSB
printf("read SSB : ");
value = __api_rd_SSB();
send_byte_ascii(value);
send_cr_lf();
// Write SSB = LEVEL 0
printf("Write SSB = SSB_NO_SECURITY \n");
__api_wr_SSB_NO_SECURITY();
// Read SSB
printf("read SSB : ");
value = __api_rd_SSB();
send_byte_ascii(value);
send_cr_lf();
// Write SSB = LEVEL1
printf("Write SSB = SSB_WR_SECURITY\n");
__api_wr_SSB_WR_SECURITY();
// Read SSB
printf("read SSB : ");
value = __api_rd_SSB();
send_byte_ascii(value);
send_cr_lf();
// Write SSB = LEVEL2
printf("Write SSB = SSB_RD_WR_SECURITY\n");
__api_wr_SSB_RD_WR_SECURITY();
// Read SSB
printf("read SSB : ");
value = __api_rd_SSB();
send_byte_ascii(value);
send_cr_lf();
// Write SSB = LEVEL 0
printf("Write SSB = SSB_NO_SECURITY \n");
__api_wr_SSB_NO_SECURITY();
// Read SSB
printf("read SSB : ");
value = __api_rd_SSB();
send_byte_ascii(value);
send_cr_lf();
// Write SSB = LEVEL2
printf("Write SSB = SSB_RD_WR_SECURITY\n");
__api_wr_SSB_RD_WR_SECURITY();
// Read SSB
printf("read SSB : ");
value = __api_rd_SSB();
send_byte_ascii(value);
send_cr_lf();
// Write SSB = LEVEL1
printf("Write SSB = SSB_WR_SECURITY\n");
__api_wr_SSB_WR_SECURITY();
// Read SSB
printf("read SSB : ");
value = __api_rd_SSB();
send_byte_ascii(value);
send_cr_lf();
// Write SSB = LEVEL 0
printf("Write SSB = SSB_NO_SECURITY \n");
__api_wr_SSB_NO_SECURITY();
// Read SSB
printf("read SSB : ");
value = __api_rd_SSB();
send_byte_ascii(value);
send_cr_lf();
/* ----- Flash ----------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
// Read code address 0x3F00
printf("read code at 0x3F00 : ");
value = __api_rd_code_byte(0x3F00);
send_byte_ascii(value);
send_cr_lf();
// Write code at address 0x3F00 = 0xAA
printf("Write code add 0x3F00 = 0xAA \n");
if (__api_wr_code_byte(0x3F00, 0xAA)==0x00)
{
printf("prog ok\n");
}
// Read code address 0x3F00
printf("read code at 0x3F00 : ");
value = __api_rd_code_byte(0x3F00);
send_byte_ascii(value);
send_cr_lf();
// Write code page between address 0x3F00-3F7F to 0xFF
// Read this page
// Write code page between address 0x3F00-3F7F to 0x00
// Read this page
// Write code page between address 0x3F00-3F7F to 0xFF
// Read this page
for (cpt=0;; cpt++)
{
tab[cpt] = 0xFF;
if (cpt==255) break;
}
printf("Write code page between 0x3F00-3F7F to 0xFF \n");
send_cr_lf();
if ( __api_wr_code_page(0x3F00,(Uint16)tab,0x80)==0x00)
{
printf("prog ok\n");
}
// Read code between address 0x3F00-0x3F7F
printf("read code between 0x3F00-0x3F7F : ");
send_cr_lf();
for (cpt=0; cpt< 128; cpt++)
{
value = __api_rd_code_byte(0x3F00+cpt);
send_byte_ascii(value);
}
send_cr_lf();
for (cpt=0;; cpt++)
{
tab[cpt] = 0x00;
if (cpt==255) break;
}
printf("Write code page between 0x3F00-3F7F to 0x00 \n");
send_cr_lf();
if ( __api_wr_code_page(0x3F00,(Uint16)tab,0x80)==0x00)
{
printf("prog ok\n");
}
// Read code between address 0x3F00-0x3F7F
printf("read code between 0x3F00-0x3F7F : ");
send_cr_lf();
for (cpt=0; cpt< 128; cpt++)
{
value = __api_rd_code_byte(0x3F00+cpt);
send_byte_ascii(value);
}
send_cr_lf();
for (cpt=0;; cpt++)
{
tab[cpt] = 0xFF;
if (cpt==255) break;
}
printf("Write code page between 0x3F00-3F7F to 0xFF \n");
send_cr_lf();
if ( __api_wr_code_page(0x3F00,(Uint16)tab,0x80)==0x00)
{
printf("prog ok\n");
}
// Read code between address 0x3F00-0x3F7F
printf("read code between 0x3F00-0x3F7F : ");
send_cr_lf();
for (cpt=0; cpt< 128; cpt++)
{
value = __api_rd_code_byte(0x3F00+cpt);
send_byte_ascii(value);
}
send_cr_lf();
/* ----- Eeprom ----------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
// Read eeprom at address 0x10
printf("read eeprom at address 0x10 : ");
if (!__api_eeprom_busy())
{
value = __api_rd_eeprom_byte(0x10);
send_byte_ascii(value);
send_cr_lf();
}
else
{
printf("eeprom busy\n");
}
// Write eeprom at address 0x10 = 0x55
printf("write eeprom at address 0x10 =0x55\n");
if (!__api_eeprom_busy())
{
__api_wr_eeprom_byte(0x10, 0x55);
}
else
{
printf("eeprom busy\n");
}
// Read eeprom at address 0x10
while (__api_eeprom_busy());
printf("read eeprom at address 0x10 : ");
value = __api_rd_eeprom_byte(0x10);
send_byte_ascii(value);
send_cr_lf();
for (add_flash=0;add_flash<2048;add_flash++)
{
value = __api_rd_eeprom_byte(add_flash);
send_byte_ascii(value);
if ((add_flash&15)==15)
{
send_cr_lf();
}
}
while(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -