'******************************************************************* ' Program name Pressure measure ' Start Date 5th.Apr.2010 ' Author Yoshio KATO ' Description : Used MCU ATtiny2313 ' : System Clock 8MHz Internal OSC ' : Pressure sensor is SCP1000-D01 '******************************************************************* $regfile = "attiny2313.dat" $crystal = 8000000 'Internal 8MHz $baud = 38400 Dim Tmcount As Byte Dim Command_data As Byte Dim Pressure As Long Dim Pressure_high As Word Dim Pressure_low As Word Dim Temperature As Word Dim Wk As Word Dim Wk1 As Word Dim Address As Byte Dim I As Byte , J As Byte Dim Get_dt8 As Byte , Put_dt As Byte Dim Get_dt_high As Byte , Get_dt_low As Byte Dim Minus_flg As Byte Const Sck = 0 'PB0 Const Drdy = 1 'PB1 Const Mosi = 2 'PB2 Const Miso = 3 'PB3 Const Csb = 4 'PB4 Const Led = 5 'PD5 Config Portb = &HF5 Config Portd = &HFC Config Timer0 = Timer , Prescale = 1024 , Clear Timer = 1 Ocr0a = 77 On Oc0a Tm0_ovf ' if timer0 overflow go to Tmovf interrupt routine Main: Stop Ac 'Analog comparater power off Gosub Init_scp1000 Do 'ever loop Loop End '******************************************************************* ' Get 8bit data '******************************************************************* Get_data8: Shift Address , Left , 2 Put_dt = Address Reset Portb.csb 'csb low Gosub Spi 'write address Put_dt = &H00 Gosub Spi 'get data, result is the Get_dt8 Set Portb.csb 'csb high Waitms 10 Return '******************************************************************* ' Write indirect mode '******************************************************************* Write_indirect: Address = &H02 Command_data = I Gosub Put_data8 Address = &H01 Command_data = J Gosub Put_data8 Address = &H03 Command_data = &H02 Gosub Put_data8 Waitms 50 Return '******************************************************************* ' Get 16bit data '******************************************************************* Get_data16: Shift Address , Left , 2 Put_dt = Address And &HFC Reset Portb.csb 'csb=0 Gosub Spi 'write address, get nothing Put_dt = &H00 Gosub Spi 'send nothing, get high byte (D15-D8) Get_dt_high = Get_dt8 Put_dt = &H00 Gosub Spi 'send nothing ,get low byte(D7-D0) Get_dt_low = Get_dt8 Set Portb.csb Return '******************************************************************* ' Put 8bit data '******************************************************************* Put_data8: Shift Address , Left , 2 Put_dt = Address Or &H02 'address + write mode Reset Portb.csb Gosub Spi Put_dt = Command_data Gosub Spi Set Portb.csb Return '******************************************************************* ' Timer1 interrupt ' Interrupt ever 10ms '******************************************************************* Tm0_ovf: Incr Tmcount If Tmcount >= 100 Then Reset Portd.led End If If Tmcount >= 200 Then 'past 1 secod ? Tmcount = 0 Set Portd.led Gosub Measurement Gosub Send_data End If Return '******************************************************************* ' measurement temperature and pressure ' this routine is called every 1 second from timer overflow interrupt '******************************************************************* Measurement: Address = &H21 'get temperature Gosub Get_data16 Temperature = Makeint(get_dt_low , Get_dt_high) 'convert to integer from 2 byte data If Temperature.13 = 1 Then 'minus ? Temperature = Not Temperature Temperature = Temperature And &H1FFF Incr Temperature Minus_flg = 1 Else Minus_flg = 0 End If Temperature = Temperature / 2 Address = &H1F Gosub Get_data8 'get pressure Pressure = Get_dt8 And &H07 'get lower 3bit Shift Pressure , Left , 16 Address = &H20 Gosub Get_data16 Pressure_low = Makeint(get_dt_low , Get_dt_high) 'convert to integer from 2 byte data Pressure = Pressure Or Pressure_low Pressure = Pressure / 40 Return '******************************************************************* ' Send message to disp unit ' upper 4bit are digit postion '******************************************************************* Send_data: For Wk1 = &H10 To &H90 Step &H10 Select Case Wk1 Case Is < &H40 : Wk = Temperature Mod 10 Temperature = Temperature / 10 Case &H40 : If Minus_flg = 1 Then Wk = &HA Else Wk = &HB End If Case Is > &H40 : Wk = Pressure Mod 10 Pressure = Pressure / 10 End Select Wk = Wk Or Wk1 Printbin Wk Next Wk1 Return '******************************************************************* ' Initialize SCP1000 sensor unit ' High resolution mode '******************************************************************* Init_scp1000: Do 'if error,repeat loop Set Portb.csb Waitms 200 Address = &H06 'software reset Command_data = &H01 Gosub Put_data8 Waitms 100 For I = 1 To 6 'startup Address = &H07 Gosub Get_data8 If Get_dt8.0 = 0 Then Exit For End If Waitms 10 Next I If I = 6 Then Goto Error End If Address = &H1F 'checksum Gosub Get_data8 If Get_dt8 <> 0 Then Exit Do 'checksum OK!,no error End If Error: For I = 1 To 20 'LED on/off 20 times Set Portd.led Waitms 100 Reset Portd.led Waitms 100 Next I Loop I = &H2D 'address J = &H03 'data Gosub Write_indirect 'set low noise Waitms 100 Address = &H03 'reset Command_data = &H0 Gosub Put_data8 Waitms 10 Address = &H03 'set high resolution mode Command_data = &H0A Gosub Put_data8 Waitms 100 Enable Oc0a 'enable timer0 interrupt Enable Interrupts Return '******************************************************************* ' out_put data address ->Put_dt ' input data -> in_data8 '******************************************************************* Spi: Get_dt8 = 0 For I = 1 To 8 J = Put_dt And &H80 If J = 0 Then Reset Portb.mosi Else Set Portb.mosi End If Waitus 20 'data setup time Shift Put_dt , Left , 1 Set Portb.sck Waitus 20 Shift Get_dt8 , Left , 1 If Pinb.miso = 1 Then Set Get_dt8.0 End If Reset Portb.sck Waitus 20 Next I Return