Multi-Button CSM with Pic16LF722A


Closed Thread
Results 1 to 32 of 32

Hybrid View

  1. #1
    Join Date
    Apr 2016
    Location
    Mi, USA
    Posts
    24


    Did you find this post helpful? Yes | No

    Default Re: Multi-Button CSM with Pic16LF722A

    Hello,

    Wow, I can't believe how fast you can do this stuff. I tried to compile the code above, but I keep getting an [ASM WARNING] Argument out of range. Least significant bits used. (0) : Warning[202] error. Could it be because lookup only works with 8-bit Constants? Usually I can tell where the problem is by the line number given, but I can't figure this one out. Any idea? Thanks again! Sorry about the code above not being in the Window. I think I lost something when I inserted it.
    Last edited by RWright; - 19th May 2016 at 20:52.

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,644


    Did you find this post helpful? Yes | No

    Default Re: Multi-Button CSM with Pic16LF722A

    QUOTE]Could it be because lookup only works with 8-bit Constants? [/QUOTE]
    probably , I seldom use lookup(2) , try lookup2
    my preferred method is to do it this way on chips that can read their own flash memory
    Code:
     pw var word ;   pulse width
     pl var byte  ;  power level  0-9
     paddress       var word   ; power level array address
    ''''''''''''''''''''''''''''''''''''''''
    goto overpwm   
    my_pwm:
    ;10 step 0.85 gamma curve   
    @ dW 3,6,13,26,50,95,177,328,600,1023 
    overpwm:
    '''''''''''''''''''''''''''''
    @ GetAddress _my_pwm,_paddress  ;get address macro is part of dt_ints-14/18.bas
    '''''''''''''''''''''''''''''''''''''
    
     readcode paddress+pl ,pw  ;  address + offset
    Warning I'm not a teacher

  3. #3
    Join Date
    Jan 2011
    Location
    Sydney, Australia
    Posts
    172


    Did you find this post helpful? Yes | No

    Default Re: Multi-Button CSM with Pic16LF722A

    Hi Richard,

    Would you please explain in a little more detail as to how the "GetAddress" section of code works?

    Cheers
    Barry
    VK2XBP

  4. #4
    Join Date
    May 2013
    Location
    australia
    Posts
    2,644


    Did you find this post helpful? Yes | No

    Default Re: Multi-Button CSM with Pic16LF722A

    the get address macro is part of dt-ints . it places the address of a label in a pbp word var.
    this is the actual macro , its already there so why not use it.
    Code:
    ASM
    ;---[Returns the Address of a Label as a Word]------------------------------
    GetAddress macro Label, Wout
        CHK?RP Wout
        movlw low Label          ; get low byte
        movwf Wout
    ;    movlw High Label         ; get high byte  MPLAB 8.53 killed high
        movlw Label >> 8         ; get high byte
        movwf Wout + 1
      endm
    endasm

    so

    @ GetAddress _my_pwm,_paddress

    gets the address of my_pwm: and places it in var paddress


    the data
    my_pwm:
    @ DW 3,6,13,26,50,95,177,328,600,1023 [data stored as an array of 14 bit values]

    each flash memory address can hold a maximum 14 bit piece of data

    i'm using that address plus an offset to index the array

    readcode paddress+pl ,pw ; address + offset
    [note pw is a word var]
    so if pl= 3
    readcode paddress+3, and returns the value 26

    hope that's understandable

    ps I might add that in my setup that saves 44 words of code space compared to using lookup2
    Last edited by richard; - 23rd May 2016 at 12:55. Reason: ps
    Warning I'm not a teacher

  5. #5
    Join Date
    Jan 2011
    Location
    Sydney, Australia
    Posts
    172


    Did you find this post helpful? Yes | No

    Default Re: Multi-Button CSM with Pic16LF722A

    Hi Richard,

    Thank you for your very clear and concise explanation.

    When you store the data, what does "DW" refer to?
    In the example you use "@ DW" however in previous code snippets you use "@ dW"
    Can you clarify please?

    Cheers
    Barry

  6. #6
    Join Date
    May 2013
    Location
    australia
    Posts
    2,644


    Did you find this post helpful? Yes | No

    Default Re: Multi-Button CSM with Pic16LF722A

    In the example you use "@ DW" however in previous code snippets you use "@ dW"
    Any line beginning with @ is a statement for the assembler to process [just a shorthand way of ASM--DO STUFF ---ENDASM]
    the assembler is usually case sensitive CARE NEEDS TO TAKEN
    but for Data Directives like da db dw etc it seems to have no problem with the case . I only fix typing if I have to , mpasm doesn't care about the case here so nether do i
    dW == DW == Dw == [ dw – DECLARE DATA OF ONE WORD]

    for more info google mpasm assembler directives
    or
    http://ww1.microchip.com/downloads/e...Doc/33014L.pdf
    Warning I'm not a teacher

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


    Did you find this post helpful? Yes | No

    Default Re: Multi-Button CSM with Pic16LF722A

    in this example I use indirect addressing to retrieve the data ,but due to the 12f1822 architecture only the low 8 bits are retrievable .wastes a bit of space but its heaps quicker

    Code:
    '****************************************************************
    '*  Name    : ws2812.BAS                                                                              *
    '*  Author  : richard                                                                                       *
    '*  Notice  :                                                                                                   *
    '*          :                                                                                                     *
    '*  Date    : 1/11/2016                                                                                  *
    '*  Version : 1.0                                                                                            *
    '*  Notes   : pic12f1822  @32 mhz 3v3                                                           *
    '*          : porta.5 >>>> ws2821 Din                                                             *
    '****************************************************************
     #CONFIG
    cfg1 = _FOSC_INTOSC
    cfg1&= _WDTE_ON
    cfg1&= _PWRTE_OFF
    cfg1&= _MCLRE_ON
    cfg1&= _CP_OFF
    cfg1&= _CPD_OFF
    cfg1&= _BOREN_ON
    cfg1&= _CLKOUTEN_OFF
    cfg1&= _IESO_ON
    cfg1&= _FCMEN_ON
      __CONFIG _CONFIG1, cfg1
    
    cfg2 = _WRT_OFF
    cfg2&= _PLLEN_ON
    cfg2&= _STVREN_ON
    cfg2&= _BORV_19
    cfg2&= _LVP_OFF
      __CONFIG _CONFIG2, cfg2
    
    #ENDCONFIG
    
     bt VAR byte  bank0
     px VAR byte  bank0
     pc var byte bank0   ; number of rgb led to write x3
    
     pixels  var byte[3]    ;  rgb led  x3
     DURATION  var byte
     
     palette var word    ;address of pallet
     TEMP VAR WORD
     OFFSET VAR WORD     ;pallet index
    
    
     
     
       
     goto overpalette   ; green,red,blue data   
     my_palette:     ;  note !!!  inde read can only access the low byte of each  stored  word
     @ dw 100,120,0  ;
     @ dW 0,100,0  ; RED
     @ dW 0,80,0  ;
     @ dW 0,60,0  ;
     @ dW 0,20,0  ;
     @ dW 200,200,0  ;OR
     @ dW 160,160,0  ;
     @ dW 100,100,0  ;
     @ dW 80,80,0  ;
     @ dW 2,5,0  ; 
     @ dW 200,150,0  ;YEL
     @ dW 160,120,0  ;
     @ dW 100,75,0  ;
     @ dW 80,60,0  ;
     @ dW 40,30,0  ; 
     @ dW 150,200,0  ; ?
     @ dW 120,160,0  ;
     @ dW 75,100,0  ;
     @ dW 60,80,0  ;
     @ dW 30,40,0  ; 
     @ dW 120,100,0  ;
     @ dW 160,160,160  ; WH
     @ dW 100,100,100  ;
     @ dW 80,80,80  ;
     @ dW 30,30,30  ;
    ASM  
     
    
    ;---[Returns the Address of a Label as a Word]------------------------------
    GetAddress macro Label, Wout
        CHK?RP Wout
        movlw low Label          ; get low byte
        movwf Wout
        movlw Label >> 8         ; get high byte
        movwf Wout + 1
      endm     
    endasm
    overpalette :  
     
     goto overasm
       asm
    _pram                     ;entry point to read from sram
            movlw   low _pixels
            movwf   FSR0L
            movlw   high _pixels
            movwf   FSR0H 
            goto _nxby
    _prom                     ;entry point to read from flash 
            CHK?RP _OFFSET
            MOVE?WW   _OFFSET , FSR0
            BSF FSR0H ,7
    _nxby   MOVIW  FSR0 ++
            movwf _px
            MOVLW 8
            movwf _bt
            RLF _px,F
    _nxbt   BSF LATA ,5
            BTFSS STATUS,C
            GOTO XX
            NOP
            NOP
            NOP
    XX      BCF  LATA ,5
            NOP
            RLF _px,F
            DECFSZ _bt,F
            GOTO _nxbt 
            DECFSZ _pc,F
            GOTO _nxby
            RETURN 
       
    endasm
    
    overasm:
    
    
    
     CLEAR
     define OSC 32
     osccon=$f0
     trisa=%011110
     @ GetAddress _my_palette,_palette  ;set pallet address
     
     
    loopy: 
     GOSUB FLICKER
     PC=3    ; value is  number of rgb leds X 3 
     call prom
     pause DURATION
     goto loopy
     
     
     
     
    FLICKER:
     RANDOM TEMP
     DURATION=TEMP&$7F
     RANDOM TEMP
    ' temp=0
     OFFSET =(TEMP //25)*3 + palette
    RETURN
    Warning I'm not a teacher

  8. #8
    Join Date
    Apr 2016
    Location
    Mi, USA
    Posts
    24


    Did you find this post helpful? Yes | No

    Default Re: Multi-Button CSM with Pic16LF722A

    Hello,

    Well after changing some pause timing and sharing it before and after the indicator LED routine, It seems to be working pretty good now. However, I still need to be able to use the 1/0 button as a single shot
    button where pressing once shuts it down, as you have it, and pressing it again turns it back on with full pwm and level 5 selected, without the 1/0 button looping. You mentioned a finite machine, but I'm not sure as to how that
    applies. Also, is there a quick way that I can change the pwm frequency to 1Khz, instead of 256? Thanks.
    Code:
    '****************************************************************'*  Name    : CAPSENSE.BAS                                      *
    '*  Author  : RICHARD                                           *
    '*  Notice  : Copyright (c) 2011 [select VIEW...EDITOR OPTIONS] *
    '*          : All Rights Reserved                               *
    '*  Date    : 10/20/2011                                        *
    '*  Version : 1.0                                               *
    '*  Notes   :                                                   *
    '*          :     16LF722A                                      *
    '*                                                              *
    '****************************************************************
    
    
    '#DEFINE DBUG 1 
    #CONFIG
     __config _CONFIG1, _INTRC_OSC_NOCLKOUT & _WDTE_OFF & _BOR_OFF & _PLLEN_ON
    #ENDCONFIG
    
    
    define OSC 4
    include "dt_ints-14.bas"
    include "REENTERPBP.bas"
    ' variables used by interrupt handler, comment out as per assembler
    wsave VAR BYTE $20 SYSTEM ' location for W if in bank0
    'wsave VAR BYTE $70 SYSTEM ' alternate save location for W 
    ' if using $70, comment wsave1-3
    ' --- IF any of these three lines cause an error ?? ------------------------
    ' Comment them out to fix the problem ----
    ' -- Which variables are needed, depends on the Chip you are using -- 
    wsave1 VAR BYTE $A0 SYSTEM ' location for W if in bank1
    'wsave2 VAR BYTE $120 SYSTEM ' location for W if in bank2
    'wsave3 VAR BYTE $1A0 SYSTEM ' location for W if in bank3
    asm
    INT_LIST macro
         INT_HANDLER T1GATE_INT, _TB1, PBP,YES
         endm
         INT_CREATE
    ENDASM
    
    
        THRESH CON 16000 'needs to be established
    
    
        FLG VAR BYTE 'GOT A KEY 
        KP VAR BYTE 'WHICH KEY pressed
        key_inx var byte ;key index
        PW VAR WORD ;PULSEWIDTH
        p_level var byte
    
    
    DEFINE DEBUG_REG PORTB
    DEFINE DEBUG_BIT 7 ;pgd
    DEFINE DEBUG_BAUD 9600
    DEFINE DEBUG_MODE 0 
    
    
    
    
        OSCCON = 010000 '4 mhz
        CPSCON0 = 001100 'high range
        CPSCON1 = 0 'cps ch 0
    
    
    
    
        ANSELB = 000111 'SETS INPUTS B0-B2 AS ANALOG
        TRISB = 111111 'SETS INPUTS B0-B5 AS INPUTS b7 output
        WPUB = 000000 'DISABLES WEAK PULL-UP BITS
        TRISC = 111011 'ccp1 output portc.2
    
    
        OPTION_REG=$86 'edge int. prescale 1:128
        PIE1.7=1 'timer1 gate interrupt
        T1GCON=$f9 'single shot timer0 overflow as input
        T1CON=000101 'capsense as input no prescale no sync
        INTCON= $C0 'peripheral interrupts
        PW=1023
        p_level=5
        t2con=7 
        LED1 VAR PORTA.1 ' SET LED LEVEL INDICATORS
        LED2 VAR PORTA.2
        LED3 VAR PORTA.3  
        LED4 VAR PORTA.4
        LED5 VAR PORTA.5
    
    
        LED VAR portc.2
        'up                  down               off
        'key0=portb.0=ch0, key1=portb.1=ch1, key2=portb.2=ch2
    
    
    
    
    
    
    #IFDEF DBUG 
    
    
        portb.7=1
        LED=1 
        pause 2000
        Debug "Start",13 ,10
        LED=0
        pause 500 
    #ENDIF 
        ccp1con=12
        CCPR1L=255 'SET TO MAX BRIGHTNESS
        FLG=0 
        key_inx=0
    
    
    
    
    
    
    MAIN:
        IF FLG=1 THEN 
        IF KP = 0 THEN '1/0 BUTTON PRESSED
        ccp1con=0 ;kill it
        led=0
        p_level=0
        ELSEif KP = 1 THEN 'UP BUTTON PRESSED
        p_level= (p_level+1) min 5 
        'PW= (PW+256) MIN 1023 
        else 'DOWN BUTTON PRESSED
        p_level= p_level-1
        'PW = PW-256 
        IF p_level.7 THEN p_level=0 
        'IF PW.15 THEN PW=0 
        endif
     
     
        lookup2 p_level, [0,12,48,218,586,1023],pw  'gamma 1.35,5 levels plus OFF
        CCPR1L = PW>>2;
        ccp1con=12|((PW&3)<<4);
    #IFDEF DBUG 
    debug "KEY ",#KP,13,10 
    debug #PW,13,10 
    #ENDIF
        
        
        
        
        PAUSE 50
        FLG=0 'clr key pressed flag
        ENDIF
     
        'SET DISPLAY LEDS ACCORDING TO THE BRIGHTNESS LEVEL
        IF p_level= 5 THEN
        HIGH LED5
        ENDIF
        IF p_level=4 THEN
        HIGH LED4
        ENDIF
        IF p_level=3 THEN
        HIGH LED3
        ENDIF
        IF p_level=2 THEN
        HIGH LED2
        ENDIF
        if p_level=1 THEN 
        high LED1
        endif
        if p_level=0 then
        porta = 0
        ENDIF
            
        PAUSE 250
        
        GOTO MAIN 
    
    
    
    
    TB1: 
    #IFDEF DBUG 
    debug #TMR1,13,10
    #ENDIF
        IF TMR1 < THRESH THEN ' KEY DETECTED 
        if !flg then
        KP=key_inx 
        FLG=1 'set key pressed flag
        endif
        ENDIF
        key_inx=key_inx+1 ;check next key
        if key_inx > 2 then key_inx=0 
        CPSCON1 = key_inx
        TMR1=0
        T1GCON.3= 1 ;single shot reset
    
    
    
    
    @ INT_RETURN

  9. #9
    Join Date
    May 2013
    Location
    australia
    Posts
    2,644


    Did you find this post helpful? Yes | No

    Default Re: Multi-Button CSM with Pic16LF722A

    Also, is there a quick way that I can change the pwm frequency to 1Khz, instead of 256? Thanks.
    change t2con=7 to t2con=5


    I still need to be able to use the 1/0 button as a single shot
    button where pressing once shuts it down, as you have it, and pressing it again turns it back on with full pwm and level 5 selected, without the 1/0 button looping
    I cannot interpret that request at all
    do you mean
    a single shot , turns it off and it stays off until reset [the real meaning of a single shot]
    or
    a toggle , turns it off if its on and turns it fully on if its off [ like an on/off .. 0/1 switch]

    You mentioned a finite machine
    no a finite STATE machine , google it (p_level could be the state )
    Warning I'm not a teacher

  10. #10
    Join Date
    Apr 2016
    Location
    Mi, USA
    Posts
    24


    Did you find this post helpful? Yes | No

    Default Re: Multi-Button CSM with Pic16LF722A

    Hi,

    Yes, sorry, I guess single-shot wouldn't be a good description. I do mean a toggle , but without any auto-repeat function, as my earlier version had been.
    I meant finite STATE machine. Been a long day. Thanks for your patience. Thanks for the info on the pwm speed. I have only used the hpwm command under
    PBP to set my pwm frequencies and duty cycles in the past, I haven't got down into the nitty-gritty as much as I probably should have to get a better understanding of what's actually happening.

Similar Threads

  1. Replies: 5
    Last Post: - 18th March 2012, 21:40
  2. multi functions button press
    By malwww in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 27th May 2009, 00:12
  3. Multi-Threading
    By Ted's in forum mel PIC BASIC Pro
    Replies: 22
    Last Post: - 2nd September 2008, 05:55
  4. Multi-tasking
    By malc-c in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 13th July 2006, 18:50
  5. Multi Interrupt How To ?
    By capitano in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 3rd February 2005, 14:48

Members who have read this thread : 3

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