|
晶振:11.0592MHZ
#include <iom169.h>
#include <stdio.h>
#include <inavr.h>
#define uint unsigned int
#define uchar unsigned char
#define SETBIT(x,y) x |= (1 << y)
#define CLRBIT(x,y) x &= ~(1 << y)
#define enable485_senddata SETBIT(PORTF,3)
uchar adress_data[8];
/*************************************************************
函数功能:USART_senddata
应用范围:
入口参数:
出口参数:
*************************************************************/
void USART_senddata(void)
{
uchar a;
enable485_senddata;
for(a=0;a<8;a++)
{
while(!(UCSR0A&0x20));
UDR0 = adress_data[a];
UCSR0A=UCSR0A&0x00;
}
}
void MCU_initial(void)
{
__enable_interrupt();//全局中断使能
DDRA = 0xFF;
DDRF = 0xF8;
TCCR1A = 0; //5us
TCCR1B = 1;
TCNT1 = 65480;
TIMSK1 = 0;
UBRR0H = 0x00; //SCI initial
UBRR0L = 0x47; //set bouad 9600
UCSR0B = 0x98; //RXCIE=1,TXCIE=0
UCSR0C = 0x06;
}
void timer1_delay(uint timer_delay)
{
uint number;
TCNT1 = 65480;//5us
for(number = timer_delay;number > 0;number --)
{
while((TIFR1&0x01==0));
CLRBIT(TIFR1,0);
TCNT1 = 65480;
}
}
/*************************************************************
函数功能: Initial
应用范围:
入口参数:
出口参数:
*************************************************************/
void Initial(void)
{
DDRE=0x04;
CLRBIT(PORTE,2);
timer1_delay(100);//500us
DDRE=0;
SETBIT(PORTE,2);
timer1_delay(10);//40us
while((PINE&0x04)==0x04);
timer1_delay(40);//200us
while(PINE&0x04==0);
timer1_delay(40);//200us
}
/*************************************************************
函数功能: Write
应用范围:
入口参数:
出口参数:
*************************************************************/
void Write(uchar write_data)
{
uchar j;
DDRE=DDRE|0x04;
for(j=0;j<8;j++)
{
DDRE=DDRE|0x04;
SETBIT(PORTE,2);
nop;
CLRBIT(PORTE,2);
timer1_delay(2);//15us
if(write_data%2==1)
{SETBIT(PORTE,2);}
else
{CLRBIT(PORTE,2);}
timer1_delay(10);//40us
write_data=write_data>>1;
}
}
/*************************************************************
函数功能: Read
应用范围:
入口参数:
出口参数:
*************************************************************/
uchar read(void)
{
uchar i,a;
a=0;
for(i=0;i<8;i++)
{
DDRE=DDRE|0x04;
SETBIT(PORTE,2);
timer1_delay(1);//5us
CLRBIT(PORTE,2);
timer1_delay(1);//5us
DDRE=DDRE&0xFB;
SETBIT(PORTE,2);
timer1_delay(2);//10us
if(PINE&0x04==0x04)
{a=a>>1;a=a+0x80;}
else
{a=a>>1;a=a+0x00;}
timer1_delay(9);//45us
SETBIT(PORTE,2);
timer1_delay(1);//5us
}
return(a);
}
void main (void)
{
uchar b;
MCU_initial();
for(;;)
{
Initial();//复位1820
Write(0x33);//读ROM命令
for(b=0;b<8;b++)
{adress_data=read();}//读64位序列号
USART_senddata();用串口将读出的数据发出去
}
}
延时用的是定时器1做延时
上述程序运行的结果是第一次读出的序列号为8个FF,从第二次开始就是8个0
请问为什么?谢谢! |
阿莫论坛20周年了!感谢大家的支持与爱护!!
一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。
|