'18F4520
'internal 8 MHz clock
'Full size SD Card Reader
'porte2 = chip select SD card
'porte0 = chip select MAX6675
'NOTE
'This program worked as expected
'AFTER I REMOVED THE SPI SETUP PARAMETERS
'12/14/15
'adding thermocouple solution using MAX6675
'uses SPI - testing to see if this will work with the
'existing sd card configuration
'If this does not work, I will try the sd card using a
'direct interface as opposed to the card module in
'curretn use
Define CONFIG1L = 0x00
Define CONFIG1H = 0x08
Define CONFIG2L = 0x19
Define CONFIG2H = 0x00
Define CONFIG3L = 0x00
Define CONFIG3H = 0x85
Define CONFIG4L = 0x80
Define CONFIG4H = 0x00
Define CONFIG5L = 0x0f
Define CONFIG5H = 0xc0
Define CONFIG6L = 0x0f
Define CONFIG6H = 0xe0
Define CONFIG7L = 0x0f
Define CONFIG7H = 0x40
'LCD Configuration - 4-bit lower
Define LCD_LINES = 2
Define LCD_CHARS = 16
Define LCD_BITS = 4
Define LCD_DREG = PORTD
Define LCD_DBIT = 4
Define LCD_RSREG = PORTD
Define LCD_RSBIT = 0
Define LCD_RWREG = PORTD
Define LCD_RWBIT = 1
Define LCD_EREG = PORTD
Define LCD_EBIT = 2
'SPI Configuration
Define SPI_CS_REG = PORTE
Define SPI_CS_BIT = 2
Define SPI_SCK_REG = PORTC
Define SPI_SCK_BIT = 3
Define SPI_SDI_REG = PORTC
Define SPI_SDI_BIT = 4
Define SPI_SDO_REG = PORTC
Define SPI_SDO_BIT = 5
'SPIINIT is NOT used in the program
Define CLOCK_FREQUENCY = 8
'global variables
Dim flags As Byte
'variables required for MAX6675 sections
Dim mb1 As Byte
Dim mb2 As Byte
Dim max6675 As Word
Dim temp As Single
Dim deg As Byte
main:
OSCCON = 0x72 'internal 8MHz
Gosub setup
Lcdinit 0
Lcdout "Ready"
WaitMs 1000
Lcdcmdout LcdClear
SDCardInit
WaitMs 2000
Lcdcmdout LcdLine1Clear
Lcdcmdout LcdLine1Home
If sd_status.sd_error = 1 Then Lcdout "Unknown Card"
If sd_status.sd_mmc = 1 Then Lcdout "MMC Card"
If sd_status.sd_sd = 1 Then Lcdout "SD Card"
If sd_status.sd_sdsc = 1 Then Lcdout "SDSC Card"
If sd_status.sd_sdhc = 1 Then Lcdout "SDHC Card"
Lcdcmdout LcdLine2Clear
loop:
If flags.0 = 1 Then
flags.0 = 0
While PORTB.0 = 1
Wend
WaitMs 200
Toggle PORTE.1
Gosub get_temp
Gosub convert
Lcdcmdout LcdLine2Clear
Lcdout #temp
Lcdout deg, "C "
temp = temp * 1.8 + 32
Lcdout #temp, deg, "F"
Endif
Goto loop 'infinite loop
End
setup:
INTCON = 0xd0 'int0 en
INTCON2 = 0xc0 'pull ups disabled, int0 on rising egde
CCP1CON = 0x00 'cap / comp off
CCP2CON = 0x00
'SSPCON1 = 0x21 'spi on, fosc/16, clk idle state low
'SSPSTAT = 0x40 'sample in middle, cke=1 - xfer on clk high to low
ADCON0 = 0x00 'A/D off
ADCON1 = 0x0f 'all digital
CMCON = 0x07 'comp off, digital io
CVRCON = 0x00
HLVDCON = 0x00 'Low volt detect off
TRISA = 0x00
TRISB = 0x01
TRISC = 0x00
TRISC.4 = 1 'sdi input
TRISD = 0x00
TRISE = 0x00
LATA = 0x00
LATB = 0x00
LATC = 0x00
LATD = 0x00
LATE = 0x0f 'porte2 set for sd card chip select
flags = 0x00
deg = 0xdf 'degree symbol
Return
get_temp:
'get new temperature reading and store in eeprom
'MAX6675 temperature reading
'this chip has no SPI input
'all this chip is expecting is a clock source
PORTE.0 = 0 'chip select
SSPBUF = 0x11 'send dummy byte to receive first byte from chip
While SSPSTAT.0 = 0
Wend
mb1 = SSPBUF
SSPSTAT.0 = 0
SSPBUF = 0x22 'send dummy byte to receive second byte from chip
While SSPSTAT.0 = 0
Wend
mb2 = SSPBUF
SSPSTAT.0 = 0
PORTE.0 = 1 'deselct chip
Return
convert:
max6675.HB = mb1
max6675.LB = mb2
max6675 = ShiftRight(max6675, 3)
max6675 = max6675 - 5
mb1 = max6675.HB
mb2 = max6675.LB
temp = max6675 / 4
Return
On High Interrupt
If INTCON.1 = 1 Then 'int0
INTCON.1 = 0
flags.0 = 1
Endif
Resume
Define SPI_CS_REG = PORTC
Define SPI_CS_BIT = 2
Define SPI_SCK_REG = PORTC
Define SPI_SCK_BIT = 3
Define SPI_SDI_REG = PORTC
Define SPI_SDI_BIT = 4
Define SPI_SDO_REG = PORTC
Define SPI_SDO_BIT = 5
Define LCD_LINES = 2
Define LCD_CHARS = 16
Define LCD_BITS = 4
Define LCD_DREG = PORTD
Define LCD_DBIT = 4
Define LCD_EREG = PORTD
Define LCD_EBIT = 3
Define LCD_RSREG = PORTD
Define LCD_RSBIT = 2
Define LCD_RWREG = PORTD
Define LCD_RWBIT = 1
Define 1WIRE_REG = PORTE
Define 1WIRE_BIT = 0
Define CLOCK_FREQUENCY = 8
'Define SIMULATION_WAITMS_VALUE = 10
Dim finish As Bit
Dim temp_lsb As Byte
Dim temp_msb As Byte
Dim dat As Byte
Dim tenths As Single
Dim temp_out As Single
Dim loop As Bit
Dim lp_cnt As Byte
Dim end_loop As Bit
Dim e_loop As Bit
Dim ad_res As Long
Dim tc_temp As Single
main:
OSCCON = 0x72 'internal, 8MHz
Gosub setup
Lcdinit LcdCurBlink
WaitMs 2000
Lcdcmdout LcdClear
Lcdout "UL248 Fuse Test"
Lcdcmdout LcdCurOff
Gosub sd_start
Lcdcmdout LcdClear
Lcdout "Current Temp"
While loop
ADCON0.1 = 1 'start a/d
1wireInit
1wireSendByte 0xcc, 0x44
WaitMs 750
lp:
1wireGetBit finish
If finish = 0 Then Goto lp
1wireInit
1wireSendByte 0xcc, 0xbe
1wireGetByte temp_lsb, temp_msb
'Lcdout #temp_msb, " ", #temp_lsb
Lcdcmdout LcdLine2Home
dat = temp_lsb
dat = dat And 0x0f
tenths = dat * 0.062500
temp_lsb = ShiftRight(temp_lsb, 4)
temp_msb = ShiftLeft(temp_msb, 4)
temp_lsb = temp_lsb Or temp_msb
temp_out = temp_lsb + tenths
WaitMs 200
'get a/d result and convert to temp and
'subtract cold junction temp
ad_res.LB = ADRESL
ad_res.HB = ADRESH
tc_temp = ad_res * 0.00488280
tc_temp = tc_temp / 416
Call sd_t_out(temp_out) 'write degC to sd card
Lcdout #temp_out, "C "
temp_out = temp_out * 1.8000 + 32
Lcdout #temp_out, "F"
Call sd_t_out(temp_out) 'write degF to sd card
SDCardFAT32FileWrite CrLf 'new line
Toggle LATA.3 'heartbeat
WaitMs 3000
lp_cnt = lp_cnt + 1
If lp_cnt = 20 Then
SDCardFAT32FileClose
If sd_fat32_status.sd_fat32_wr_opened = 0 Then
Lcdcmdout LcdClear
Lcdout "UL248.txt clsd!"
Endif
While end_loop
Wend
Endif
Wend
End
setup:
INTCON = 0x00 'glbl/periph int dis
CCP1CON = 0x00 'cap/comp off
ADCON0 = 0x01 'a/d off
ADCON1 = 0x0e 'an0
ADCON2 = 0x91 'rt just
CMCON = 0x07
CVRCON = 0x00
HLVDCON = 0x00
'all output
TRISA = 0x01
LATA = 0x00
TRISB = 0x00
LATB = 0x00
TRISC = 0x00
LATC = 0x00
TRISD = 0x00
LATD = 0x00
TRISE = 0x00
LATE = 0x00
lp_cnt = 0
loop = 1
end_loop = 1
e_loop = 1
Return
sd_start:
SDCardInit
WaitMs 2000
Lcdcmdout LcdClear
If sd_status.sd_error = 1 Then
Lcdout "Unknown card!"
While e_loop = 1
Wend
Endif
If sd_status.sd_mmc = 1 Then Lcdout "MMC card!"
If sd_status.sd_sd = 1 Then Lcdout "SD card!"
If sd_status.sd_sdsc = 1 Then Lcdout "SDSC card!"
If sd_status.sd_sdhc = 1 Then Lcdout "SDHC card!"
WaitMs 1000 'time to reseat if needed
SDCardFAT32Init
Lcdcmdout LcdClear
If sd_fat32_status.sd_fat32_error = 0 Then
Lcdout "FAT32 initlized!"
Endif
WaitMs 2000
SDCardFAT32FileDelete "UL248.txt"
If sd_fat32_status.sd_fat32_deleted = 1 Then
Lcdcmdout LcdClear
Lcdout "File Deleted"
Endif
SDCardFAT32FileCreate "UL248.txt"
Lcdcmdout LcdClear
If sd_fat32_status.sd_fat32_wr_opened = 1 Then
Lcdout "UL248.txt open!"
Endif
WaitMs 2000
SDCardFAT32FileWrite "degC, degF", CrLf
Return
Proc sd_t_out(dat As Single)
'dat is composed of degC in xxx.xxxx format
Dim lng_num_str As String
Dim new_cnt As Single
Dim num_part As String
Dim digit As String
lng_num_str = #dat
new_cnt = dat
If new_cnt >= 0 And new_cnt < 10 Then
digit = LeftStr(lng_num_str, 1)
SDCardFAT32FileWrite digit, "."
num_part = LeftStr(lng_num_str, 3)
digit = RightStr(num_part, 1)
SDCardFAT32FileWrite digit
num_part = LeftStr(lng_num_str, 4)
digit = RightStr(num_part, 1)
Endif
If new_cnt >= 10 And new_cnt < 100 Then
digit = LeftStr(lng_num_str, 1)
SDCardFAT32FileWrite digit
num_part = LeftStr(lng_num_str, 2)
digit = RightStr(num_part, 1)
SDCardFAT32FileWrite digit, "."
num_part = LeftStr(lng_num_str, 4)
digit = RightStr(num_part, 1)
SDCardFAT32FileWrite digit
num_part = LeftStr(lng_num_str, 5)
digit = RightStr(num_part, 1)
Endif
If new_cnt >= 100 Then
digit = LeftStr(lng_num_str, 1)
SDCardFAT32FileWrite digit
num_part = LeftStr(lng_num_str, 2)
digit = RightStr(num_part, 1)
SDCardFAT32FileWrite digit
num_part = LeftStr(lng_num_str, 3)
digit = RightStr(num_part, 1)
SDCardFAT32FileWrite digit, "."
num_part = LeftStr(lng_num_str, 5)
digit = RightStr(num_part, 1)
SDCardFAT32FileWrite digit
num_part = LeftStr(lng_num_str, 6)
digit = RightStr(num_part, 1)
Endif
SDCardFAT32FileWrite digit, ", "
End Proc
'LCD Configuration - 4-bit lower
Define LCD_LINES = 2
Define LCD_CHARS = 16
Define LCD_BITS = 4
Define LCD_DREG = PORTD
Define LCD_DBIT = 4
Define LCD_RSREG = PORTD
Define LCD_RSBIT = 0
Define LCD_RWREG = PORTD
Define LCD_RWBIT = 1
Define LCD_EREG = PORTD
Define LCD_EBIT = 2
'SPI Configuration
Define SPI_CS_REG = PORTE
Define SPI_CS_BIT = 2
Define SPI_SCK_REG = PORTC
Define SPI_SCK_BIT = 3
Define SPI_SDI_REG = PORTC
Define SPI_SDI_BIT = 4
Define SPI_SDO_REG = PORTC
Define SPI_SDO_BIT = 5
SPIPrepare
Define CLOCK_FREQUENCY = 40
Define SINGLE_DECIMAL_PLACES = 2
'global variables
Dim flags As Byte
Dim i As Byte
Dim mb1 As Byte
Dim mb2 As Byte
Dim max6675 As Word
Dim temp As Single
Dim t_av As Single
Dim x As Byte
main:
OSCCON = 0x00 'external osc
Gosub setup
Lcdinit 0
Lcdout "Ready"
WaitMs 1000
Lcdcmdout LcdClear
Lcdout "Temperature:"
Gosub get_temp
Gosub convert
Lcdcmdout LcdLine2Home
Lcdout #temp
x = 0xdf
Lcdout x, "C "
temp = temp * 1.8 + 32
Lcdout #temp, x, "F"
loop:
If flags.0 = 1 Then 'interrupt has occurred
flags.0 = 0
Gosub write_data
Endif
t_av = 0
For i = 1 To 5
Gosub get_temp
Gosub convert
t_av = t_av + temp
WaitMs 400
Next i
temp = t_av / 5
Lcdcmdout LcdLine2Clear
Lcdout #temp
Lcdout x, "C "
temp = temp * 1.8 + 32
Lcdout #temp, x, "F"
For i = 0 To 2 'blink to show complete
LATE.1 = 1
WaitMs 50
LATE.1 = 0
WaitMs 50
Next i
Goto loop 'infinite loop
End
setup:
INTCON = 0xd0 'int0 en
INTCON2 = 0xc0 'pull ups disabled, int0 on rising egde
CCP1CON = 0x00 'cap / comp off
CCP2CON = 0x00
SSPCON1 = 0x21 'spi on, fosc/16, clk idel state low
SSPSTAT = 0x40 'sample in middle, cke=1 - xfer on clk high to low
ADCON0 = 0x00 'A/D off
ADCON1 = 0x0f 'all digital
CMCON = 0x07 'comp off, digital io
CVRCON = 0x00
HLVDCON = 0x00 'Low volt detect off
TRISA = 0x00
'LATA.5 = 1
TRISB = 0x01
TRISC = 0x00
'TRISC.4 = 1 'sdi input
TRISD = 0x00
TRISE = 0x00
LATA = 0x00
LATB = 0x00
LATC = 0x00
LATD = 0x00
LATE = 0x01 'porte0 set for MAX6675 chip select
flags = 0x00
Return
get_temp:
'get new temperature reading and store in eeprom
'MAX6675 temperature reading
'this chip has no SPI input
'all this chip is expecting is a clock source
PORTE.0 = 0 'chip select
SSPBUF = 0x11 'send dummy buyte to receive first byte from chip
While SSPSTAT.0 = 0
Wend
mb1 = SSPBUF
SSPSTAT.0 = 0
SSPBUF = 0x22 'send dummy buyte to receive first byte from chip
While SSPSTAT.0 = 0
Wend
mb2 = SSPBUF
SSPSTAT.0 = 0
PORTE.0 = 1 'deselct chip
Return
convert:
max6675.HB = mb1
max6675.LB = mb2
max6675 = ShiftRight(max6675, 3)
max6675 = max6675 - 5
mb1 = max6675.HB
mb2 = max6675.LB
temp = max6675 / 4
Return
write_data:
'I never got this to work - This was going to be the SD Card data logging function
Return
'Proc sspsend(adr As Byte, dat As Byte)
'PORTA.5 = 0 'chip select
'SSPBUF = 0x40 'send chip id, RW bit = 0 (write)
'While SSPSTAT.0 = 0
'Wend
'SSPSTAT.0 = 0
'SSPBUF = adr 'send address
'While SSPSTAT.0 = 0
'Wend
'SSPSTAT.0 = 0
'SSPBUF = dat 'send data
'While SSPSTAT.0 = 0
'Wend
'SSPSTAT.0 = 0
'PORTA.5 = 1
'End Proc
'Function sspread(adr As Byte) As Byte
'PORTA.5 = 0 'chip select
'SSPBUF = 0x41 'send chip id, RW bit = 1 (read)
'While SSPSTAT.0 = 0
'Wend
'SSPSTAT.0 = 0
'SSPBUF = adr 'send address
'While SSPSTAT.0 = 0
'Wend
'SSPSTAT.0 = 0
'send/receive is simultaneous, so when the dummy byte is sent
'the byte I want to receive is also being clocked in
'See datasheet: satates "17.3 SPI MODE
'The SPI mode allows 8 bits of data to be SYNCHRONOUSLY
'transmitted and received simulateously
'SSPBUF = 0xff
'While SSPSTAT.0 = 0
'Wend
'SSPSTAT.0 = 0
'sspread = SSPBUF
'PORTA.5 = 1
'End Function
On High Interrupt
If INTCON.1 = 1 Then 'int0
INTCON.1 = 0
flags.0 = 1
Endif
Resume
Define CONFIG1L = 0x3c
Define CONFIG1H = 0x0c
Define CONFIG2L = 0x18
Define CONFIG2H = 0x00
Define CONFIG3L = 0x00
Define CONFIG3H = 0x83
Define CONFIG4L = 0x80
Define CONFIG4H = 0x00
Define CONFIG5L = 0x0f
Define CONFIG5H = 0xc0
Define CONFIG6L = 0x0f
Define CONFIG6H = 0xe0
Define CONFIG7L = 0x0f
Define CONFIG7H = 0x40
'redo, 16Dec2015 this program works OK with 8Gb SD
'demo5b 18F2550
Define STRING_MAX_LENGTH = 16
Define CLOCK_FREQUENCY = 20
AllDigital
Dim cnt As Byte
Dim word_var As Word
Dim file_data(32) As Byte
Define LCD_LINES = 4
Define LCD_CHARS = 16
Define LCD_BITS = 4
Define LCD_DREG = PORTB
Define LCD_DBIT = 4
Define LCD_EREG = PORTB
Define LCD_EBIT = 3
Define LCD_RSREG = PORTB
Define LCD_RSBIT = 2
Define LCD_RWREG = PORTC
Define LCD_RWBIT = 0
Define LCD_READ_BUSY_FLAG = 1
Define SPI_CS_REG = PORTA
Define SPI_CS_BIT = 5
Define SPI_SCK_REG = PORTB
Define SPI_SCK_BIT = 1
Define SPI_SDI_REG = PORTB
Define SPI_SDI_BIT = 0
Define SPI_SDO_REG = PORTC
Define SPI_SDO_BIT = 7
INTCON2 = 0x00
PORTB = 0
LATB = 0
ADCON1 = 0xff
TRISB = %11110101
TRISA = 0x00
Lcdinit
Lcdout "FAT32a 17-12-15"
WaitMs 500
SDCardInit
SDCardFAT32Init
Lcdcmdout LcdClear
If sd_fat32_status.sd_fat32_error = 0 Then
Lcdout "FAT32 init OK"
Else
Lcdout "Init Fail!"
Endif
WaitMs 500
SDCardFAT32FileCreate "mydata1.txt"
Lcdcmdout LcdClear
If sd_fat32_status.sd_fat32_wr_opened = 1 Then
Lcdout "mydata1 Created"
Endif
WaitMs 500
For cnt = 1 To 100
SDCardFAT32FileWrite "17December2015", CrLf
Next cnt
Lcdcmdout LcdLine1Clear
If sd_fat32_status.sd_fat32_wr_opened = 0 Then
Lcdout "mydata1 written!"
Endif
SDCardFAT32FileClose
WaitMs 500
SDCardFAT32FileOpen "mydata1.txt"
Lcdcmdout LcdLine1Clear
If sd_fat32_status.sd_fat32_notfound = 1 Then
Lcdout "Not found!"
Endif
If sd_fat32_status.sd_fat32_opened = 1 Then
Lcdout "mydata1 found!"
WaitMs 500
Lcdcmdout LcdLine1Clear
Lcdout "filesize: ", #sd_fat32_filelen
WaitMs 500
Gosub read_file
Endif
SDCardFAT32FileClose
End
read_file:
SDCardFAT32FileRead
Lcdcmdout LcdLine2Clear
Lcdout "Rd Cnt Dwn:", #sd_fat32_filelen
WaitMs 100
For cnt = 0 To 31
file_data(cnt) = sd_fat32_buff(cnt)
Next cnt
Lcdcmdout LcdLine3Clear
Lcdcmdout LcdLine4Clear
Lcdcmdout LcdLine3Home
For cnt = 0 To 15
Lcdout file_data(cnt)
Next cnt
Lcdcmdout LcdLine4Home
For cnt = 16 To 31
Lcdout file_data(cnt)
Next cnt
WaitMs 100
If sd_fat32_bytes_read = 0 Then Return
Goto read_file
Return
'18F4520
'internal 8MHz osc
'the purpose of this program is to receive data via
'USART from the slave device 18F4550, process the
'2 bytes received into a deg C value, display
'the value on the LCD and store the value on an SD Card
'The original version of this program works as anticipated.
'This rev will attempt to add SD Card datalogging
'This works as expected.
'Timer 0 is added to give timing for roughly 5 minutes.
'This will need to be changed to an RTC for the prototype
'The unit should have approximately 12 entries per hour.
'For this test there should be 13 entries:
'initial temp + 12 at roughly 5 minumte increments
'need to implement a write finish routine for each write cycle
'so that if the card is removed, the data is retained.
'This program works as expected.
Define CONFIG1L = 0x00
Define CONFIG1H = 0x08
Define CONFIG2L = 0x19
Define CONFIG2H = 0x00
Define CONFIG3L = 0x00
Define CONFIG3H = 0x85
Define CONFIG4L = 0x80
Define CONFIG4H = 0x00
Define CONFIG5L = 0x0f
Define CONFIG5H = 0xc0
Define CONFIG6L = 0x0f
Define CONFIG6H = 0xe0
Define CONFIG7L = 0x0f
Define CONFIG7H = 0x40
'LCD Configuration - 4-bit lower
Define LCD_LINES = 2
Define LCD_CHARS = 16
Define LCD_BITS = 4
Define LCD_DREG = PORTD
Define LCD_DBIT = 4
Define LCD_RSREG = PORTD
Define LCD_RSBIT = 0
Define LCD_RWREG = PORTD
Define LCD_RWBIT = 1
Define LCD_EREG = PORTD
Define LCD_EBIT = 2
'SPI Configuration
Define SPI_CS_REG = PORTE
Define SPI_CS_BIT = 2
Define SPI_SCK_REG = PORTC
Define SPI_SCK_BIT = 3
Define SPI_SDI_REG = PORTC
Define SPI_SDI_BIT = 4
Define SPI_SDO_REG = PORTC
Define SPI_SDO_BIT = 5
Define CLOCK_FREQUENCY = 8
Define SINGLE_DECIMAL_PLACES = 2
Define SDCARD_DEFAULT_WRITE = 0x00
'global variables
Dim flags As Byte
Dim x As Byte 'deg symbol
Dim max_dat As Word
Dim temp As Single
Dim dat As Byte
Dim i As Word
Dim cnt As Byte
main:
OSCCON = 0x72 'internal 8MHz
Gosub setup
Lcdinit 0
Hseropen 9600
Lcdout "Ready"
WaitMs 1000
Lcdcmdout LcdClear
Lcdout "Temperature:"
'initialize the sd card
'This was originally in a subroutine, but the append command
'would not work wihtout the initialize command comming first
'and initializing a second time caused the program to freeze.
'---------------------------------------------------
SDCardInit
WaitMs 1000
Lcdcmdout LcdClear
If sd_status.sd_error = 1 Then Lcdout "Unknown card!"
While sd_status.sd_error = 1
Wend
If sd_status.sd_mmc = 1 Then Lcdout "MMC card!"
If sd_status.sd_sd = 1 Then Lcdout "SD card!"
If sd_status.sd_sdsc = 1 Then Lcdout "SDSC card!"
If sd_status.sd_sdhc = 1 Then Lcdout "SDHC card!"
WaitMs 1000
SDCardFAT32Init
Lcdcmdout LcdClear
If sd_fat32_status.sd_fat32_error = 0 Then
Lcdout "FAT32 initialized!"
Else
Lcdout "Not init."
Endif
WaitMs 2000
SDCardFAT32FileCreate "UL248.txt"
Lcdcmdout LcdClear
If sd_fat32_status.sd_fat32_wr_opened = 1 Then
Lcdout "UL248.txt created!"
Else
Lcdout "not created"
Endif
WaitMs 2000
'--------------------------------------------------------
SDCardFAT32FileWrite "TC1, TC2", CrLf
SDCardFAT32FileClose
Lcdcmdout LcdLine2Home
x = 0xdf 'degree symbol
'Lcdout x, "C"
WaitMs 1000
T0CON.7 = 1 'start tmr0
i = 0
flags.0 = 1 'take initial temp
cnt = 1 'make this 1 less than the setting for the loop
loop:
If flags.0 = 1 Then
flags.0 = 0
cnt = cnt + 1
If cnt = 2 Then '(should be set for 5 minutes)start routine: get temps, send to sd card
cnt = 0 'reset interrupt count
SDCardFAT32FileAppend "UL248.txt" 'this reopens the file for additional data
WaitMs 1000
Toggle PORTE.1
Lcdcmdout LcdLine2Home
Hserout 0x11 'send rdy
'get tc1 data
Hserin dat 'the program will wait here till a byte is received
max_dat.HB = dat
Hserin dat
max_dat.LB = dat
temp = max_dat / 4
Lcdout "1:", #temp
Call sd_t_out(temp)
'clear possible errors
RCSTA.FERR = 0 'framing
RCSTA.OERR = 0 'overrun
WaitMs 200
Toggle PORTE.1
'get tc2 data
Hserin dat 'the program will wait here till a byte is received
max_dat.HB = dat
Hserin dat
max_dat.LB = dat
temp = max_dat / 4
Lcdout " 2:", #temp
Call sd_t_out(temp)
SDCardFAT32FileWrite CrLf
SDCardFAT32FileClose
WaitMs 500
i = i + 1
If i = 500 Then 'closing will be only after themral stability
SDCardFAT32FileClose
Lcdcmdout LcdClear
Lcdout "Finished"
here: Goto here
Endif
Endif
Endif
Goto loop 'infinite loop
End
setup:
INTCON = 0xe0 'tmr0 int enabled
INTCON2 = 0x84 'pull ups disabled, tmr0 int hi priority
RCON.7 = 1 'priority ints enabled
CCP1CON = 0x00 'cap / comp off
CCP2CON = 0x00
ADCON0 = 0x00 'A/D off
ADCON1 = 0x0f 'all digital
CMCON = 0x07 'comp off, digital io
CVRCON = 0x00
HLVDCON = 0x00 'Low volt detect off
TRISA = 0x00
TRISB = 0x00
TRISC = 0x00
TRISD = 0x00
TRISE = 0x00
LATA = 0x00
LATB = 0x00
LATC = 0x00
LATD = 0x00
LATE = 0x00
'setup timer3 for interval timing
T0CON = 0x07 'timer0 off, 1:256 prescale: approx 8.4s int
flags = 0x00
Return
Proc sd_t_out(dat As Single)
'dat is composed of degC in xxx.xx format
Dim lng_num_str As String
Dim new_cnt As Single
Dim num_part As String
Dim digit As String
lng_num_str = #dat
new_cnt = dat
If new_cnt >= 0 And new_cnt < 10 Then
digit = LeftStr(lng_num_str, 1)
SDCardFAT32FileWrite digit, "."
num_part = LeftStr(lng_num_str, 3)
digit = RightStr(num_part, 1)
Endif
If new_cnt >= 10 And new_cnt < 100 Then
digit = LeftStr(lng_num_str, 1)
SDCardFAT32FileWrite digit
num_part = LeftStr(lng_num_str, 2)
digit = RightStr(num_part, 1)
SDCardFAT32FileWrite digit, "."
num_part = LeftStr(lng_num_str, 4)
digit = RightStr(num_part, 1)
Endif
If new_cnt >= 100 Then
digit = LeftStr(lng_num_str, 1)
SDCardFAT32FileWrite digit
num_part = LeftStr(lng_num_str, 2)
digit = RightStr(num_part, 1)
SDCardFAT32FileWrite digit
num_part = LeftStr(lng_num_str, 3)
digit = RightStr(num_part, 1)
SDCardFAT32FileWrite digit, "."
num_part = LeftStr(lng_num_str, 5)
digit = RightStr(num_part, 1)
Endif
SDCardFAT32FileWrite digit, ", "
End Proc
On High Interrupt
If INTCON.2 = 1 Then 'tmr0 int
INTCON.2 = 0
flags.0 = 1
Endif
Resume
That seems to be the best way to achieve what you are trying to do. You could do something similar with a single Arduino, with a single PIC using a different compiler, or with a single PIC doing your own SPI/MMC library; but most of Oshonsoft's routines use bit-banged access which limits multiplexing the SPI port.I have a 18F4550 that takes thermocouple readings and sends the data via USART to a 18F4520. The 18F4520 crunches the numbers and stores the temperatures on the SD Card using the commands.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?