Counting led blinks..


Closed Thread
Results 1 to 40 of 93

Hybrid View

  1. #1
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: Counting led blinks..

    Give this a shot. It works here.

    Code:
    INCLUDE "cdc_desc.bas"
    INCLUDE "USB_ASM_Service.pbp"    ' Base Interrupt System
    
    
    LED       VAR PORTB.0
    Buffer    VAR BYTE[10]
    Buffer2    VAR BYTE[10]
    Cnt       VAR BYTE
    
    
    i    var byte
    ADCON1 = 15               ' Set all I/Os to Digital      
    CMCON = 7                 ' Disable Comparators
    Cnt = 5
    i = 0
    '****************************************************
    TRISA.4 = 1 ' RA4/T0CKI = input to TMR0 counter
    'If you prefer, then increment on low-to-high transitions
    T0CON   = 111000        
    LOW LED
    Loop1:
    TMR0L = 0     ' Clear TMR0 count before start   
    '**************ADDED COde
    gosub usbtx   ' transmit character c to pc
    '**************************** 
    Loop2:
    WHILE TMR0L = 0 ' Wait for high-to-low transition      
    WEND           ' on RA4/T0CKI
    pause 5
    PORTA.2 = 1    ' LED on to indicate transition seen     LED PORTA.2 
    i=i+1       ' Clear screen
    buffer[0] = 49
    Buffer[1] = 13       
    USBOut 3, Buffer, 2,Loop2
    LOW LED   ' LED off
    goto Loop1   
    
    
    usbtx:
    TOGGLE LED
    Buffer2[0] = 99
    Buffer2[1] = 13       
    USBOut 3, Buffer2,2,usbtx
    return
    Dave
    Always wear safety glasses while programming.

  2. #2
    Join Date
    Jun 2011
    Location
    Philippines
    Posts
    223


    Did you find this post helpful? Yes | No

    Default Re: Counting led blinks..

    Hi, thanks for the time. I run your code...but the character "c" appears only when a pulse is detected in my setup. Hmm it should be running as expected...

    regards,
    tacbanon
    Attached Images Attached Images  

  3. #3
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: Counting led blinks..

    That is what I thought you wanted?

    If you want it to send "C" continuously
    Code:
    WHILE TMR0L = 0 ' Wait for high-to-low transition  
      GOSUB usbtx  
      PAUSE 100 
    WEND           ' on RA4/T0CKI
    Dave
    Always wear safety glasses while programming.

  4. #4
    Join Date
    Jun 2011
    Location
    Philippines
    Posts
    223


    Did you find this post helpful? Yes | No

    Default Re: Counting led blinks..

    Hi, sorry if was not making clear..it's working now. acctually the reason I need to send "c" is to identify if the usb device is connected to the my pc application. My next step is to incorporate rtc and sdcards for record keeping. I will post my code later... thanks mackrackit.

    regards,
    tacbanon

  5. #5
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: Counting led blinks..

    glad I could help.
    Dave
    Always wear safety glasses while programming.

  6. #6
    Join Date
    Jun 2011
    Location
    Philippines
    Posts
    223


    Did you find this post helpful? Yes | No

    Default Re: Counting led blinks..

    Hi, sorry for the delay. I managed to send and display the content of the rtc to communication terminal using CDC.
    Next I tried to incorporate the SDcard using macrackit's codes. As I understood the code, once powered it should write a filename(timestamp) to sdcard. But When I checked the sdcard there was none.
    This is the code...
    Code:
    asm    ;_PLLDIV_2_1L         EQU  H'F9'    ; Divide by 2 (8 MHz oscillator input)
        
       ; __CONFIG    _CONFIG1L, _PLLDIV_2_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L  
        ;__CONFIG    _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
        ;__CONFIG    _CONFIG1H, _FOSC_HSPLL_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
        __CONFIG    _CONFIG1L, _PLLDIV_1_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L   
                                ;              ;                      ; USB clock source comes from the 96 MHz PLL divided by 2
                                ;              ; [OSC1/OSC2 Src: /1][96 MHz PLL Src: /2]
                                ; No prescale (4 MHz oscillator input drives PLL directly)
    
    
    
    
        ;__CONFIG    _CONFIG1H, _FOSC_XTPLL_XT_1H & _FCMEN_OFF_1H & _IESO_OFF_1H   '2
                                ;                  ;               ; Oscillator Switchover mode disabled
                                ;                  ; Fail-Safe Clock Monitor disabled
                                ; XT oscillator, PLL enabled, XT used by USB
        __CONFIG    _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L  & _BORV_2_2L  & _VREGEN_ON_2L   
        __CONFIG    _CONFIG2H, _WDT_OFF_2H 
        __CONFIG    _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_ON_3H 
        __CONFIG    _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L  & _XINST_OFF_4L & _DEBUG_OFF_4L 
        endasm
    DEFINE    OSC 48
    DEFINE LCD_DREG PORTB
    DEFINE LCD_DBIT 0
    DEFINE LCD_EREG PORTB
    DEFINE LCD_EBIT 5
    DEFINE LCD_RSREG PORTB
    DEFINE LCD_RSBIT 4
    DEFINE LCD_BITS 4
    DEFINE LCD_LINES 2
    DEFINE LCD_COMMANDUS 2000
    DEFINE LCD_DATAUS 50
    
    
    'RTC pins on 4550
    'SDA	Var	PORTB.0
    'SCL	Var	PORTB.1
      ' Alias PIC pins and registers for SD/MMC card
        SD_WE        VAR    PORTA.4    ' SD card write protect
        SD_WE_TRIS   VAR    TRISA.4    ' SD card write protect direction
        SDI          VAR    PORTA.5    ' SPI data in SD #7
        SDI_TRIS     VAR    TRISA.5    ' SPI data in direction
        SCL          VAR    PORTA.3    ' SPI clock  SD #5
        SCL_TRIS     VAR    TRISA.3    ' SPI clock direction
        SD_CS        VAR    PORTC.2    ' SD card chip select SD #1
        SD_CS_TRIS   VAR    TRISC.2    ' SD card chip select direction
        SD_CD        VAR    PORTC.0    ' SD card detect
        SD_CD_TRIS   VAR    TRISC.0    ' SD card detect direction
        SDO          VAR    PORTC.1    ' SPI data out   SD #2
        SDO_TRIS     VAR    TRISC.1    ' SPI data out direction
    INCLUDE "SDFS.PBP"
    SDC_UseHardSPI = FALSE    ' Use hardware SSP port for SPI.
    '########################################################################################3
        INCLUDE "cdc_desc.bas"     
        INCLUDE "DT_INTS-18.bas"
        INCLUDE "ReEnterPBP-18.bas"
    ASM
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?    
            INT_Handler   INT_INT,  _CNT_PLUS,   PBP,  yes      
            INT_Handler   USB_INT,  _SERVICE_USB,  ASM,  yes
        endm
        INT_CREATE               ; Creates the interrupt processor
    ENDASM
    '#########
    'START USB AND INTERRUPTS
        PAUSE 100           'TIME TO SETTLE
        USBINIT             'INITIALIZE USB
        USBSERVICE          'SERVICE USB
        UIE = $7F           'ENABLE USB INTERRUPTS
        UEIE = $9F          'ENABLE USB ERROR INTERRUPTS
        PAUSE 500           'MORE TIME
        USBINIT
        USBSERVICE
        @   INT_ENABLE  USB_INT
    '
    '##########################################################################################
    
    
     ADCON1 = %00001110
     LED      VAR PORTA.2
     TEXT_TIME VAR BYTE[6]
     TEXT_DATE VAR BYTE[6]
     CRON VAR     BYTE[11]
     CRON_D VAR   BYTE[11]
     TEXT_NO_CARD VAR BYTE[9]
     MAC_FileName VAR BYTE[11]
     T_BUFFER   VAR BYTE[8]
     T_ONES   VAR    BYTE
     T_TENS   VAR    BYTE
     T_HUNS   VAR    BYTE
     B0       VAR  BYTE
     B1       VAR  BYTE
     CNT      VAR  WORD
     CIU VAR     BYTE 'CARD IN USE 
     'RTC DEFINES
        DS_SCL      VAR     PORTB.1    'CLOCK
        DS_SDA      VAR     PORTB.2    'DATA
        RTC CON     %11010000
        SEC_REG CON $00
        CONT_REG CON $0E
        CNTRL CON %00000000
    '##########################################################################################
    'RTC VARS
        sec VAR BYTE: mins VAR BYTE: hr VAR BYTE: day VAR BYTE
        date VAR BYTE: mon VAR BYTE: yr VAR BYTE
    'RTC DEC VARS
         SEC_O VAR BYTE: SEC_T VAR BYTE: 
         MIN_O VAR BYTE: MIN_T VAR BYTE
         HR_O VAR BYTE:  HR_T VAR BYTE: 
         MON_O VAR BYTE: MON_T VAR BYTE
         DATE_O VAR BYTE: DATE_T VAR BYTE: 
         YR_O VAR BYTE: YR_T VAR BYTE
    'SD CARD FILE
         FILE_seconds VAR BYTE: FILE_minutes VAR BYTE: FILE_hours VAR BYTE
         FILE_day VAR BYTE: FILE_month VAR BYTE: FILE_year VAR BYTE
    '#########
    'SETS FILE NAME TO THE TIME BOARD IS POWERED
        GOSUB READ_RTC
        MAC_FileName[0] = $30+HR_T
        MAC_FileName[1] = $30+HR_O
        MAC_FileName[2] = $30+MIN_T
        MAC_FileName[3] = $30+MIN_O
        MAC_FileName[4] = $30+SEC_T
        MAC_FileName[5] = $30+SEC_O
        MAC_FileName[6] = " "
        MAC_FileName[7] = " "
        MAC_FileName[8] = "T"
        MAC_FileName[9] = "X"
        MAC_FileName[10] = "T"
    
    
    '##########################################################################################
    
    
        FOR B0 = 0 TO 5
         LOOKUP B0,[" TIME "],B1
         TEXT_TIME(B0) = B1
        NEXT B0
        FOR B0 = 0 TO 5
         LOOKUP B0,[" DATE "],B1
         TEXT_DATE(B0) = B1
        NEXT B0
        FOR B0 = 0 TO 9
        LOOKUP B0,[" NO CARD",$d,$a],B1
        TEXT_NO_CARD(B0) = B1
        NEXT B0
    '##########################################################################################
    
    
    CMCON = %00000111                   ' Comparators = off
    ADCON1 = %00001111      ' A/D converter off
    ' Initialize LCD
    LCDOUT $FE,1:FLAGS=0:PAUSE 250:LCDOUT $FE,1:PAUSE 250 
    
    
    Goto mainloop		' Skip over subroutines
    
    
    
    
    'ISRs
        CNT_PLUS:
             IF CIU = 1 THEN
             PWM LED,75,250
             ELSE
             IF (SD_WE = 0) AND (SD_CD = 0)  THEN
             TOGGLE LED
             ELSE
             LOW LED
             ENDIF
             ENDIF
             CNT = CNT + 1
        @ INT_RETURN
    
    
    '#########
    ' Subroutine to read time from RTC
    READ_RTC:
        I2CREAD DS_SDA, DS_SCL, RTC, SEC_REG, [sec,mins,hr,day,date,mon,yr]
        SEC_T = sec & $70
        SEC_T = SEC_T>>4
        SEC_O = sec & $0F
    
    
        MIN_T = mins & $70
        MIN_T = MIN_T>>4
        MIN_O = MINs & $0F
    
    
        HR_T = hr & $70
        HR_T = HR_T>>4
        HR_O = hr & $0F
    
    
        MON_T = mon & $70
        MON_T = MON_T>>4
        MON_O = mon & $0F
    
    
        DATE_T = date & $70
        DATE_T = DATE_T>>4
        DATE_O = date & $0F
    
    
        YR_T = yr & $70
        YR_T = YR_T>>4
        YR_O = yr & $0F
    
    
        CRON[0] = " "
        CRON[1] = $30+HR_T
        CRON[2] = $30+HR_O
        CRON[3] = ":"
        CRON[4] = $30+MIN_T
        CRON[5] = $30+MIN_O
        CRON[6] = ":"
        CRON[7] = $30+SEC_T
        CRON[8] = $30+SEC_O
        CRON[9] = $d
        CRON[10] = $a
    
    
        CRON_D[0] = " "
        CRON_D[1] = $30+MON_T
        CRON_D[2] = $30+MON_O
        CRON_D[3] = "/"
        CRON_D[4] = $30+DATE_T
        CRON_D[5] = $30+DATE_O
        CRON_D[6] = "/"
        CRON_D[7] = $30+YR_T
        CRON_D[8] = $30+YR_O
        CRON_D[9] = $d
        CRON_D[10] = $a  
        
        FILE_seconds = (SEC_T*10)+SEC_O
        FILE_minutes = (MIN_T*10)+MIN_O
        FILE_hours = (HR_T*10)+HR_O
        FILE_day = (DATE_T*10)+DATE_O
        FILE_month = (MON_T*10)+MON_O
        FILE_year = (YR_T*10)+YR_O 
    	Return
    
    
    mainloop:
           
    	Gosub READ_RTC		' Read the time from the RTC
    
    
            ' Display time on LCD
    	Lcdout $fe, 128,"Date:", CRON_D[1],CRON_D[2],CRON_D[3],CRON_D[4],CRON_D[5],CRON_D[6],CRON_D[7],CRON_D[8]
    	Lcdout $fe, $c0, "Time:", CRON[1], CRON[2],CRON[3],CRON[4],CRON[5],CRON[6],CRON[7],CRON[8]
    	Pause 500		
        GOSUB USB_DISPLAY
    	Goto mainloop
    
    
    
    
    '#########
        USB_DISPLAY:
        USBOUT 3, TEXT_TIME, 6, mainloop
        PAUSE 1
        USBOUT 3, CRON, 11, mainloop
        PAUSE 1
        USBOUT 3, TEXT_DATE, 6, mainloop
        PAUSE 1
        USBOUT 3, CRON_D, 11, mainloop
        PAUSE 1
    
    
        RETURN
    '#########
    
    
    '####################
    
    
    @ INT_RETURN
    
    
        SERVICE_USB:
            USBSERVICE
        @   INT_RETURN
    '#########
    
    
    'SD CARD ROUTINES
        SD_WRITE:
        CIU = 1
        SDINIT:
        ' FSInit initializes the card and reads all the preliminary information from it
        GOSUB FSInit
        IF (FAT_error != 0) THEN STOP
    
    
        ' Display card directory
        GOSUB FINDfirst     ' Find first file on card
        WHILE (FAT_error = 0)
            GOSUB FINDnext  ' Find next file on card
        WEND
    
    
        SDFILENAME:
        ' This section defines a specific short (8.3) filename
        ' Note that spaces are use in empty elements and must be upper case for Windows
        FAT_FileName[0] = MAC_FileName[0]
        FAT_FileName[1] = MAC_FileName[1]
        FAT_FileName[2] = MAC_FileName[2]
        FAT_FileName[3] = MAC_FileName[3]
        FAT_FileName[4] = MAC_FileName[4]
        FAT_FileName[5] = MAC_FileName[5]
        FAT_FileName[6] = MAC_FileName[6]
        FAT_FileName[7] = MAC_FileName[7]
        FAT_FileName[8] = MAC_FileName[8]
        FAT_FileName[9] = MAC_FileName[9]
        FAT_FileName[10] = MAC_FileName[10]
    
    
        FAT_seconds = FILE_seconds
        FAT_minutes = FILE_minutes
        FAT_hours = FILE_hours
        FAT_day = FILE_day
        FAT_month = FILE_month
        FAT_year = FILE_year+20
    
    
        SDOPEN_W:
        ' Open a file for write
        FAT_mode = "A"      ' Write mode APPEND
        GOSUB FSfopen   ' Open file pointed to by Byte array FAT_FileName
        IF (FAT_error = 10) THEN STOP
    
    
        SD_WRITE_FILE:
        ' Write to file
        FAT_src[0] = "T"
        FAT_src[1] = "I"
        FAT_src[2] = "M"
        FAT_src[3] = "E"
        FAT_src[4] = $d
        FAT_src[5] = $a
        FAT_src[6] = $30+HR_T
        FAT_src[7] = $30+HR_O
        FAT_src[8] = ":"
        FAT_src[9] = $30+MIN_T
        FAT_src[10] = $30+MIN_O
        FAT_src[11] = ":"
        FAT_src[12] = $30+SEC_T
        FAT_src[13] = $30+SEC_O
        FAT_src[14] = $d
        FAT_src[15] = $a
        FAT_src[16] = "D"
        FAT_src[17] = "A"
        FAT_src[18] = "T"
        FAT_src[19] = "E"
        FAT_src[20] = $d
        FAT_src[21] = $a
        FAT_src[22] = $30+MON_T
        FAT_src[23] = $30+MON_O
        FAT_src[24] = ":"
        FAT_src[25] = $30+DATE_T
        FAT_src[26] = $30+DATE_O
        FAT_src[27] = ":"
        FAT_src[28] = $30+YR_T
        FAT_src[29] = $30+YR_O
        FAT_src[30] = $d
        FAT_src[31] = $a
        FAT_src[32] = " "
        FAT_src[33] = $30+T_HUNS
        FAT_src[34] = $30+T_TENS
        FAT_src[35] = $30+T_ONES
        FAT_src[36] = " "
        FAT_src[37] = "F"
        FAT_src[38] = $d
        FAT_src[39] = $a
        FAT_src[40] = $d
        FAT_src[41] = $a
        FAT_count = 42
        GOSUB FSfwrite
    
    
        IF (FAT_error = 10) THEN STOP
        IF (FAT_error != 0) THEN STOP
    
    
        SDCLOSE:
        ' Close file
        GOSUB FSfclose
        IF (FAT_error != 0) THEN STOP
        PAUSE 5000
        CIU = 0
        GOTO mainloop
    I'm using Pic4550, 4Mhz crystal, PBP2.60. Can you tell me what I'm doing wrong in my setup?

    thanks in advance,
    tacbanon
    Attached Images Attached Images  
    Last edited by tacbanon; - 23rd October 2011 at 13:26.

  7. #7
    Join Date
    Jun 2011
    Location
    Philippines
    Posts
    223


    Did you find this post helpful? Yes | No

    Default Re: Counting led blinks..

    Okay I missed this line of code in the main loop "GOTO SD_WRITE".
    The modified mainloop..
    Code:
    mainloop:           
       Gosub READ_RTC        ' Read the time from the RTC 
       ' Display time on LCD
        Lcdout $fe, 128,"Date:", CRON_D[1],CRON_D[2],CRON_D[3],CRON_D[4],CRON_D[5],CRON_D[6],CRON_D[7],CRON_D[8]
        Lcdout $fe, $c0, "Time:", CRON[1], CRON[2],CRON[3],CRON[4],CRON[5],CRON[6],CRON[7],CRON[8]
        Pause 500        
        GOSUB USB_DISPLAY
        IF (SD_WE = 0) AND (SD_CD = 0)  THEN
        GOTO SD_WRITE
        ELSE
        PWM LED,25,250
        USBOUT 3, TEXT_NO_CARD, 10, mainloop
        PAUSE 500
        ENDIF   
        
    Goto mainloop
    But this time even the rtc output is not displaying in the communication terminal and freezed on the lcd as well...I'm lost with the sdcard code part.

    tacbanon
    Last edited by tacbanon; - 23rd October 2011 at 13:50.

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts