📄 ex4_portb.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: portb.c REVISION 0.2
*
* DESCRIPTION: Application code is placed here.
* This module handles all tasks related to the PortB
* Program to control the PORTB module to contorl bit hi/lo
* to turn on/off LED
*
* 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.20 Kenny Lam Initial version
* 0.1 2002.09.12 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 "const.h"
#include "ut_extern.h"
#include "ex4_extern.h"
#include "GlobalVar.h"
#include "DP256Port.h"
/**********************************************
* PORTB bit test menu subroutine
* input : PORTB menu
* output : Corresponding sub-menu
***********************************************/
void portb_test_menu(void)
{
byte cdata,loop=ENABLE;
while(loop==ENABLE)
{
printf0("\n\t\tPORTB Test Menu\r");
printf0("\n\t0. Root Test Menu\r");
printf0("\n\t1. Configures PORTB In/Out\r");
printf0("\n\t2. Set PORTB LED on DP256\r");
printf0("\n\t9. Display DATA\r");
printf0("\n\r");
printf0("\nDPORTB PORTB Hour Min Sec\n\r");
do
{
cdata=menurx_char0(PORTB_PAGE);
if(cdata==CR || cdata=='9')
break;
}
while((cdata>'2') || (cdata<'0'));
//get input from user
switch(cdata)
{
case '0': loop=DISABLE;
break;
case '1': portb_config();
break;
case '2': portb_in_out();
break;
case '9': data_display();
break;
}
}
}
/**********************************************
* PORTB bit configuration subroutine
* modify : DDRB
***********************************************/
void portb_config()
{
byte cdata=0;
byte cmask=0xff;
int i,j;
// cdata = DDRB;
/*=== Display DDRB as In/Out ===*/
for (i=0;i<8;i++) //display DDRB
{
printf0("\nPORTB bit \r");
tx_char0(i+'0');
if (((DDRB>>i)&1)==0)
printf0(" configures as input\r");
else
// tx_char0(i+'0');
printf0(" configures as output\r");
}
/*=== Display option for user select ===*/
printf0("\n 0. Quit\r");
printf0("\n 1. Configures PORTB In/Out\n\r");
do //get input from user
{
if(cdata!=0) // do once
break;
cdata=rx_char0();
if (cdata=='1') //get input from user
{
for (i=0;i<8;i++) //display bit by bit
{
printf0("\nPORTB bit \r");
tx_char0(i+'0');
printf0(" (0 = In, 1 = Out, ESC = Quit)\r");
do
{
cdata=rx_char0();
if(cdata==ESC)
{
i=8;
break;
} //force to finish the for loop.
}
while((cdata>'1') || (cdata<'0'));
if(cdata=='0')
{
cmask=0xfe;
for(j=0;j<i;j++)
cmask= (cmask << 1) | ((cmask & 0x80)? 0x1 : 0x00);
DDRB = DDRB & cmask;
}
if(cdata=='1')
{
cmask=0x1;
for(j=0;j<i;j++)
cmask= (cmask << 1) | ((cmask & 0x80)? 0x1 : 0x00);
DDRB = DDRB | cmask;
}
}
}
}
while((cdata!='1') || (cdata!='0')); //get input from user
}
/**********************************************
* Toggle portb corresponding bit subroutine
* modify : PORTB
***********************************************/
void portb_in_out()
{
byte cdata,loop=ENABLE,ctemp;
printf0("\n PORTB On/Off Menu\n\r");
printf0(" Toggle the numeric key for the corresponding\n\r");
printf0(" PORTB Channel On/Off\n\r");
printf0("\n\r");
printf0(" 0. PORTB 0\n\r");
printf0(" 1. PORTB 1\n\r");
printf0(" 2. PORTB 2\n\r");
printf0(" 3. PORTB 3\n\r");
printf0(" 4. PORTB 4\n\r");
printf0(" 5. PORTB 5\n\r");
printf0(" 6. PORTB 6\n\r");
printf0(" 7. PORTB 7\n\r");
printf0(" Q. Exit to PORTB Test Menu\n\r");
printf0("\n\r");
printf0("Bt0 Bt1 Bt2 Bt3 Bt4 Bt5 Bt6 Bt7 LED0 LED1 LED2 LED3 LED4 LED5 LED6 LED7\n\r");
while(loop==ENABLE)
{
do
{
cdata=menurx_char0(PORTB_PAGE_DE); //get input from user
switch(cdata)
{
case '0': ctemp=0x1;
break;
case '1': ctemp=0x2;
break;
case '2': ctemp=0x4;
break;
case '3': ctemp=0x8;
break;
case '4': ctemp=0x10;
break;
case '5': ctemp=0x20;
break;
case '6': ctemp=0x40;
break;
case '7': ctemp=0x80;
break;
case 'q':
case 'Q': loop=DISABLE;
break;
}
}
while((cdata>'z') || (cdata<'0')); //get input from user
if((cdata<='7')&&(cdata>='0'))
PORTB ^= ctemp;
}
}
/**********************************************
* Display portb status subroutine
* output : DDRB, PORTB & hour, min and sec
***********************************************/
void portb_info()
{
hex_asc_sp(DDRB,3);
printf0(" \r");
hex_asc_sp(PORTB,3);
printf0(" \r");
hex_clk_dec(HourData);
printf0(" \r");
hex_clk_dec(MinData);
printf0(" \r");
hex_clk_dec(SecData);
tx_char0(CR);
}
/**********************************************
* Display portb & LED information subroutine
** output : PORTB bit and LED on/off
***********************************************/
void portb_info_detail()
{
int i;
for(i=0;i<8;i++)
{
if (((PORTB>>i)&1))
hex_asc_sp(1,2);
else
hex_asc_sp(0,2);
}
tx_char0(' ');
for(i=0;i<8;i++)
{
if (((PORTB>>i)&1))
printf0("0FF \r");
else
printf0("ON \r");
}
tx_char0(CR);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -