#include<iom2560v.h>
#include<eeprom.h>
#include<AVR_PORT_BIT.H>
//#define SIZE 25
//开始命令
#define CMD_START 0x11
//读命令
#define CMD_READ 0x22
//写命令
#define CMD_WRITE 0x33
#define CMD_SS 0x44
//停止命令
#define CMD_STOP 0x55
unsigned char index=0,rx_buffer[25],Back_buf[18]; //接收缓冲区
unsigned char Uart_Flag=0;
unsigned char K=0,sig=0;
unsigned int Num=0,M=0,n_T3=0;
unsigned int n_T0=0,n_T1=0,n_T4=0,n_T5=0,n4=0;
void f_uart1Init(void)
{
UBRR1L=0x33;
UBRR1H=0x00; //波特率设置为9600
UCSR1B=(1<<RXEN1)|(1<<TXEN1)|(1<<RXCIE1);//使能接收和发送
UCSR1C=(1<<UCSZ11)|(1<<UCSZ10);//8位数据+1位停止
}
void PortInit()
{
DDRA=0xFF;
DDR_PB7=0;
DDR_PG2=1;
PORTA=0x00;
_PB7=1;
_PG2=0;
}
/*
void f_putchar(unsigned char c)
{
while(!(UCSR1A&(1<<UDRE1)));
UDR1=c;
}
unsigned char f_getchar1(void)
{
while(!(UCSR1A&(1<<RXC1)));
return UDR1;
}
*/
#pragma interrupt_handler Usart1_rx:37
void Usart1_rx(void)
{
unsigned char temp=UDR1;
unsigned int *pptr=(unsigned int *)rx_buffer;
unsigned int n=0,m=0;
if(Uart_Flag)
{
rx_buffer[++index]=temp;
if(index==30)
{
Uart_Flag=0;
index=0;
EEPROMWriteBytes(0x00,rx_buffer,31);//写进EEPROM
//for(m=0;m<1000;m++);
/*
EEPROMReadBytes(0x00,rx_buffer+1,24);//从EEPROM里读出,
rx_buffer[0]=0x33;
for(index=0;index<25;index++)
{
while(!(UCSR1A&(1<<UDRE1)));
UDR1=rx_buffer[index];//与发的值进行对比校验
for(;n<1000;n++);
n=0;
}
index=0;*/
//T11--定时器1
Back_buf[0]=(65536-(rx_buffer[3]+rx_buffer[2]*256+rx_buffer[1]*65536))%256;//低字节
Back_buf[1]=(65536-(rx_buffer[3]+rx_buffer[2]*256+rx_buffer[1]*65536))/256;//高字节
//T12--定时器1
Back_buf[2]=(65536-(rx_buffer[6]+rx_buffer[5]*256+rx_buffer[4]*65536))%256;//低字节
Back_buf[3]=(65536-(rx_buffer[6]+rx_buffer[5]*256+rx_buffer[4]*65536))/256;//高字节
//T--定时器5
Back_buf[4]=(65536-(rx_buffer[9]+rx_buffer[8]*256+rx_buffer[7]*65536))%256;//低字节
Back_buf[5]=(65536-(rx_buffer[9]+rx_buffer[8]*256+rx_buffer[7]*65536))/256;//高字节
//T21--定时器3
Back_buf[6]=(65536-(rx_buffer[12]+rx_buffer[11]*256+rx_buffer[10]*65536))%256;//低字节
Back_buf[7]=(65536-(rx_buffer[12]+rx_buffer[11]*256+rx_buffer[10]*65536))/256;//高字节
//T22--定时器3
Back_buf[8]=(65536-(rx_buffer[15]+rx_buffer[14]*256+rx_buffer[13]*65536))%256;//低字节
Back_buf[9]=(65536-(rx_buffer[15]+rx_buffer[14]*256+rx_buffer[13]*65536))/256;//高字节
//T31--定时器4
Back_buf[10]=(65536-(rx_buffer[18]+rx_buffer[17]*256+rx_buffer[16]*65536))%256;//低字节
Back_buf[11]=(65536-(rx_buffer[18]+rx_buffer[17]*256+rx_buffer[16]*65536))/256;//高字节
//T32--定时器4
// Back_buf[12]=(65536-(rx_buffer[14]+rx_buffer[13]*256)*8/64)%256;//低字节
// Back_buf[13]=(65536-(rx_buffer[14]+rx_buffer[13]*256)*8/64)/256;//高字节
//N
Num=rx_buffer[24]+rx_buffer[23]*256+rx_buffer[22]*65536;//脉冲数
//K
K=rx_buffer[27]+rx_buffer[26]*256+rx_buffer[25]*65536;//点火方式1或2
//T41--定时器0
//Back_buf[14]=(65536-(rx_buffer[27]+rx_buffer[26]*256+rx_buffer[25]*65536))%256;//低字节
//Back_buf[15]=(65536-(rx_buffer[27]+rx_buffer[26]*256+rx_buffer[25]*65536))/256;//高字节
//T42--定时器0
// Back_buf[16]=(65536-(rx_buffer[30]+rx_buffer[29]*256+rx_buffer[28]*65536))%256;//低字节
//Back_buf[17]=(65536-(rx_buffer[30]+rx_buffer[39]*256+rx_buffer[28]*65536))/256;//高字节
//M
M=rx_buffer[30]+rx_buffer[29]*256+rx_buffer[28]*65536;//周期数M
//T32--定时器4
n_T4=2*Num;
Back_buf[12]=(65536-(rx_buffer[21]+rx_buffer[20]*256+rx_buffer[19]*65536)/2)%256;//低字节
Back_buf[13]=(65536-(rx_buffer[21]+rx_buffer[20]*256+rx_buffer[19]*65536)/2)/256;//高字节
}
}
else
{
switch(temp)
{
case CMD_WRITE: //写EEPROM
Uart_Flag=1;
break;
case CMD_START:
EEPROMReadBytes(0x00,rx_buffer,25);
rx_buffer[0]=0x22;
for(index=0;index<25;index++)
{
while(!(UCSR1A&(1<<UDRE1)));
UDR1=rx_buffer[index];
for(;n<1000;n++);
n=0;
}
index=0;
break;
case CMD_SS:
//sig=1;
break;
case CMD_STOP:
TCCR1B=0x00;
TCCR2B=0x00;
TCCR3B=0x00;
TCCR4B=0x00;
TCCR5B=0x00;
_PA7=0;
n_T0=0;n_T1=0;n_T4=0;n_T5=0;n4=0;
n_T3=0;sig=0;
break;
// case CMD_READ://读数据
// break;
default:
break;
}
}
}
void produce_impulse()
{
//SREG=0x80;
//T0初始化--定时T41,T42
//TCCR2B=0x06;//256分频
TCNT2=0x64;//只给定时器0赋低字节的数
//OCR2B=0xFF;
//TIFR2=0x00;
//T1初始化--定时T11
//TCCR1B=0x02; //8分频
TCNT1H=Back_buf[1];
TCNT1L=Back_buf[0];
//T3初始化--定时T21
//TCCR3B=0x02; //8分频
TCNT3H=Back_buf[7];
TCNT3L=Back_buf[6];
//T4初始化--定时T31
//TCCR4B=0x02; //8分频
TCNT4H=Back_buf[11];
TCNT4L=Back_buf[10];
//T5初始化--定时T
//TCCR5B=0x02; //8分频
TCNT5H=Back_buf[5];
TCNT5L=Back_buf[4];
//启动定时器
_PA6=1;
TIMSK2=0x01;//启动定时器0
TIMSK1=0x01;//启动定时器1
TIMSK3=0x01;//启动定时器3
TIMSK4=0x01;//启动定时器4
TIMSK5=0x01;//启动定时器5
TCCR2B=0x06;
TCCR1B=0x02;
TCCR3B=0x02;
TCCR4B=0x02;
TCCR5B=0x02;
/* _PA6=1;
for(n_T0=0;n_T0<2000;n_T0++);
_PA6=0;*/
}
//T0中断服务函数
/*#pragma interrupt_handler Service_T0:24
void Service_T0()
{
TIFR0=0x00;
_PA6=0;
TIMSK0=0x00;//关定时器1
}*/
#pragma interrupt_handler Service_T2:16
void Service_T2()
{
//TIFR0=0x00;
TCCR2B=0x00;
// TIMSK2=0x00;//关定时器1
_PA6=0;
}
#pragma interrupt_handler Service_T1:21
void Service_T1()
{
if(n_T1==0)
{
_PA3=1;
TCNT1H=Back_buf[3];
TCNT1L=Back_buf[2];
n_T1++;
}
else
{
_PA3=0;
//TIMSK1=0x00;//关定时器1
TCCR1B=0x00;
n_T1=0;
}
}
#pragma interrupt_handler Service_T4:46
void Service_T4()
{
if(n_T4!=0)
{
_PA5=~_PA5;
//TCCR4B=0x02;
TCNT4H=Back_buf[13];
TCNT4L=Back_buf[12];
n_T4--;
}
else
{
//TIMSK4=0x00;//关定时器4
TCCR4B=0x00;
n_T4=2*Num;
}
}
#pragma interrupt_handler Service_T3:36
void Service_T3()
{
if(n_T3==0)
{
_PA4=1;
TCNT3H=Back_buf[9];
TCNT3L=Back_buf[8];
n_T3++;
}
else
{
_PA4=0;
TCCR3B=0x00;
//TIMSK3=0x00;//关定时器3
n_T3=0;
}
}
#pragma interrupt_handler Service_T5:51
void Service_T5()
{
if(K==1)
_PA5=0;
else _PA5=1;
//T0初始化--定时T41,T42
// TCCR0B=0x02;//8分频
//TCNT0=Back_buf[14];//只给定时器0赋低字节的数
//T1初始化--定时T11
TCNT1H=Back_buf[1];
TCNT1L=Back_buf[0];
//T3初始化--定时T21
//TCCR3B=0x02; //8分频
TCNT3H=Back_buf[7];
TCNT3L=Back_buf[6];
//T4初始化--定时T31
//TCCR4B=0x02; //8分频
TCNT4H=Back_buf[11];
TCNT4L=Back_buf[10];
//T5初始化--定时T
//TCCR5B=0x02; //8分频
TCNT5H=Back_buf[5];
TCNT5L=Back_buf[4];
//启动定时器
//_PA6=1;
//TIMSK2=0x01;//启动定时器0
// TIMSK1=0x01;//启动定时器1
//TIMSK3=0x01;//启动定时器3
//TIMSK4=0x01;//启动定时器4
//TIMSK5=0x01;//启动定时器5
//TCCR2B=0x06;
TCCR1B=0x02;
TCCR3B=0x
评论0