16LF1824 strange problem


Closed Thread
Results 1 to 32 of 32

Hybrid View

  1. #1
    Join Date
    Jan 2014
    Posts
    84


    Did you find this post helpful? Yes | No

    Default Re: 16LF1824 strange problem

    Something is strange. Now it work with the same variable in init and in main loops. Yesterday I tried a many time and don't worked.
    @ richard : thx for your suggestions
    Here is working code :

    Code:
    #CONFIG
            __CONFIG _CONFIG1, _FCMEN_OFF & _FOSC_INTOSC & _WDTE_OFF & _MCLRE_OFF & _CP_ON & _IESO_OFF & _BOREN_OFF & _PWRTE_OFF
            __CONFIG _CONFIG2, _PLLEN_OFF & _STVREN_OFF & _LVP_OFF
    #ENDCONFIG
    
    DEfine  OSC 4 
    OSCCON = %01101010   'sets the internal oscillator to 4Mhz
    
    
    
    ADCON0 = %00000000    
    ADCON1 = %00000000     
    FVRCON = %11000010     'turn fixed voltage reference on & set it to 2.048V (last two bits set the voltage ...01 =1.024V,10=2.048
    CM1CON0 = 0   ' COMPARATORS OFF
    CM1CON1 = 0
    CM2CON0 = 0   ' COMPARATORS OFF
    CM2CON1 = 0    
    CPSCON0 = 0   'CAPACTIVE SENSE MODULE OFF
    RCSTA.7  = 0   'SERIAL PORT OFF
    
    TRISA   = %00000000
    TRISC   = %00000100    
    ANSELA = %00000000
    ANSELC = %00000000
    
    OPTION_REG.7 = 0
    WPUA  =  %00000000
    
    PORTA = 0
    PORTC = 0 
    
    Symbol front  = PORTA.4
    
    
    x      var  byte
    varb   var  byte
    divf   var  byte
    
    x        = 0
    varb   = 0
    divf    = 0
    
    pause 100
    
    init:
    pulsin portc.2 , 1, varb
     if (varb < 140) or (varb > 160) then init
     pause 100
     pulsin portc.2 , 1, varb
     if (varb < 140) or (varb > 160) then init
     if (varb > 140) and (varb < 160) then goto beep
    goto init
    
    beep:      
    hpwm 1 , 10, 3000
        high front
        pause 80
        low front
        pause 200
    hpwm 1 , 5, 1200
        high front
        pause 80
        low front
        pause 200
    hpwm 1 , 10, 3000
        high front
        pause 80
        low front
     
    
    '************************************************************
    main:
    pulsin portc.2 , 1, varb
    hpwm 1 , x, 1954 
    
    '=======================miss signal=========================
    if varb < 50 then
         varb = 150
         x=0
         low front
    endif
    '============================================================
    
    '========================front===============================
    if varb> 160  then
            high front
        if varb > 190 then varb = 190
            divf=255*(varb-160)/30
        if x > 255 then x = 255
        if divf > 255 then divf = 255            
        if divf >x then x=x+2
        if divf <x then x=x-6
    endif
    '============================================================
    
    '==================== stay ==================================
    if varb < 160 and varb > 140 then
        low front
        x= 0 
    endif
    '=============================================================
    
    '==========================light==============================
    if varb < 140 and varb > 50 then
         high PORTA.0
    else
        low PORTA.0
    endif
    goto main
    end
    Last edited by midali; - 20th August 2014 at 15:06.

  2. #2
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: 16LF1824 strange problem

    Well done you have made a lot of progress. Just a couple of small points

    Code:
    DEfine  OSC 4
    should be

    DEFINE OSC 4

    if only to make it more readable.

    Code:
    OSCCON = %01101010   'sets the internal oscillator to 4Mhz
    not that it matters in this case and simply to further understanding.

    _FOSC_INTOSC directs the internal oscillator to be used
    OSCCON = %01101000 sets the oscillator defined by FOSC to 4Mhz

    OSCCON = %01101010 switches to the internal oscillator and sets it to 4Mhz

    So what difference does it make? It looks like there are two methods for achieving the same thing.

  3. #3
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: 16LF1824 strange problem

    "not that it matters in this case and simply to further understanding." I beg your pardon? When using the internal oscillator, "OSCCON" register is the only way to select the internal oscillator speed. Unless it is set it will default to 500Khz.
    Dave Purola,
    N8NTA
    EN82fn

  4. #4
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: 16LF1824 strange problem

    Quote Originally Posted by Dave View Post
    "not that it matters in this case and simply to further understanding." I beg your pardon? When using the internal oscillator, "OSCCON" register is the only way to select the internal oscillator speed. Unless it is set it will default to 500Khz.

    I am asking a question not stating anything why say "I beg your pardon?" did I say something wrong.

    You are correct Dave, about the speed did I say otherwise?

    The question is why are there two methods to select the internal oscillator and what is the difference. In the code FOSC is used to select the internal oscillator and also OSCCON is used to select the internal oscillator.

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


    Did you find this post helpful? Yes | No

    Default Re: 16LF1824 strange problem

    FOSC in the configs tells the type of oscillator used. In this case internal.

    OSCCON sets the speed of the internal with bits 6-3.

    Bit 1-0 of OSCCON is for two-speed start-up. If internal is selected in the config, two-speed start-up is disabled.
    Dave
    Always wear safety glasses while programming.

  6. #6
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: 16LF1824 strange problem

    From the datasheet.

    5.4.1 TWO-SPEED START-UP MODE
    CONFIGURATION
    Two-Speed Start-up mode is configured by the
    following settings:
    • IESO (of the Configuration Word 1) = 1; Internal/
    External Switchover bit (Two-Speed Start-up
    mode enabled).
    • SCS (of the OSCCON register) = 00.
    • FOSC<2:0> bits in the Configuration Word 1
    configured for LP, XT or HS mode.
    Which I take to mean that SCS has to be 00 for Two-Speed Start-up to work. I am not certain this is the same as

    Bit 1-0 of OSCCON is for two-speed start-up. If internal is selected in the config, two-speed start-up is disabled.
    This section of the datasheet is the source of the issue

    5.2.2.1 HFINTOSC
    The High-Frequency Internal Oscillator (HFINTOSC) is
    a factory calibrated 16 MHz internal clock source. The
    frequency of the HFINTOSC can be altered via
    software using the OSCTUNE register (Register 5-3).
    The output of the HFINTOSC connects to a postscaler
    and multiplexer (see Figure 5-1). One of nine
    frequencies derived from the HFINTOSC can be
    selected via software using the IRCF<3:0> bits of the
    OSCCON register. See Section 5.2.2.7 “Internal
    Oscillator Clock Switch Timing” for more information.
    The HFINTOSC is enabled by:
    • Configure the IRCF<3:0> bits of the OSCCON
    register for the desired HF frequency, and
    • FOSC<2:0> = 100, or
    • Set the System Clock Source (SCS) bits of the
    OSCCON register to ‘1x’.
    Which as I read it states either set FOSC<2:0> = 100 or SCS to '1x' but not both. Yet in the code

    Code:
    #CONFIG
            __CONFIG _CONFIG1, _FCMEN_OFF & _FOSC_INTOSC & _WDTE_OFF & _MCLRE_OFF & _CP_ON & _IESO_OFF & _BOREN_OFF & _PWRTE_OFF
            __CONFIG _CONFIG2, _PLLEN_OFF & _STVREN_OFF & _LVP_OFF
    #ENDCONFIG
    
    DEfine  OSC 4 
    OSCCON = %01101010   'sets the internal oscillator to 4Mhz
    Both are set.

    Obviously the code works and setting SCS to '1x' is having no detrimental effect in this case. Am I interpreting the datasheet wrong?

  7. #7
    Join Date
    May 2013
    Location
    australia
    Posts
    2,680


    Did you find this post helpful? Yes | No

    Default Re: 16LF1824 strange problem

    best way to view it
    from the data sheet

    bit 1-0 SCS<1:0>: System Clock Select bits
    1x = Internal oscillator block
    01 = Timer1 oscillator
    00 = Clock determined by FOSC<2:0> in Configuration Word 1.
    when FOSC<2:0> = 100 is equivalent to 1x = Internal oscillator block ie they both set the same option (logical or )

    but watch out if you want the pll engaged

Similar Threads

  1. A strange problem
    By mombasa in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 25th January 2012, 22:08
  2. Very strange problem...
    By alanmcf in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 7th May 2011, 07:28
  3. strange serout2 problem
    By KaanInal in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 7th January 2010, 10:33
  4. Strange problem with PBP 2.50
    By Charles Linquis in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 16th October 2007, 21:45
  5. strange problem 12f675
    By peterdeco1 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 5th July 2007, 14:47

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