News:

;) This forum is the property of Proton software developers

Main Menu

Is it really easier on Arduino for this

Started by RGV250, May 24, 2021, 09:02 AM

Previous topic - Next topic

RGV250

Hi,
Hopefully not too controversial topic.
I have put this in the beginners section as although I have used Proton/Positron for years I know next to nothing about the use of analogs.
The reason for the title, I find Positron streets ahead of pretty much everything Arduino does which makes easy tasks hard work.
Except this...  analogWrite(DEV_BL_PIN,140)
I might be wrong but this seems to be the only area that I have found that Arduino is easier than Positron, here I just define the pin and value and it creates a PWM output.
At the moment it will be for Amicus18 but hopefully easily exportable to a 16 bit device, any advice would be greatly received.
Am I wrong and I have missed something or could this likely be a future upgrade to the compiler.

Regards,
Bob

trastikata

Bob,

With PWM you have Duty Cycle and Frequency - it seems that in the case of Arduino the Freq is missing ... whereas you have PWM and HPWM commands in Positron, so I am not sure why you say Positron is harder to use?.

My point is this is only software emulation and when it comes to software emulations it is easy adding subroutines or procedures in Positron. My observation of Arduino libraries is that they are not as well thought as Positron's, often simplified or lacking functionality.

When it comes to directives related to Hardware execution, Positron is much better in my opinion.


RGV250

Hi Trastikata,
I stand corrected, I had been looking at all the analog registers etc and had totally missed the PWM command.

Bob

RGV250

Hi,
It looks like it will need to be HPWM so is this all I need or do I need to set other registers etc
HPWM 1,140,32767 to use it on Port C.2 on 18F25K20?

Bob

okmn

#4
a little code parts from my project
pwm signal's duty and frequency control able with usart.


Device = 16F877A

Config FOSC_HS, WDTE_OFF, PWRTE_OFF, BOREN_OFF, LVP_OFF, CPD_OFF, WRT_OFF, DEBUG_OFF, CP_OFF

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------
Declare Xtal = 20
ADCON0.0 = 0
ADCON1 = 07 
All_Digital  true

TRISC = %10000000
PORTC = 127

Declare HPWM2_Pin PORTC.1 ' Select HPWM port and bit for PWM 2 module (ch 2)

'BAUDRATE 19200 20MHZ
'**********************************************************************
    Declare Hserial_RCSTA=$90
    Declare Hserial_TXSTA=$24
    Declare Hserial_SPBRG=64 ' 19200 Bauds
    Declare Hserial_Clear=On

INTCON: Interrupt CONTROL REGISTER
'**********************************************************************
Symbol GIE    = INTCON.7  'genel interruptlar� a�iyor
Symbol PEIE  = INTCON.6  'peripheral interruptlar� a�iyor
Symbol TMR0IE = INTCON.5  'Timer0 Overflow Interrupt Enable bit
Symbol INTE  = INTCON.4  'INT External Interrupt Enable bit
Symbol IOCIE  = INTCON.3  'Interrupt-on-Change Enable bit
Symbol TMR0IF = INTCON.2  'Timer0 Overflow Interrupt Flag bit
Symbol INTF  = INTCON.1  'INT External Interrupt Flag bit
Symbol IOCIF  = INTCON.0  'Interrupt-on-Change Interrupt Flag bit(1)
Symbol RCIE = PIE1.5      'USART Recive interrupt enable bit
Symbol RCIF = PIR1.5      'USART Receive interrupt flag bit

RCIE = 1  'USART Recive Interrupt ENABLE YAPILDI
 'sspen = 0
 SSPCON = 0
 SSPCON2 = 0
 TMR0 = 12
'**********************************************************************
INTCON = $E0'11100000 $F0'$B0'$A0
'ADCON1 = 7
OPTION_REG = $07'00000111 $06  1:256


Dim PWM_CALISMA As Bit
Dim GUC_TEMP As Byte
Dim DUTY As Float
Dim PWM_FREKANS As Word

