PIC18F2550 Configuration

Discuss PIC and electronic related things

Moderators: David Barker, Jerry Messina

ARd16
Posts: 10
Joined: Mon Apr 10, 2023 10:23 am

PIC18F2550 Configuration

Post by ARd16 » Tue Apr 11, 2023 5:55 am

Hello Everyone,
I am newbie in Swordfish.
I just first time to use SF compiler and purchase this compiler on yesterday.
Well, today I just started to use and compiled some pieces code use a PIC18F2550, I plan to use a Crystal 20 Mhz with external and I refer to use a Plugin configGenerator through SF compiler, I have an error on Config.
Please help what is I do wrong, refer to below code :

Code: Select all

{
*****************************************************************************
*  Name    : Tried_New_Test Compiler.BAS                                                   *
*  Author  : Test Code - Tried New Compiler                                 *
*  Notice  : Copyright (c) 2023 [select VIEW...EDITOR OPTIONS]              *
*          : All Rights Reserved                                            *
*  Date    : 11/04/2023                                                     *
*  Version : 1.0                                                            *
*  Notes   :                                                                *
*          :                                                                *
*****************************************************************************
}

Device = 18F2550        //uC use
Declare Xtal = 20       // Ue external crystal

Config
    PLLDIV = 5,         // Divide by 5 (20 MHz oscillator input)
    CPUDIV = OSC1_PLL2, // [Primary Oscillator Src: /1][96 MHz PLL Src: /2]
    USBDIV = 1,         // USB clock source comes directly from the primary oscillator block with no postscale
    FOSC = XT_XT,       // XT oscillator (XT)
    FCMEN = OFF,        // Fail-Safe Clock Monitor disabled
    IESO = OFF,         // Oscillator Switchover mode disabled
    PWRT = OFF,         // PWRT disabled
    BOR = ON,           // Brown-out Reset disabled in hardware and software
    BORV = 3,           // Minimum setting 2.05V
    VREGEN = OFF,       // USB voltage regulator disabled
    WDT = OFF,          // WDT disabled (control is placed on the SWDTEN bit)
    WDTPS = 32768,      // 1:32768
    CCP2MX = ON,        // CCP2 input/output is multiplexed with RC1
    PBADEN = ON,        // PORTB<4:0> pins are configured as analog input channels on Reset
    LPT1OSC = OFF,      // Timer1 configured for higher power operation
    MCLRE = ON,         // MCLR pin enabled; RE3 input pin disabled
    STVREN = ON,        // Stack full/underflow will cause Reset
    LVP = ON,           // Single-Supply ICSP enabled
    XINST = OFF,        // Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
    DEBUG = OFF,        // Background debugger disabled, RB6 and RB7 configured as general purpose I/O pins
    CP0 = OFF,          // Block 0 (000800-001FFFh) is not code-protected
    CP1 = OFF,          // Block 1 (002000-003FFFh) is not code-protected
    CP2 = OFF,          // Block 2 (004000-005FFFh) is not code-protected
    CP3 = OFF,          // Block 3 (006000-007FFFh) is not code-protected
    CPB = OFF,          // Boot block (000000-0007FFh) is not code-protected
    CPD = OFF,          // Data EEPROM is not code-protected
    WRT0 = OFF,         // Block 0 (000800-001FFFh) is not write-protected
    WRT1 = OFF,         // Block 1 (002000-003FFFh) is not write-protected
    WRT2 = OFF,         // Block 2 (004000-005FFFh) is not write-protected
    WRT3 = OFF,         // Block 3 (006000-007FFFh) is not write-protected
    WRTC = OFF,         // Configuration registers (300000-3000FFh) are not write-protected
    WRTB = OFF,         // Boot block (000000-0007FFh) is not write-protected
    WRTD = OFF,         // Data EEPROM is not write-protected
    EBTR0 = OFF,        // Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks
    EBTR1 = OFF,        // Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks
    EBTR2 = OFF,        // Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks
    EBTR3 = OFF,        // Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks
    EBTRB = OFF         // Boot block (000000-0007FFh) is not protected from table reads executed in other blocks


Dim timing As Byte            'Declare timing    
Dim blink_led As Word         'Declare blink_led  
Dim pinLed PORTB.5            'Declare  pinLed      
Dim OnLed PORTB.7             'Declare OnLed       
timing = 12                   '12 times waiting
 
 
TRISB = 0
'LATB.F0 = 0


For blink_led = 0 To 12       'reach 12 time then 
High  OnLed                   'Led On

pinLed = 1                    'Make it on
pause 50                      'Delay a second
pinLed = 0                    'Make it off
pause 50                      'Delay a second, then 
High  OnLed                   'Reach make Led High
Next

While 
Asm

EndAsm
PORTB = 1
pauseus(timing)

Asm
GoTo $+1 ; 2uS                 'Produce a frequency
Nop      ; 1uS

EndAsm

PORTB = 2
pauseus(timing)
Asm
GoTo $+1 ; 2uS
Nop      ; 1uS
EndAsm

PORTB = $41
pauseus(timing)

Asm
GoTo $+1 ; 2uS
Nop      ; 1uS
EndAsm

PORTB = $42
pauseus(timing)

Asm
GoTo $+1 ; 2uS
Nop      ; 1uS
Nop      ; 1uS


EndAsm

Wend
    
 
:) :)
Thanks,
Ar

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Re: PIC18F2550 Configuration

Post by David Barker » Tue Apr 11, 2023 11:37 am

Hi

The code you have posted looks looks more like PROTON (or POSITRON as it is called now). Some examples include:

Code: Select all

Declare Xtal = 20  => clock = 20
Dim pinLed PORTB.5 => Dim pinLed As PORTB.5
High  OnLed => High(OnLed)
pause 50  => delayms(50)
also your block and loop constructs:

Code: Select all

while
wend

=>

while [boolean]
end while

endasm => end asm
you need to look at these first. Maybe try building up your code a little bit at a time, hit F12 to syntax check, and see if there are any errors before continuing.

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: PIC18F2550 Configuration

Post by Jerry Messina » Tue Apr 11, 2023 11:53 am

It looks like you're using examples from another form of BASIC.
I would read through the Swordfish language manual, but try this.

At least it will compile. The issue wasn't with the config statement, it was with all the other stuff.

Code: Select all

device = 18F2550        //uC use
clock = 20              // Use external crystal

config
    PLLDIV = 5,         // Divide by 5 (20 MHz oscillator input)
    CPUDIV = OSC1_PLL2, // [Primary Oscillator Src: /1][96 MHz PLL Src: /2]
    USBDIV = 1,         // USB clock source comes directly from the primary oscillator block with no postscale
    FOSC = HS,          // HS oscillator
    FCMEN = OFF,        // Fail-Safe Clock Monitor disabled
    IESO = OFF,         // Oscillator Switchover mode disabled
    PWRT = OFF,         // PWRT disabled
    BOR = ON,           // Brown-out Reset disabled in hardware and software
    BORV = 3,           // Minimum setting 2.05V
    VREGEN = OFF,       // USB voltage regulator disabled
    WDT = OFF,          // WDT disabled (control is placed on the SWDTEN bit)
    WDTPS = 32768,      // 1:32768
    CCP2MX = ON,        // CCP2 input/output is multiplexed with RC1
    PBADEN = ON,        // PORTB<4:0> pins are configured as analog input channels on Reset
    LPT1OSC = OFF,      // Timer1 configured for higher power operation
    MCLRE = ON,         // MCLR pin enabled; RE3 input pin disabled
    STVREN = ON,        // Stack full/underflow will cause Reset
    LVP = ON,           // Single-Supply ICSP enabled
    XINST = OFF,        // Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
    DEBUG = OFF,        // Background debugger disabled, RB6 and RB7 configured as general purpose I/O pins
    CP0 = OFF,          // Block 0 (000800-001FFFh) is not code-protected
    CP1 = OFF,          // Block 1 (002000-003FFFh) is not code-protected
    CP2 = OFF,          // Block 2 (004000-005FFFh) is not code-protected
    CP3 = OFF,          // Block 3 (006000-007FFFh) is not code-protected
    CPB = OFF,          // Boot block (000000-0007FFh) is not code-protected
    CPD = OFF,          // Data EEPROM is not code-protected
    WRT0 = OFF,         // Block 0 (000800-001FFFh) is not write-protected
    WRT1 = OFF,         // Block 1 (002000-003FFFh) is not write-protected
    WRT2 = OFF,         // Block 2 (004000-005FFFh) is not write-protected
    WRT3 = OFF,         // Block 3 (006000-007FFFh) is not write-protected
    WRTC = OFF,         // Configuration registers (300000-3000FFh) are not write-protected
    WRTB = OFF,         // Boot block (000000-0007FFh) is not write-protected
    WRTD = OFF,         // Data EEPROM is not write-protected
    EBTR0 = OFF,        // Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks
    EBTR1 = OFF,        // Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks
    EBTR2 = OFF,        // Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks
    EBTR3 = OFF,        // Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks
    EBTRB = OFF         // Boot block (000000-0007FFh) is not protected from table reads executed in other blocks

Dim timing As Byte            'Declare timing    
Dim blink_led As Word         'Declare blink_led  
Dim pinLed as PORTB.5            'Declare  pinLed      
Dim OnLed as  PORTB.7             'Declare OnLed       

For blink_led = 0 To 12       'reach 12 time then 
	High(OnLed)                 'Led On

	pinLed = 1                 'Make it on
	delayms(1000)              'Delay a second
	pinLed = 0                 'Make it off
	delayms(1000)              'Delay a second
	High(OnLed)                'Reach make Led High
Next

ARd16
Posts: 10
Joined: Mon Apr 10, 2023 10:23 am

Re: PIC18F2550 Configuration

Post by ARd16 » Tue Apr 11, 2023 7:02 pm

Jerry,
Thanks for your answer, I change refer to your guidance.

