|
楼主 |
发表于 2009-9-27 20:40:03
|
显示全部楼层
T4模板(usart.tt)
========================================================================================
<#@ template language="C#" HostSpecific="True" #>
<# AVR_USART host = (AVR_USART)Host; #>
#include <avr\io.h>
<# if(host.STDIO_W | host.STDIO_R) { #>
#include <stdio.h>
<# } #>
int uart_putchar(char c) {
if(c=='\n')
uart_putchar('\r');
loop_until_bit_is_set(UCSRA,UDRE);
UDR=c;
return 0;
}
int usart_getchar(void) {
loop_until_bit_is_set(UCSRA,RXC);
return UDR;
}
<# if(host.STDIO_W) { #>
static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
<# } #>
<# if(host.STDIO_R) { #>
static FILE mystdin = FDEV_SETUP_STREAM(NULL, usart_getchar, _FDEV_SETUP_READ);
<# } #>
void usart_init(void) {
UBRRH = 0x<#= host.UBRRH_VALUE.ToString("X2") #>;
UBRRL = 0x<#= host.UBRRL_VALUE.ToString("X2") #>;
#warning 波特率误差:<#= host.Error #>
<# if(host.USE_2X) { #>
UCSRA = UCSRA | (_BV(U2X);
<# } else { #>
UCSRA = UCSRA & ~_BV(U2X);
<# } #>
UCSRB= <#= host.UCSRB #>;
<# if(host.STDIO_W) { #>
stdout = &mystdout;
<# } #>
<# if(host.STDIO_R) { #>
stdin = &mystdin;
<# } #>
} |
|