linhaimi 发表于 2009-9-27 20:35:29

初始化代码生成器(提供源代码)

http://cache.amobbs.com/bbs_upload782111/files_19/ourdev_486137.jpg
(原文件名:1.jpg)

http://cache.amobbs.com/bbs_upload782111/files_19/ourdev_486138.jpg
(原文件名:2.jpg)

http://cache.amobbs.com/bbs_upload782111/files_19/ourdev_486139.jpg
(原文件名:3.jpg)

可执行文件下载:点击此处下载 ourdev_486133.rar(文件大小:172K) (原文件名:Release.rar)(适用于Windows7/Vista(x86 & x64)
工程文件下载:点击此处下载 ourdev_486136.rar(文件大小:208K) (原文件名:UsartConfig.rar)

使用了微软的T4引擎来生产代码,扩展性很强,而且修改起来很方便。

有一个叫AVR_USART的类型,串口初始化参数的计算,参数的传递是通过WPF的数据绑定功能完成的。设定完成以后,将AVR_USART的实例交给T4引擎生产初始化代码。

标题虽然是168的,但是实际上是32的。

linhaimi 发表于 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;
<# } #>
}

zwcled 发表于 2015-4-18 22:19:48

我试过了,我是XP的系统,打不开。不知道是不是我的电脑有问题。
页: [1]
查看完整版本: 初始化代码生成器(提供源代码)