Code: Select all

 For blink_led = 0 To 12       'reach 12 time then 

     High(OnLed)                 'Led Off - Inverted
     
	'pinLed = 1                
	high(pinLed)                 'Make it on
	delayMs(100)              'Delay a second
	'pinLed = 0                 'Make it off
	 low(pinLed)
	delayMs(100)              'Delay a second
	'High(OnLed)                'Reach make Led High
	Low(OnLed)
Next  
Above code now is working, I reduce a time due for more fast
But I still have a problem, due my PORTB for an output (PORTB1-PORTB2 and PORTB6) doesn't working, I watch there no produce some frequency and also PORTB there are low state.

Code: Select all

 While
PORTB = %11111110
LATB.0 = 0
'PORTB = 0 '1
DelayMS(timing)
'pauseus(timing)

Asm
GoTo $+1 ; 2uS
Nop      ; 1uS
End Asm

PORTB = 2
LATB.0=1
 DelayMS(timing)
'pauseus(timing)

Asm
GoTo $+1 ; 2uS
Nop      ; 1uS
End Asm

PORTB = $41
LATB.1=1
 DelayMS(timing)
'pauseus(timing)

Asm
GoTo $+1 ; 2uS
Nop      ; 1uS
End Asm

PORTB = $42
DelayMS(timing)
'pauseus(timing)

Asm
GoTo $+1 ; 2uS
Nop      ; 1uS
Nop      ; 1uS
End Asm
   
End While
  
I tried to added LATB but it doesn't work too.
Could you please see what is wrong above code.

Thanks,
Ar.

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: PIC18F2550 Configuration

Post by Jerry Messina » Tue Apr 11, 2023 10:15 pm

I think your 'while' statement isn't doing what you want.

Code: Select all

While
PORTB = %11111110
LATB.0 = 0
is being evaluated as

Code: Select all

While (PORTB = %11111110)
LATB.0 = 0
If you want to create an infinite loop then change that to

Code: Select all

While (true)
    PORTB = %11111110
    LATB.0 = 0
   ' other statements
end while    
You should be sure to set any IO pins to digital mode since the ports powerup in analog mode.
You can do this by adding

Code: Select all

include "setdigitalio.bas"

SetAllDigital()

Also, just so you're aware, if you're running at 20MHz the asm code

Code: Select all

Asm
GoTo $+1 ; 2uS
Nop      ; 1uS
End Asm
is not going to take 3us. It's 3 instruction cycles, and at 20MHz each instruction cycle is (1/20MHz) * 4 = 200ns for a total delay of 3 * 200ns = 600ns

ARd16
Posts: 10
Joined: Mon Apr 10, 2023 10:23 am

Re: PIC18F2550 Configuration

Post by ARd16 » Wed Apr 12, 2023 5:05 am

Hello Jerry,
Thanks for your answer again.
Here are code has been changes refer to your advice, but I still have a problem at RB0-RB1 and RB6 doesn't produced an output.

Code: Select all

 Device = 18F2550        //uC use
Clock = 20              // Use external crystal

Config
    PLLDIV = 5,         // Divide by 5 (20 MHz oscillator input)
    CPUDIV = OSC1_PLL2, // [Primary Oscillator Src: /1][96 MHz PLL Src: /2]
    USBDIV = 1,         // USB clock source comes directly from the primary oscillator block with no postscale
   ' FOSC = HS,          // HS oscillator
    FOSC = XT_XT,       // XT oscillator (XT)
    FCMEN = OFF,        // Fail-Safe Clock Monitor disabled
    IESO = OFF,         // Oscillator Switchover mode disabled
    PWRT = OFF,         // PWRT disabled
    BOR = ON,           // Brown-out Reset disabled in hardware and software
    BORV = 3,           // Minimum setting 2.05V
    VREGEN = OFF,       // USB voltage regulator disabled
    WDT = OFF,          // WDT disabled (control is placed on the SWDTEN bit)
    WDTPS = 32768,      // 1:32768
    CCP2MX = ON,        // CCP2 input/output is multiplexed with RC1
    PBADEN = ON,        // PORTB<4:0> pins are configured as analog input channels on Reset
    LPT1OSC = OFF,      // Timer1 configured for higher power operation
    MCLRE = ON,         // MCLR pin enabled; RE3 input pin disabled
    STVREN = ON,        // Stack full/underflow will cause Reset
    LVP = ON,           // Single-Supply ICSP enabled
    XINST = OFF,        // Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
    DEBUG = OFF,        // Background debugger disabled, RB6 and RB7 configured as general purpose I/O pins
    CP0 = OFF,          // Block 0 (000800-001FFFh) is not code-protected
    CP1 = OFF,          // Block 1 (002000-003FFFh) is not code-protected
    CP2 = OFF,          // Block 2 (004000-005FFFh) is not code-protected
    CP3 = OFF,          // Block 3 (006000-007FFFh) is not code-protected
    CPB = OFF,          // Boot block (000000-0007FFh) is not code-protected
    CPD = OFF,          // Data EEPROM is not code-protected
    WRT0 = OFF,         // Block 0 (000800-001FFFh) is not write-protected
    WRT1 = OFF,         // Block 1 (002000-003FFFh) is not write-protected
    WRT2 = OFF,         // Block 2 (004000-005FFFh) is not write-protected
    WRT3 = OFF,         // Block 3 (006000-007FFFh) is not write-protected
    WRTC = OFF,         // Configuration registers (300000-3000FFh) are not write-protected
    WRTB = OFF,         // Boot block (000000-0007FFh) is not write-protected
    WRTD = OFF,         // Data EEPROM is not write-protected
    EBTR0 = OFF,        // Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks
    EBTR1 = OFF,        // Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks
    EBTR2 = OFF,        // Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks
    EBTR3 = OFF,        // Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks
    EBTRB = OFF         // Boot block (000000-0007FFh) is not protected from table reads executed in other blocks
    
