/****************************************************************************
* 文 件 名:ms5534b.c
* 说 明:
PB2=>SCLK (时钟信号)
PB1=>DOUT (数据输出)
PB0=>DIN (数据输入)
***************************************************************************/
//ICC-AVR application builder : 2009-8-5 9:46:41
// Target : 2313
// Crystal: 3.6864Mhz
#include <iot2313v.h>
#include <macros.h>
#define fosc 3686400
#define baud 9600
unsigned int WORD1,WORD2,WORD3,WORD4;
unsigned int C1,C2,C3,C4,C5,C6;
unsigned int D1,D2;
unsigned char bit,PINC_DATA;
unsigned char hzcount;
unsigned char U_DATA[6];
unsigned char start_second;
unsigned char WTX400[6],start_frist,stop_second,WTX400_timers,CrcData;
unsigned int UT1;
unsigned long TEMP,TEMP1,dT;
unsigned int OFF,SENS,X,P;
unsigned char negative;
void USART_Transmit( unsigned char data )
{
/* 等待发送缓冲器为空 */
while ( !( UCSRA & (1<<UDRE)) );
/* 将数据放入缓冲器,发送数据 */
UDR = data;
}
void port_init(void)
{
PORTB = 0x00;
DDRB = 0x0D;
PORTD = 0x03;
DDRD = 0x03;
}
//UART0 initialisation
// desired baud rate: 9600
// actual: baud rate:9600 (0.0%)
// char size: 8 bits
// parity: Disabled
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = (1<<USBS)|(3<<UCSZ0);
UBRRL = (fosc/16/baud-1)%256; //set baud rate lo
UBRRH =( fosc/16/baud-1)/256; //set baud rate hi
UCSRB = (1<<RXCIE)|(1<<RXEN)|(1<<TXEN);
}
//TIMER1 initialisation - prescale:1024
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 1Hz
// actual value: 1.000Hz (0.0%)
void timer1_init(void)
{
TCCR1B = 0x00; //stop timer
TCNT1H = 0xF1; /*INVALID SETTING*/; //set count value
TCNT1L = 0x99; /*INVALID SETTING*/;
TCCR1B = 0x00; //stopo Timer
}
#pragma interrupt_handler timer1_ovf_isr:6
void timer1_ovf_isr(void)
{
//TIMER1 has overflowed
TCNT1H = 0xF1; //reload counter high value
TCNT1L = 0x99; //reload counter low value
bit=1;
}
//*******************************************************
void delay1ms(unsigned int data_1ms)
{
unsigned int i;
for(i=0;i<data_1ms;i++)
{
//********
TCCR1B = 0x01; //Start Timer
while(bit!=1);//1ms
TCCR1B = 0x00; //Stop Timer
bit=0;
//*******
}
}
//************************读数据-WORD***************************
unsigned int READ_MS5534B_WORD(unsigned int seq,unsigned char ADC_data)
{
unsigned char VBUF,count,count1,i;
unsigned char data3;
unsigned int bit_ctr,data1,data2;
data2=seq;
bit_ctr=0x0001;
count1=15;
//USART_Transmit(data/256);
//USART_Transmit(ADC_data%256);
for (count=0;count<ADC_data;count++)
{
if((data2&bit_ctr)==0x0000)
{
PORTB&=~BIT(PB0);//PB0=0;
PORTB&=~BIT(PB2);//PB2=0
delay1ms(1);
PORTB|=BIT(PB2);//PB2=1
delay1ms(1);
} //000000000000000000000000000000000000000000000000000
else
{
PORTB|=BIT(PB0);//PB0=1;
PORTB&=~BIT(PB2);//PB2=0
delay1ms(1);
PORTB|=BIT(PB2);//PB2=1
delay1ms(1);
} //1111111111111111111111111111111111111111111111111111
bit_ctr=bit_ctr*2;
}
PORTB&=~BIT(PB2);//PB2=0
delay1ms(1);
PORTB|=BIT(PB2);//PB2=1
delay1ms(1);
PORTB&=~BIT(PB2);//PB2=0
delay1ms(1);
for (count=0;count<16;count++)
{
PORTB|=BIT(PB2); //PB2=1
delay1ms(1);
VBUF=PINB;
PORTB&=~BIT(PB2);//PB2=0
delay1ms(1);
VBUF=VBUF&0x02; // READ PB1
if(VBUF==0x02)
data1|=BIT(count1);
else if(VBUF==0x00)
data1&=~BIT(count1);
count1--;
}
PORTB|=BIT(PB2);//PB2=1
delay1ms(1);
PORTB&=~BIT(PB2);//PB2=0
delay1ms(1);
return data1;
}
//************************读数据-D***************************
unsigned int READ_MS5534B_D(unsigned int seq,unsigned char ADC_data)
{
unsigned char VBUF,count,count1,i;
unsigned char data3;
unsigned int bit_ctr,data1,data2;
data2=seq;
bit_ctr=0x0001;
count1=15;
//USART_Transmit(data/256);
// USART_Transmit(data%256);
for (count=0;count<ADC_data;count++)
{
if((data2&bit_ctr)==0x0000)
{
PORTB&=~BIT(PB0);//PB0=0;
PORTB&=~BIT(PB2);//PB2=0
delay1ms(1);
PORTB|=BIT(PB2);//PB2=1
delay1ms(1);
} //000000000000000000000000000000000000000000000000000
else
{
PORTB|=BIT(PB0);//PB0=1;
PORTB&=~BIT(PB2);//PB2=0
delay1ms(1);
PORTB|=BIT(PB2);//PB2=1
delay1ms(1);
} //1111111111111111111111111111111111111111111111111111
bit_ctr<<=1;
}
for(count=0;count<2;count++)
{
PORTB&=~BIT(PB2);//PB2=0
delay1ms(1);
PORTB|=BIT(PB2);//PB2=1
delay1ms(1);
}
PORTB&=~BIT(PB2);//PB2=0
delay1ms(33);
/*
while((PINC_DATA&0x02)==0x00)
PINC_DATA=PINB;
while((PINC_DATA&0x02)==0x02)
PINC_DATA=PINB;
*/
for (count=0;count<16;count++)
{
PORTB|=BIT(PB2); //PB2=1
delay1ms(1);
PORTB&=~BIT(PB2);//PB2=0
delay1ms(1);
VBUF=PINB;
VBUF=VBUF&0x02; // READ PB1
if(VBUF==0x02)
data1|=BIT(count1);
else if(VBUF==0x00)
data1&=~BIT(count1);
count1--;
}
/*
PORTB|=BIT(PB2);//PB2=1
delay1ms(1);
PORTB&=~BIT(PB2);//PB2=0
delay1ms(1);
*/
return data1;
}
/*
void hextohz(unsigned long ldata)
{
unsigned char hexhz[6];
hexhz[0] = ldata/100000;
hexhz[1] = ldata/10000-hexhz[0]*10;
hexhz[2] = ldata/1000-ldata/10000*10;
hexhz[3] = ldata/100-ldata/1000*10;
hexhz[4] = ldata/10-ldata/100*10;
hexhz[5] = ldata%10;
U_DATA[0] = hexhz[0]*16+hexhz[1];
U_DATA[1] = hexhz[2]*16+hexhz[3];
U_DATA[2] = hexhz[4]*16+hexhz[5];
}
*/
#pragma interrupt_handler uart0_rx_isr:8
void uart0_rx_isr(void)
{
unsigned char PRESS;
// USART_Transmit(UDR);
PRESS=UDR;
//USART_Transmit(PRESS);
if(start_second==1)
WTX400[WTX400_timers++]=PRESS;
if(PRESS==0x10)
start_frist=1;
else if((start_frist==1)&(PRESS==0x02))
{start_second=1;start_frist=0;}
else if((start_frist==1)&(PRESS==0x03))
stop_second=1;
//else if((start_frist==1)&(PRESS!=0x02)&(PRESS!=0x03))
// start_frist=0;
if(stop_second==1)
{
stop_second=0;
start_second=0;
start_frist=0;
WTX400_timers=0;
}
//uart has received a character in UDR
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer1_init();
uart0_init();
MCUCR = 0x00;
TIMSK = 0x80;
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//*******************************************
void main()
{
init_devices();
bit=0;
PORTB&=~BIT(PB2);
delay1ms(1000);
WORD1 = READ_MS5534B_WORD(0x0157,12); //WORD1;
WORD2 = READ_MS5534B_WORD(0x00D7,12); //WORD2;
WORD3 = READ_MS5534B_WORD(0x0137,12); //WORD3;
WORD4 = READ_MS5534B_WORD(0x00B7,12); //WORD4;
delay1ms(40);
C1 = WORD1/2;
C2 = (WORD3%64)*64+(WORD4%64);//C2 = (WORD4%64)*64+(WORD3%64);
C3 = WORD4/64;
C4 = WORD3/64;
C5 = (WORD1%2)*1024+(WORD2/64);
C6 = WORD2%64;
/*
cc0=C1/256;
cc1=C1%256;
cc2=C2/256;
cc3=C2%256;
cc4=C3/256;
cc5=C3%256;
cc6=C4/256;
cc7=C4%256;
cc8=C5/256;
cc9=C5%256;
cc10=C6/256;
cc11=C6%256;
*/
while(1)
{
/*
USART_Transmit(WORD1/256);
USART_Transmit(WORD1%256);//reset();
USART_Transmit(WORD2/256);
USART_Transmit(WORD2%256);//reset();
USART_Transmit(WORD3/256);
USART_Transmit(WORD3%256);//reset();
USART_Transmit(WORD4/256);
USART_Transmit(WORD4%256);//reset();
*/
D1 = READ_MS5534B_D(0x002F,10); //D1;
//reset();
D2 = READ_MS5534B_D(0x004F,10); //D2;
//reset();
/*
UT1 = 8*C5+20224;
if(D2>UT1)
{
dT = D2-UT1;
TEMP = 200