simpleio._c

来自「chipcom公司CC1100无线模块底层驱动程序」· _C 代码 · 共 230 行

_C
230
字号
/****************************************************/
/* Application note                                 */
/* Reference design : CC1100 RF Test Board          */
/* File:        Simple.c                            */
/* Revision:  1.0                                   */
/* Description:                                     */
/* Microcontroller:ATmega48/48V                     */
/* Author:  Zcg, Field Applications Engineer, SunRay*/
/****************************************************/
#include <ctype.h>
#include "iom88v.h"
#include "macros.h"
#include "main.h"
#include "simpleio.h"
 
void putchar(INT8U ch)
{
    while ( !(UCSR0A & (1<<UDRE0)) )
	;
    UDR0=ch;
}

INT8U getchar(void)
{
	INT8U temp;

   	if ((UCSR0A & (1<<RXC0)))
   	{
		temp=UDR0;
		return temp;
   	}
	else
	{
		return 0;
	}
}

/****************************************************************************/
/* This routine outputs a string                                            */
/****************************************************************************/

void putstr(INT8U *str)
{
    while (*str!='\0')
    {
        putchar(*str++);
    }
}
int strlen(const char *s)
{
	int i = 0;

	for(; *s; s++)
		i++;

	return i;
}
/*---------------------printf and support routines ---------------------*/
/* print c count times */
void PutRepChar(char c, int count)
{
	while (count--) putchar(c);
}

/* put string reverse */
void PutStringReverse(char *s, int index)
{
    while ((index--) > 0) putchar(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 ' '
 */

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 '%':
                    putchar('%');
                    return(f);
                case '-': leftjust = TRUE;
                    break;
                case 'c':
                    {
                      if (leftjust) putchar(a & 0x7f);
                      if (fieldwidth > 0) PutRepChar(fill, fieldwidth - 1);
                      if (!leftjust) putchar(a & 0x7f);
                      return(f);
                    }
                case 's':
                    {
                        if (leftjust) putstr((char *) a);
                        if (fieldwidth > strlen((char *) a))
	                        PutRepChar(fill, fieldwidth - strlen((char *)a));
                        if (!leftjust) putstr((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)&param + sizeof(param))
#define vaArg(list, type) ((type *)(list += sizeof(type)))[-1]
#define vaEnd(list)

void printf(char *f, ...)       /* variable arguments */
{
	char *argP;

	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
			putchar(*f++);
	}
	vaEnd(argP);
}

⌨️ 快捷键说明

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