'----------------------------------------------------------------------------/////////////////////////////////////////////
'--------------------Jerry -  You should be sure to set any IO pins to digital mode since the ports powerup in analog mode.
'                             You can do this by adding ---------------------/////////////////////////////////////////////
'----------------------------------- NOTE -----------------------------------/////////////////////////////////////////////
'------------------------------------ Produced Frequency At Output PORT RB0-RB1-RB6 ------////////////////////////////////
'----------------------------------------------------------------------------/////////////////////////////////////////////

include "setdigitalio.bas"
'SetAllDigital()

Dim timing1 As Byte            'Declare timing    
Dim blink_led As Word         'Declare blink_led  
Dim pinLed As PORTB.5            'Declare  pinLed      
Dim OnLed As PORTB.7             'Declare OnLed       
timing1 = 12 

'----------------- Proton To SF---------------------------------------------------------------------- 
'Declare Xtal = 20  =>           clock = 20
'Dim pinLed PORTB.5 =>           Dim pinLed As PORTB.5
'High  OnLed =>                  High(OnLed)
'pause 50  =>                    delayms(50)
'-----------------------------------------------------------------------------------------------------
'------------------------------------------------------------------------------------------------------
'Proton
    'while
    'wend
        'endasm
'SF
    'while [boolean]
    'end while
         'end asm
'-------------------------------------------------------------------------------------------------------

For blink_led = 0 To 12       'reach 12 time then 

High(OnLed)                 'Led Off - Inverted            
	High(pinLed)                 'Make it on
	   DelayMS(100)              'Delay a second
    Low(pinLed)
    	DelayMS(100)              'Delay a second
Low(OnLed)
Next
'------------  Jerry - If you want to create an infinite loop then change that to ------------------------
   ' While (true)
   ' PORTB = %11111110
   ' LATB.0 = 0
   ' other statements
   ' end while   
'-----------------------------------------------------------------------------------------------------------

While (PORTB = %11111110)
LATB.0 = 0

'---------------------------------------- Jerry ------------------------------------------------------------- 
'--------------------     Also, just so you're aware, if you're running at 20MHz the asm code
'--------------------  is not going to take 3us. It's 3 instruction cycles, and at 20MHz 
'--------------------  each instruction cycle is (1/20MHz) * 4 = 200ns for a total delay of 3 * 200ns = 600ns
      'Asm
      'GoTo $+1 ; 2uS
      'Nop      ; 1uS
      'End Asm
      
       'Micro-	0.000001 m	1 x 10-6 m
       'Nano-	0.000000001 m	1 x 10-9 m
       '        At one-billionth of a second, a nanosecond is smaller than a millisecond and microsecond,
       '        but larger than a picosecond, femtosecond and attosecond.
'--------------------------------------------------------------------------------------------------------------
Asm
GoTo $+1 ; 2uS
Nop      ; 1uS
End Asm

PORTB = %11111101     'PORT RB1
 LATB.1 = 0

 DelayMS(timing1)

Asm
GoTo $+1 ; 2uS
Nop      ; 1uS
End Asm

PORTB = %00000001     'PORT RB0
 LATB.0 = 0
   DelayMS(timing1)
   
Asm
 GoTo $+1 ; 2uS
 Nop      ; 1uS
End Asm

PORTB = %01000010     'PORT RB1 And PORT RB6
 LATB.1 = 0
 LATB.6 = 0
   DelayMS(timing1)
   
Asm
 GoTo $+1 ; 2uS
 Nop      ; 1uS
 Nop      ; 1uS
End Asm
   
   End While
 
Above code I has disabled function

Code: Select all

 SetAllDigital() 
Due if I enabled, I have

Code: Select all

 Dim timing1 As Byte            'Declare timing  
{Error} - Said Symbol Not Expected, then If I disabled it, I compiled no have problem, what is wrong according this errors ?.

As well as you said below :
'---------------------------------------- Jerry -------------------------------------------------------------
'-------------------- Also, just so you're aware, if you're running at 20MHz the asm code
'-------------------- is not going to take 3us. It's 3 instruction cycles, and at 20MHz
'-------------------- each instruction cycle is (1/20MHz) * 4 = 200ns for a total delay of 3 * 200ns = 600ns
'Asm
'GoTo $+1 ; 2uS
'Nop ; 1uS
'End Asm
How I should do to increase a timing to reach more higher than 20Mhz of Crystal at 600ns ?.

