/*******************************************
File:HS.C
SoftWare Enviroment:Complier is ICC AVR7.25
HardWare:ATmega16
Clock:internal clock 11.0592MHZ
Data:2012-4-20
Function:上盖板测量
Remarks:
/******************************************/
#include <iom32v.h> //IC type
#include <macros.h>
#include <stdio.h>
#include <Eeprom.h>
#include "DMulti.h"
unsigned char DisplayBuffer[8];
unsigned char TimeDelayParameter[20]={0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2};
unsigned char TimePositionFlag[20]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
unsigned int MotorParameter[24]={0};
unsigned char SaveMotorParameterAddress[24]={50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73};
unsigned int *P;
unsigned char SavePosiotionAddress=100;
unsigned char PosiontPrameter;
/*******************************************
Function Name: Delayus
Function: Delay us (11.0592M)
Remarks: this function isn't accurate,the bigger us the more accurate of the delay time
Return value:
/********************************************/
void Delayus(unsigned int US)
{
unsigned int i;
US=US*3/2;
for( i=0;i<US;i++);
}
/*******************************************
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<1577;j++);
}
/*******************************************
Function Name:LCDDataIn
Function:
Parameter:
Return value:
/********************************************/
void U595OutData(unsigned char ByteData,unsigned char UT)
{
unsigned char Count,Temp;
PORTA&= ~(BIT(SerialCLK));
Delayus(1);
for(Count=0;Count<8;Count++)
{
Temp=PORTA;
Temp&=0x7f;
PORTA=(Temp|(ByteData&0x80));
ByteData<<=1;
Delayus(1);
PORTA|= BIT(SerialCLK);
Delayus(1);
PORTA&= ~(BIT(SerialCLK));
Delayus(1);
}
if(U30==UT)
{
PORTA|= BIT(SerialOutPut0);
Delayus(1);
PORTA&= ~(BIT(SerialOutPut0));
}
else if(U4==UT)
{
PORTA|= BIT(SerialOutPut1);
Delayus(1);
PORTA&= ~(BIT(SerialOutPut1));
}
else
{
PORTA|= BIT(SerialOutPut2);
Delayus(1);
PORTA&= ~(BIT(SerialOutPut2));
}
}
/*******************************************
Function Name: Board_init
Function:
Parameter:
Return value:
********************************************/
void Board_init(void)
{
//Set I/O Direction
DDRA=0xf8;
PORTA=0x00;
DDRB=0xfC;
PORTB=0xff;
DDRC=0xff;
PORTC=0x00;
DDRD=0x02;
PORTD=0xff;
U595OutData(0x0f,U30);
U595OutData(0,U4);
U595OutData(0x79,U43);
U595OutData(0x0f,U30);
U595OutData(0,U4);
U595OutData(0x79,U43);
}
/*******************************************
Function Name: Initial_INT
Function: External Interrupt Initialization
Parameter:
Return Value:
/********************************************/
void Initial_INT(void)
{
MCUCR|=0x0a; //the falling edge of INTx generate an interrupt request
GICR|=BIT(INT0);//Enable the INT0,INT1 interrupt
DDRD&=~(BIT(PD2)|BIT(PD3));
PORTD|=BIT(PD2)|BIT(PD3);
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;
UCSRB=0x00;
UCSRA=0x00;
UBRRL=baud; //4800
UBRRH=baud>>8;
UCSRB=BIT(RXCIE)|BIT(RXEN)|BIT(TXEN); //Enable Receive Data Interrupt,Receive data enable,Transmite data enable,
UCSRC=(1<<URSEL)|(0<<UPM0)|(3<<UCSZ0); //选择UCSRC,异步模式,禁止校验,1位停止位,8位数据位
SREG=(1<<7); //enable globle interrupt
DDRD|=0X02; //Ser TX pin output mode
}
/*******************************************
Function Name: Uart_Disable
Function:
Parameter:
Return value:
********************************************/
void Uart_Disable(void)
{
U595OutData(TempU43|BIT(CD4052_INH), U43);
TempU43=TempU43|(BIT(CD4052_INH));
}
/*******************************************
Function Name: Uart_init
Function:USART initialization
Parameter:
Return value:
********************************************/
void Uart_Select(unsigned char Num)
{
if(Num==ComputerS)
{
U595OutData(TempU43&~(BIT(CD4052_A)|BIT(CD4052_B)), U43);
TempU43=TempU43&~(BIT(CD4052_A)|BIT(CD4052_B));
}
else if(Num==BiaoChi)
{
U595OutData(TempU43|BIT(CD4052_A), U43);
TempU43=TempU43|BIT(CD4052_A);
U595OutData(TempU43&~(BIT(CD4052_B)), U43);
TempU43=TempU43&~(BIT(CD4052_B));
}
else if(Num==GSC)
{
U595OutData(TempU43|BIT(CD4052_B), U43);
TempU43=TempU43|BIT(CD4052_B);
U595OutData(TempU43|BIT(CD4052_A), U43);
TempU43=TempU43|BIT(CD4052_A);
}
U595OutData(TempU43&~(BIT(CD4052_INH)), U43);
TempU43=TempU43&~(BIT(CD4052_INH));
Delayus(10);
}
/*******************************************
Function Name: Uart_RX
Function: Asynchronism date receive interrupt
Parameter:
Return Value:
/********************************************/
void Uart_RX(void)
{
UCSRB&=~(BIT(RXCIE)); //Closed the receive data interrupt
ReceiveBuffer[Pwrite++]=UDR;
if(Pwrite>49)
Pwrite=48;
UCSRB|=BIT(RXCIE); //Closed the receive data interrupt
}
/*******************************************
Function Name: OpenBuzzer
Function:
Parameter:
Return value:
********************************************/
void OpenBuzzer(void)
{
;
//U595OutData(TempU43|BIT(BZCon),U43);
//TempU43=TempU43|BIT(BZCon);
}
/*******************************************
Function Name: OpenBuzzer
Function:
Parameter:
Return value:
********************************************/
void CloseBuzzer(void)
{
;
//U595OutData(TempU43&~(BIT(BZCon)),U43);
//TempU43=TempU43&~(BIT(BZCon));
}
/******************************************
发送一个字节的子程序:
上升沿发送数据,
MSB first
******************************************/
void Write_Max7219_byte(unsigned char temp)
{
unsigned char i;
unsigned char Temp1;
U595OutData(TempU30&~(BIT(MAx7219_CLK)),U30);
TempU30=TempU30&~(BIT(MAx7219_CLK));
Delayus(1);
for(i=0;i<8;i++)
{
Temp1=TempU30;
Temp1&=~(BIT(MAx7219_DIN));
U595OutData(Temp1|((temp&0x80)>>7),U30);
TempU30=Temp1|((temp&0x80)>>7);
Delayus(1);
U595OutData(TempU30|BIT(MAx7219_CLK),U30); // data in
TempU30=TempU30|BIT(MAx7219_CLK);
temp<<=1;
Delayus(1);
U595OutData(TempU30&~(BIT(MAx7219_CLK)),U30);
TempU30=TempU30&~(BIT(MAx7219_CLK));
Delayus(1);
}
}
/**********************************************************
向寄存器中写入一个数据
先写地址,后写数据
load 上升沿锁存数据
**********************************************************/
void Write_Max7219(unsigned char address,unsigned char dat)
{
U595OutData(TempU30&~(BIT(MAx7219_CS)),U30);
TempU30=TempU30&~(BIT(MAx7219_CS));
Write_Max7219_byte(address);
Write_Max7219_byte(dat);
U595OutData(TempU30|BIT(MAx7219_CS),U30);
TempU30=TempU30|BIT(MAx7219_CS);
}
/**********************
初始化max7219 子函数
设置工作寄存器
需要查看芯片手册
**********************/
void Init_Max7219(void)
{
Write_Max7219(SHUT_DOWN,0x01); //Normal Operation XXXXXXX1 Shutdown Mode XXXXXXXX0
Write_Max7219(DISPLAY_TEST,0x00); //Normal Operation XXXXXXX0 Display Test Mode XXXXXXXX1
Write_Max7219(DECODE_MODE,0xff); //Decode Mode Select D7~D0 1 B decode 0 No decode
Write_Max7219(SCAN_LIMIT,0x07); //SCAN LIMIT 0~7 0xX0~0xX7
Write_Max7219(INTENSITY,0x06); //Set Intensity 0xX0~0xXf
}
/*******************************************
Function Name: Uart_init
Function:USART initialization
Parameter:
Return value:
****************