PSoCのSDモジュールを使ってみる

by K.I
2006/09/01〜

Index


概要


[top]

SDcardモジュール

インストール

→Beta版の所為か白黒で、ちょっと寂しいけど

MMC基板を作る

→半田付けしたのはJ1以下のSDカード接続のみ

モジュールを配置する

プログラム

#include <m8c.h>        // part specific constants and macros  
#include "PSoCAPI.h"    // PSoC API definitions for all User Modules  
  
#define  CARD_PRESENT      1  
#define  CARD_NOT_PRESENT  0  
  
char helloFile[] = "hello.txt";  
  
void main()  
{  
   char   cardInfo;                      // Card information  
   char   fp;                            // File Pointer  
   BYTE   CardState, oldCardState;       // Card inserted state variables  
  
   LED_1_Start();  
  
   LED_1_Switch(0);                      // Make sure LED is Off  
   oldCardState = CARD_NOT_PRESENT;      // Initialize card insertion state  
   SDCard_1_Start();                     // initialize hardware and SDCard_lib buffers  
  
   while(1)   
   {  
      CardState = SDCard_1_Present();     // Get current card insertion state  
      if(CardState != oldCardState)       // Check for a change  
      {  
         oldCardState = CardState;        // Save last state  
         if(CardState == CARD_PRESENT)    // Card inserted  
         {  
            LED_1_Switch(1);              // About to talk to card, turn on busy LED  
            SDCard_1_Select(SDCard_1_ENABLE); // Select card  
            cardInfo = 0;  
            while ( ! cardInfo )          // Wait for card to communicate  
            {  
               // initialize card, determine card type and file system type  
               cardInfo = SDCard_1_InitCard();  
            }  
  
            fp = SDCard_1_fopen(helloFile,"a");      // Open file to append data  
            SDCard_1_fputcs("Hello World\r\n", fp);  // Write string   
            SDCard_1_fclose(fp);                     // Close file  
            SDCard_1_Select(SDCard_1_DISABLE);       // Deselect card  
            LED_1_Switch(0);                         // Turn off busy LED  
         }  
      }  
   }  
}  

コンパイル時のエラー

動かしてみる


1これがパズルみたいで面白い。ブレッドボードを使う醍醐味?

[top]

GPSをつないでみよう

グローバルバスとI/Oの接続切替え

ダイナミック・リコンフィグレーション

GPSのデータ

→MiniProgで電源を供給3する
        $GPGGA,,2458.9724,N,12125.6550,E,0,00,00.00,000025.0,M,0015.9,M,,*79
        $GPZDA,005416,01,10,2005,+00,00*62
        $GPGSV,2,1,08,05,44,057,29,06,05,165,29,09,37,033,29,14,43,335,29*7E
        $GPGSV,2,2,08,17,12,191,29,18,58,309,31,21,55,314,31,30,38,189,27*70
        $GPVTG,,T,,M,,N,,K,N*2C
        $GPRMC,,V,2458.9724,N,12125.6550,E,,,,003.5,W,N*17

書き込みテストプログラム

#include <m8c.h>                        // part specific constants and macros
#include "PSoCAPI.h"                    // PSoC API definitions for all User Modules

#define  CARD_PRESENT      1
#define  CARD_NOT_PRESENT  0

char gps_file[] = "hhmmss.gps";

void main()
{
        char   cardInfo;                                // Card information
        char   fp;                                      // File Pointer
        BYTE   CardState, oldCardState;                 // Card inserted state variables
        char c;
        int i=0;

// ---- UART setup
        UART_1_IntCntl(UART_1_ENABLE_RX_INT);           // Enable RX interrupts
        UART_1_Start(UART_1_PARITY_NONE);               // Enable UART
        M8C_EnableGInt ;                                // Turn on interrupts
        
        LED_1_Start();

        LED_1_Switch(0);                                // Make sure LED is Off
        oldCardState = CARD_NOT_PRESENT;                // Initialize card insertion state
        SDCard_1_Start();                               // initialize hardware and SDCard_lib buffers

        while(1) {
                CardState = SDCard_1_Present();                 // Get current card insertion state
                if(CardState != oldCardState) {                 // Check for a change
                        oldCardState = CardState;               // Save last state
                        if(CardState == CARD_PRESENT) {         // Card inserted
                                LED_1_Switch(1);                        // About to talk to card, turn on busy LED
                                SDCard_1_Select(SDCard_1_ENABLE);       // Select card
                                cardInfo = 0;
                                while ( ! cardInfo ) {                  // Wait for card to communicate
                                        cardInfo = SDCard_1_InitCard();
                                }
                                fp = SDCard_1_fopen(gps_file,"a");      // Open file to append data

                                while (UART_1_bReadRxData()!='$');
                                SDCard_1_fputc('$',fp);                 // Write char

                                while (1) {
                                        c = UART_1_bReadRxData();       // Read char
                                        if (c == '$') if (i++>10) break;
                                        SDCard_1_fputc(c,fp);           // Write char
                                }

                                SDCard_1_fclose(fp);                    // Close file
                                SDCard_1_Select(SDCard_1_DISABLE);      // Deselect card
                                LED_1_Switch(0);                        // Turn off busy LED
                        }
                }
        }
}

