Wykonałem termometr, który ma rejestrować wyniki z pomiaru temperatury na karcie SD w pliku TXT. Wzorowałem się na projekcie rejestratora pogodowego wykonanego przez kolegę Duch_a. Napisałem, krótki program na podstawie w/w projektu, który ma utworzyć plik na karcie i zapisać w nim krótki tekst. Niestety w wyniku działania programu plik nie zostaje utworzony na karcie. Jeżeli utworzę sam plik na PC i próbuję zapisać coś do niego przez mój program to po przełożeniu karty ponownie do PC otrzymuję informację o uszkodzonym pliku. Program prawidłowo rozpoznaję rozmiar karty i ilość wolnego miejsca. Format karty FAT16. W załączniku zamieściłem schemat połączeń. $regfile = "m32def.dat" $include "Config_MMC.bas" 'Konfiguracja karty SD $include "Config_AVR-DOS.BAS" $crystal = 8000000 $hwstack = 128 $swstack = 128 $framesize = 128 $baud = 9600 'AVR-DOS config 'Biblioteka AVR-DOS 'MMC adapter pin config /atmega32/ , hardware SPI 'DATA3 -> PORTB.4 'DI -> PORTB.5 'DATA0 -> PORTB.6 'piny do których podpięte jest karta SD 'CLK -> PORTB.7 Config Lcd = 20 * 4 Config Lcdpin = Pin , Db4 = Portc.3 , Db5 = Portc.2 , Db6 = Portc.1 , Db7 = Portc.0 , E = Portc.4 , Rs = Portc.5 Config Timer1 = Pwm , Pwm = 10 , Compare A Pwm = Clear Down , Compare B Pwm = Disconnect , Prescale = 1 Config Clock = Soft Config Porta.0 = Output Config Porta.1 = Output Config Porta.2 = Output Dim A As String * 5 Dim Errorcode As Byte Dim Blad_sd As Bit Led1 Alias Porta.0 Led2 Alias Porta.1 Led3 Alias Porta.2 Set Porta.0 Set Porta.1 Set Porta.2 Time$ = "12:00:00" Date$ = "01.01.09" Cursor Off Cls Enable Interrupts Wait 1 A = "tekst" Gbdriveerror = Driveinit() If Gbdriveerror = 0 Then Errorcode = Initfilesystem(1) Locate 2 , 1 Lcd Errorcode If Errorcode <> 0 Then Blad_sd = 1 Else Locate 1 , 1 Lcd "Zapis" Wait 1 Open "testtest.txt" For Output As #2 Write #2 , A Flush #2 Close #2 Locate 1 , 1 Lcd "Koniec zapisu" End If Else Locate 2 , 1 Lcd "blad" End If Wait 1 Cls Do Locate 1 , 1 Lcd Disksize() Locate 2 , 1 Lcd Diskfree() Locate 3 , 1 Lcd Date$ Locate 4 , 1 Lcd Time$ Loop Config mmc '------------------------------------------------------------------------------- ' Config_MMC.BAS ' Config File for MMC Flash Cards Driver ' (c) 2003-2005 , MCS Electronics / Vögel Franz Josef '------------------------------------------------------------------------------- ' Place MMC.LIB version 5.6 in the LIB-Path of BASCOM-AVR installation ' 'Connection as following 'MMC M128/M103 M32 '1 MMC_CS PORTB.0 MMC_CS PORTB.4 '2 MOSI PORTB.2 MOSI PORTB.5 '3 GND GND '4 +3.3V +3.3V '5 CLOCK PORTB.1 CLOCK PORTB.7 '6 GND GND '7 MISO, PORTB.3 MISO, PORTB.6 ' you can vary MMC_CS on HW-SPI and all pins on SOFT-SPI, check settings ' ========== Start of user definable range ===================================== ' you can use HW-SPI of the AVR (recommended) or a driver build in Soft-SPI, if ' the HW-SPI of the AVR is occupied by an other SPI-Device with different settings ' Declare here you SPI-Mode ' using HW-SPI: cMMC_Soft = 0 ' not using HW_SPI: cMMC_Soft = 1 Const Cmmc_soft = 0 #if Cmmc_soft = 0 ' --------- Start of Section for HW-SPI ---------------------------------------- ' define Chip-Select Pin Config Pinb.4 = Output ' define here Pin for CS of MMC/SD Card Mmc_cs Alias Portb.4 Set Mmc_cs ' Define here SS Pin of HW-SPI of the CPU (f.e. Pinb.0 on M128) 'Config Pinb.4 = Output ' define here Pin of SPI SS 'Spi_ss Alias Portb.4 'Set Spi_ss ' Set SPI-SS to Output and High por Proper work of ' SPI as Master ' HW-SPI is configured to highest Speed Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = High , Phase = 1 , Clockrate = 4 , Noss = 1 Spsr = 1 ' Double speed on ATMega128 default = commented changed by author Spiinit ' Init SPI ' --------- End of Section for HW-SPI ------------------------------------------ #else ' Config here SPI pins, if not using HW SPI ' --------- Start of Section for Soft-SPI -------------------------------------- ' Chip Select Pin => Pin 1 of MMC/SD Config Pinb.4 = Output Mmc_cs Alias Portb.4 Set Mmc_cs ' MOSI - Pin DI => Pin 2 of MMC/SD Config Pinb.5 = Output Set Pinb.5 Mmc_portmosi Alias Portb Bmmc_mosi Alias 2 ' MISO - Pin D0 => Pin 7 of MMC/SD Config Pinb.6 = Input Mmc_portmiso Alias Pinb Bmmc_miso Alias 3 ' SCK - Pin CLK => Pin 1 of MMC/SD Config Pinb.1 = Output Set Pinb.7 Mmc_portsck Alias Portb Bmmc_sck Alias 1 ' --------- End of Section for Soft-SPI ---------------------------------------- #endif ' ========== End of user definable range ======================================= ' Error Const Cperrdrivereset = 225 ' Error response Byte at Reset command Const Cperrdriveinit = 226 ' Error response Byte at Init Command Const Cperrdrivereadcommand = 227 ' Error response Byte at Read Command Const Cperrdrivewritecommand = 228 ' Error response Byte at Write Command Const Cperrdrivereadresponse = 229 ' No Data response Byte from MMC at Read Const Cperrdrivewriteresponse = 230 ' No Data response Byte from MMC at Write Const Cperrdrive = 231 Const Cperrdrivenotsupported = 232 ' return code for DriveGetIdentity, not supported yet Waitms 1 ' Wait some time before initialising MMC/SD Dim Gbdriveerror As Byte ' General Driver Error register Dim Gbdriveerrorreg As Byte ' Driver load Error-Register of HD in case of error Dim Gbdrivestatusreg As Byte ' Driver load Status-Register of HD on case of error Dim Gbdrivedebug As Byte $lib "MMC.LIB" ' link driver library $external _mmc Gbdriveerror = Driveinit() ' Init MMC/SD Card
IC3
7805T
1
VI
+5V
VO
GND
C4
2
C3
3
+
D1
1N4004
X1-1
C7
X1-2
GND
GND
+3V3
IN
OUT
ADJ
R8
IC4
+5V
SV1
2
4
6
C10
+5V
SV2-1
SV2-2
SV2-3
SV2-4
SV2-5
SV2-6
SV2-7
SV2-8
SV2-9
SV2-10
SV2-11
SV2-12
SV2-13
SV2-14
SV2-15
SV2-16
+5V
GND
X2-3
X2-2
X2-1
R11
MEGA32-P
WRITE_PROT
CARD_DETECT
9
6
5
4
+3V3
R13
LED1
1
3
5
R2
GND
GND
6173-15XXXX
COMMON
21
20
19
18
17
16
15
14
3
1
(OC2)PD7
(ICP)PD6
(OC1A)PD5
(OC1B)PD4
(INT1)PD3
(INT0)PD2
(TXD)PD1
(RXD)PD0
R1
29
28
27Q1
26
25
24
23
22
R4
+
(TOSC2)PC7
(TOSC1)PC6
(TDI)PC5
(TDO)PC4
(TMS)PC3
(TCK)PC2
(SDA)PC1
(SCL)PC0
R6
R3
R10
VCC
GND
R5
8
7
6
5
4
3
2
1
R12
10
11
(SCK)PB7
(MISO)PB6
(MOSI)PB5
(SS)PB4
(AIN1/OC0)PB3
(AIN0/INT2)PB2
(T1)PB1
(T0/XCK)PB0
R14
AREF
AVCC
AGND
LED2
XTAL1
33
34
35
36
37
38
39
40
GND
+5V
XTAL2
(ADC7)PA7
(ADC6)PA6
(ADC5)PA5
(ADC4)PA4
(ADC3)PA3
(ADC2)PA2
(ADC1)PA1
(ADC0)PA0
LED3
C2 C1
Q2
C9
12
13
+5V
RESET
32
30
31
+5V
9
R15
IC1
R7
2
U$1
+5V
GND
8
C8
7
+
C6
R9
C5