?? 44blib.c
字號(hào):
{
char c;
puts(" [y/n] ");
while(1) {
c = getch();
if((c=='y')||(c=='Y')||(c=='n')||(c=='N'))
break;
}
putch(c);
putch('\n');
return c&1; //y&Y are odd, n&N are even
}
/****************************************************************************
【功能說明】以標(biāo)準(zhǔn)輸出格式向串口輸出各種信息
****************************************************************************/
#if 1
/*---------------------printf and support routines ---------------------*/
/* print c count times */
void PutRepChar(char c, int count)
{
while (count--) Uart_SendByte(c);
}
/* put string reverse */
void PutStringReverse(char *s, int index)
{
while ((index--) > 0) Uart_SendByte(s[index]);
}
/*-------------------------------------------------------------------------*/
/*
prints value in radix, in a field width width, with fill
character fill
if radix is negative, print as signed quantity
if width is negative, left justify
if width is 0, use whatever is needed
if fill is 0, use ' '
*/
#define FALSE 0
#define TRUE 1
static void PutNumber(int value, int radix, int width, char fill)
{
char buffer[40];
int bi = 0;
int unsigned uvalue;
short int digit;
short int left = FALSE;
short int negative = FALSE;
if (fill == 0) fill = ' ';
if (width < 0) {
width = -width;
left = TRUE;
}
if (width < 0 || width > 80) width = 0;
if (radix < 0) {
radix = -radix;
if (value < 0) {
negative = TRUE;
value = -value;
}
}
uvalue = value;
do {
if (radix != 16) {
digit = uvalue % radix ;
uvalue = uvalue / radix ;
}
else {
digit = uvalue & 0xf;
uvalue = uvalue >> 4;
}
buffer[bi] = digit + ((digit <= 9) ? '0' : ('A' - 10));
bi++;
if (uvalue != 0) {
if ((radix==10)&&((bi==3)||(bi==7)||(bi==11)|(bi==15))) {
buffer[bi++]=',';
}
}
} while (uvalue != 0);
if (negative) {
buffer[bi] = '-';
bi += 1;
}
if (width <= bi) PutStringReverse(buffer, bi);
else {
width -= bi;
if (!left) PutRepChar(fill, width);
PutStringReverse(buffer, bi);
if (left) PutRepChar(fill, width);
}
}
/*-------------------------------------------------------------------------*/
static char *FormatItem(char *f, int a)
{
char c;
int fieldwidth = 0;
int leftjust = FALSE;
int radix = 0;
char fill = ' ';
if (*f == '0') fill = '0';
while ((c = *f++)!=0) {
if (c >= '0' && c <= '9') {
fieldwidth = (fieldwidth * 10) + (c - '0');
}
else switch (c) {
case '\000': return(--f);
case '%': Uart_SendByte('%');
return(f);
case '-': leftjust = TRUE;
break;
case 'c': {
if (leftjust) Uart_SendByte(a & 0x7f);
if (fieldwidth > 0) PutRepChar(fill, fieldwidth - 1);
if (!leftjust) Uart_SendByte(a & 0x7f);
return(f);
}
case 's': {
if (leftjust) Uart_SendString((char *) a);
if (fieldwidth > strlen((char *) a))
PutRepChar(fill, fieldwidth - strlen((char *)a));
if (!leftjust) Uart_SendString((char *) a);
return(f);
}
case 'd':
case 'i': radix = -10;break;
case 'u': radix = 10;break;
case 'x': radix = 16;break;
case 'X': radix = 16;break;
case 'o': radix = 8;break;
default : radix = 3;break;/* unknown switch! */
}
if (radix) break;
}
if (leftjust) fieldwidth = -fieldwidth;
PutNumber(a, radix, fieldwidth, fill);
return(f);
}
#define vaStart(list, param) list = (char*)((int)¶m + sizeof(param))
#define vaArg(list, type) ((type *)(list += sizeof(type)))[-1]
#define vaEnd(list)
void Uart_Printf(char *f, ...) /* variable arguments */
{
// U32 mode ;
char *argP;
/* disable IRQs and FIQs */
// mode = uHALir_ReadMode() ;
// uHALir_WriteMode(mode | NoFIQ | NoIRQ) ;
vaStart(argP,f); /* point at the end of the format string */
while (*f) { /* this works because args are all ints */
if (*f == '%') f = FormatItem(f + 1, vaArg(argP, int));
else Uart_SendByte(*f++);
}
vaEnd(argP);
/* restore the previous mode */
// uHALir_WriteMode(mode) ;
}
#else
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
//If you don't use vsprintf(), the code size is reduced very much.
typedef int *__va_list[1];
int vsprintf(char * /*s*/, const char * /*format*/, __va_list /*arg*/);
//if you don't use vsprintf(), the code size is reduced very much.
void Uart_Printf(char *fmt,...)
{
va_list ap;
char string[256];
va_start(ap,fmt);
vsprintf(string,fmt,ap);
Uart_SendString(string);
va_end(ap);
}
#endif
//***************************************************************************
/****************************************************************************
【功能說明】蜂鳴器鳴叫time個(gè)100us
****************************************************************************/
void Beep(unsigned int time)
{
rPDATE = (rPDATE | 0x08);
Delay(time); //延時(shí)若干個(gè)100us
rPDATE = (rPDATE & 0x1f7);
}
//***************************************************************************
/****************************************************************************
【功能說明】檢測四個(gè)按鍵,有任何一個(gè)按鍵按下就讓蜂鳴器鳴叫,否則不鳴叫
****************************************************************************/
void Key_Speaker(void)
{
unsigned int m;
m = rPDATG;
if((m & 0xf8) < 0xf8) {rPDATE = (rPDATE | 0x08);}
else {rPDATE = (rPDATE & 0x1f7);}
}
//***************************************************************************
/****************************************************************************
【功能說明】四個(gè)LED 點(diǎn)亮/熄滅狀態(tài)設(shè)置(LedStatus低四位電平高低對應(yīng)著四個(gè)LED亮/熄)
****************************************************************************/
void Led_Set(int LedStatus)
{
if(LedStatus&1) //PE7狀態(tài)設(shè)置
rPDATE=rPDATE&0x17f;
else
rPDATE=rPDATE|0x80;
if(LedStatus&2) //PE6狀態(tài)設(shè)置
// rPDATE=rPDATE&0x1bf;
rPDATC |= 2;
else
// rPDATE=rPDATE|0x40;
rPDATC &= ~2;
if(LedStatus&4) //PE5狀態(tài)設(shè)置
// rPDATE=rPDATE&0x1df;
rPDATC |= 4;
else
// rPDATE=rPDATE|0x20;
rPDATC &= ~4;
if(LedStatus&8) //PE4狀態(tài)設(shè)置
// rPDATE=rPDATE&0x1ef;
rPDATC |= 8;
else
// rPDATE=rPDATE|0x10;
rPDATC &= ~8;
}
//***************************************************************************
/****************************************************************************
【功能說明】LED來回閃爍顯示
****************************************************************************/
void Led_Disp(void)
{
rPDATE = (rPDATE | 0x20); //蜂鳴器開始鳴叫
Led_Set(0x08); //LED點(diǎn)亮/熄滅狀態(tài)設(shè)置
Delay(800); //延時(shí)若干個(gè)100us
Led_Set(0x04); //LED點(diǎn)亮/熄滅狀態(tài)設(shè)置
Delay(800); //延時(shí)若干個(gè)100us
Led_Set(0x02); //LED點(diǎn)亮/熄滅狀態(tài)設(shè)置
Delay(800); //延時(shí)若干個(gè)100us
// Led_Set(0x01); //LED點(diǎn)亮/熄滅狀態(tài)設(shè)置
// Delay(800); //延時(shí)若干個(gè)100us
Led_Set(0x02); //LED點(diǎn)亮/熄滅狀態(tài)設(shè)置
Delay(800); //延時(shí)若干個(gè)100us
Led_Set(0x04); //LED點(diǎn)亮/熄滅狀態(tài)設(shè)置
Delay(800); //延時(shí)若干個(gè)100us
Led_Set(0x08); //LED點(diǎn)亮/熄滅狀態(tài)設(shè)置
Delay(800); //延時(shí)若干個(gè)100us
Led_Set(0x00); //LED點(diǎn)亮/熄滅狀態(tài)設(shè)置
rPDATE = (rPDATE & ~0x20); //蜂鳴器停止鳴叫
}
//***************************************************************************
/****************************************************************************
【功能說明】定時(shí)器啟動(dòng)
****************************************************************************/
void Timer_Start(int divider) //0:16us,1:32us 2:64us 3:128us
{
rWTCON=((MCLK/1000000-1)<<8)|(divider<<3);
rWTDAT=0xffff;
rWTCNT=0xffff;
// 1/16/(65+1),nRESET & interrupt disable
rWTCON=((MCLK/1000000-1)<<8)|(divider<<3)|(1<<5);
}
//***************************************************************************
/****************************************************************************
【功能說明】定時(shí)器停止
****************************************************************************/
int Timer_Stop(void)
{
// int i;
rWTCON=((MCLK/1000000-1)<<8);
return (0xffff-rWTCNT);
}
//***************************************************************************
/****************************************************************************
【功能說明】鎖相環(huán)設(shè)置,修改系統(tǒng)主頻
Fout = (8 + M_DIV) * Fin / [ (2+P_DIV) * (2^S_DIV) ]
****************************************************************************/
void ChangePllValue(int mdiv,int pdiv,int sdiv)
{
int i = 1;
rPLLCON = (mdiv << 12) | (pdiv << 4) | sdiv;
while(sdiv--)
i *= 2;
MCLK = (EXT_OSC_CLK*(mdiv+8))/((pdiv+2)*i);
}
//***************************************************************************
/****************************************************************************
【功能說明】
****************************************************************************/
void * malloc(unsigned nbyte)
/*Very simple; Use malloc() & free() like Stack*/
//void *mallocPt=Image$$RW$$Limit;
{
void *returnPt=mallocPt;
mallocPt= (int *)mallocPt+nbyte/4+((nbyte%4)>0); //to align 4byte
if( (int)mallocPt > HEAPEND )
{
mallocPt=returnPt;
return NULL;
}
return returnPt;
}
//***************************************************************************
/****************************************************************************
【功能說明】
****************************************************************************/
void free(void *pt)
{
mallocPt=pt;
}
//***************************************************************************
/****************************************************************************
【功能說明】
****************************************************************************/
void Cache_Flush(void)
{
int i,saveSyscfg;
saveSyscfg=rSYSCFG;
rSYSCFG=SYSCFG_0KB;
for(i=0x10004000;i<0x10004800;i+=16)
{
*((int *)i)=0x0;
}
rSYSCFG=saveSyscfg;
}
//***************************************************************************
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -