⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 board.c

📁 IAR AVR 中用串口格式化打印调试的示例程序
💻 C
字号:
//包含所需头文件
#include "config.h"

/********************************************************************
** 系统初始化功能函数
********************************************************************/

//端口初始化
void port_init(void)
{
    SFIOR &= ~ BIT(PUD);
	PORTA = 0x00;
	DDRA  = 0x00;
	PORTB = 0x00;
	DDRB  = 0x00;
	PORTC = 0x00;
	DDRC  = 0x00;
}


//定时器T0初始化
void timer0_init(void)
{
	TCCR0  = 0x00;          // 停止定时器
	TCNT0  = 0x00;          // 初始值
	OCR0   = TIMER0_DATA;   // 比较匹配值
	//OCR0   = 0x25;   // 比较匹配值
	TIMSK |= BIT(OCIE0);    // 比较匹配中断允许
	TCCR0  = 0x0F;          // 启动定时器 Fosc/1024
}


// 使用串口UART

//串口通信初始化
void uart_init(void)
{
	UCSRB = 0x00;//禁止中断
	UCSRA = 0x00;
	UCSRC = 0x06;
	UBRRL = UBRR_VALUE & 0xFF;
	UBRRH = UBRR_VALUE / 0xFF;
	UCSRB = BIT(RXEN) | BIT(TXEN) | BIT(RXCIE);
    UDR   = 0x00;
}


uint8 Uart_SendChar(uint8 iData)
{
	while (!(UCSRA & BIT(UDRE)));  /* Wait until transmit is complete */
	UDR = iData;

    return iData;
}

void Uart_SendString(uint8 *string)
{
    char temp;

    temp = *string++;

    do
    {
		Uart_SendChar(temp);
        temp = *string;
        string ++;
    } while(temp);
}

int uart_printf(const char *fmt, ...)
{
    va_list arg;
    int cnt;

    char buffer[30];

    va_start(arg, fmt);
    cnt = vsprintf(buffer, fmt, arg);
    va_end(arg);

    Uart_SendString((uint8 *)buffer);

    return cnt;
}


/********************************************************************
** 系统初始化总函数
********************************************************************/

void init_devices(void)
{
	__disable_interrupt();          // 禁止所有中断
	//wdt_disable();
	MCUCR  = 0x00;

	port_init();
	timer0_init();
	uart_init();
	__enable_interrupt();          // 开全局中断
}


uint16 volatile Delay_Timer;

void Delay_10ms(uint16 Delay)
{
    Delay_Timer = Delay;

    do{
        NOP();
        __watchdog_reset();
        //sleep_mode();
        NOP();
    } while(Delay_Timer > 0);
}

⌨️ 快捷键说明

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