Request to C aficionados, what is the difference in the C to Positron code

Started by TimB, Aug 26, 2021, 06:44 PM

Previous topic - Next topic

TimB

Hi friends


I have some C code to set up a PWM (RC.2) on a Pic16F1779. The Hex works. You can see the project for it in the file attached

Below is Positron conversion. It is to my eyes pretty much the same. But it does not make the PWM work.

In case you have any doubt I hate the new Pics. Its only I cannot buy the ones I want that I have to use the devils spawn the 16f1779

Any help appreciated

Cheers

Tim

Device = 16F1779
 Declare Xtal = 16


'**** Added by Fuse Configurator ****
' Use the Fuses Tab to change these settings

Config1 FOSC_INTOSC, WDTE_OFF, PWRTE_ON, MCLRE_ON, CP_OFF, BOREN_ON, CLKOUTEN_OFF, IESO_ON, FCMEN_ON
Config2 WRT_OFF, PPS1WAY_ON, ZCD_OFF, PLLEN_ON, STVREN_ON, BORV_LO, LPBOR_OFF, LVP_OFF

'**** End of Fuse Configurator Settings ****
'-------------------------------------------------------------------------------


MAIN:
    '------------------ OSC INIT  ---------------------------
    OSCCON = $78
    OSCSTAT = $80
    OSCTUNE = 0
    BORCON = 0

    '------------------ PREPARE FOR PWM on RC.2 PWM9 --------
    '1. Disable the PWM9 pin output driver
    TRISC.2 = 1
    '2. Clear the PWM9CON register
    PWM9CON = 0
    '3. Load T2PR with PWM period
    T2PR = $31
    '4. Load PWM9DCH and PWM9DCL with the duty cycle
    PWM9_LoadDutyValue(99)      ' for 50%
    '5. Configure and start TMR2
    TMR2_Initialize()
    '6. Enable PWM9 pin output
    TRISC.2 = 0
    While PIR1bits_TMR1IF = 0: Wend   ' wait till TMR2 overflows
    '7.
    RC2PPS = $1B    ' RC2->PWM9:PWM9OUT
    '8. Configure and Enable PWM Module PWM9CON
    PWM9_Initialize()
    '---------------------------------------------------------


     While 1=1: Wend




Proc PWM9_Initialize()
    ' Set the PWM to the options selected in the PIC10 / PIC12 / PIC16 / PIC18 MCUs.
    ' PWM9POL active_hi; PWM9EN enabled;
    PWM9CON = $80;
    ' DC 24;
    PWM9DCH = $18;
    ' DC 3;
    PWM9DCL = $C0;
    ' Select timer
    CCPTMRS2bits_P9TSEL0 = 0
EndProc


Proc PWM9_LoadDutyValue(dutyValue As Word)
     ' Writing to 8 MSBs of PWM duty cycle in PWMDCH register
     PWM9DCH = (dutyValue & $03FC) >> 2;
     ' Writing to 2 LSBs of PWM duty cycle in PWMDCL register
     PWM9DCL = (dutyValue & $0003) << 6;
EndProc

Proc TMR2_Initialize()
    ' Set TMR2 to the options selected in the User Interface

    ' T2CS FOSC/4;
    T2CLKCON = 1;
    ' T2PSYNC Not Synchronized; T2MODE Software control; T2CKPOL Rising Edge; T2CKSYNC Not Synchronized;
    T2HLT = 0;
    ' T2RSEL T2CKIPPS pin;
    T2RST = 0;
    ' T2PR 49;
    T2PR = $31;
    ' TMR2 0;
    T2TMR = 0;
    ' Clearing IF flag.
    PIR1bits_TMR2IF = 0;

    ' T2CKPS 1:4; T2OUTPS 1:1; TMR2ON on;
    T2CON = $A0;
EndProc


[attach id=674]pic16F1779-PWM-RC2.X.zip[/attach]

RGV250

Hi Tim,
Try doing the 2 lines where you & and shift in 2 lines so the shift etc is done before the move. I have had similar issues in the past so might work.

PWM9DCH_Temp = (dutyValue & $03FC) >> 2
PWM9DCH = PWM9DCH_Temp

Bob

TimB

Thanks RGV

My good friend Gabi got the code working. Code below

His comment was that there may be an error in the Def file

