2008.02.29 -----2008.03.1
串口是经常用到的,以前都弄过,而且基本没出什么错.可昨天晚上到现在才弄好,一直不知道出什么原因,不能发送很接收数据,或者有时候是不正常.很郁闷,程序怎么看好象都没什么问题,可就是....
因为没有液晶在这里,有的一个NOKIA3310也听同学说好象坏了.所以才打算用串口显示,而且是标准的,这样在显示程序流程结果时候比较好.我呢,在AVR Studio配置fuse(熔丝位)的时候竟然忘记把那个Atmega103 Compatibility那项的钩去掉了.所以搞了N久.主要是因为我平时实验主要用的是mega16L,所以,没有这项.知道是知道的,但就是````说什么好呢,只能怪自己粗心,浪费了好多时间.
顺便说一下,因为是7.3728M的外部晶振,所以AVR Studio配置fuse里面选这个 :Ext. Crystal/Resonator High Freq.; Start-up time: 16K CK + 64 ms; [CKSEL=1111 SUT=11]
下面贴一下基本是照搬的代码.
/**/
/***********************************************
**** 名 称:AVRmega128 标准I/O流调试程序
****
**** 作 者:zhiyu
**** 编译器:WINAVR20070525
****
**** 参 考:<<AVR单片机GCC程序设计>>
**** 日 期:2008.03.01
****
**** 芯 片:M128L
**** 时钟源:外部7.2728M晶振
****
**** 结 果:测试成功
**** 问 题:当时配置熔丝位的时候,忘记把103的那个钩去掉,害得搞了一天!
***********************************************/


#include
<
avr
/
io.h
>
#include
<
avr
/
pgmspace.h
>
#include
<
stdio.h
>
#include
<
util
/
delay.h
>

char
g_aString[
100
];
//
读取字符串缓冲区
static
int
uart_putchar(
char
c, FILE
*
stream);
static
int
uart_getchar(FILE
*
stream);

static
FILE mystdio
=
FDEV_SETUP_STREAM(uart_putchar, uart_getchar,_FDEV_SETUP_RW);

//
向UART写一字节
static
int
uart_putchar(
char
c, FILE
*
stream)

...
{
if(c==' ')
uart_putchar(' ', stream);
loop_until_bit_is_set(UCSR0A,UDRE0);
UDR0=c;
return 0;
}

//
从UART读一字节
static
int
uart_getchar(FILE
*
stream)

...
{
loop_until_bit_is_set(UCSR0A,RXC0);
return UDR0;
}

//
初始化i/o
void
IoInit(
void
)

...
{
//UART初始化
UCSR0C=0x06;
UBRR0H=0;
UBRR0L=47; //9600 baud

UCSR0B=_BV(RXEN0)|_BV(TXEN0);/**//*(1<<RXCIE0)|(1<<TXCIE0)|*/
//i/o流UART连接
//fdevopen(usart_putchar,usart_getchar,0);
}


//
测试主程序
int
main(
void
)

...
{
int tmp;
IoInit();
stdout = stdin = &mystdio ;
_delay_ms(30); //延时一下显示就不会出什么错了,可能是没延时,一上电没来得及那么快,输出就有误吧

while(1)

...{
//测试1
printf("测试1[输入一字符串]: ");
scanf("%s",g_aString);
printf("您的输入为:%s ",g_aString);

//测试2:使用FLASH区的字符串
printf_P(PSTR("测试2[输入数字]: "));
scanf_P(PSTR("%d"),&tmp);
printf_P(PSTR("您的输入为:%d "),tmp);
}
}

测试是通过的,参考的书也是比较经典的.
弄好这个,以后就比较好调试了.但实际应用却不会要这样的输出的.
CC2420还没驱动起来.IEEE802.15.04标准只有PHY和MAC两层的东西,chipcon公司给的驱动程序范例还不是很难理解,但我这就不是很好弄.两块板接了40多根线.挺乱的.还有,本来mega128板子上还有几个按键的,都不怎么好用,很是失灵,消抖和延时也都有了,但`````究竟在哪里有误,明天继续做,把这个键盘搞掂.然后再看能不能驱动这两个CC2420无线通信!