Pwm portb in pic 16f628 in same time


Closed Thread
Results 1 to 23 of 23
  1. #1
    Join Date
    May 2010
    Posts
    43

    Default Pwm portb in pic 16f628 in same time

    Can anybody tell me, is possible to write some program in Pic Basic,to
    PWM ALL PORTB, IN PIC 16F628 IN SAME TIME !!!
    I thing to all PORTB.0,PORTB.1,PORTB.2,PORTB.3,PORTB.4,PORTB.5,PO RTB.6,
    fade in SAME TIME,TOGETHER ALL PORTS,GO 0-100%,
    and SAME TIME, go 100%-0 !!!
    ANY HELP,IS GOOD FOR ME!!!
    Last edited by dragan77; - 13th February 2011 at 01:16.

  2. #2
    Join Date
    Jun 2006
    Location
    Bangalore, India
    Posts
    136


    Did you find this post helpful? Yes | No

    Post Re: Pwm portb in pic 16f628 in same time

    The output of the hardware PWM module in the 16F628A is on PORTB.3. Hence this the only pin you can use for the PWM output.
    Do you need exactly the same signal on 8 pins? If so, you can just take multiple connections from the same pin (PORTB.3).
    If you need 8 Different PWM signals, you could use a software interrupt based routine.
    You can have a look at Darrel's Multiple Software PWM.
    http://www.darreltaylor.com/DT_INTS-14/SPWM.html
    He has included a very nice description on how to use it.

  3. #3
    Join Date
    May 2010
    Posts
    43


    Did you find this post helpful? Yes | No

    Default Pwm portb in pic 16f628 in same time

    Thks.so much!
    yes, I need 8 Different PWM signals!!!
    I looking this site,I found code for PWM,
    but when I tried COMPILE,
    I have so many errors,I dont now what is happed!!
    Code is:
    Code:
    '****************************************************************
    '*  Name    : Test_SPWM.pbp                                     *
    '*  Author  : Darrel Taylor                                     *
    '*  Date    : 9/30/2006                                         *
    '*  Version : 1.0                                               *
    '*  Notes   :                                                   *
    '*          :                                                   *
    '****************************************************************
    ; Initialize your hardware first
    
    DEFINE OSC 20
    CLEAR
    
    INCLUDE "DT_INTS-14.bas"         ; Base Interrupt System
    INCLUDE "SPWM_INT.bas"              ; Software PWM module
    
    DEFINE SPWM_FREQ  40                ; SPWM Frequency
    DEFINE SPWM_RES   256               ; SPWM Resolution
    
    DutyVars   VAR BYTE[3]              ; DutyCycle Variables
      DutyVar1 VAR DutyVars[0]          ; group them in an array for easy access
      DutyVar2 VAR DutyVars[1]          ; with FOR loops etc.
      DutyVar3 VAR DutyVars[2]
    
    ASM
    SPWM_LIST  macro                    ; Define PIN's to use for SPWM
         SPWM_PIN  PORTB, 0, _DutyVar1
         SPWM_PIN  PORTB, 1, _DutyVar2
         SPWM_PIN  PORTB, 2, _DutyVar3
      endm
      SPWM_INIT  SPWM_LIST              ; Initialize the Pins
    ENDASM
    
    ASM
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler   TMR1_INT,  SPWMhandler,  ASM,  yes
        endm
        INT_CREATE                      ; Creates the interrupt processor
    ENDASM
    
    @ INT_ENABLE  TMR1_INT              ; enable Timer 1 interrupts
    
    ;_____________________________________________________________________________
    
    RandVar   VAR WORD : RandVar = 12345
    LoopCount VAR WORD
    
    Main:                               ; Simple Demo to fade/flash some LED's
        DutyVar1 = 5                        ; Start with 3 LED's at different
        DutyVar2 = 50                       ; brightness
        DutyVar3 = 150
        pause 3000
         
        FOR LoopCount = 1 to 4              ; Reapeat 4 times
            for DutyVar1 = 0 to 150         ; Fade LED1 UP
                pause 10
                RANDOM  RandVar             
                DutyVar3 = RandVar & $3F          ; Give LED3 a random dutycycle
            next DutyVar1
             
            for DutyVar1 = 150 to 0 STEP -1 ; Fade LED1 Down
                pause 10
                RANDOM  RandVar
                DutyVar2 = RandVar & $3F         ; Give LED2 a random dutycycle
            next DutyVar1
        NEXT LoopCount     
    GOTO Main
    and to many errors
    Attached Images Attached Images  
    Last edited by dragan77; - 13th February 2011 at 18:35.

  4. #4
    Join Date
    May 2010
    Posts
    43


    Did you find this post helpful? Yes | No

    Default Re: Pwm portb in pic 16f628 in same time

    And I tried,compile in PicBasic,this code:
    Code:
    define OSC 20
    
    define SPWMFREQ  100           ' PWM frequency in Hz    
    
    DutyCycle1   VAR  byte    ' 0-255  0=Idle Low   255=Idle High
    DutyCycle2   VAR  byte 
    DutyCycle3   VAR  byte    
    
    SPWM1PIN  VAR PORTB.0          ' SPWM channel 1
    define SPWM1VAR  _DutyCycle1
    
    SPWM2PIN  VAR PORTB.1          ' SPWM channel 2
    define SPWM2VAR  _DutyCycle2
    
    SPWM3PIN  VAR PORTB.2          ' SPWM channel 3
    define SPWM3VAR  _DutyCycle3
     
    Include "Multi_SPWM.pbp"
    
    DutyCycle1 = 0
    DutyCycle2 = 127
    DutyCycle3 = 255
    
    Loop:
       Pause 100
    Goto Loop
    and I have errors:
    Code:
    ERROR: Variable wsave3 position request 416 beyond RAM_END 335.
    Can anybody now what is problem!!!

  5. #5
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default Re: Pwm portb in pic 16f628 in same time

    Hello,

    Your code from post # 3 compiles fine over here.

    For your wsave error, you have to open the DT_INTS14 file and follow it's instructions:

    Code:
    ' --- 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
    ' --------------------------------------------------------------------------

  6. #6
    Join Date
    May 2010
    Posts
    43


    Did you find this post helpful? Yes | No

    Default Re: Pwm portb in pic 16f628 in same time

    thks,but I dont now what I must fix in this file,there is all code inside:
    Code:
     '***************************************************************************
    '*  Name    : DT_INTS-14.bas                                               *
    '*  Author  : Darrel Taylor                                                *
    '*  Version : 1.10 (8/13/2010)                                             *
    '*  Date    : OCT 13, 2009                                                 *
    '***************************************************************************
    '* REV 1.10  Fixes Duplicate label error when Handlers cross page boundary *
    '*           Fixes error with 16F1's and MPLAB 8.53 (high)                 *
    '* REV 1.00  Completely re-written, with optimization and F1 chips in mind *
    '* REV 0.93  Fixed CMIF and EEIF problem with older PIC's                  *
    '*           that have the Flags in PIR1 instead of PIR2                   *
    '* Rev 0.92  solves a "Missed Interrupt" and                               *
    '*           banking switching problem                                     *
    '***************************************************************************
    DEFINE  DT_INTS_VERSION  110
    DEFINE  INTHAND  INT_ENTRY
    
    ;-- Place a copy of these variables in your Main program -------------------
    ;--   The compiler will tell you which lines to un-comment                --
    ;--   Do Not un-comment these lines                                       --
    ;---------------------------------------------------------------------------
    ;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
    ' --------------------------------------------------------------------------
    
    ssave       VAR BYTE    BANK0   SYSTEM      ' location for STATUS register
    psave       VAR BYTE    BANK0   SYSTEM      ' location for PCLATH register
    fsave       VAR BYTE    BANK0   SYSTEM      ' location for FSR register
    RetAddr     VAR WORD    BANK0   
    INT_Bits    VAR BYTE    BANK0
      Serviced  VAR INT_Bits.0
      Vars_Saved VAR INT_Bits.1
    
    GIE         VAR INTCON.7 
    PEIE        VAR INTCON.6
    
    ASM
      ifdef PM_USED                             ; verify MPASM is the assembler
        "ERROR: DT_INTS does not support the PM assembler, USE MPASM"
      endif
    
    ;---------------------------------------------------------------------------
      ifdef ReEnterUsed
        ifdef ReEnterVersion
          if (ReEnterVersion < 34)
            error "Wrong version of ReEnterPBP.bas - Ver 3.4 or higher required
          endif
        else
          error "Wrong version of ReEnterPBP.bas - Ver 3.4 or higher required
        endif
      endif
    
    ;---------------------------------------------------------------------------
        if (BANK0_END == 0x7F)
          ifdef BANK1_END
            if (BANK1_END == 0xEF)   ; doesn't find 12F683
              variable ACCESSRAM = 1
            else
              variable ACCESSRAM = 0    
            endif
          else
              variable ACCESSRAM = 0
          endif
        else
          variable ACCESSRAM = 0
        endif
        
    ;---------------------------------------------------------------------------
    #define OrChange Or change to   wsave BYTE $70 SYSTEM
    AddWsave macro B
      if (B == 0)
        if (ACCESSRAM == 1)
          error   "                     Add:"       wsave VAR BYTE $70 SYSTEM
        else
          error   "                     Add:"       wsave VAR BYTE $20 SYSTEM
        endif
      endif
      if (B == 1)
        if (ACCESSRAM == 1)
          error   "   Add:"       wsave1 VAR BYTE $A0 SYSTEM, OrChange
        else
          error   "                     Add:"       wsave1 VAR BYTE $A0 SYSTEM
        endif
      endif
      if (B == 2)
        if (ACCESSRAM == 1)
          error   "   Add:"       wsave2 VAR BYTE $120 SYSTEM, OrChange
        else
          error   "                     Add:"       wsave2 VAR BYTE $120 SYSTEM
        endif
      endif
      if (B == 3)
        if (ACCESSRAM == 1)
          error   "   Add:"       wsave3 VAR BYTE $1A0 SYSTEM, OrChange
        else
          error   "                     Add:"       wsave3 VAR BYTE $1A0 SYSTEM
        endif
      endif
      endm
      
    #define WsaveE1(B) Chip has RAM in BANK#v(B), but wsave#v(B) was not found.
    ;#define WsaveE2(B) Uncomment wsave#v(B) in the DT_INTS-14.bas file.
    #define WsaveCouldBe This chip has access RAM at $70
      
    #define WsaveError(B) error  WsaveE1(B)
      ifndef FSR0L     ; not a 16F1
        ifndef wsave
    ;      if (ACCESSRAM == 1)
            error wsave variable not found,
            AddWsave(0)
            variable wsave = 0 ; stop further wsave errors
    ;      else
            
    ;      endif
        else
          if (wsave == 0x70)
            if (ACCESSRAM == 0)
              error This chip does not have AccessRAM at $70, change to   wsave VAR BYTE $20 SYSTEM
            endif
          else
              if (wsave != 0x20)
                error wsave must be either $20 or $70
              endif
          endif
        endif
        ifdef BANK1_START
          ifndef wsave1
            ifdef wsave
              if (wsave != 0x70)
                WsaveError(1)
                AddWsave(1)
              endif
            else
              if (ACCESSRAM == 1)
                if (wsave != 0x70)
                  WsaveCouldBe 
                endif
              endif
            endif
          endif
        endif
        ifdef BANK2_START
          ifndef wsave2
            ifdef wsave
              if (wsave != 0x70)
                WsaveError(2)
                AddWsave(2)
              endif
            endif
          endif
        endif
        ifdef BANK3_START
          ifndef wsave3
            ifdef wsave
              if (wsave != 0x70)
                WsaveError(3)
                AddWsave(3)
              endif
            endif
          endif
        endif
      
            
      endif
    ENDASM
    
    ASM
    asm = 0
    Asm = 0
    ASM = 0
    pbp = 1
    Pbp = 1
    PBP = 1
    yes = 1
    Yes = 1
    YES = 1
    no  = 0
    No  = 0
    NO  = 0
    
    
      #define ALL_INT      INTCON,GIE, INTCON,GIE      ;-- Global Interrupts   *
      #define T1GATE_INT   PIR1,TMR1GIF, PIE1,TMR1GIE  ;-- Timer1 Gate         *
      #define INT_INT      INTCON,INTF,  INTCON,INTE   ;-- External INT
      #define GPC_INT      INTCON,GPIF,  INTCON,GPIE   ;-- GPIO Int On Change  *
      #define IOC_INT      INTCON,IOCIF, INTCON,IOCIE  ;-- Int On Change       *
      #define RAC_INT      INTCON,RAIF,  INTCON,RAIE   ;-- RA Port Change      *
      #define RBC_INT      INTCON,RBIF,  INTCON,RBIE   ;-- RB Port Change
      #define RABC_INT     INTCON,RABIF, INTCON,RABIE  ;-- RAB Port Change     *
      ifdef T0IF
        #define TMR0_INT   INTCON,T0IF, INTCON,T0IE    ;-- TMR0 Overflow
      else
        ifdef TMR0IF
          #define TMR0_INT INTCON,TMR0IF, INTCON,TMR0IE ;-- TMR0 alternate sym
        endif
      endif
      ifdef TMR1IF
        #define TMR1_INT   PIR1,TMR1IF, PIE1,TMR1IE    ;-- TMR1 Overflow
      else
        ifdef T1IF
          #define TMR1_INT PIR1,T1IF, PIE1,T1IE        ;-- TMR1 alternate sym
        endif
      endif
      ifdef TMR2IF
        #define TMR2_INT   PIR1,TMR2IF, PIE1,TMR2IE    ;-- TMR2 - PR2 Match 
      else
        #define TMR2_INT   PIR1,T2IF, PIE1,T2IE        ;-- TMR2 - PR2 Match alt
      endif
      #define TMR4_INT     PIR3,TMR4IF, PIE3,TMR4IE    ;-- TMR4 - PR4 Match    *
      #define TMR6_INT     PIR3,TMR6IF, PIE3,TMR6IE    ;-- TMR6 - PR6 Match    *
      #define TX_INT       PIR1,TXIF, PIE1,TXIE        ;-- USART Transmit 
      #define RX_INT       PIR1,RCIF, PIE1,RCIE        ;-- USART Receive 
    
      #define PSP_INT      PIR1,PSPIF, PIE1,PSPIE      ;-- Parallel Slave Port
      #define AD_INT       PIR1,ADIF, PIE1,ADIE        ;-- A/D Converter 
    
      ifdef SSPIF
        #define SSP_INT    PIR1,SSPIF, PIE1,SSPIE      ;-- (M)SSP module
        #define BUS_INT    PIR2,BCLIF, PIE2,BCLIE      ;-- Bus Collision 
      else
        ifdef SSP1IF
          #define SSP_INT  PIR1,SSP1IF, PIE1,SSP1IE    ;-- (M)SSP module 1     *
          #define SSP1_INT PIR1,SSP1IF, PIE1,SSP1IE    ;                       *
          #define BUS_INT  PIR2,BCL1IF, PIE2,BCL1IE    ;-- Bus Collision 1     *
          #define BUS1_INT PIR2,BCL1IF, PIE2,BCL1IE    ;                       *
        endif
        ifdef SSP2IF
          #define SSP2_INT PIR4,SSP2IF, PIE4,SSP2IE    ;-- (M)SSP module 2     *
          #define BUS2_INT PIR4,BCL2IF, PIE4,BCL2IE    ;-- Bus Collision 2     *
        endif
      endif      
      #define CCP1_INT     PIR1,CCP1IF, PIE1,CCP1IE    ;-- CCP1 
      #define CCP2_INT     PIR2,CCP2IF, PIE2,CCP2IE    ;-- CCP2 
      #define CCP3_INT     PIR3,CCP3IF, PIE3,CCP3IE    ;-- CCP3                *
      #define CCP4_INT     PIR3,CCP4IF, PIE3,CCP4IE    ;-- CCP4                *
      #define CCP5_INT     PIR3,CCP5IF, PIE3,CCP5IE    ;-- CCP5                *
    
      ifdef CMIF
        ifdef PIR2
          #define CMP_INT  PIR2,CMIF, PIE2,CMIE        ;-- Comparator
        else
          #define CMP_INT  PIR1,CMIF, PIE1,CMIE
        endif
      else
        ifdef C1IF
          #define CMP_INT  PIR2,C1IF, PIE2,C1IE        ;-- Comparator 1        *
          #define CMP1_INT PIR2,C1IF, PIE2,C1IE        ;-- Comparator 1        *
        endif
        ifdef C2IF
          #define CMP2_INT PIR2,C2IF, PIE2,C2IE        ;-- Comparator 2        *
        endif
      endif
       
      ifndef PIR2
        #define EE_INT     PIR1,EEIF, PIE1,EEIE
        #define OSCF_INT   PIR1,OSFIF, PIE1,OSFIE      ;-- OSC Fail if no PIR2 *
        #define LVD_INT    PIR1,LVDIF, PIE1,LVDIE      ;-- Low-Voltage Detect  *
      else
        #define EE_INT     PIR2,EEIF, PIE2,EEIE        ;-- EEPROM/FLASH Write
        #define OSCF_INT   PIR2,OSFIF, PIE2,OSFIE      ;-- OSC Fail            *
        #define LVD_INT    PIR2,LVDIF, PIE2,LVDIE      ;-- Low-Voltage Detect  *
      endif
      
      #define LCD_INT      PIR2,LCDIF, PIE2,LCDIE      ;-- LCD controller      *
      #define CRYPT_INT    PIR1,CRIF, PIE1,CRIE        ;-- KeeLoq Cryptographic*
      #define USB_INT      PIR1,USBIF, PIE1,USBIE      ;-- USB 16C745/765 only *
    ENDASM
    
    
    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
    
    ;---[find correct bank for a BIT variable]----------------------------------
    CHKRP?T  macro reg, bit
        CHK?RP  reg
      endm
        
    ;---[This creates the main Interrupt Service Routine (ISR)]-----------------
    INT_CREATE  macro
      local OverCREATE
        L?GOTO OverCREATE
    
    INT_ENTRY
        ifndef FSR0L  
          if (CODE_SIZE <= 2)
              movwf   wsave       ; 1 copy W to wsave register
              swapf   STATUS,W    ; 2 swap status reg to be saved into W
              clrf    STATUS      ; 3 change to bank 0
              movwf   ssave       ; 4 save status reg to a bank 0 register
              movf    PCLATH,W    ; 5 move PCLATH reg to be saved into W reg
              movwf   psave       ; 6 save PCLATH reg to a bank 0 register
          endIF
          movf      FSR,W         ; 7 move FSR reg to be saved into W reg
          movwf     fsave         ; 8 save FSR reg to a bank 0 register
        else
          banksel 0               ; BANK 0 for F1 chips
        endif  
        variable  PREV_BANK = 0
        MOVE?CT  0, _Vars_Saved
        
    List_Start
        ifdef LoopWhenServiced
          MOVE?CT  0, _Serviced   ; indicate nothing has been serviced
        endif
    
        INT_LIST                  ; Expand the users list of interrupt handlers
                                ; INT_LIST macro must be defined in main program
        
        ifdef LoopWhenServiced
          BIT?GOTO  1, _Serviced, List_Start
        endif
    
        ifdef ReEnterUsed         ; if ReEnterPBP.bas was included
            CHKRP?T  _Vars_Saved
            btfss    _Vars_Saved  ; if PBP system vars have been saved 
            goto     INT_EXIT
            L?GOTO   _RestorePBP  ; Restore PBP system Vars
        endif
        
    INT_EXIT
        variable  PREV_BANK = 0
        ifndef FSR0L              ; if chip is not an F1 - restore context
          clrf    STATUS          ; BANK 0
          movf    fsave,W         ; Restore the FSR reg
          movwf   FSR
          movf    psave,w         ; Restore the PCLATH reg
          movwf   PCLATH
          swapf   ssave,w         ; Restore the STATUS reg
          movwf   STATUS
          swapf   wsave,f
          swapf   wsave,w         ; Restore W reg
        endif
        retfie                    ; Exit the interrupt routine
    ;-----------------------------
      LABEL?L OverCREATE
        bsf      INTCON, 6      ; Enable Peripheral interrupts
        bsf      INTCON, 7      ; Enable Global interrupts
        endm
        
    ENDASM
    
    ASM
    ;---[Add an Interrupt Source to the user's list of INT Handlers]------------
    #INT_HANDLER  macro  FlagReg,FlagBit, EnableReg,EnableBit, Label, Type,Reset
      list
        local AfterSave, AfterUserRoutine, NoInt
          ifdef FlagBit
            CHK?RP   EnableReg
            btfss    EnableReg, EnableBit        ; if the INT is enabled
            goto     NoInt
            CHK?RP   FlagReg                    
            btfss    FlagReg, FlagBit            ; and the Flag set?
            goto     NoInt
            ifdef LoopWhenServiced
              MOVE?CT  1, _Serviced
            endif
                
            if (Type == PBP)                     ; If INT handler is PBP
              ifdef ReEnterUsed
                btfsc  _Vars_Saved
                goto   AfterSave
                GetAddress  AfterSave, _RetAddr  
                L?GOTO  _SavePBP                 ; Save PBP system Vars
                LABEL?L  AfterSave
              else
                error ReEnterPBP must be INCLUDEd to use PBP type interrupts
              endif
            endif
            GetAddress  AfterUserRoutine, _RetAddr   ; save return address
            L?GOTO   Label                       ; goto the users INT handler
            LABEL?L AfterUserRoutine
    
            if (Reset == YES)
              CHK?RP   FlagReg
              bcf      FlagReg, FlagBit        ; reset flag (if specified)
            endif
          else
            INT_ERROR  "INT_Handler"
          endif
    NoInt
          banksel  0
    PREV_BANK = 0        
        endm
    ;-----------------------------------
    #define INT_HANDLER(FlagReg,FlagBit, EnableReg,EnableBit, Label, Type,Reset)  #INT_HANDLER FlagReg,FlagBit, EnableReg,EnableBit, Label, Type,Reset
      ifndef INT_Handler
    #define INT_Handler(FlagReg,FlagBit, EnableReg,EnableBit, Label, Type,Reset)  #INT_HANDLER FlagReg,FlagBit, EnableReg,EnableBit, Label, Type,Reset
    #define int_handler(FlagReg,FlagBit, EnableReg,EnableBit, Label, Type,Reset)  #INT_HANDLER FlagReg,FlagBit, EnableReg,EnableBit, Label, Type,Reset
    #define Int_Handler(FlagReg,FlagBit, EnableReg,EnableBit, Label, Type,Reset)  #INT_HANDLER FlagReg,FlagBit, EnableReg,EnableBit, Label, Type,Reset
    #define Int_handler(FlagReg,FlagBit, EnableReg,EnableBit, Label, Type,Reset)  #INT_HANDLER FlagReg,FlagBit, EnableReg,EnableBit, Label, Type,Reset
    #define int_Handler(FlagReg,FlagBit, EnableReg,EnableBit, Label, Type,Reset)  #INT_HANDLER FlagReg,FlagBit, EnableReg,EnableBit, Label, Type,Reset
      endif
      
    ;---[Returns from a "goto" subroutine]--------(RetAddr must be set first)---
    #INT_RETURN  macro
          CHK?RP  _RetAddr
          movf    _RetAddr + 1, W  ; Set PCLATH with top byte of return address
          movwf   PCLATH
          movf    _RetAddr, W      ; Go back to where we were
          movwf   PCL
        endm    
    ;_____________________________
    #define INT_RETURN  #INT_RETURN
      ifndef INT_Return
    #define INT_Return  #INT_RETURN 
    #define int_return  #INT_RETURN 
    #define Int_Return  #INT_RETURN 
    #define Int_return  #INT_RETURN 
    #define int_Return  #INT_RETURN 
      endif
    
    ;----[Display not found error]----------------------------------------------
    INT_ERROR macro From
        error From -  Interrupt Flag ( FlagReg,FlagBit ) not found.
      endm
    
    ;---[Enable an interrupt source]--------------------------------------------
      ifndef INT_ENABLECLEARFIRST
        #define INT_ENABLECLEARFIRST 1             ; default to Clear First
      endif          ; use DEFINE INT_ENABLECLEARFIRST 0 to NOT clear First
      
    #INT_ENABLE  macro  FlagReg, FlagBit, EnableReg, EnableBit
          ifdef FlagBit
            ifdef INT_ENABLECLEARFIRST
              if (INT_ENABLECLEARFIRST == 1)       ; if specified
                MOVE?CT 0, FlagReg, FlagBit        ;   clear the flag first
              endif
            endif
            MOVE?CT  1, EnableReg, EnableBit       ; enable the INT source
          else
            INT_ERROR  "INT_ENABLE"
          endif
        endm    
    ;_____________________________
    #define INT_ENABLE(FlagReg, FlagBit, EnableReg, EnableBit)  #INT_ENABLE FlagReg, FlagBit, EnableReg, EnableBit
      ifndef INT_Enable
    #define INT_Enable(FlagReg, FlagBit, EnableReg, EnableBit)  #INT_ENABLE FlagReg, FlagBit, EnableReg, EnableBit
    #define int_enable(FlagReg, FlagBit, EnableReg, EnableBit)  #INT_ENABLE FlagReg, FlagBit, EnableReg, EnableBit
    #define Int_Enable(FlagReg, FlagBit, EnableReg, EnableBit)  #INT_ENABLE FlagReg, FlagBit, EnableReg, EnableBit
    #define Int_enable(FlagReg, FlagBit, EnableReg, EnableBit)  #INT_ENABLE FlagReg, FlagBit, EnableReg, EnableBit
    #define int_Enable(FlagReg, FlagBit, EnableReg, EnableBit)  #INT_ENABLE FlagReg, FlagBit, EnableReg, EnableBit
      endif
    
    ;---[Disable an interrupt source]-------------------------------------------
    #INT_DISABLE  macro  FlagReg, FlagBit, EnableReg, EnableBit
          ifdef FlagBit
            MOVE?CT  0, EnableReg, EnableBit       ; disable the INT source  
          else
            INT_ERROR  "INT_DISABLE"
          endif
        endm    
    ;_____________________________
    #define INT_DISABLE(FlagReg, FlagBit, EnableReg, EnableBit)  #INT_DISABLE FlagReg, FlagBit, EnableReg, EnableBit
      ifndef INT_Disable
    #define INT_Disable(FlagReg, FlagBit, EnableReg, EnableBit)  #INT_DISABLE FlagReg, FlagBit, EnableReg, EnableBit
    #define int_disable(FlagReg, FlagBit, EnableReg, EnableBit)  #INT_DISABLE FlagReg, FlagBit, EnableReg, EnableBit
    #define Int_Disable(FlagReg, FlagBit, EnableReg, EnableBit)  #INT_DISABLE FlagReg, FlagBit, EnableReg, EnableBit
    #define Int_disable(FlagReg, FlagBit, EnableReg, EnableBit)  #INT_DISABLE FlagReg, FlagBit, EnableReg, EnableBit
    #define int_Disable(FlagReg, FlagBit, EnableReg, EnableBit)  #INT_DISABLE FlagReg, FlagBit, EnableReg, EnableBit
      endif
    
    ;---[Clear an interrupt Flag]-----------------------------------------------
    #INT_CLEAR  macro  FlagReg, FlagBit, EnableReg, EnableBit
          ifdef FlagBit
            MOVE?CT  0, FlagReg, FlagBit           ; clear the flag
          else
            INT_ERROR "INT_CLEAR" 
          endif
        endm
    ;_____________________________
    #define INT_CLEAR(FlagReg, FlagBit, EnableReg, EnableBit)  #INT_CLEAR FlagReg, FlagBit, EnableReg, EnableBit
      ifndef INT_Clear
    #define INT_Clear(FlagReg, FlagBit, EnableReg, EnableBit)  #INT_CLEAR FlagReg, FlagBit, EnableReg, EnableBit
    #define int_clear(FlagReg, FlagBit, EnableReg, EnableBit)  #INT_CLEAR FlagReg, FlagBit, EnableReg, EnableBit
    #define Int_Clear(FlagReg, FlagBit, EnableReg, EnableBit)  #INT_CLEAR FlagReg, FlagBit, EnableReg, EnableBit
    #define Int_clear(FlagReg, FlagBit, EnableReg, EnableBit)  #INT_CLEAR FlagReg, FlagBit, EnableReg, EnableBit
    #define int_Clear(FlagReg, FlagBit, EnableReg, EnableBit)  #INT_CLEAR FlagReg, FlagBit, EnableReg, EnableBit
      endif
    ENDASM

    I like use this,for PIC 16f628,and I dont now how compile,in PicBasic,all time I have errors!

  7. #7
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default Re: Pwm portb in pic 16f628 in same time

    In your code for in post 4, where did you get

    Multi_SPWM.pbp ?

    Edit: I deleted the bad advice
    Last edited by ScaleRobotics; - 13th February 2011 at 20:50.

  8. #8
    Join Date
    May 2010
    Posts
    43


    Did you find this post helpful? Yes | No

    Default Re: Pwm portb in pic 16f628 in same time

    I get this code in this site:
    http://www.darreltaylor.com/DT_INTS-14/SPWM.html

  9. #9
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default Re: Pwm portb in pic 16f628 in same time

    Perhaps I am missing something.

    The code you show in post # 4 has no DT_INTS include file. It does have an include file named Multi_SPWM.pbp , which I gather is really SPWM_INT.BAS from this link: http://www.darreltaylor.com/DT_INTS-14/SPWM_INT.bas.txt .

    If this is correct, then you should have an include in your file for DT_INTS, really I think that is the only way to get a WSAVE error, but I could be wrong.

    Your might try something more along the lines of the example code, here: http://www.darreltaylor.com/DT_INTS-14/SPWM.html with both include files, etc. If you get a wsave error, make sure you are editing the right DT_INTS_14.bas file, whatever you call it in your include statement.

    For your chip, it should be edited so that:
    Code:
    ' --- 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
    ' --------------------------------------------------------------------------
    EDIT: A little late, but I found where Multi_SPWM.pbp came from http://www.pbpgroup.com/modules/wfse...p?articleid=12
    Last edited by ScaleRobotics; - 14th February 2011 at 00:25. Reason: a little late, but found Multi_SPWM.pbp

  10. #10
    Join Date
    May 2010
    Posts
    43


    Did you find this post helpful? Yes | No

    Default Re: Pwm portb in pic 16f628 in same time

    Yes,I have DT_INTS_14.bas file,I write correct name.
    I do what you tell me,but now I have errors.
    If I understudy you,i MUST WRITE THAT,CODE?

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

    ' --------------------------------------------------------------------------

    I do that but I have errors when I compile.

    This program in my PicBasic :
    Code:
    '****************************************************************
    '*  Name    : Test_SPWM.pbp                                     *
    '*  Author  : Darrel Taylor                                     *
    '*  Date    : 9/30/2006                                         *
    '*  Version : 1.0                                               *
    '*  Notes   :                                                   *
    '*          :                                                   *
    '****************************************************************
    ; Initialize your hardware first
    
    DEFINE OSC 20
    CLEAR
    
    INCLUDE "DT_INTS-14.bas"         ; Base Interrupt System
    INCLUDE "SPWM_INT.bas"              ; Software PWM module
    
    DEFINE SPWM_FREQ  40                ; SPWM Frequency
    DEFINE SPWM_RES   256               ; SPWM Resolution
    
    
    DutyVars   VAR BYTE[3]              ; DutyCycle Variables
      DutyVar1 VAR DutyVars[0]          ; group them in an array for easy access
      DutyVar2 VAR DutyVars[1]          ; with FOR loops etc.
      DutyVar3 VAR DutyVars[2]
    
    ASM
    SPWM_LIST  macro                    ; Define PIN's to use for SPWM
         SPWM_PIN  PORTB, 0, _DutyVar1
         SPWM_PIN  PORTB, 1, _DutyVar2
         SPWM_PIN  PORTB, 2, _DutyVar3
      endm
      SPWM_INIT  SPWM_LIST              ; Initialize the Pins
    ENDASM
    
    ASM
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler   TMR1_INT,  SPWMhandler,  ASM,  yes
        endm
        INT_CREATE                      ; Creates the interrupt processor
    ENDASM
    
    @ INT_ENABLE  TMR1_INT              ; enable Timer 1 interrupts
    
    ;_____________________________________________________________________________
    
    RandVar   VAR WORD : RandVar = 12345
    LoopCount VAR WORD
    
    Main:
    wsave   VAR BYTE    $70     SYSTEM      ' location for W if in bank0
    ;wsave   VAR BYTE    $70     SYSTEM      ' alternate save location for W 
                                             ' if using $70, comment wsave1-3
    
    
                                   ; Simple Demo to fade/flash some LED's
        DutyVar1 = 5                        ; Start with 3 LED's at different
        DutyVar2 = 50                       ; brightness
        DutyVar3 = 150
        pause 3000
         
        FOR LoopCount = 1 to 4              ; Reapeat 4 times
            for DutyVar1 = 0 to 150         ; Fade LED1 UP
                pause 10
                RANDOM  RandVar             
                DutyVar3 = RandVar & $3F          ; Give LED3 a random dutycycle
            next DutyVar1
             
            for DutyVar1 = 150 to 0 STEP -1 ; Fade LED1 Down
                pause 10
                RANDOM  RandVar
                DutyVar2 = RandVar & $3F         ; Give LED2 a random dutycycle
            next DutyVar1
        NEXT LoopCount     
    GOTO Main
    is correct?

  11. #11
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default Re: Pwm portb in pic 16f628 in same time

    Sorry, you are correct, you just need the wsave at $70. If you are going to put it in your file, just make sure they are all edited out in the include file. But put:

    wsave VAR BYTE $70 SYSTEM ' location for W if in bank0

    above the include line.

    Sorry for giving you bad advice.
    </pre>

  12. #12
    Join Date
    May 2010
    Posts
    43


    Did you find this post helpful? Yes | No

    Default Re: Pwm portb in pic 16f628 in same time

    Ic,ok I am not professional in PicBasic, I learning,but thks,to your help!

    Is correct this :
    Code:
    ;-- Place a copy of these variables in your Main program -------------------
    ;--   The compiler will tell you which lines to un-comment                --
    ;--   Do Not un-comment these lines                                       --
    ;---------------------------------------------------------------------------
    ;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 --
    wsave VAR BYTE $70 SYSTEM ' location for W if in bank0
    ;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
    
    
    ' --------------------------------------------------------------------------
    I write now this in my file DT_INTS-14.bas
    but now I have error message :

    Code:
    ERROR Line 50: Redefinition of VAR.
    I dont now what is now???

  13. #13
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default Re: Pwm portb in pic 16f628 in same time

    That means you have this line in two places, like in your code, as well as they include file:


    wsave VAR BYTE $70 SYSTEM ' location for W if in bank0

    You can have it in either, but not both.
    </pre>

  14. #14
    Join Date
    May 2010
    Posts
    43


    Did you find this post helpful? Yes | No

    Default Re: Pwm portb in pic 16f628 in same time

    ok,and what i must do, to fix my problem?

  15. #15
    Join Date
    May 2010
    Posts
    43


    Did you find this post helpful? Yes | No

    Default Re: Pwm portb in pic 16f628 in same time

    Can you ask you for this code,why i cant compile this code?
    What is wrong?
    Code:
    define OSC 20
    
    define SPWMFREQ  100           ' PWM frequency in Hz    
    
    DutyCycle1   VAR  byte    ' 0-255  0=Idle Low   255=Idle High
    DutyCycle2   VAR  byte 
    DutyCycle3   VAR  byte    
    
    SPWM1PIN  VAR PORTB.0          ' SPWM channel 1
    define SPWM1VAR  _DutyCycle1
    
    SPWM2PIN  VAR PORTB.1          ' SPWM channel 2
    define SPWM2VAR  _DutyCycle2
    
    SPWM3PIN  VAR PORTB.2          ' SPWM channel 3
    define SPWM3VAR  _DutyCycle3
     
    Include "Multi_SPWM.pbp"
    
    DutyCycle1 = 0
    DutyCycle2 = 127
    DutyCycle3 = 255
    
    Loop:
       Pause 100
    Goto Loop

  16. #16
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default Re: Pwm portb in pic 16f628 in same time

    Edit it out of your include file, and I would not have it in your Main. I would move it above your include statement like this:

    Code:
    '****************************************************************
    '*  Name    : Test_SPWM.pbp                                     *
    '*  Author  : Darrel Taylor                                     *
    '*  Date    : 9/30/2006                                         *
    '*  Version : 1.0                                               *
    '*  Notes   :                                                   *
    '*          :                                                   *
    '****************************************************************
    ; Initialize your hardware first
    
    DEFINE OSC 20
    CLEAR
    wsave   VAR BYTE    $70     SYSTEM      ' location for W if in bank0
    INCLUDE "DT_INTS-14.bas"         ; Base Interrupt System
    INCLUDE "SPWM_INT.bas"              ; Software PWM module
    
    DEFINE SPWM_FREQ  40                ; SPWM Frequency
    DEFINE SPWM_RES   256               ; SPWM Resolution
    
    
    DutyVars   VAR BYTE[3]              ; DutyCycle Variables
      DutyVar1 VAR DutyVars[0]          ; group them in an array for easy access
      DutyVar2 VAR DutyVars[1]          ; with FOR loops etc.
      DutyVar3 VAR DutyVars[2]
    
    ASM
    SPWM_LIST  macro                    ; Define PIN's to use for SPWM
         SPWM_PIN  PORTB, 0, _DutyVar1
         SPWM_PIN  PORTB, 1, _DutyVar2
         SPWM_PIN  PORTB, 2, _DutyVar3
      endm
      SPWM_INIT  SPWM_LIST              ; Initialize the Pins
    ENDASM
    
    ASM
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler   TMR1_INT,  SPWMhandler,  ASM,  yes
        endm
        INT_CREATE                      ; Creates the interrupt processor
    ENDASM
    
    @ INT_ENABLE  TMR1_INT              ; enable Timer 1 interrupts
    
    ;_____________________________________________________________________________
    
    RandVar   VAR WORD : RandVar = 12345
    LoopCount VAR WORD
    
    Main:
    ;wsave   VAR BYTE    $70     SYSTEM      ' location for W if in bank0
    ;wsave   VAR BYTE    $70     SYSTEM      ' alternate save location for W 
                                             ' if using $70, comment wsave1-3
    
    
                                   ; Simple Demo to fade/flash some LED's
        DutyVar1 = 5                        ; Start with 3 LED's at different
        DutyVar2 = 50                       ; brightness
        DutyVar3 = 150
        pause 3000
         
        FOR LoopCount = 1 to 4              ; Reapeat 4 times
            for DutyVar1 = 0 to 150         ; Fade LED1 UP
                pause 10
                RANDOM  RandVar             
                DutyVar3 = RandVar & $3F          ; Give LED3 a random dutycycle
            next DutyVar1
             
            for DutyVar1 = 150 to 0 STEP -1 ; Fade LED1 Down
                pause 10
                RANDOM  RandVar
                DutyVar2 = RandVar & $3F         ; Give LED2 a random dutycycle
            next DutyVar1
        NEXT LoopCount 
    GOTO Main
    For anyone trying to follow along, here is a much better explanation for a PIC16F628 http://www.picbasic.co.uk/forum/show...9084#post79084
    Last edited by ScaleRobotics; - 13th February 2011 at 22:37.

  17. #17
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default Re: Pwm portb in pic 16f628 in same time

    Quote Originally Posted by dragan77 View Post
    Can you ask you for this code,why i cant compile this code?
    What is wrong?
    Code:
    define OSC 20
    
    define SPWMFREQ  100           ' PWM frequency in Hz    
    
    DutyCycle1   VAR  byte    ' 0-255  0=Idle Low   255=Idle High
    DutyCycle2   VAR  byte 
    DutyCycle3   VAR  byte    
    
    SPWM1PIN  VAR PORTB.0          ' SPWM channel 1
    define SPWM1VAR  _DutyCycle1
    
    SPWM2PIN  VAR PORTB.1          ' SPWM channel 2
    define SPWM2VAR  _DutyCycle2
    
    SPWM3PIN  VAR PORTB.2          ' SPWM channel 3
    define SPWM3VAR  _DutyCycle3
     
    Include "Multi_SPWM.pbp"
    
    DutyCycle1 = 0
    DutyCycle2 = 127
    DutyCycle3 = 255
    
    Loop:
       Pause 100
    Goto Loop
    What error do you get?

  18. #18
    Join Date
    May 2010
    Posts
    43


    Did you find this post helpful? Yes | No

    Default Re: Pwm portb in pic 16f628 in same time

    I get this error.
    Code:
    ERROR: Variable wsave3 position request 416 beyond RAM_END 335.

  19. #19
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default Re: Pwm portb in pic 16f628 in same time

    Quote Originally Posted by dragan77 View Post
    I get this error.
    Code:
    ERROR: Variable wsave3 position request 416 beyond RAM_END 335.
    Somewhere in your include file, mysteriously named "Multi_SPWM.pbp" is a DT_INTS file that still has

    wsave3 VAR BYTE $1A0 SYSTEM ' location for W if in bank3

    and it is not commented out.
    </pre>

  20. #20
    Join Date
    May 2010
    Posts
    43


    Did you find this post helpful? Yes | No

    Default Re: Pwm portb in pic 16f628 in same time

    This is this Multi_SPWM.pbp file:



    Code:
    '****************************************************************
    '*  Name    : Multi_SPWM.pbp                                    *
    '*  Author  : Darrel Taylor                                     *
    '*  Notice  : Copyright (c) 2003                                *
    '*  Date    : May 16, 2004                                      *
    '****************************************************************
    
    DEFINE INTHAND INT_CODE                 ' Tell PBP Where the code starts on an interrupt
    
    wsave       var byte    $20     SYSTEM          ' location for W if in bank0
    
    ' --- IF any of these three lines cause an error ??  Simply Comment them out to fix the problem ----
    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
    ' ------------------------------------------------------------------------------
    
    ssave       var byte    BANK0   SYSTEM          ' location for STATUS register
    psaveU      var byte    BANK0   SYSTEM          ' location for PCLATU register
    psave       var byte    BANK0   SYSTEM          ' location for PCLATH register
    bsave       var byte    BANK0   SYSTEM          ' location for BSR register
    
    DutyCount   VAR byte
    
    
    ASM
        ifdef SPWMFREQ
    FOSC4 = OSC * 1000000 / 4
    IntFREQ = SPWMFREQ * 256
    TimerCount = FOSC4 / IntFREQ
    TimerConst = 65536 - TimerCount + 7
        endif
    ENDASM
    
    ASM
    InitPin  macro  Port, Pin
        MOVE?CT 0, Port, Pin                  ; Make Port.Pin LOW
        ifdef BSR  ; if chip is 16-bit        ; Set TRIS to output
            MOVE?CT 0, ((Port) + 12h), Pin
        else       ; chip is 14-bit
            MOVE?CT 0, ((Port) + 80h), Pin    
        endif
        endm
    ENDASM
    
    ASM
    SPWMActive = 0
    ; ----------------------------------------------------------------------------1-    
    SPWM1used = 0
        ifdef  _SPWM1PIN
            ifdef SPWM1VAR
    SPWM1used = 1
    SPWMActive = 1
            InitPin  _SPWM1PIN
            else
                error "SPWM1VAR must be defined to use SPWM1PIN"
            endif
        else
            ifdef SPWM1VAR
                error "SPWM1PIN must be defined to use SPWM1VAR"
            endif
        endif
    ; ----------------------------------------------------------------------------2-    
    SPWM2used = 0
        ifdef  _SPWM2PIN
            ifdef SPWM2VAR
    SPWM2used = 1
    SPWMActive = 1
            InitPin  _SPWM2PIN
            else
                error "SPWM2VAR must be defined to use SPWM2PIN"
            endif
        else
            ifdef SPWM2VAR
                error "SPWM2PIN must be defined to use SPWM2VAR"
            endif
        endif
    ; ----------------------------------------------------------------------------3-    
    SPWM3used = 0
        ifdef  _SPWM3PIN
            ifdef SPWM3VAR
    SPWM3used = 1
    SPWMActive = 1
            InitPin  _SPWM3PIN
            else
                error "SPWM3VAR must be defined to use SPWM3PIN"
            endif
        else
            ifdef SPWM3VAR
                error "SPWM3PIN must be defined to use SPWM3VAR"
            endif
        endif
    ; ----------------------------------------------------------------------------4-    
    SPWM4used = 0
        ifdef  _SPWM4PIN
            ifdef SPWM4VAR
    SPWM4used = 1
    SPWMActive = 1
            InitPin  _SPWM4PIN
            else
                error "SPWM4VAR must be defined to use SPWM4PIN"
            endif
        else
            ifdef SPWM4VAR
                error "SPWM4PIN must be defined to use SPWM4VAR"
            endif
        endif
    ; ----------------------------------------------------------------------------5-    
    SPWM5used = 0
        ifdef  _SPWM5PIN
            ifdef SPWM5VAR
    SPWM5used = 1
    SPWMActive = 1
            InitPin  _SPWM5PIN
            else
                error "SPWM5VAR must be defined to use SPWM5PIN"
            endif
        else
            ifdef SPWM5VAR
                error "SPWM5PIN must be defined to use SPWM5VAR"
            endif
        endif
    ; ----------------------------------------------------------------------------6-    
    SPWM6used = 0
        ifdef  _SPWM6PIN
            ifdef SPWM6VAR
    SPWM6used = 1
    SPWMActive = 1
            InitPin  _SPWM6PIN
            else
                error "SPWM6VAR must be defined to use SPWM6PIN"
            endif
        else
            ifdef SPWM6VAR
                error "SPWM6PIN must be defined to use SPWM6VAR"
            endif
        endif
    ; ----------------------------------------------------------------------------7-    
    SPWM7used = 0
        ifdef  _SPWM7PIN
            ifdef SPWM7VAR
    SPWM7used = 1
    SPWMActive = 1
            InitPin  _SPWM7PIN
            else
                error "SPWM7VAR must be defined to use SPWM7PIN"
            endif
        else
            ifdef SPWM7VAR
                error "SPWM7PIN must be defined to use SPWM7VAR"
            endif
        endif
    ; ----------------------------------------------------------------------------8-    
    SPWM8used = 0
        ifdef  _SPWM8PIN
            ifdef SPWM8VAR
    SPWM8used = 1
    SPWMActive = 1
            InitPin  _SPWM8PIN
            else
                error "SPWM8VAR must be defined to use SPWM8PIN"
            endif
        else
            ifdef SPWM8VAR
                error "SPWM8PIN must be defined to use SPWM8VAR"
            endif
        endif
    EndASM
    
    ASM
    ; -----------------  ADD TimerConst to TMR1H:TMR1L
    ADD2_TIMER   macro
        CHK?RP  T1CON
        BCF     T1CON,TMR1ON           ; Turn off timer
        MOVLW   LOW(TimerConst)        ;  1
        ADDWF   TMR1L,F                ;  1    ; reload timer with correct value
        BTFSC   STATUS,C               ;  1/2
        INCF    TMR1H,F                ;  1
        MOVLW   HIGH(TimerConst)       ;  1
        ADDWF   TMR1H,F                ;  1
        endm
    ENDASM
    
    ASM
    ; -----------------  ADD TimerConst to TMR1H:TMR1L and restart TIMER1 
    RELOAD_TIMER  macro
     ADD2_TIMER
        BSF     T1CON,TMR1ON           ;  1    ; Turn TIMER1 back on
        CHK?RP  PIR1
        bcf     PIR1, TMR1IF           ; Clear Timer1 Interrupt Flag
        endm
    ENDASM
    
    ASM
    ; -----------------  Load TimerConst into TMR1H:TMR1L 
    LOAD_TIMER  macro
        MOVE?CB  0, T1CON              ; Turn OFF Timer1
        MOVE?CB  0, TMR1L              ; Reset Timer1 to 00:00
        MOVE?CB  0, TMR1H
        ADD2_TIMER
        endm
    EndAsm
    
    ; -----------------  Start PWM's (IF active)  ----------------------------------    
    ASM
        ifdef SPWMFREQ
            if SPWMActive == 1
                LOAD_TIMER
                CHK?RP  PIE1
                bsf   PIE1, TMR1IE    ; Enable Timer1 Interrupts
                CHK?RP  INTCON
                bsf   INTCON, PEIE    ; Turn ON peripheral interrupts
                bsf   INTCON, GIE     ; Turn ON global interrupts
                CHK?RP  PIR1
                bcf   PIR1, TMR1IF
                bsf   T1CON, TMR1ON  ; Turn ON Timer1
            else
                messg  "SPWMFREQ has been defined, but no SPWM channels are allocated"
            endif
        else
            if SPWMActive == 1
                error "SPWMFREQ must be defined to use any SPWM channels"
            else
                messg "Multi_SPWM included but no Channels have been defined"
            endif
        endif  
    ENDASM
    
       Goto Over_Code
    Asm
    INT_START  macro
        ifdef BSR                  ; if chip is 16-bit
            movff   WREG, wsave          ;6   Wreg
    ;        movff   PCLATU, psaveU       ;8   PCLATU
    ;        movff   PCLATH, psave        ;10  PCLATH
            movff   STATUS, ssave        ;12  STATUS
            movff   BSR, bsave           ;14  BSR
    SaveCycles = 10        
        else                       ; chip is 14-bit
            if (CODE_SIZE <= 2)          ;4  cycles to push PC and jump to Int_Code
                movwf   wsave            ;5  copy W to wsave register
                swapf   STATUS,W         ;6  swap status reg to be saved into W
                clrf    STATUS           ;7  change to bank 0 regardless of current bank
                movwf   ssave            ;8  save status reg to a bank 0 register
                movf    PCLATH,w         ;9  move PCLATH reg to be saved into W reg
                movwf   psave            ;10 save PCLATH reg to a bank 0 register
            endif
    SaveCycles = 10        
        endif
        endm
    EndAsm
    
    
    Asm
    INT_RETURN   macro
        ifdef BSR                  ; if chip is 16-bit
            movff   bsave, BSR           ;2   BSR
            movff   ssave, STATUS        ;4   STATUS
    ;        movff   psaveU, PCLATU       ;6   PCLATU
    ;        movff   psave, PCLATH        ;8   PCLATH
            movff   wsave, WREG          ;6  WREG
    RestoreCycles = 10                   ;10 - add 4 to POP and return
        else                       ; chip is 14-bit
    ;        movf    psave,w              ;1  Restore the PCLATH reg
    ;        movwf   PCLATH               ;2
            swapf   ssave,w              ;1  Restore the STATUS reg			
            movwf   STATUS               ;2
            swapf   wsave,f              ;3
            swapf   wsave,w              ;4  Restore W reg
    RestoreCycles = 8                    ;8 - add 4 to POP and return
        endif
        retfie                           ; Return from Interrupt
        endm
    EndAsm
    
    ASM
    SPWMint  macro Port, Pin, DutyVar   ; Interrupt code for each Pin
      local  NotIdle, IdleHigh, SPWMdone, StartAdd1, EndAdd1, StartAdd2, EndAdd2
    StartAdd1 = $
        MOVE?BA   DutyVar              ; Copy DutyVar to W reg
        btfss     STATUS, Z            ; if DutyVar = 0
        goto      NotIdle              ; No, Skip Idle
    EndAdd1 = $
        CHK?RP    Port                 ; YES, then idle Low
        bcf       Port, Pin            ; Set Pin Low
        goto      SPWMdone
    NotIdle
    StartAdd2 = $
        MOVE?BA   _DutyCount           ; Copy DutyCount to W reg
        CHK?RP    DutyVar              ; Select proper bank
        subwf     DutyVar, W           ; Subtract DutyVar from DutyCount
        MOVE?TT   STATUS,C, Port,Pin   ; Copy carry bit to Port/Pin
    EndAdd2 = $
    SPWMdone
    
      ifdef BSR        ; if chip is 16-bit core
    InstCount = InstCount + ((EndAdd1 - StartAdd1 + EndAdd2 - StartAdd2) / 2)  
      else             ; chip is 14-bit core
    InstCount = InstCount + (EndAdd1 - StartAdd1 + EndAdd2 - StartAdd2)
      endif
        endm
    ENDASM
    
    
    ; ------- Interrupt Code ------------------------------------------------------- 
    ASM
    INT_CODE
    InstCount = 0
        INT_START
        CHK?RP  PIR1
        btfss   PIR1, TMR1IF
        goto    NoTMR1Int
        RELOAD_TIMER
        if SPWM1used == 1
            SPWMint  _SPWM1PIN, SPWM1VAR
        endif
        if SPWM2used == 1
            SPWMint  _SPWM2PIN, SPWM2VAR
        endif
        if SPWM3used == 1
            SPWMint  _SPWM3PIN, SPWM3VAR
        endif
        if SPWM4used == 1
            SPWMint  _SPWM4PIN, SPWM4VAR
        endif
        if SPWM5used == 1
            SPWMint  _SPWM5PIN, SPWM5VAR
        endif
        if SPWM6used == 1
            SPWMint  _SPWM6PIN, SPWM6VAR
        endif
        if SPWM7used == 1
            SPWMint  _SPWM7PIN, SPWM7VAR
        endif
        if SPWM8used == 1
            SPWMint  _SPWM8PIN, SPWM8VAR
        endif
        CHK?RP   _DutyCount
        incf     _DutyCount, F
    NoTMR1Int
    ; ---- Test for other interrupts here (If needed) ------------------------------
       
        INT_RETURN
    ENDASM
    ; ------- End of Interrupt Code ------------------------------------------------
    
    ASm
    InstCount = InstCount + SaveCycles + RestoreCycles
    BetweenInts = TimerCount - InstCount
      if (BetweenInts < 0)
          error Current SPWM configuration is INVALID - There is no time left between interrupts
          messg 1 - "Try using a SPWMFREQ lower than" SPWMFREQ
          messg 2 - Use a faster cyrstal than OSC Mhz
          messg 3 - Use fewer SPWM channels
      endif
    ENDASM
    
    Over_Code:
    
    [TabIndex]
    Value=0

  21. #21
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default Re: Pwm portb in pic 16f628 in same time

    I think that May 16, 2004 code (pretty old) came before DT_INTS. I still don't know where you found it. Your error is from all the wsaves in the Multi_SPWM.pbp include file. So simply edit them to be:
    Code:
    ;wsave       var byte    $20     SYSTEM          ' location for W if in bank0 
    wsave       var byte    $70     SYSTEM          ' location for W if in bank0 
    ' --- IF any of these three lines cause an error ??  
    'Simply Comment them out to fix the problem ---- 
    ;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 
    ' ------------------------------------------------------------------------------
    if you are not going to specify it in your code.

  22. #22
    Join Date
    May 2010
    Posts
    43


    Did you find this post helpful? Yes | No

    Default Re: Pwm portb in pic 16f628 in same time

    Ok,I try this code some time to fix,thks allot,for your help!!!! http://www.picbasic.co.uk/forum/imag...lies/smile.gif
    Just one question
    can you tell me, because I like learn Pic Basic ,where I can find some nice tutorial or some video tutorial,about Pic Basic,to I learn write code in Pic Basic,and where i can find some expansion,for how i write code in Pic Basic!

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


    Did you find this post helpful? Yes | No

    Default Re: Pwm portb in pic 16f628 in same time

    To help learn.

    Here are some samples
    http://melabs.com/samples/PBP-mixed/index.htm

    And read the questions/answers on this forum daily.
    Dave
    Always wear safety glasses while programming.

Members who have read this thread : 1

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