📄 ex3_utility.c
字号:
/******************************************************************************
*
* (c) copyright Freescale Semiconductor Hong Kong Ltd. 2004
* ALL RIGHTS RESERVED
*
*******************************************************************************
** THIS CODE IS ONLY INTENDED AS AN EXAMPLE FOR DEMONSTRATING THE FREESCALE **
** MICROCONTROLLERS. IT HAS ONLY BEEN GIVEN A MIMIMUM LEVEL OF TEST. IT IS **
** PROVIDED 'AS SEEN' WITH NO GUARANTEES AND NO PROMISE OF SUPPORT. **
*******************************************************************************
*
* FILE: ex3_utility.c REVISION 0.2
*
* DESCRIPTION: Application code is placed here.
* This module handles all tasks related to the rs232 port
* Program to control the SCI0 module to send and receive ASCII
* to and from PC serial port RS232
*
* TARGET DEVICE: Hardware EVB912DP256 or equivalent
*
* COMPILER: Metrowerks VERSION: ADS v1.2 (initial version)
* VERSION: ADS v2.0 (Demo beta version)
* VERSION: ADS v3.1 (Demo version)
*
* NOTES
* -----
* All modules remain at their reset addresses.
*
*
* UPDATED HISTORY:
*
* REV YYYY.MM.DD AUTHOR DESCRIPTION OF CHANGE
* --- ---------- ------ ---------------------
* 0.0 2002.04.27 Kenny Lam Initial version
* 0.1 2002.12.18 Kenny Lam Demo beta version
* 0.2 2004.04.12 Kenny Lam Demo version
*
******************************************************************************/
/* Freescale is not obligated to provide any support, upgrades or new */
/* releases of the Software. Freescale may make changes to the Software at */
/* any time, without any obligation to notify or provide updated versions of */
/* the Software to you. Freescale expressly disclaims any warranty for the */
/* Software. The Software is provided as is, without warranty of any kind, */
/* either express or implied, including, without limitation, the implied */
/* warranties of merchantability, fitness for a particular purpose, or */
/* non-infringement. You assume the entire risk arising out of the use or */
/* performance of the Software, or any systems you design using the software */
/* (if any). Nothing may be construed as a warranty or representation by */
/* Freescale that the Software or any derivative work developed with or */
/* incorporating the Software will be free from infringement of the */
/* intellectual property rights of third parties. In no event will Freescale */
/* be liable, whether in contract, tort, or otherwise, for any incidental, */
/* special, indirect, consequential or punitive damages, including, but not */
/* limited to, damages for any loss of use, loss of time, inconvenience, */
/* commercial loss, or lost profits, savings, or revenues to the full extent */
/* such may be disclaimed by law. The Software is not fault tolerant and is */
/* not designed, manufactured or intended by Freescale for incorporation */
/* into products intended for use or resale in on-line control equipment in */
/* hazardous, dangerous to life or potentially life-threatening environments */
/* requiring fail-safe performance, such as in the operation of nuclear */
/* facilities, aircraft navigation or communication systems, air traffic */
/* control, direct life support machines or weapons systems, in which the */
/* failure of products could lead directly to death, personal injury or */
/* severe physical or environmental damage (High Risk Activities). You */
/* specifically represent and warrant that you will not use the Software or */
/* any derivative work of the Software for High Risk Activities. */
/* Freescale and the Freescale logos are registered trademarks of Freescale */
/* Semiconductor Inc. */
/*****************************************************************************/
#include <hidef.h>
#include <math.h>
#include "const.h"
#include "ut_extern.h"
#include "DP256Port.h"
#include "GlobalVar.h"
/*******************************************************************
* Init SCI port 0 as baud 9600 subroutine
* Description : Initial SCI as 9600 and enable tx and rx
* :
* Example : N/A
* Input : N/A
* Output : N/A
* Modify : N/A
* Return : N/A
********************************************************************/
void init_SCI0 ()
{
SC0BDW=Eclock/16/9600; //9600 buad rate
SC0CR2=0xc; //enable tx,rx
}
/*******************************************************************
* Transmit char (ASCII) subroutine
* Description : Transmit ascii via RS232
* :
* Example : N/A
* Input : data
* Output : single ascii to screen
* Modify : N/A
* Return : N/A
********************************************************************/
void tx_char0(char data)
{
while ((SC0SR1&0x40)!=0x40);
SC0DRL = data;
}
/*******************************************************************
* printf0 subroutine
* Description : Transmit string via RS232
* :
* Example : N/A
* Input : *str
* Output : ascii to screen without lf and cr at the end
* Modify : N/A
* Return : N/A
********************************************************************/
void printf0(char *str)
{
while((*str != '\r'))
{
tx_char0(*str);
if(*str++=='\n')
tx_char0('\r');
}
}
/**********************************************
* eat time subroutine
***********************************************/
void delay1()
{
unsigned int data;
data=0xffff;
while((data--)!=0);
}
/*******************************************************************
* int_div subroutine
* Description : Integer Divide Subroutine
* :
* Example : N/A
* Input : Intger to be divide, Divider
* Output : .quot, .mem
* Modify : N/A
* Return : N/A
********************************************************************/
div_t int_div( int num, int denum)
{
div_t static result;
result.quot = 0;
while(num >= denum)
{
num -= denum;
result.quot++;
}
result.rem = num;
return(result);
}
/*******************************************************************
* Convert HEX to DEC and send out ASCII subroutine
* Description : Convert hex to dec and send out via RS232
* :
* :
* Example : 0x32 <=> 50
* Input : 0x32 (cdata)
* Output : 5 0 (ascii)
* Modify : N/A
* Return : void
********************************************************************/
void hex_clk_dec(unsigned char data)
{
div_t static digit;
digit=int_div((int)data,10);
tx_char0(digit.quot+'0');
tx_char0(digit.rem+'0');
}
/*******************************************************************
* Wait for receiving char subroutine
* Description : Waiting for rx char
* :
* Example : N/A
* Input : N/A
* Output : N/A
* Modify : N/A
* Return : char
********************************************************************/
byte rx_char0()
{
char data;
while( (SC0SR1 & 0x20)==0);
data = SC0SR1;
return(SC0DRL);
}
/*******************************************************************
* Wait for receiving char subroutine
* Description : Waiting for rx char and display page information
* :
* Example : N/A
* Input : menu_page
* Output : N/A
* Modify : N/A
* Return : char
********************************************************************/
byte menurx_char0(char menu_page)
{
char data;
while( (SC0SR1 & 0x20)==0)
{
delay1();
disp_info(menu_page); //display corresponding menu info
}
data = SC0SR1;
return(SC0DRL);
}
/*******************************************************************
* Check user abort data display subroutine
* Description : Checking whether user abort or not
* :
* Example : N/A
* Input : SC0SR1 empty?
* Output : 3 2 (ascii)
* Modify : N/A
* Return : status [OK for (DOT or ESC) or NOTOK for others chars]
********************************************************************/
byte check_user_abort()
{
char data;
if((SC0SR1 & 0x20)!=0)
{
data=SC0DRL;
if(data==DOT||data==ESC)
return(OK);
else
return(NOTOK);
}
else
return(NOTOK);
}
/*******************************************************************
* Transmit out 1 char (ASCII) subroutine
* Description : Transmit out upper and lower 4 bit of char as ascii
* : via RS232
* :
* Example : 0x32
* Input : 0x32 (cdata)
* Output : 3 2 (ascii)
* Modify : N/A
* Return : void
********************************************************************/
void send_asc(byte cdata)
{
if(cdata>9)
tx_char0(cdata+'0'+7);
else
tx_char0(cdata+'0');
}
/*******************************************************************
* Convert HEX to ASCII subroutine
* Description : Convert hex to ascii and send out via RS232
* :
* Example : 0x32
* Input : 0x32 (cdata)
* Output : 3 2 (ascii)
* Modify : N/A
* Return : void
********************************************************************/
void hex_asc(byte cdata)
{
send_asc((cdata>>4)&0x0f);
send_asc((cdata)&0x0f);
}
/*******************************************************************
* Convert HEX to ASCII with additional spaces subroutine
* Description : Convert hex to ascii and send out spaces followed
* : via RS232
* :
* Example : 0x32
* Input : 0x32 (cdata), 3(space)
* Output : 3 2 _ _ _ (ascii)
* Modify : N/A
* Return : void
********************************************************************/
void hex_asc_sp(byte cdata, byte space)
{
int i;
hex_asc(cdata);
for (i=0;i<space;i++)
tx_char0(' ');
}
/*******************************************************************
* Input int (dec) subroutine
* Description : Get the data(word) from user
* :
* :
* Example : N/A
* Input : N/A
* Output : *i_ptr
* Modify : N/A
* Return : status [OK for ('0'-'z') or NOTOK for others chars]
*********************************************************************/
byte input_int(unsigned int *i_ptr) //3046
{
byte echo,cdata,loop=ENABLE;
int count=0,i,j;
unsigned int i_data,i_data1,i_data2;
i_data=0;
*i_ptr=0;
while(loop == ENABLE)
{
echo=rx_char0();
if((echo==ESC)||(echo==DOT)||(echo==CR))
loop = DISABLE; //break the while loop
if (echo==BS || echo==DEL)
{
if(count>0) //del 4 bit data in buffer
{
count--;
i_data=i_data>>4;
tx_char0(echo); //send bs the char on screen
tx_char0(' '); //send space to clear the char
tx_char0(echo); //send bs again to move back the cursor
}
}
if (count<4) //update 4 bit data to buffer
{
if((echo<='9') && (echo>='0'))
{
// if ((echo>='0') && (echo<='9'))
cdata=echo-0x30;
count++;
tx_char0(echo); //send echo to screen
i_data=(i_data<<4);
i_data+=cdata;
}
}
}
if(count!=0 && echo==CR)
{
for (i=0;i<count;i++)
{
i_data1 = ((i_data)>>((unsigned char)(4*i))&0xf);
i_data2=1;
for(j=0;j<i;j++)
i_data2 *= 10;
*i_ptr+= i_data1*i_data2;
}
return(OK);
}
else
return(NOTOK);
}
/*******************************************************************
* Input word (hex) subroutine
* Description : Get the data(word) from user
* :
* :
* Example : N/A
* Input : N/A
* Output : *i_ptr
* Modify : N/A
* Return : status [OK for ('0'-'z') or NOTOK for others chars]
*********************************************************************/
byte input_word(unsigned int *i_ptr) //3046
{
byte echo,cdata,loop=ENABLE;
int count=0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -