$programmer = 22 'ARDUINO (using stk500v1 protocol)
'
' ********************************************
' * MAX30205 Human Body Temperature Sensor *
' * Arduino LCD Keypad Shield *
' * 2021. 6.22 *
' ********************************************
'
$regfile = "m328pdef.dat" 'Set the AVR to use.
$crystal = 16000000 'Set the AVR clock.
'
$hwstack = 64 'Set the capacity of the hardware stack.
$swstack = 10 'Set the capacity of the software stack.
$framesize = 24 'Set the capacity of the frame area.
'
' * Variable declaration *
'
Dim Temperature As Long 'MAX30205 Sensor temperature value.
Dim I2cbuff(5) As Byte 'I2C send / receive data buffer.
'
Dim Tempi1 As Integer 'General-purpose temporary.
Dim Tempstr As String * 20 'General-purpose temporary.
'
' * Port initialization *
'
Config Portb.2 = Output 'Set the LCD backlight control connection port to output.
Set Portb.2 'Turn on the LCD backlight.
'
' * LCD initial settings *
'
Config Lcdmode = Port 'Set LCD to 4-bit port mode.
Config Lcdbus = 4 'Set the LCD data bus to 4bit.
Config Lcdpin = Pin , Db4 = Portd.4 , Db5 = Portd.5 'LCD port assignment.
Config Lcdpin = Pin , Db6 = Portd.6 , Db7 = Portd.7
Config Lcdpin = Pin , E = Portb.1 , Rs = Portb.0
Config Lcd = 16 * 2 'Set the LCD display to 16 characters and 2 lines.
Cls 'Erase all LCD display.
'
Deflcdchar 0 , &H08 , &H14 , &H08 , &H06 , &H09 , &H08 , &H09 , &H06 'Write the custom character [C] to the LCD.
Cursor Off 'Turn off the LCD cursor.
'
' * Serial port settings *
'
Config Com1 = 9600 , Parity = None , Stopbits = 1 , Databits = 8 'Hardware UART settings.
'
' * I2C initial settings *
'
Const I2c_select = 1 '0:Software I2C , 1:TWI
#if I2c_select = 0
'------[For software I2C]------
Config Scl = Portc.5 'Set the port pin to connect the SCL line of the I2C bus.
Config Sda = Portc.4 'Set the port pin to connect the SDA line of the I2C bus.
I2cinit 'Initialize the SCL and SDA lines of the I2C bus.
'-------------------------------
#else
'------[For TWI]------------------
$lib "i2c_twi.lib" 'Incorporate the hardware I2C/TWI library.
Config Twi = 400000 'I2C bus clock = 400KHz
Config Scl = Portc.5 'You must specify the SCL pin name.
Config Sda = Portc.4 'You must specify the SDA pin name.
I2cinit 'Initialize the SCL and SDA lines of the I2C bus.
'-------------------------------
#endif
'
Const Max30205_address = &H90 'The slave address of the MAX30205.
'--------------------------------------------------------------------------------------------------
'
' * Display the initial value of the MAX30205 register *
'
Print : Print "* MAX30205 *" 'Title output.
'
I2cbuff(1) = &H01 'Register address = [Configuration register]
I2creceive Max30205_address , I2cbuff(1) , 1 , 1 'Send 1 byte to MAX30205 and receive 1 byte.
Locate 1 , 1
Lcd "Config: "
Locate 2 , 1
Lcd Bin(i2cbuff(1)) 'Display the contents of [Configuration Register] in binary on the LCD.
'
Print "-Configuration : " ; Bin(i2cbuff(1)) 'Output the contents of [Configuration Register] to the terminal.
'
I2cbuff(1) = &H02 'Register address = [THYST register]
I2creceive Max30205_address , I2cbuff(1) , 1 , 2 'Sends 1 byte to MAX30205 and receives 2 bytes.
Tempi1 = Makeint(i2cbuff(2) , I2cbuff(1)) 'Convert 2 bytes of [THYST register] to integer type.
Locate 1 , 9
Lcd "THY:" ; Hex(tempi1) 'HEX the contents of the [THYST register] on the LCD.
'
Print "-THYST : " ; Hex(tempi1) 'Output the contents of [THYST register] to the terminal.
'
I2cbuff(1) = &H03 'Register address = [TOS register]
I2creceive Max30205_address , I2cbuff(1) , 1 , 2 'Sends 1 byte to MAX30205 and receives 2 bytes.
Tempi1 = Makeint(i2cbuff(2) , I2cbuff(1)) 'Convert 2 bytes of [TOS register] to integer type.
Locate 2 , 9
Lcd "TOS:" ; Hex(tempi1) 'HEX the contents of the [TOS register] on the LCD.
'
Print "-TOS : " ; Hex(tempi1) 'Output the contents of [TOS register] to the terminal.
Print
'
Wait 3
'
' * Initial display of LCD screen *
'
Cls 'Erase all LCD display.
Locate 1 , 1
Lcd "MAX30205" 'Display of title.
Locate 2 , 3
Lcd "temp:"
Main:
'
' * Get the temperature value of the sensor *
'
Do
I2cbuff(1) = &H00 'Register address = [Temperature register]
I2creceive Max30205_address , I2cbuff(1) , 1 , 2 'Sends 1 byte to MAX30205 and receives 2 bytes.
'
Tempi1 = Makeint(i2cbuff(2) , I2cbuff(1)) 'Converts 2 bytes of ADC value to integer type.
Shift Tempi1 , Right , 1 , Signed 'Drop the least significant bit. (Make it 15 bits)
Temperature = Tempi1 * 78125 'ADC value * 0.0078125 C (15-bit resolution).
Temperature = Temperature / 1000000 'Make it a 3-digit integer including a decimal point.
'
Tempstr = Str(temperature) 'Convert numeric variables to character variables.
Locate 2 , 9
Lcd Format(tempstr , " 0.0") ; " " ; Chr(0) ; " " 'Display the temperature value on the LCD.
'
Print " " ; Format(tempstr , " 0.0") ; " c" 'Output the temperature value to the terminal.
'
Waitms 500 'Waiting time for display interval.
Loop
End
|