16f877 generate pwm according 2 analog input


Closed Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2008
    Posts
    2

    Default 16f877 generate pwm according 2 analog input

    Hello everybody,

    I am trying to make buck converter and control it with 16f877. The logic is simple. If the output voltage of the buck is higher than reference voltage the pwm duty cycle will decrease, if the output voltage is lower than the reference the duty cycle will increase. It seems very simple, but I couldn't be able to write the program I write but i doesn't work. I am new at pic programming. Help me please!!!

    My crystal is 20MHz.

    And it gives an error " ADC conversion clock period (1e-7) is possible invalid for device clock frequency" in proteus simulation.


    Code:
     
    
      DEFINE OSC 20
     DEFINE ADC_BITS  8    
     DEFINE ADC_SAMPLES 50
    
     TRISA=%00000011 
     TRISB=00 
     TRISC=00 
     TRISD=00 
     TRISE=00
    
    
    Reference var byte
    Voltage var byte
    
    CCP1CON=%000011100
    CCP2CON=%000011100
    T2CON=%00000101    'timer 2 on, 1:1 postscale, 1:4 prescale
    PR2= 49            
    CCPR1L=%00011001
    
    
    get_reference:
    ADCON1=%00000000
    ' Set LEFT Justification
    ' Enable ALL ADC's (using PIC16F87x as example)
    ADCON0=%11000001
    
    ' Set ADC Channel 0 (RA0/AN0)
    ' Enable ADC Module
    PauseUS 50
    ' 50uS Pause to allow sampling Capacitor to charge
    ADCON0.1=1
    ' Start Conversion
    While ADCON0.1=1:Wend
    ' Wait for conversion to complete
    Reference=ADRESH
    ' Read 8-bit Result
    return
    
    get_voltage:                  
    ADCON0=%11001001
    ' Set Fosc/32
    ' Set ADC Channel 1 (RA1/AN1)
    ' Enable ADC Module
    PauseUS 50
    ADCON0.1=1
    While ADCON0.1=1:Wend
    Voltage=ADRESH
    return
    
    azalt:
    CCPR1L=CCPR1L-1
    return
    
    arttir:
    CCPR1L=CCPR1L+1
    return
    
    sabit:
    CCPR1L=CCPR1L
    return
    
    main_loop:
    Gosub get_Reference ' Get reference value
    Gosub get_voltage ' Get voltage value
    if  Reference>Voltage    then azalt
    if  Reference< Voltage    then arttir
    IF Reference=Voltage   then sabit
    goto main_loop         ' Do it forever
    
    end
    Attached Images Attached Images  
    Last edited by Darrel Taylor; - 24th December 2008 at 06:28. Reason: added space

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


    Did you find this post helpful? Yes | No

    Default

    Welcome to the forum.

    First thing I see in the code "main-loop" it just stops, no return, goto or even a THEN to go with the IF.

    Second thing. If the only place you are trying this is on a simulator, good luck.
    Use a bread board for your simulator.
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    There's more to the code.
    For some reason vBulletin saw "Reference&lt;Voltage" as being HTML and it messed up the post. I added a space, and now it shows the whole thing.<hr>
    I think Proteus is complaining about using ADCON0=%11000001.
    A/D Clock mode 3 is not recommended at 20mhz, but I wouldn't call it an error.
    Using ADCON0 = %10000001 might clear the problem, by selecting FOSC/32, which is what the comments say you wanted anyhow.

    But I think the biggest problem is that the execution from the beginning falls into the get_reference Subroutine.
    When it hits the RETURN, it has no idea where to go, because nothing GOSUB'd to it to begin with.
    You should have a GOTO main_loop before that point.

    You should also put some limits on CCPR1L.
    It shouldn't go above PR2 (49) or try to go below 0.
    Just some IF statements will fix that.

    hth,
    DT

  4. #4
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by madxice View Post
    Hello everybody,

    I am trying to make buck converter and control it with 16f877. The logic is simple. If the output voltage of the buck is higher than reference voltage the pwm duty cycle will decrease, if the output voltage is lower than the reference the duty cycle will increase. It seems very simple, but I couldn't be able to write the program I write but i doesn't work. I am new at pic programming. Help me please!!!

    My crystal is 20MHz.

    And it gives an error " ADC conversion clock period (1e-7) is possible invalid for device clock frequency" in proteus simulation.


    Code:
     
    
      DEFINE OSC 20
     DEFINE ADC_BITS  8    
     DEFINE ADC_SAMPLES 50
    
     TRISA=%00000011 
     TRISB=00 
     TRISC=00 
     TRISD=00 
     TRISE=00
    
    
    Reference var byte
    Voltage var byte
    
    CCP1CON=%000011100
    CCP2CON=%000011100
    T2CON=%00000101    'timer 2 on, 1:1 postscale, 1:4 prescale
    PR2= 49            
    CCPR1L=%00011001
    
    
    get_reference:
    ADCON1=%00000000
    ' Set LEFT Justification
    ' Enable ALL ADC's (using PIC16F87x as example)
    ADCON0=%11000001
    
    ' Set ADC Channel 0 (RA0/AN0)
    ' Enable ADC Module
    PauseUS 50
    ' 50uS Pause to allow sampling Capacitor to charge
    ADCON0.1=1
    ' Start Conversion
    While ADCON0.1=1:Wend
    ' Wait for conversion to complete
    Reference=ADRESH
    ' Read 8-bit Result
    return
    
    get_voltage:                  
    ADCON0=%11001001
    ' Set Fosc/32
    ' Set ADC Channel 1 (RA1/AN1)
    ' Enable ADC Module
    PauseUS 50
    ADCON0.1=1
    While ADCON0.1=1:Wend
    Voltage=ADRESH
    return
    
    azalt:
    CCPR1L=CCPR1L-1
    return
    
    arttir:
    CCPR1L=CCPR1L+1
    return
    
    sabit:
    CCPR1L=CCPR1L
    return
    
    main_loop:
    Gosub get_Reference ' Get reference value
    Gosub get_voltage ' Get voltage value
    if  Reference>Voltage    then azalt
    if  Reference< Voltage    then arttir
    IF Reference=Voltage   then sabit
    goto main_loop         ' Do it forever
    
    end
    I do not know how or if it will affect your code, but I did notice your CCP1CON and CCP2CON register settings have 9 bits.
    Last edited by Archangel; - 24th December 2008 at 06:59.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  5. #5
    Join Date
    Dec 2008
    Posts
    2


    Did you find this post helpful? Yes | No

    Default pwm and SPI at the same time

    Hello again,

    For my buck problem, I found a good example. With little changes I applied to my circuit. Now it is working. This circuit was maintaining the voltage.It was infiinite loop.

    Now, I want to use SPI module of 16f877 for TLC5941 led driver IC. It uses CLK, SDO outputs continuously. This should be infinite loop too.

    May be it will be a silly question. But is there any way to use same pic for 2 different infnite loops? Do I have to use 2 pics. I can use, but if it is available I want to do it with one pic. Because I think to use rgb leds. And if I use 2 pics for one color, it will increse the total number of pics in my whole project.

  6. #6
    Join Date
    Apr 2009
    Posts
    1


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by madxice View Post
    Hello again,

    For my buck problem, I found a good example. With little changes I applied to my circuit. Now it is working. This circuit was maintaining the voltage.It was infiinite loop.
    How nice you found code, but why not share it so we can follow.

  7. #7
    Join Date
    Jul 2009
    Location
    Worcester South Africa
    Posts
    18


    Did you find this post helpful? Yes | No

    Default

    Yes I would also like to see that code

Similar Threads

  1. Variable PWM PIC18F2431
    By trr1985 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 12th June 2009, 06:03
  2. How to generate sine PWM using 16f877
    By kvrajasekar in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 12th September 2008, 08:40
  3. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 17:27
  4. 10bits adc using 16f877
    By sixty9sandals in forum mel PIC BASIC
    Replies: 7
    Last Post: - 8th March 2007, 18:23
  5. Can anyone help a beginner in a struggle?
    By douglasjam in forum mel PIC BASIC
    Replies: 1
    Last Post: - 5th May 2005, 23:29

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