Counting led blinks..


Closed Thread
Results 1 to 40 of 93

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default Re: Counting led blinks..

    Code:
    ' Name        : Coin_Internet_CDC.pbp' Compiler    : PICBASIC PRO Compiler 2.6
    ' Assembler   : PM or MPASM
    ' Target PIC  : PIC18F4550 
    ' Hardware    : Easypic6 Experimenter Board
    ' Oscillator  : 4MHz external crystal (only???)
    ' Thanks      :  To Henrik, and other PBP experts 
    ' Description : PICBASIC PRO program to show virtual serial comm. Working
    
    
    asm
       __CONFIG    _CONFIG1L, _PLLDIV_1_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L   
        __CONFIG    _CONFIG1H, _FOSC_HSPLL_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
        __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
    PAUSE 100 
    Include    "cdc_desc.bas"    ' Include the HID descriptors  
    Buffer    VAR BYTE[10]
    Buffer2    VAR BYTE[10]
    Cnt       VAR BYTE
    
    
    b0   var byte
    B1   VAR BYTE   ' Working buffer 1 for button command
    sel   VAR BYTE   ' Working buffer 2 for button command
    B3   VAR BYTE   ' Working buffer 3 for button command
    i    var byte
    Clear        ' Clear buffers
    
    
    INTCON2.7 = 0        ' Enable PORTB pull-ups
    TRISB = 110000          ' Enable all buttons
    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
    TRISA.0 = 0 ' RA0 = output for LED
    CMCON = 7   ' All digital 
    
    
    'If you prefer, then increment on low-to-high transitions
    T0CON   = %11111000    
    
    
    '*****************************************************
    USBInit                   ' Initialize USART
    ' Main Program Loop
    Loop1:
    Lcdout $fe, 128, "Counter: ", #i 
    USBService ' Must service USB regularly
    TMR0L = 0     ' Clear TMR0 count before start   
    '**************ADDED COde
    'gosub usbtx
    '**************************** 
    'goto Loop1
    
    
    Loop2:
        USBService
        WHILE TMR0L = 0 ' Wait for high-to-low transition 
        USBService       
        WEND           ' on RA4/T0CKI
        pause 5
        PORTB.0 = 1    ' LED on to indicate transition seen 
        i=i+1       ' Clear screen
        USBService
        buffer[0] = 49
        Buffer[1] = 13       
        USBOut 3, Buffer, cnt,Loop2
        Lcdout $fe, 128, "Counter: ", #i   
        'PAUSE 30
        PORTB.0 = 0    ' LED off
        goto Loop1
    Hi mackrackit, the above code works. Now what I'm trying to do is add a ntoher line of code to send "c" in the main loop but I can not seem to make it work.

    Code:
    asm   __CONFIG    _CONFIG1L, _PLLDIV_1_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L   
        __CONFIG    _CONFIG1H, _FOSC_HSPLL_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
        __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
    PAUSE 100 
    Include    "cdc_desc.bas"    ' Include the HID descriptors  
    Buffer    VAR BYTE[10]
    Buffer2    VAR BYTE[10]
    Cnt       VAR BYTE
    
    
    b0   var byte
    B1   VAR BYTE   ' Working buffer 1 for button command
    sel   VAR BYTE   ' Working buffer 2 for button command
    B3   VAR BYTE   ' Working buffer 3 for button command
    i    var byte
    Clear        ' Clear buffers
    
    
    INTCON2.7 = 0        ' Enable PORTB pull-ups
    TRISB = 110000          ' Enable all buttons
    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
    TRISA.0 = 0 ' RA0 = output for LED
    CMCON = 7   ' All digital 
    
    
    'If you prefer, then increment on low-to-high transitions
    T0CON   = %11111000        
    
    
    '*****************************************************
    
    
    USBInit                   ' Initialize USART
    
    
    
    
    ' Main Program Loop
    Loop1:
    Lcdout $fe, 128, "Counter: ", #i 
    USBService ' Must service USB regularly
    TMR0L = 0     ' Clear TMR0 count before start   
    '**************ADDED COde
    gosub usbtx   ' transmit character c to pc
    '**************************** 
    Loop2:
        USBService
        WHILE TMR0L = 0 ' Wait for high-to-low transition 
        USBService       
        WEND           ' on RA4/T0CKI
        pause 5
        PORTB.0 = 1    ' LED on to indicate transition seen 
        i=i+1       ' Clear screen
        USBService
        buffer[0] = 49
        Buffer[1] = 13       
        USBOut 3, Buffer, cnt,Loop2
        Lcdout $fe, 128, "Counter: ", #i   
        'PAUSE 30
        PORTB.0 = 0    ' LED off
        goto Loop1   
        
    usbtx:
    USBService
    Buffer2[0] = 99
    Buffer2[1] = 13       
    USBOut 3, Buffer2, cnt,usbtx
    return
    The above code I use to attempt sending to pc...BTW I dont know what happened (T0CON = %11111000 and ADCON1 = %00001111) to the bits (but I think I did not alter it).

    thanks,
    tacbanon

  2. #2
    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..

    Does the Loop2 part still work in the code with the usbtx sub routine? It looks to me like it should work. Or maybe it is too long between USBservices.

    If that is the case include the routine to service the USB in the background like the example gave for testing.
    Dave
    Always wear safety glasses while programming.

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


    Did you find this post helpful? Yes | No

    Default Re: Counting led blinks..

    Hi, yes Loop2 still works(i increments), but I wonder why usbtx does not sends the "c" character to pc?

    regards,
    tacbanon

  4. #4
    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.

  5. #5
    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  

  6. #6
    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.

  7. #7
    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

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