壊しちゃったかな?

APIって何やってるの?

最終的なプログラム

#include <m8c.h>                        // part specific constants and macros
#include "PSoCAPI.h"                    // PSoC API definitions for all User Modules

#define  CARD_PRESENT      1
#define  CARD_NOT_PRESENT  0

char gps_file[] = "hhmmss.gps";

void main()
{
        char    cardInfo;                               // Card information
        char    fp;                                     // File Pointer
        BYTE    CardState, oldCardState;                // Card inserted state variables
        char    c;
        int             i=0;

// ---- UART setup
        UART_1_Start(UART_1_PARITY_NONE);               // Enable UART
        
        LED_1_Start();

        LED_1_Switch(0);                                // Make sure LED is Off
        oldCardState = CARD_NOT_PRESENT;                // Initialize card insertion state
        SDCard_1_Start();                               // initialize hardware and SDCard_lib buffers

        while(1) {
                CardState = SDCard_1_Present();                 // Get current card insertion state
                if(CardState != oldCardState) {                 // Check for a change
                        oldCardState = CardState;               // Save last state
                        if(CardState == CARD_PRESENT) {         // Card inserted
                                LED_1_Switch(1);                        // About to talk to card, turn on busy LED
                                SDCard_1_Select(SDCard_1_ENABLE);       // Select card
                                cardInfo = 0;
                                while ( ! cardInfo ) {                  // Wait for card to communicate
                                        cardInfo = SDCard_1_InitCard();
                                }

                                while (1) {
                                        while (UART_1_cGetChar() != '$');
                                        if (UART_1_cGetChar() != 'G') continue;
                                        if (UART_1_cGetChar() != 'P') continue;
                                        if (UART_1_cGetChar() != 'Z') continue;
                                        if (UART_1_cGetChar() != 'D') continue;
                                        if (UART_1_cGetChar() != 'A') continue;
                                        if (UART_1_cGetChar() == ',') break;
                                }

//                              gps_file[0] = UART_1_cGetChar();        // Read hour
//                              gps_file[1] = UART_1_cGetChar();
//                              gps_file[2] = UART_1_cGetChar();        // Read minute
//                              gps_file[3] = UART_1_cGetChar();
//                              gps_file[4] = UART_1_cGetChar();        // Read second
//                              gps_file[5] = UART_1_cGetChar();

                                fp = SDCard_1_fopen(gps_file,"a");              // Open file to append data

                                while (UART_1_cGetChar() != '$');
                                SDCard_1_fputc('$',fp);                         // Write char

                                while (1) {
                                        c = UART_1_cGetChar();                  // Read char
                                        if (c == '$') if (i++>10) break;
                                        SDCard_1_fputc(c,fp);                   // Write char
                                }

                                SDCard_1_fclose(fp);                    // Close file
                                SDCard_1_Select(SDCard_1_DISABLE);      // Deselect card
                                LED_1_Switch(0);                        // Turn off busy LED
                        }
                }
        }
}

StringがFlush上にある?

SDカードのフォーマット


2でも、これですんなりは行かなかったんだけれども。。
3でも、GPSモジュールを接続すると、電源電圧が4.57Vまで低下する。実際、ちょっとキツイかな。。

[top]

実際に記録してみよう

電源はどうする

→5Vレギュレータをブレッドボードに追加 →ダッシュボードに載せてガムテープで止める

データが化けてる

データ化けを除く

結果は

→部品はこれだけ、配線もこれだけ!
[top] [電子工作関連に戻る]

comments powered by Disqus