Quantcast
Channel: MEL PICBASIC Forum
Viewing all articles
Browse latest Browse all 4787

HPWM with Pic16F886 not working

$
0
0
Hi,
I am trying to make a HPWM on pin RC2 ( CCP1 on a Pic 16F886 chip.
I am not getting anything out.
I used the same code but formatted for 16F88 and it worked, so I dont know why I am not getting anything out.
Any ideas ?
thanks

Ken


Code:

OSCCON = %01110000 '8 Mhz
DEFINE OSC 8
ANSELH = 0
ADCON1 = 7
 
'///////////////////////////
'// Interrupt section //
'///////////////////////////

'////////// ADCin Config //////////////////
Define ADC_BITS 8 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uSec
ADCON0 = %00010001 ' AN4, Configure and turn on A/D Module
ADCON1 = %00000001 ' Analog input

'/////////////////////////
'// LCD configuration //
'/////////////////////////


DEFINE LCD_DREG PORTB        ' Set LCD Data port
DEFINE LCD_DBIT 4                ' Set starting Data bit (0 or 4) if 4-bit bus  RB.4, RB.5, RB.6, RB.7
DEFINE LCD_RSREG PORTC        ' Set LCD Register Select port
DEFINE LCD_RSBIT 1                ' Set LCD Register Select bit
DEFINE LCD_EREG PORTA        ' Set LCD Enable port
DEFINE LCD_EBIT 7                ' Set LCD Enable bit
DEFINE LCD_BITS 4                ' Set LCD bus size (4 or 8 bits) '4 therefore put wire at 4, 5, 6 and 7 of LCD
DEFINE LCD_LINES 2                ' Set number of lines on LCD
DEFINE LCD_COMMANDUS 2500
DEFINE LCD_DATAUS 250
DEFINE CHAR_PACING 2000
pause 1000

'/////////////////////////
'// PIN configuration //
'/////////////////////////

Digits  Var  PORTA
VoltageInput var byte
'///////////////////////////////////////////////
'// Variable Declaration and initialization //
'///////////////////////////////////////////////


VoltageDiff var byte

VoltageREF con 120 ' to get 12v output
DutyCycle var byte
DutyCycle = 127
Frequency con 30000


'////////////////// HPWM /////////////////////////
DEFINE CCP1_REG PORTC
DEFINE CCP1_BIT 2
Pause 20


'////////////////////////////////////////////////////
'////////////////// PROGRAM /////////////////////////
'////////////////////////////////////////////////////

Mainloop:

    Gosub VoltageCheck

                                lcdout $FE,1, "V_Input: ", dec VoltageInput, ":"
                                lcdout $FE,$C0, " Adcin: ", dec VoltageInput
                                Pause 150 

                     
goto Mainloop
End

   
'////////////////////////////////////////////////////   
VoltageCheck:
    ADCON0.2 = 1                        'Start Conversion
   
    adcin 4,VoltageInput
   
    If voltageInput < voltageREF then        ' voltageREF is 127 for 12v dc
        VoltageDiff = VoltageREF - VoltageInput    '=127+(127-115)=139
        DutyCycle = voltageREF + ( voltageREF - VoltageDiff)
        DutyCycle = DutyCycle/255 *100
        HPWM 1,DutyCycle,Frequency        'channel, dutycycle, frequency
        pause 20
    ENDIF
   
    If voltageInput = voltageREF then
        HPWM 1,DutyCycle,Frequency          'channel, dutycycle, frequency   
        pause 20
    else
        VoltageDiff =  VoltageInput - VoltageREF      '=127-(182-127)
        DutyCycle = voltageREF - ( VoltageDiff - voltageREF )
        DutyCycle = DutyCycle/255 *100
        HPWM 1,DutyCycle,Frequency 'channel, dutycycle, frequency
        pause 20
    endif

Return


Viewing all articles
Browse latest Browse all 4787

Trending Articles