Thanks.
Ar.

ARd16
Posts: 10
Joined: Mon Apr 10, 2023 10:23 am

Re: PIC18F2550 Configuration

Post by ARd16 » Wed Apr 12, 2023 5:21 am

David Barker wrote:
Tue Apr 11, 2023 11:37 am
Hi

The code you have posted looks looks more like PROTON (or POSITRON as it is called now). Some examples include:

Code: Select all

Declare Xtal = 20  => clock = 20
Dim pinLed PORTB.5 => Dim pinLed As PORTB.5
High  OnLed => High(OnLed)
pause 50  => delayms(50)
also your block and loop constructs:

Code: Select all

while
wend

=>

while [boolean]
end while

endasm => end asm
you need to look at these first. Maybe try building up your code a little bit at a time, hit F12 to syntax check, and see if there are any errors before continuing.
Hello David,
I am sorry, due I didn't see and reply it, ..
Yes, I used also Proton Basic Compiler and now become Positron, I have a license of this compiler, this compiler also very great and have often update a firmware.

Why I purchased SF, because I want to learn your compiler and as I knew and hearing from outside that your SF is great too, then I searching then I found and directly buy from https://www.mecanique.co.uk/

I am really new SF and never used, long time ago I used Visual Basic 6.0, so now I learning back.

Thanks,
Ar.

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: PIC18F2550 Configuration

Post by Jerry Messina » Wed Apr 12, 2023 11:24 am

'SetAllDigital()' is a subroutine call, so it has to be located after all the variables are declared

Code: Select all

Device = 18F2550        //uC use
Clock = 20              // Use external crystal

Config
    PLLDIV = 5,         // Divide by 5 (20 MHz oscillator input)
    CPUDIV = OSC1_PLL2, // [Primary Oscillator Src: /1][96 MHz PLL Src: /2]
    USBDIV = 1,         // USB clock source comes directly from the primary oscillator block with no postscale
    FOSC = HS,          // HS oscillator
// with a 20MHz xtal you should be using HS mode, not XT mode
// above 3.5 MHz, HS is recommended over XT
'    FOSC = XT_XT,       // XT oscillator (XT)
    FCMEN = OFF,        // Fail-Safe Clock Monitor disabled
    IESO = OFF,         // Oscillator Switchover mode disabled
    PWRT = OFF,         // PWRT disabled
    BOR = ON,           // Brown-out Reset disabled in hardware and software
    BORV = 3,           // Minimum setting 2.05V
    VREGEN = OFF,       // USB voltage regulator disabled
    WDT = OFF,          // WDT disabled (control is placed on the SWDTEN bit)
    WDTPS = 32768,      // 1:32768
    CCP2MX = ON,        // CCP2 input/output is multiplexed with RC1
    PBADEN = ON,        // PORTB<4:0> pins are configured as analog input channels on Reset
    LPT1OSC = OFF,      // Timer1 configured for higher power operation
    MCLRE = ON,         // MCLR pin enabled; RE3 input pin disabled
    STVREN = ON,        // Stack full/underflow will cause Reset
    LVP = ON,           // Single-Supply ICSP enabled
    XINST = OFF,        // Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
    DEBUG = OFF,        // Background debugger disabled, RB6 and RB7 configured as general purpose I/O pins
    CP0 = OFF,          // Block 0 (000800-001FFFh) is not code-protected
    CP1 = OFF,          // Block 1 (002000-003FFFh) is not code-protected
    CP2 = OFF,          // Block 2 (004000-005FFFh) is not code-protected
    CP3 = OFF,          // Block 3 (006000-007FFFh) is not code-protected
    CPB = OFF,          // Boot block (000000-0007FFh) is not code-protected
    CPD = OFF,          // Data EEPROM is not code-protected
    WRT0 = OFF,         // Block 0 (000800-001FFFh) is not write-protected
    WRT1 = OFF,         // Block 1 (002000-003FFFh) is not write-protected
    WRT2 = OFF,         // Block 2 (004000-005FFFh) is not write-protected
    WRT3 = OFF,         // Block 3 (006000-007FFFh) is not write-protected
    WRTC = OFF,         // Configuration registers (300000-3000FFh) are not write-protected
    WRTB = OFF,         // Boot block (000000-0007FFh) is not write-protected
    WRTD = OFF,         // Data EEPROM is not write-protected
    EBTR0 = OFF,        // Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks
    EBTR1 = OFF,        // Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks
    EBTR2 = OFF,        // Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks
    EBTR3 = OFF,        // Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks
    EBTRB = OFF         // Boot block (000000-0007FFh) is not protected from table reads executed in other blocks
    
'----------------------------------------------------------------------------/////////////////////////////////////////////
'--------------------Jerry -  You should be sure to set any IO pins to digital mode since the ports powerup in analog mode.
'                             You can do this by adding ---------------------/////////////////////////////////////////////
'----------------------------------- NOTE -----------------------------------/////////////////////////////////////////////
'------------------------------------ Produced Frequency At Output PORT RB0-RB1-RB6 ------////////////////////////////////
'----------------------------------------------------------------------------/////////////////////////////////////////////
'----------------- Proton To SF---------------------------------------------------------------------- 
'Declare Xtal = 20  =>           clock = 20
'Dim pinLed PORTB.5 =>           Dim pinLed As PORTB.5
'High  OnLed =>                  High(OnLed)
'pause 50  =>                    delayms(50)
'-----------------------------------------------------------------------------------------------------

include "setdigitalio.bas"

// SetAllDigital is a run-time subroutine call
// move it to after all the variables are declared
'SetAllDigital()

Dim timing1 As Byte           'Declare timing    
Dim blink_led As Word         'Declare blink_led  
Dim pinLed As PORTB.5         'Declare  pinLed      
Dim OnLed As PORTB.7          'Declare OnLed       

// program code starts here:
// set all IO pins to digital mode
SetAllDigital()

timing1 = 12 

For blink_led = 0 To 12       'reach 12 time then 
    High(OnLed)               'Led Off - Inverted            
    High(pinLed)              'Make it on
    DelayMS(100)              'Delay 100ms
    Low(pinLed)
    DelayMS(100)              'Delay 100ms
    Low(OnLed)
Next

// at this point you have set PORTB.5 (pinLed) and PORTB.7 (OnLed) to output mode above with High() and Low(),
// but the other pins on PORTB remain as inputs
// in order to use the entire PORTB as outputs you must set the TRIS register to 0    
TRISB = 0

// to get short delays in the usec range you can use the 'delayus()' library
// replace this asm code:
//		Asm
//		 GoTo $+1 ; 2uS
//		 Nop      ; 1uS
//		End Asm
// with this:
//    delayus(3)
// for very short delays of a few usec it won't be entirely accurate, but it should be close

While (true)
    PORTB = %11111110
    LATB.0 = 0
    delayus(3)
    LATB.1 = 0

    DelayMS(timing1)

    delayus(3)

    PORTB = %00000001     'PORT RB0
    LATB.0 = 0
    DelayMS(timing1)
   
    delayus(3)

    PORTB = %01000010     'PORT RB1 And PORT RB6
    LATB.1 = 0
    LATB.6 = 0
    DelayMS(timing1)
   
    delayus(4)
End While

As is, much of the code inside the while loop is going to produce VERY short pulses, just an instruction cycle or two.
You may want to rearrange where the delayus calls are.

If you have used VisualBasic then learning Swordfish should be fairly easy.
Instead of trying to use Proton code, I'd suggest reading the users manual and looking at some of the samples.

ARd16
Posts: 10
Joined: Mon Apr 10, 2023 10:23 am

Re: PIC18F2550 Configuration

Post by ARd16 » Thu Apr 13, 2023 5:01 am

Hello Jerry,
Thanks for reply again, some code I has changes, due I test it on Proteus it seems very lower pulse and I am not reach to expected point, please see below :

Code: Select all

Device = 18F2550        //uC use
Clock = 20              // Use external crystal

Config
    PLLDIV = 5,         // Divide by 5 (20 MHz oscillator input)
    CPUDIV = OSC1_PLL2, // [Primary Oscillator Src: /1][96 MHz PLL Src: /2]
    USBDIV = 1,         // USB clock source comes directly from the primary oscillator block with no postscale
    FOSC = HS,          // HS oscillator
// with a 20MHz xtal you should be using HS mode, not XT mode
// above 3.5 MHz, HS is recommended over XT
'    FOSC = XT_XT,       // XT oscillator (XT)
    FCMEN = OFF,        // Fail-Safe Clock Monitor disabled
    IESO = OFF,         // Oscillator Switchover mode disabled
    PWRT = OFF,         // PWRT disabled
    BOR = ON,           // Brown-out Reset disabled in hardware and software
    BORV = 3,           // Minimum setting 2.05V
    VREGEN = OFF,       // USB voltage regulator disabled
    WDT = OFF,          // WDT disabled (control is placed on the SWDTEN bit)
    WDTPS = 32768,      // 1:32768
    CCP2MX = ON,        // CCP2 input/output is multiplexed with RC1
    PBADEN = ON,        // PORTB<4:0> pins are configured as analog input channels on Reset
    LPT1OSC = OFF,      // Timer1 configured for higher power operation
    MCLRE = ON,         // MCLR pin enabled; RE3 input pin disabled
    STVREN = ON,        // Stack full/underflow will cause Reset
    LVP = ON,           // Single-Supply ICSP enabled
    XINST = OFF,        // Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
    DEBUG = OFF,        // Background debugger disabled, RB6 and RB7 configured as general purpose I/O pins
    CP0 = OFF,          // Block 0 (000800-001FFFh) is not code-protected
    CP1 = OFF,          // Block 1 (002000-003FFFh) is not code-protected
    CP2 = OFF,          // Block 2 (004000-005FFFh) is not code-protected
    CP3 = OFF,          // Block 3 (006000-007FFFh) is not code-protected
    CPB = OFF,          // Boot block (000000-0007FFh) is not code-protected
    CPD = OFF,          // Data EEPROM is not code-protected
    WRT0 = OFF,         // Block 0 (000800-001FFFh) is not write-protected
    WRT1 = OFF,         // Block 1 (002000-003FFFh) is not write-protected
    WRT2 = OFF,         // Block 2 (004000-005FFFh) is not write-protected
    WRT3 = OFF,         // Block 3 (006000-007FFFh) is not write-protected
    WRTC = OFF,         // Configuration registers (300000-3000FFh) are not write-protected
    WRTB = OFF,         // Boot block (000000-0007FFh) is not write-protected
    WRTD = OFF,         // Data EEPROM is not write-protected
    EBTR0 = OFF,        // Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks
    EBTR1 = OFF,        // Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks
    EBTR2 = OFF,        // Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks
    EBTR3 = OFF,        // Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks
    EBTRB = OFF         // Boot block (000000-0007FFh) is not protected from table reads executed in other blocks
    
'----------------------------------------------------------------------------/////////////////////////////////////////////
'--------------------Jerry -  You should be sure to set any IO pins to digital mode since the ports powerup in analog mode.
'                             You can do this by adding ---------------------/////////////////////////////////////////////
'----------------------------------- NOTE -----------------------------------/////////////////////////////////////////////
'------------------------------------ Produced Frequency At Output PORT RB0-RB1-RB6 ------////////////////////////////////
'----------------------------------------------------------------------------/////////////////////////////////////////////
'----------------- Proton To SF---------------------------------------------------------------------- 
'Declare Xtal = 20  =>           clock = 20
'Dim pinLed PORTB.5 =>           Dim pinLed As PORTB.5
'High  OnLed =>                  High(OnLed)
'pause 50  =>                    delayms(50)
'-----------------------------------------------------------------------------------------------------

Include "setdigitalio.bas"

// SetAllDigital is a run-time subroutine call
// move it to after all the variables are declared
'SetAllDigital()

'Dim timing1 As Byte           'Declare timing    
Dim blink_led As Word         'Declare blink_led  
Dim pinLed As PORTB.5         'Declare  pinLed      
Dim OnLed As PORTB.7          'Declare OnLed       

// program code starts here:
// set all IO pins to digital mode
SetAllDigital()

'timing1 = 12 '12 

For blink_led = 0 To 12       'reach 12 time then 
    High(OnLed)               'Led Off - Inverted            
    High(pinLed)              'Make it on
    DelayMS(100)              'Delay 100ms
    Low(pinLed)
    DelayMS(100)              'Delay 100ms
    Low(OnLed)
Next

// at this point you have set PORTB.5 (pinLed) and PORTB.7 (OnLed) to output mode above with High() and Low(),
// but the other pins on PORTB remain as inputs
// in order to use the entire PORTB as outputs you must set the TRIS register to 0    
TRISB = 0

// to get short delays in the usec range you can use the 'delayus()' library
// replace this asm code:
//		Asm
//		 GoTo $+1 ; 2uS
//		 Nop      ; 1uS
//		End Asm
// with this:
//    delayus(3)
// for very short delays of a few usec it won't be entirely accurate, but it should be close

While (true)
  
LoopIf_Possible:
                       
 PORTB = %11111110                
   LATB.0 = 0
 PORTB = %11111101                
   LATB.1 = 0
 'PORTB=%10111111                         
  ' LATB.6 = 0
 DelayUS(24)
 DelayUS(3)

 PORTB = %11111110             
   LATB.0 = 0
 PORTB = %11111101      
   LATB.1 = 0
 PORTB = %10111111                       
   LATB.6 = 0
  DelayUS(23)
  DelayUS(1) 
 
 PORTB = %11111110                    
  LATB.0 = 0
 PORTB = %11111101                          
  LATB.1 = 0
 PORTB = %10111111                                 
 LATB.6 = 0
 DelayUS(24)
   
  
 PORTB =%11111110                           
  LATB.0 = 0
 PORTB =%11111101             
  LATB.1 = 0
 PORTB = %10111111   
  LATB.6 = 0
 DelayUS(1) 
   DelayUS(22)
 
 Goto  LoopIf_Possible
  
End While  
Here is a screen shot picture, refer to code above:
Image

As you can see, almost I reach the point, only the PORTB6 on a top picture I need to reduce to reach till half value refer to PORTB0 and PORTB1, can you please help, what is should I change to reach half value than both PORTB0 and PORTB1.

I'd suggest reading the users manual and looking at some of the samples.
I download it few day ago once I bought SF compiler, but seems like no much information about the manual, apologize me if I wrong said, wher can I buy eBook from Online Store according SF compiler, could you please give me advice and a link if possible.

Thanks,
Ar.
Attachments
7 seg.JPG
7 seg.JPG (43.66 KiB) Viewed 11328 times

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: PIC18F2550 Configuration