PWM_FREKANS = 1500 ' CHANGE VALUE POSIBLE

Symbol LM2576_ENABLE = PORTC.0
LM2576_ENABLE = 1

MENU_NO = a[8]'BY USART
GUC_TEMP = ERead MENU_NO  'BY E2PROM


HPWM ACTIVE:

DUTY = (GUC_TEMP*145/90)+93.5
            LM2576_ENABLE = 0
            CCP1CON = 0  'Capture/Compare/PWM disabled (resets CCP1 module)
'****************************************************************

            CCP2CON.2 = 1
            CCP2CON.3 = 1
'****************************************************************
            HPWM 2, DUTY, PWM_FREKANS
            PORTC.2 = 1
            PWM_CALISMA = 1
EndIf

If DUR_BASLA = 0 And PWM_CALISMA = 1 Then ''''''''''''''''''''''''''''''
    For ONDER=0 To 9
        GoSub GECIKME_10mS
    Next ONDER
PWM_CALISMA = 0

EndIf


top204

#5
All that the Arduino code is doing is setting up the microcontroller's PWM peripheral for a frequency that will operate at 8-bits duty, so a value of 0 to 255 will alter the duty cycle from 0 to 100%, but at a set frequency.

They used the simplest, and crudest, method of producing a PWM waveform with the AVR device, but it will not work on some peripheral devices because the frequency may be too high. So it was produced to simply illuminate an LED etc... :-)

I did something similar for the Amicus18 board so users could provide a crude analogue output. See the files "Amicus18_hpwm8.inc" and "Amicus18_hpwm10.inc", within the compiler's "Sources" folder "C:\Program Files (x86)\ProtonIDE\PDS\Includes\Sources"

For example:

OpenAnalog1()

WriteAnalog1(pDutyCycle)

If using the "Amicus18_hpwm8.inc" include file, 8-bits will do for the duty cycle, and with the "Amicus18_hpwm10.inc" include file, 10-bits will do for the duty cycle.

The newer devices have quite a few CCP and PWM peripherals and PPS (Peripheral Pin Select) for them, so a user can choose a pin that will provide the waveform, within the specs of the device.

For the Positron8 board (using a PIC18F27K42 device operating at 64MHz), I have created a library that will provide up to 8 waveform outputs, of frequencies of up to 1.2MHz! The user can also choose which of the three timers will be used for a particular CCP or PWM peripheral, so 3 different square wave frequencies can be output from the device. The use of a CCP or PWM peripheral is invisible to the user and the procedures choose which one is required, and manipulates the particular peripheral's SFRs. For example:

HPWM7(HPWM_cTimer4, 6000, 127)        ' Set the frequency and duty for PWM channel 7
HPWM8(HPWM_cTimer6, 2000, 127)        ' Set the frequency and duty for PWM channel 8


The pins are set by the Declare HPWM directives, and the relevant pin's PPS is also setup as follows:

    Include "CCP_PWM.inc"                              ' Load the PWM library into the program
'
' Setup the pins used for the PWM outputs
'
    Declare HPWM1_Pin = PORTC.2
    Declare HPWM2_Pin = PORTC.1
    Declare HPWM3_Pin = PORTB.5
    Declare HPWM4_Pin = PORTB.0
    Declare HPWM5_Pin = PORTC.4
    Declare HPWM6_Pin = PORTC.5
    Declare HPWM7_Pin = PORTB.2
    Declare HPWM8_Pin = PORTC.3

'---------------------------------------------------------------------------
Main:
    PPS_Unlock()
    'RC2PPS = PPS_Fn_PWM1                    ' Configure PWM1 PPS
    'RC1PPS = PPS_Fn_PWM2                    ' Configure PWM2 PPS
    'RB5PPS = PPS_Fn_PWM3                    ' Configure PWM3 PPS
    'RB0PPS = PPS_Fn_PWM4                    ' Configure PWM4 PPS
    'RC2PPS = PPS_Fn_PWM5                    ' Configure PWM5 PPS
    'RC2PPS = PPS_Fn_PWM6                    ' Configure PWM6 PPS
    RC2PPS = PPS_Fn_PWM7                     ' Configure PWM7 PPS
    RC3PPS = PPS_Fn_PWM8                     ' Configure PWM8 PPS

