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

    Hi everyone, now the usb device is recognized and its in Port COM2. My only problem is that my pulse counter variable i is reseting back to zero when a pulse(acctually I'm using a button on RA4 to simulate the pulse) is detected. In the code I did not yet insert the command "USBOut 3, buffer, cnt, Main" to send to pc.
    Code:
    asm    ;__CONFIG    _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
        __CONFIG    _CONFIG1L, _PLLDIV_2_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L 
        __CONFIG    _CONFIG1H, _FOSC_XTPLL_XT_1H & _FCMEN_OFF_1H & _IESO_OFF_1H 
                                ;                  ;               ; 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
    PAUSE 100
    
    
    Include    "cdc_desc.bas"    ' Include the HID descriptors 
    buffer    Var    Byte[16]
    Cnt       VAR BYTE 
    Cnt = 2
     
    
    
    TRISA.4 = 1 ' RA4/T0CKI = input to TMR0 counter
    TRISA.0 = 0 ' RA0 = output for LED
    CMCON = 7   ' All digital 
    ADCON1 = 001111      ' A/D converter off
    
    
    'If you prefer, then increment on low-to-high transitions
    T0CON   = 111000       
    USBInit                   ' Initialize USART
    
    
    i var byte
    i=0
    ' Assign prescaler to WDT for 1:1 prescale on TMR0
    ' TMR0 clock will be from your external input on RA4/T0CKI
    ' Increment on high-to-low transitions
    
    
                                '         
    Lcdout $fe, 128, "Counter: ", #i 
    'pause 20
    Main:
        USBService    ' Must service USB regularly
        TMR0L = 0     ' Clear TMR0 count before start    
        
    Loop1:
        
        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       ' increment i 
        Lcdout $fe, 128, "Counter: ", #i 
        USBService
        PORTB.0 = 0    ' LED off
    I appreciate any help...

    thanks in advance,
    tacbanon
    Last edited by tacbanon; - 6th October 2011 at 13:12.

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Counting led blinks..

    When you enter the Loop1 routine you sit at the WHILE-WEND loop waiting for a pulse. When a pulse comes along you run thru the rest of the routine and finally pull PortB.0 low....then what happens?

    There's no GOTO or END or anything after the Loop1 routine so the program will continue and continue and continue thru the (empty) codespace untill it reaches the end, then it'll wrap around and start over at the beginning and what happens there? Yep - you reset the count (i = 0) ;-)

    By the way, you're now incrementing and displaying the i variable and not the TMR0 count but I guess that's intentional for now?

    /Henrik.

  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 your'e right Henrik "Goto Main" was all it need.
    By the way, you're now incrementing and displaying the i variable and not the TMR0 count but I guess that's intentional for now?
    I think TMR0 and variable i are the same but only not reseting to zero, and I need it for visual.

    I hope they can allow me here to continue, because I'm planning to incorporate an SD card module to hold the number of coins.

    regards,
    tacbanon

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


    Did you find this post helpful? Yes | No

    Default Re: Counting led blinks..

    Hi, I would like to incorporate an sdcard to the project and record the number of coins, only when pressing a button (e.g RB7) , but first I want to make a seperate test program using Pic18F4550. As I understood the connection should be simple enough but just to make sure I attached an image of my connections(Please correct me if it's wrong). I found a source code but it's in C.
    Code:
     // example writing to SD card, sford 
     #include "mbed.h"
     #include "SDFileSystem.h"
     SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool  Components workshop board
     
     int main() {
         printf("Hello World!\n");   
     
         mkdir("/sd/mydir", 0777);
         
         FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
         if(fp == NULL) {
             error("Could not open file for write\n");
        }
        fprintf(fp, "Hello fun SD Card World!");
        fclose(fp); 
    
    
       printf("Goodbye World!\n");
     }
    The code creates a folder and writes "Hello fun SD Card World" to file name "sdtest.txt".
    Hope anyone can help...

    thanks in advance,
    tacbanon
    Attached Images Attached Images  

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Counting led blinks..

    Hi,
    That C-example does nothing for or with PBP, the C-compiler used in the example (which ever it is) has a built in library function to support a SD-card. There is no such function built into PBP. HOWEVER there is a SD-card example on (who would've thought) the examples page at MELABS (SDFS3.pbp).

    With that said do you really need a SD-card? How much info do you need to store? The 4550 has 256bytes of EEPROM, can't you use that? Not that having a SD-card is "impossible" but using the EEPROM is going to be MUCH (I mean MUCH) easier than - which I think you'll see once you look at the example code.

    /Henrik.

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


    Did you find this post helpful? Yes | No

    Default Re: Counting led blinks..

    Hi Henrik, thanks for the responce. To be honest I already saw the link, but I'm skeptical because I dont really understand how it works(not simple for me). But I'm willing to try it out. I need to incorporate the sdcard because I'm trying to create a "coin operated computer rental app" I want later to save the number of coins and date/time per transaction. I also want to learn the EEPROM stuff but I was thinking about the capacity and since SDCARD can be quite cool (if I manage to incorporate it). I will try the link and post the results.

    regards,
    tacbanon

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

    Maybe you can get some use from this?
    http://www.picbasic.co.uk/forum/cont...USB-SD-LOGGING
    Dave
    Always wear safety glasses while programming.

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