Post by Jerry Messina » Thu Apr 13, 2023 11:31 am

As you can see, almost I reach the point, only the PORTB6 on a top picture I need to reduce to reach till half value refer to PORTB0 and PORTB1, can you please help, what is should I change to reach half value than both PORTB0 and PORTB1.
I'm sorry, but I don't understand the question.

I don't use Proteus, and I don't know what that attachment is displaying... count of PORTB pin changes? time of PORTB pulses?

Does this do what you want? If not can you try to describe what you're looking to do?

Code: Select all

TRISB = 0
While (true)
  PORTB = %11111110                
  PORTB = %11111101                
  PORTB = %10111111                         
  PORTB = %11111111                         
  DelayUS(24)
End While  
There's no ebook about Swordfish that I know of. Many of your questions seem very basic in nature (while vs goto) and would apply to pretty much any language.

ARd16
Posts: 10
Joined: Mon Apr 10, 2023 10:23 am

Re: PIC18F2550 Configuration

Post by ARd16 » Thu Apr 13, 2023 1:00 pm

Hello Jerry,
Thank you so much so far your advice.
As well as your latest pieces code it doesn't expected what I need, so I tried to changes some PORTB I/O to get half value with both other PORTB, now looks more better but still need to reduce little smaller, I tried many times, it's doesn't closer but to much lower going, so I think it need to reduce little bit by hardware not from compiler.
Here are latest result picture :
Image
Anyway thank you so much for a reply and response.


Thannks.
Ar.
Attachments
Result 7Segment.JPG
Result 7Segment.JPG (74.24 KiB) Viewed 11315 times

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: PIC18F2550 Configuration

Post by Jerry Messina » Thu Apr 13, 2023 2:25 pm

so I tried to changes some PORTB I/O to get half value with both other PORTB, now looks more better but still need to reduce little smaller
Sorry, but I don't understand what you need. What does "half value with both other PORTB" mean?

Can you explain what you are looking for any better?

What are those numbers that you are showing for PORTB6, PORTB1, and PORTB0?
Are they a time, a count, or some other meaning? What are they displaying?

What do they display if you change the code to this:

Code: Select all

TRISB = 0
While (true)
  PORTB = %10111110                
  PORTB = %11111101                
  DelayUS(24)
End While  

ARd16
Posts: 10
Joined: Mon Apr 10, 2023 10:23 am

Re: PIC18F2550 Configuration

Post by ARd16 » Thu Apr 13, 2023 5:38 pm

Hello Jerry,
Thanks for your reply again.
As well as your pieces code below

Code: Select all

 TRISB = 0
While (true)
  PORTB = %10111110                
  PORTB = %11111101                
  DelayUS(24)
End While   
Here is a result
Image
Sorry, but I don't understand what you need. What does "half value with both other PORTB" mean?

Can you explain what you are looking for any better?

What are those numbers that you are showing for PORTB6, PORTB1, and PORTB0?
Are they a time, a count, or some other meaning? What are they displaying?
Ask you can see before, I have a result value PORTB0 and PORTB1 is 38495 Hz and PORTB0 is 38495 HZ and PORTB6 before is 29800 HZ ( around that value, I forgot), then I asking you how I to decrease to reach half value both of PORTB0 and PORTB1, if this half than PORTB0 and PORTB1 this will get around 19247 HZ.

This code I need to produced a frequency for 38000 HZ for two PORT and 19000 HZ for one PORT

I am looking to reach 38000 HZ for PORTB0 and PORTB1 and for PORTB6 I wish 19000 HZ, but this is little difficult to get precision value, I think should do trim on hardware site.

How can I write inside a compiler to use an external crystal with frequency of crystal is 4.332 mhz and 7.3728 Mhz, can you please show me.

Here is an update, that I used a crystal 4.332 Mhz.


Thanks,
Ar.
Test Pieces Code.JPG
Test Pieces Code.JPG (45.6 KiB) Viewed 11283 times
Test Pieces Code.JPG
Test Pieces Code.JPG (28.4 KiB) Viewed 11287 times

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: PIC18F2550 Configuration

Post by Jerry Messina » Thu Apr 13, 2023 9:24 pm

In order to get exactly 38000Hz I think you'd have to have a clock that's a multiple of 4.864MHz.

With that, you could use the CCP/ECCP peripherals in PWM mode to get 38000 and 19000 and still do other things with the pic.

ARd16
Posts: 10
Joined: Mon Apr 10, 2023 10:23 am

Re: PIC18F2550 Configuration

Post by ARd16 » Sat Apr 15, 2023 5:03 am

Hello Jerry,
I tried to searching at yesterday for this crystal in my country, this crystal is a available, so could be I will do with this.....but,
I don't know exactly how to do, the CCP only have 2 pin CCP1 and CCP2, and I need 3 pin for produce a frequency, it's could made with 3 pin for a CCP ?,
I look out in this forum about the CCP setup and how to do with another type of PIC18xxxx.

:D :D
Thanks.
Ar

Post Reply