If the frequency is not going to change, but the duty cycle is, a simple procedure can be used to alter the duty cycle of each PWM output, once its frequency is set. This is very fast to operate:

    For bDuty = 0 To 255
        HPWM8_Duty(bDuty)             
        DelayMS 50
    Next

I've made the procedures as simple as possible and as flexible as possible, and not crude functions as in Arduino. :-)

I altered my Commodore 64 SID player firmware for the Positron8 board, so it has 3 seperate outputs for each synth channel, and the pins used for the channels can be altered to suite a breadboard layout or PCB. So it is a Surround Sound SID. :-)

okmn

#6
Les ,
i stil could not start my project(with 877A) to convert  the 47Q10 or 47K42 series pic mcus or 16F1976 using your procedure!!

Would you gift to us? too much sample codes from simple to complex level with procedures..please. to learn strong.

tidy "bedside resource".

RGV250

Thanks for all the replies, it is only for a backlight and it is on the Amicus18 so I think I will use that option.
The example is pretty awful as there are virtually no comments and the datasheet has loads of info missing so a bit of a challenge.
It is for a Waveshare 1.28" round display.  https://thepihut.com/products/round-1-28-lcd-display-module-240x240-ips-65k-rgb?variant=39592264302787&currency=GBP&utm_medium=product_sync&utm_source=google&utm_content=sag_organic&utm_campaign=sag_organic&gclid=EAIaIQobChMItq-y48ri8AIVx4jVCh3mFgO2EAQYAiABEgLhLfD_BwE

Bob

top204

For the Amicus18 PWM routines, see the document I created; "Amicus18 Companion Shield for Proton". It can be loaded via the Help menu in the IDE.

See the section: "Digital Meets Analogue"

top204

Okmn.

Each device family can have slightly different requirements for the CCP or the PWM peripherals, so a study of the devices's datasheet and experimentation on a breadboard is required to get things working, and understand how they work by making mistakes.

That is exactly how I do it, and I rarely ask anyone for any help in creating code or circuits etc... I just get on with it until it works, which is the fun part. :-)

Within the compiler's "Samples\New Samples\" folder, there is a set of routines I created to show how to calculate frequencies and duty cycles on a PIC18F25K22 device, but the principle works with all CCP peripherals, with a few tweaks of the code for particular SFRs that are different. The samples folder is "Proton_Hpwm_Macros"

top204

That's an interesting round graphic LCD.

I've never come across a GC9A01 display driver, so I'll have to take a look at its setup mechanism. The problem with most of these boards, including that one, is that they do not bring out the MISO pin from the LCD, so the pixels cannot be read from the display, which means they cannot be used for sprites graphics etc, because the code needs to read the underlying pixels and store them before the sprite moves, then place them back after the sprite moves.


RGV250

Hi,
It is quite slow drawing the whole screen, I have just managed to initialise it and set it with a colour background and you can see it being drawn.
I think it is static once written so it does not need constant refresh which may make it quick once the background is drawn.
I would never have got that far without the Arduino example however bad it is.

Bob

Stephen Moss

Quote from: top204 on May 24, 2021, 11:14 AMThey used the simplest, and crudest, method of producing a PWM waveform with the AVR device, but it will not work on some peripheral devices because the frequency may be too high. So it was produced to simply illuminate an LED etc... :-)
I totally agree with that.
On the Mega 2560 it is 33KHz, nearly 10KHz higher than than H-Bridge Motor driver boards it was being used with were rated for and no way to change it without digging into the awful device data sheet and manipulating the registers directly thereby making using their PWM command pointless.