This is what is in there and it looks wrong

 OPTION_REG_REG Bits
$define OPTION_REG_REGbits_PSA OPTION_REG_REG.3
$define OPTION_REG_REGbits_TMR0SE OPTION_REG_REG.4
$define OPTION_REG_REGbits_TMR0CS OPTION_REG_REG.5
$define OPTION_REG_REGbits_INTEDG OPTION_REG_REG.6
$define OPTION_REG_REGbits_NOT_WPUEN OPTION_REG_REG.7
$define OPTION_REG_REGbits_PS0_OPTION_REG_REG OPTION_REG_REG.0
$define OPTION_REG_REGbits_PS1_OPTION_REG_REG OPTION_REG_REG.1
$define OPTION_REG_REGbits_PS2_OPTION_REG_REG OPTION_REG_REG.2
$define OPTION_REG_REGbits_T0SE OPTION_REG_REG.4
$define OPTION_REG_REGbits_T0CS OPTION_REG_REG.5

Probably meant to be be $define OPTION_REG_bits.....

I note though that there is no Pin manager type code in my original code


Device = 16F1779
Declare Xtal = 16


'**** Added by Fuse Configurator ****
' Use the Fuses Tab to change these settings

Config1 FOSC_INTOSC, WDTE_OFF, PWRTE_ON, MCLRE_ON, CP_OFF, BOREN_ON, CLKOUTEN_OFF, IESO_ON, FCMEN_ON
Config2 WRT_OFF, PPS1WAY_ON, ZCD_OFF, PLLEN_ON, STVREN_ON, BORV_LO, LPBOR_OFF, LVP_OFF

'**** End of Fuse Configurator Settings ****
'-------------------------------------------------------------------------------


PIN_MANAGER:
   LATE = 0
   LATD = 0
   LATA = 0
   LATB = 0
   LATC = 0

   TRISE = 7
   TRISA = $FF;
   TRISB = $FF;
   TRISC = $FB;
   TRISD = $FF;

   ANSELD = $FF;
   ANSELC = $FC;
   ANSELB = $3F;
   ANSELE = $07;
   ANSELA = $3F;

   WPUD = 0
   WPUE = 0
   WPUB = 0
   WPUA = 0
   WPUC = 0

   OPTION_REG.7 = 1

   ODCONE = 0
   ODCONA = 0
   ODCONB = 0
   ODCONC = 0
   ODCOND = 0

   SLRCONA = $FF;
   SLRCONB = $FF;
   SLRCONC = $FF;
   SLRCOND = $FF;
   SLRCONE = $07;

   INLVLA = $3F;
   INLVLB = $F0;
   INLVLC = $FF;
   INLVLD = $FF;

   RC2PPS = $1B;   //RC2->PWM9:PWM9OUT;

OSCILATOR_SETUP:
    OSCCON = $78
    OSCSTAT = $80
    OSCTUNE = 0
    BORCON = 0

WDT_SETUP:
    WDTCON = $16    ' needed ?


TMR2_INIT:
   T2CLKCON = 1
   T2HLT = 0
   T2RST = 0
   T2PR = $31;
   T2TMR = 0
   PIR1.1 = 0  '
   T2CON = $A0

PWM9_INIT:
   PWM9CON = $80
   PWM9DCH = $18
   PWM9DCL = $C0
   CCPTMRS2.4 = 0  '    CCPTMRS2bits.P9TSEL = 0;
   CCPTMRS2.5 = 0


MAIN:

    PWM9_LoadDutyValue(99)

     While 1=1
     ' code
     Wend






Proc PWM9_LoadDutyValue(dutyValue As Word)
     ' Writing to 8 MSBs of PWM duty cycle in PWMDCH register
     PWM9DCH = (dutyValue & $03FC) >> 2;
     ' Writing to 2 LSBs of PWM duty cycle in PWMDCL register
     PWM9DCL = (dutyValue & $0003) << 6;
EndProc


top204

I'll get into the OPTION_REG issue. Because the .PPI and .def files are created from Microchip files by an application I wrote, quite some years ago, that scans and parses them, they must have changed the name format in some of them, unsurprisingly, and the parser has joined things that were not originally there when I first wrote the semi-auto creator.

I'll see if there are any other of the same things happening in the .def files, because if Microchip did it once, it is guaranteed they have done it again and again and again. :-)