• PicŪ Basic


  • USB Memory Stick

    Here's a program I wrote to teach myself how to log data from an alarm system to a memory stick, it's actually quite simple to do. The program logs a simulated Time, Date, Alarm code etc.. and writes it to a file called log.txt This was built using the Parallax FTDI data logger P/N 27937. for production I'de recommend the OEM module from www.digikey.com P/N 768-1003-ND Just US$ 18.50each for qnt of 100

    Code:
    '****************************************************************
    '*  Name    : 452.BAS                                           *
    '*  Author  : Peter Davies                                      *
    '*  Notice  : Copyright (c)2011 Pear Alliance    www.pear.co.za *
    '*          : If it helps, feel free to use this code.          *
    '*  Date    : 2011/01/nn                                        *
    '*  Version : 1.0                                               *
    '*  Notes   : Learning module, write data to a USB stick        *
    '*          : Built on VeroBoard                                *
    '****************************************************************
        Device    18F452
        Declare Reminders = Off
        Declare Warnings = Off
        Declare Optimiser_Level = 2
        Declare Dead_Code_Remove = On
        Declare Xtal 20
    
    ' Device Fuse configuration
    
       @CONFIG_REQ
       @__CONFIG config1h, OSCS_OFF_1 & HS_OSC_1
       @__CONFIG config2l, BOR_ON_2 & BORV_20_2 & PWRT_ON_2
       @__CONFIG config2h, WDT_OFF_2 & WDTPS_128_2
       @__CONFIG config3h, CCP2MX_ON_3
       @__CONFIG config4l, STVR_ON_4 & LVP_OFF_4 & DEBUG_OFF_4
    
       All_Digital = TRUE    ' 
      
       Hserial_Baud      = 9600      ' Set baud rate to 9600
       Hserial_RCSTA     = %10010000  ' Enable serial port and continuous receive
       Hserial_TXSTA     = %00100100  ' Enable transmit and High Speed asynchronous mode 
       Hserial_Clear     = On         ' Optionally clear the buffer before receiving
          
       Declare LCD_Interface = 4 ' 4-bit Interface
       Declare LCD_DTPin = PORTB.4
       Declare LCD_RSPin = PORTB.3
       Declare LCD_ENPin = PORTB.2
       Declare LCD_Lines = 4
       
    '==============================================================================       
        TRISA  = %00000000
        TRISB  = %00000000
        TRISC  = %10100000
        TRISD  = %01100000
    ' ===============================================================================
    
             Symbol USBtx   = PORTC.7
             Symbol USBrx   = PORTC.6
             Symbol USBrts  = PORTD.3
             Symbol USBcts  = PORTC.5
             
            ' Varibles defined
            Dim Var1       As Byte   
            Dim var2       As Byte
            Dim index      As Byte     
            Dim n          As Byte
            ' arrays
            Dim Buffer[16] As Byte ' FTDI rx and tx Bufer
            Dim Dbuff[10]  As Byte ' Date Buffer
            Dim Tbuff[5]   As Byte ' Time Buffer
            Dim Acode[4]   As Byte ' Alarm code
            Dim Tcode[6]   As Byte ' Type of alarm, (breakin)(panic)(test) etc..
            
            ' Preload time and date with anything for now, later will add DS1307
            Dbuff[0] = "2" : Dbuff[1] = "6" : Dbuff[2] = "-" : Dbuff[3] = "0" : Dbuff[4] = "7" : Dbuff[5] = "-"
            Dbuff[6] = "2" : Dbuff[7] = "0" : Dbuff[8] = "1" : Dbuff[9] = "1"
            
            Tbuff[0] = "1" : Tbuff[1] = "0" : Tbuff[2] = "h" : Tbuff[3] = "3" : Tbuff[4] = "0" :
            
            Acode[0] = "3" : Acode[1] = "4" : Acode[2] = "5" : Acode[3] = "6" 
            Tcode[0] = "P" : Tcode[1] = "A" : Tcode[2] = "N" : Tcode[3] = "I" : Tcode[4] = "C" : Tcode[5] = " "
            
    DelayMS 20
      Cls
    
        
     USBrts = 0             ' Take FTDI out of reset      
     DelayMS 100
     
     ' check for echo
     Cls
     HRSOut  "E",$0D  ' e and CR
     Var1 = HRSIn,{100, nOe}
     If Var1 = "E" Then
     Print At 1,1, " FTDI Init ok "
     DelayMS 200
     
     EndIf   
    
    ' set short commands
     Cls
     HRSOut  $10,$0D
     Print At 1,1, " Short code Init ok "
     DelayMS 200
       
     
     
     'Open file "log.txt"
     Cls
     HRSOut  $09,$20,"log.txt",$0D
     Print At 1,1, " log.txt open"
    
     DelayMS 200
     
     
     
    '======================================
    ' WRITE TO THE FILE
    '-------------------------------------- 
    ' write to file, and file size
     HRSOut  $08,$20,$00,$00,$00,59,$0D
    '--------------------------------------
    ' CR and LF                            
     HRSOut  $0D,$0A,
    '--------------------------------------
    ' add date to the stamp 
     HRSOut "  Date  "            
     For index = 0 To 9
     HRSOut Dbuff[index]
     Next
     HRSOut ","
     '-------------------------------------
     ' add time to the stamp
     HRSOut " Time  "    
     For index = 0 To 4
     HRSOut Tbuff[index]
     Next
     HRSOut ","
    '--------------------------------------
    ' add alarm code
     HRSOut " Alarm  "    
     For index = 0 To 3
     HRSOut Acode [index]
     Next
     HRSOut ","
    '--------------------------------------
    ' add alarm type
     HRSOut " Type  "    
     For index = 0 To 5
     HRSOut Tcode [index]
     Next
     HRSOut ","
    '--------------------------------------
    ' Finished adding to the file
     HRSOut   $0D    
     '-------------------------------------            
     ' LCD the result
     Print At 1,1, Dec n          
     GoSub Get_Data
     GoSub Print_Data 
    '--------------------------------------
    ' close and save the file  
     HRSOut  $0A,$20,"log.txt",$0D
     GoSub Get_Data
     GoSub Print_Data
     Print At 1,1, " File closed    "
    '--------------------------------------
             jj: 
        Print At 4,1,"jj       "
        GoTo jj
    
      
    '===============================================
    ' Error messages  
    '-----------------------------------------------
    nOe:
        Cls 
        Print At 1,1, " FTDI init Fail"
        DelayMS 1000
    Return
    '---------------------------------------------- 
    '==============================================
    ' Get Data from the FTDI
     Get_Data:
        ' clear the buffer   
        Var1 = " "
        For index = 0 To 14
        Buffer[index] = Var1
        Next
        
        index = 0
          Repeat
            Var1 = HRSIn, {1000,nMd}
            Buffer[index] = Var1
            index = index + 1
            If index >14 Then Break 
          Until index > 14
    nMd:  ' NO More Data      
    Return 
    '===============================================
    ' Print Data
     Print_Data:
    Print At 4,1,"PrnDat"
    
    Print At 3,1," "
    For index = 0 To 14
    Print Buffer[index]
    Next
    DelayMS 1000
    Cls
    Return
    '===============================================
    One thing I found is that it doesn't like memory sticks greater than 2GIG ?? dunno why.
    This article was originally published in forum thread: USB Memory Stick started by Peter D View original post
  • Recent Activity

    amod-29593

    Pulse counter memory problem

    Thread Starter: amod

    Please tell me my faults.My pulse counter memory does not store.When i switch on device after 5 second ,memory exist by after 30 seconds it is washed...

    amod Today, 15:25 Go to last post