/*******************************************
File:HS.C
SoftWare Enviroment:Complier is ICC AVR7.25
HardWare:ATmega48
Clock:internal clock 8MHZ
Data:2010/6
Function:Needle hit strenth test Diver
Remarks:
/******************************************/
#include <iom48v.h> //IC type
#include <macros.h>
#include <stdio.h>
#include<eeprom.h>
//#pragma interrupt_handler INT0I:2
//#pragma interrupt_handler INT1I:3
//#pragma interrupt_handler PCINT0I:4
//#pragma interrupt_handler PCINT1I:5
#pragma interrupt_handler USART0_RX:19
#pragma interrupt_handler Timer1Intrrupt:14
#pragma interrupt_handler Timer0Intrrupt:17
//#pragma interrupt_handler USART0_TX:21
//#define MCLK 22118400 //IC working clock
#define MCLK 11059200 //IC working clock
#pragma data:code
//const unsigned char Sheet[30]={3810,3810,2280,2280,1550,1550,1095,1095,790,790,580,580,440,440,345,345,272,272,222,222,175,175,145,145,125,125,113,113,100,100};
#pragma data:data
#define Y0 0
#define Y1 1
#define Y2 2
#define Y3 3
#define Y4 4
#define Y5 5
#define Y6 6
#define Y7 7
#define BufferSize 200
#define DataIn 0
#define DataOut 1
#define ModbusNormalReceive 0
#define ModbusCommandStart 1
#define ModbusReadRegister 2
#define ModbusWriteRegister 3
#define ModbusReadIOState 4
unsigned char PWrite=1;
unsigned char Pread=0;
unsigned char ReceiveBuffer[200];
struct Command
{
unsigned char HardAdress;
unsigned char Write;
unsigned char Type;
unsigned char Lenth;
unsigned char Buffer[20];
}command;
struct SendCommand
{
unsigned char Lenth;
unsigned char Buffer[20];
}sendcommand;
struct CRC16
{
unsigned char Type;
unsigned char Result;
unsigned int Vaue;
unsigned char Lenth;
unsigned char Buffer[20];
}crc16;
struct TimeDelayParameter
{
unsigned int Adress;
unsigned int Time[20];
unsigned int ERRomAddress[160];
}timedelayparameter;
struct RunP
{
unsigned char IOState;
unsigned int TimeCount;
unsigned char StartF;
unsigned char CountStep;
unsigned char SYSState;
}runp;
unsigned int SYSTime=0;
/*******************************************
FUnction Name: Delayms
Name:Delay ms(8M)
Parameter:
Return Value:
/********************************************/
void Delayms(unsigned int MS)
{
unsigned int i,j;
for( i=0;i<MS;i++)
for(j=0;j<1584;j++);
}
/*******************************************
Function Name: Timer0Init
Function:USART initialization
Parameter:
Return value:
********************************************/
void GetParameter(void)
{
unsigned char i,j,k,Temp1,Temp2;
unsigned int Da[4],T;
for(i=0;i<40;i++)
{
EEPROM_READ(timedelayparameter.ERRomAddress[i], Temp1);
EEPROM_READ(timedelayparameter.ERRomAddress[i+1], Temp2);
Da[0]=Temp1|(Temp2<<8);
EEPROM_READ(timedelayparameter.ERRomAddress[i+40], Temp1);
EEPROM_READ(timedelayparameter.ERRomAddress[i+41], Temp2);
Da[1]=Temp1|(Temp2<<8);
EEPROM_READ(timedelayparameter.ERRomAddress[i+80], Temp1);
EEPROM_READ(timedelayparameter.ERRomAddress[i+81], Temp2);
Da[2]=Temp1|(Temp2<<8);
EEPROM_READ(timedelayparameter.ERRomAddress[i+120], Temp1);
EEPROM_READ(timedelayparameter.ERRomAddress[i+121], Temp2);
Da[3]=Temp1|(Temp2<<8);
for(j=0;j<3;j++)
{
for(k=0;k<3-j;k++)
{
if(Da[k]<Da[k+1])
{
T=Da[k];
Da[k]=Da[k+1];
Da[k+1]=T;
}
}
}
if(Da[0]!=Da[3])
{
Temp1=(unsigned char)Da[2];
EEPROM_WRITE(timedelayparameter.ERRomAddress[i],Temp1);
Temp1=(unsigned char)(Da[2]>>8);
EEPROM_WRITE(timedelayparameter.ERRomAddress[i+1],Temp1);
Temp1=(unsigned char)Da[2];
EEPROM_WRITE(timedelayparameter.ERRomAddress[i+40],Temp1);
Temp1=(unsigned char)(Da[2]>>8);
EEPROM_WRITE(timedelayparameter.ERRomAddress[i+41],Temp1);
Temp1=(unsigned char)Da[2];
EEPROM_WRITE(timedelayparameter.ERRomAddress[i+80],Temp1);
Temp1=(unsigned char)(Da[2]>>8);
EEPROM_WRITE(timedelayparameter.ERRomAddress[i+81],Temp1);
Temp1=(unsigned char)Da[2];
EEPROM_WRITE(timedelayparameter.ERRomAddress[i+120],Temp1);
Temp1=(unsigned char)(Da[2]>>8);
EEPROM_WRITE(timedelayparameter.ERRomAddress[i+121],Temp1);
}
timedelayparameter.Time[i/2]=Da[2];
i++;
}
}
void GetParameterReadRegiser(unsigned char Adress)
{
unsigned char i,j,k,Temp1,Temp2;
unsigned int Da[4],T;
for(i=0;i<40;i++)
{
if(i==Adress)
{
i++;
continue;
}
EEPROM_READ(timedelayparameter.ERRomAddress[i], Temp1);
EEPROM_READ(timedelayparameter.ERRomAddress[i+1], Temp2);
Da[0]=Temp1|(Temp2<<8);
EEPROM_READ(timedelayparameter.ERRomAddress[i+40], Temp1);
EEPROM_READ(timedelayparameter.ERRomAddress[i+41], Temp2);
Da[1]=Temp1|(Temp2<<8);
EEPROM_READ(timedelayparameter.ERRomAddress[i+80], Temp1);
EEPROM_READ(timedelayparameter.ERRomAddress[i+81], Temp2);
Da[2]=Temp1|(Temp2<<8);
EEPROM_READ(timedelayparameter.ERRomAddress[i+120], Temp1);
EEPROM_READ(timedelayparameter.ERRomAddress[i+121], Temp2);
Da[3]=Temp1|(Temp2<<8);
for(j=0;j<3;j++)
{
for(k=0;k<3-j;k++)
{
if(Da[k]<Da[k+1])
{
T=Da[k];
Da[k]=Da[k+1];
Da[k+1]=T;
}
}
}
if(Da[0]!=Da[3])
{
Temp1=(unsigned char)Da[2];
EEPROM_WRITE(timedelayparameter.ERRomAddress[i],Temp1);
Temp1=(unsigned char)(Da[2]>>8);
EEPROM_WRITE(timedelayparameter.ERRomAddress[i+1],Temp1);
Temp1=(unsigned char)Da[2];
EEPROM_WRITE(timedelayparameter.ERRomAddress[i+40],Temp1);
Temp1=(unsigned char)(Da[2]>>8);
EEPROM_WRITE(timedelayparameter.ERRomAddress[i+41],Temp1);
Temp1=(unsigned char)Da[2];
EEPROM_WRITE(timedelayparameter.ERRomAddress[i+80],Temp1);
Temp1=(unsigned char)(Da[2]>>8);
EEPROM_WRITE(timedelayparameter.ERRomAddress[i+81],Temp1);
Temp1=(unsigned char)Da[2];
EEPROM_WRITE(timedelayparameter.ERRomAddress[i+120],Temp1);
Temp1=(unsigned char)(Da[2]>>8);
EEPROM_WRITE(timedelayparameter.ERRomAddress[i+121],Temp1);
}
timedelayparameter.Time[i/2]=Da[2];
i++;
}
}
/*******************************************
Function Name: Board_init
Function:I/OPort initialization
Parameter:
Return value:
********************************************/
void Board_init()
{
unsigned int i;
DDRB=0x1e;
PORTB=0x01;
DDRC=0x2e;
PORTC=0x31;
DDRD=0x62;
PORTD=0xff;
timedelayparameter.Adress=0;
for(i=0;i<20;i++)
{
timedelayparameter.Time[i]=0;
}
for(i=0;i<160;i++)
{
timedelayparameter.ERRomAddress[i]=i;
}
runp.StartF=0;
runp.CountStep=0;
}
/*******************************************
Function Name: Timer0Init
Function:USART initialization
Parameter:
Return value:
********************************************/
void Timer1Init(void)
{
TCCR1A=0;
TCCR1B=0x00;
TCCR0A=0;
TCCR0B=0x00;
TIMSK0|=BIT(TOIE0); //Timer0 Overflow Interrupt Enable
TIMSK1|=BIT(TOIE1); //Timer0 Overflow Interrupt Enable
SREG|=BIT(7); //enable Global Interrupt
}
/*******************************************
Function Name: Initial_INT
Function: External Interrupt Initialization
Parameter:
Return Value:
/********************************************/
void Initial_INT(void)
{
EICRA=0x05;// edge change generate an interrupt
EIMSK|=BIT(INT1)|BIT(INT0);
PCICR|=BIT(PCIE0)|BIT(PCIE1);
PCMSK1=0x01;
PCMSK0=0x01;
SREG=(1<<7); //enable globle interrupt
}
/*******************************************
Function Name: Uart_init
Function:USART initialization
Parameter:
Return value:
********************************************/
void Uart_init(unsigned int baud)
{
baud=MCLK/16/baud-1; //Band rate
UCSR0B=0x00;
UCSR0A=0x00; //clear control register
//UCSR0C=(1<<UMSEL01)|(0<<UPM00)|(3<<UCSZ00);