16f877 generate pwm according 2 analog input


Results 1 to 7 of 7

Threaded View

  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

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 : 0

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