full-bridge pwm wave generation using 16f690

Find out how to setup your programmer's software and how to solve many common problems.

Moderator: phalanx

Post Reply
big guy
Posts: 1
Joined: Wed Sep 10, 2014 9:31 pm

full-bridge pwm wave generation using 16f690

Post by big guy » Wed Sep 10, 2014 9:48 pm

I have a problem with my code, im tryng to generate square but it only gives me one peak
thank you in advance

Code: Select all

[;**********************************************************************
    ;   This file is a basic code template for assembly code generation   *
    ;   on the PIC16F690. This file contains the basic code               *
    ;   building blocks to build upon.                                    * 
    ;                                                                     *
    ;   Refer to the MPASM User's Guide for additional information on     *
    ;   features of the assembler (Document DS33014).                     *
    ;                                                                     *
    ;   Refer to the respective PIC data sheet for additional             *
    ;   information on the instruction set.                               *
    ;                                                                     *
    ;**********************************************************************
    ;                                                                     *
    ;    Filename:       PWM.asm                                           *
    ;    Date:                                                            *
    ;    File Version:                                                    *
    ;                                                                     *
    ;    Author:        9208155642085                                                  *
    ;    Company:                                                         *
    ;                                                                     *
    ;                                                                     *
    ;**********************************************************************
    ;                                                                     *
    ;    Files Required: P16F690.INC                                      *
    ;                                                                     *
    ;**********************************************************************
    ;                                                                     *
    ;    Notes:                                                           *
    ;                                                                     *
    ;**********************************************************************


       list      p=16f690      ; list directive to define processor
       #include   <P16F690.inc>      ; processor specific variable definitions
       
       __CONFIG    _CP_OFF & _CPD_OFF & _BOR_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT & _MCLRE_ON & _FCMEN_OFF & _IESO_OFF
       errorlevel -203
       errorlevel -205
       errorlevel -302

    ; '__CONFIG' directive is used to embed configuration data within .asm file.
    ; The labels following the directive are located in the respective .inc file.
    ; See respective data sheet for additional information on configuration word.


    ;***** VARIABLE DEFINITIONS
    w_temp      EQU   0x7D         ; variable used for context saving
    status_temp   EQU   0x7E         ; variable used for context saving
    pclath_temp   EQU   0x7F         ; variable used for context saving


    ;**********************************************************************
       ORG      0x000         ; processor reset vector
         goto      main         ; go to beginning of program


       ORG      0x004         ; interrupt vector location
       movwf      w_temp         ; save off current W register contents
       movf      STATUS,w      ; move status register into W register
       movwf      status_temp      ; save off contents of STATUS register
       movf      PCLATH,w      ; move pclath register into W register
       movwf      pclath_temp      ; save off contents of PCLATH register


    ; isr code can go here or be located as a call subroutine elsewhere

       movf      pclath_temp,w      ; retrieve copy of PCLATH register
       movwf      PCLATH         ; restore pre-isr PCLATH register contents   
       movf      status_temp,w      ; retrieve copy of STATUS register
       movwf      STATUS         ; restore pre-isr STATUS register contents
       swapf      w_temp,f
       swapf      w_temp,w      ; restore pre-isr W register contents
       retfie               ; return from interrupt


    main

    ; remaining code goes here

    ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    ;1. Disable the PWM pin (CCP1) output driver by
    ;setting the associated TRIS bit.
    ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    banksel   TRISC;
    clrf   TRISC;   ;init


    banksel TRISC;
    bsf     TRISC,5;
    ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    ;2. Set the PWM period by loading the PR2 register.
    ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    banksel PR2;
    movlw   d'10'; ??10Khz??
    movwf   PR2;

    ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    ;3. Configure the CCP module for the PWM mode
    ;by loading the CCP1CON register with the
    ;appropriate values.
    ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
             ;PWM output configuration
    banksel CCP1CON;
             ;ECCP mode select bits
    movlw   b'00101100';
    movwf   CCP1CON;

    ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    ;4. Set the PWM duty cycle by loading the CCPR1L
    ;register and DC1B<1:0> bits of the CCP1CON
    ;register.
    ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
             ;50% Duty cycle @ 10KHz
    banksel   CCPR1L;
    movlw      d'50'; ??50%??
    movwf   CCPR1L;

    ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    ;5. Configure and start Timer2:
    ;• Clear the TMR2IF interrupt flag bit of the PIR1 register.
    ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    banksel PIE1;
    bsf      PIE1,TMR2IE;

    banksel PIR1;
    bcf      PIR1,TMR2IF;
    banksel   TMR2IF;
    clrf   TMR2IF;

    ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    ;• Set the Timer2 prescale value by loading the
    ;T2CKPS bits of the T2CON register.
    ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                      ;1:4 prescalar
    banksel   T2CON;
    bcf      T2CON,T2CKPS0;
    bcf      T2CON,T2CKPS1;

    ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    ;• Enable Timer2 by setting the TMR2ON bit of
    ;the T2CON register.
    ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    banksel   T2CON;
    bsf      T2CON, TMR2ON;

    ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    ;6. Enable PWM output after a new PWM cycle has
    ;started:
    ;• Wait until Timer2 overflows (TMR2IF bit of the PIR1 register is set).
    ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    banksel   PIR1;
    btfss   PIR1,TMR2IF;
    goto $-1;

    ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    ;• Enable the CCP1 pin output driver by clearing
    ;the associated TRIS bit.
    ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    banksel   TRISC;
    bcf      TRISC,5;

    goto 0x00;

    ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    END                       ; directive 'end of program'

]

User avatar
leon_heller
Support Volunteer
Posts: 5734
Joined: Sun May 01, 2005 11:20 am
Location: St. Leonards-on-Sea, E. Sussex, UK.

Re: full-bridge pwm wave generation using 16f690

Post by leon_heller » Thu Sep 11, 2014 7:54 am

Use code tags to make the code readable.
Leon Heller
G1HSM

waltr
Support Volunteer
Posts: 2823
Joined: Tue Sep 08, 2009 12:07 pm
Location: Philadelphia, USA

Re: full-bridge pwm wave generation using 16f690

Post by waltr » Fri Sep 12, 2014 6:55 am

Use the MPLAB Simulator to debug your code. Use the StopWatch feature to time the output changes.

Post Reply