Menu

16F628A pwm

Help
2006-10-01
2013-05-30
  • Stefano Delfiore

    I tried pwm using the 16F628A, but I have encounterd some problem.
    I began my test using the following code:

    'f_pwm 38kHz duty_cycle 50%
    #chip 16F628A, 20
    #mem 224

    #config  _CP_OFF & _DATA_CP_OFF & _PWRTE_OFF & _WDT_OFF & _LVP_OFF & _MCLRE_OFF & _HS_OSC

    dir PORTB b'11110111'  'PORTB.3 as output
    dir PORTA b'11111111'

    movlw 0x07   'comparator off
    movwf CMCON

    #define PWM_Freq 38
    #define PWM_Duty 50

    Main:
    InitPWM
    PWMOn
    loop1:
    goto loop1

    'end

    I compiled the code (I got the assembler)and then I used mplab to simulate and see the output at PORTB.3.
    The output is a 38kHz square wave but the duty cycle is about 96%.
    So I tried to change the duty cycle changing the line:
    #define PWM_Duty 50
    for example into the line:
    #define PWM_Duty 20
    when I compiled the code in the dos window I got the message:

    Great Cow BASIC (0.9 17/9/2006)

    Compiling COMPILED.TXT ...

    Type mismatch in module GCBASIC  at address 0F8D:E0B4

    Hit any key to return to system

    I realized that using other value for PWM_Duty different from 50, I get always this type of error.

    At this point following the datasheet of 16F628A I wrote a simple code to generate a 18kHz square wave with a 50% duty cycle:

    'f_pwm 38kHz duty_cycle 50%
    #chip 16F628A, 20
    #mem 224
    #config  _CP_OFF & _DATA_CP_OFF & _PWRTE_OFF & _WDT_OFF & _LVP_OFF & _MCLRE_OFF & _HS_OSC

    dir PORTB b'11110111'  'PORTB.3 as output
    dir PORTA b'11111111'  'PORTA as input

    CMCON=0x07  'comparator off

    'PWM settings

    PR2=131 'set the frequency of square vawe

    SET T2CON.TMR2ON 1  'enable timer2
       
    CCPR1L=64           'set the duty
    SET CCP1CON.CCP1X 1
    SET CCP1CON.CCP1Y 1
       
    CCP1CON=0x0C    'enable pwm
       
    Main:
    loop1:
    goto loop1

    this code, using mplab to simulate the output, produce a correct waveform.
    The difference between the two code is a different value for the register CCPR1L and for the bits CCP1CON.CCP1X and CCP1CON.CCP1Y

    Regards
    Stefano

     
    • Hugh Considine

      Hugh Considine - 2006-10-01

      Hardware PWM in GCBASIC is not behaving well!

      The error that occurs when the duty cycle is changed from 50% is due to a bug in the part of GCBASIC that handles scripts which I've just fixed. Try downloading GCBASIC.EXE from http://gcbasic.sourceforge.net/newfiles.

      From what I can see in the above program, you've actually cleared CCP1CON.CCP1X and CCP1CON.CCP1Y by setting CCP1CON to 0x0C (b'00001100'). This may be something to do with the maximum resolution of the PWM.

      I'll make sure that PWM is better in the next version of GCBASIC!

       
    • Stefano Delfiore

      Hi Hugh, thank you for your answers, I try to visit the link http://gcbasic.sourceforge.net/newfiles but  the page returns the follow message:

      An error has been encountered in accessing this page.

      1. Server: gcbasic.sourceforge.net
      2. URL path: /newfiles.
      3. Error notes: File does not exist: /home/groups/g/gc/gcbasic/htdocs/newfiles.
      4. Error type: 404
      5. Request method: GET
      6. Request query string:
      7. Time: 2006-10-01 08:54:26 PDT (1159718066)

      Reporting this problem: The problem you have encountered is with a project web site hosted by SourceForge.net. This issue should be reported to the SourceForge.net-hosted project (not to SourceForge.net).

      If this is a severe or recurring/persistent problem, please do one of the following, and provide the error text (numbered 1 through 7, above):

         1. Contact the project via their designated support resources.
         2. Contact the project administrators of this project via email (see the upper right-hand corner of the Project Summary page for their usernames) at user-name@users.sourceforge.net

      If you are a member of the project that maintains this web content, please refer to the Site Documentation regarding the project web service for further assistance.

      regards
      stefano

       
    • Stefano Delfiore

      I found the problem, in your link there is a dot after the word "newfiles", the dot is in the link, so the word becomes "newfiles."

      regards
      Stefano

       
    • Nobody/Anonymous

      Stefano, had the same problem with PWM as you on the 16f684.  Depending on Hughs fix, things that worked for me, (more as a proof of concept then a fix):
      1)  Keep PWM_Freq =<25
      2)  Keep PWM_Duty =>25

      Also had to set OSCCON register for 8mhz internal clock, because still using PICKIT1 as platform.

      Maybe define PWM_Freq, and PWM_Duty as words, and use as a sub in your program?

      Good luck,
      Kent

       
    • Stefano Delfiore

      Hi Hugh, I tried your fix, but something has gone wrong. When I try to compile I get the following message in the dos windows:

      Great Cow BASIC (0.9 27/9/2006)

      Compiling COMPIL~1.TXT ...

      Cannot find COMPIL~1.TXT!
      Press any key to exit

      stefano

       
      • Hugh Considine

        Hugh Considine - 2006-10-02

        Yesterday I changed the file reading code to try and make GCBASIC handle filenames of more than 8 characters (a limit imposed by QBASIC). This new code is probably the source of the error. Try the newest version - it should be fixed.

         
    • Stefano Delfiore

      Hi Kent, I don't know pic 16F684,so I download the manual to have an idea of this micro and I saw that its pwm module is a little bit more complex than 16F628A.
      I don't if  define PWM_Freq and PWM_Duty as words could change something, I will wait for the next version of program.

      Stefano

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.