News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

PWM feedback and variable frequency

Started by Abdullah, Aug 31, 2025, 06:04 PM

Previous topic - Next topic

Pepe

demo proteus

Abdullah

Quote from: Pepe on Sep 23, 2025, 11:57 AMdemo proteus
Muchas gracias.
Si te resulta fácil por favor explícalo un poco para que sea más fácil de entender.
Abdullah

Pepe

another example

Fanie

My advice Abdullah is that you learn how the micro and the software interact to produce an outcome.
Otherwise you will always be dependent on others.

Abdullah

Quote from: Fanie on Oct 02, 2025, 08:15 AMMy advice Abdullah is that you learn how the micro and the software interact to produce an outcome.
Otherwise you will always be dependent on others.
Ok sir how I can learn it
Abdullah

CPR

Quote from: Abdullah on Oct 02, 2025, 08:56 AMOk sir how I can learn it

The best way is to start with a VERY simple program that you can create by yourself. Think "Hello World" for a PIC - that might be a flashing LED. You will make mistakes, you will have frustration, but you will then learn. If you copy and paste code you do not understand, as Fanie said, you will always be reliant on others, not independent, and not "learn". I wish you all the best. Go for it!  :)

Abdullah

Quote from: CPR on Oct 02, 2025, 08:30 PM
Quote from: Abdullah on Oct 02, 2025, 08:56 AMOk sir how I can learn it

The best way is to start with a VERY simple program that you can create by yourself. Think "Hello World" for a PIC - that might be a flashing LED. You will make mistakes, you will have frustration, but you will then learn. If you copy and paste code you do not understand, as Fanie said, you will always be reliant on others, not independent, and not "learn". I wish you all the best. Go for it!  :)
Hello Sir I strat the programming 4 mouths age and I make LCD 16x2 code for DC or AC volt showing and I make battery cut off circuit and I make HC 05 module with 16f887 16 pins on and off but I don't understand how to control feedback
And I make variable frequency auto adjustable with SG 3525 and 16f676
Control with ADin 0 and etc
Abdullah

Abdullah

Hello sir, I have tried many times in 16F676. I want to control the duty cycle through simple PWM for feedback, but I have tried many times, the duty cycle does not work.Device 16F676
Xtal 4
All_Digital True
Declare Adin_Res 10
Declare Adin_Tad FRC
Declare Adin_Stime 50
TRISC = 0x00
Dim adc_value As Word
Dim volt As Float
Dim DUTY As Byte
Dim Point As Float
adc_value = 0
DUTY = 127
Point = 2.5
Do
    adc_value = ADIn 0
    volt = (adc_value * 5.0) / 1023.0
    If volt < Point Then
    If DUTY < 250 Then
    DUTY = DUTY + 1
    EndIf
    ElseIf volt > Point Then
    If DUTY > 5 Then
    DUTY = DUTY - 1
    EndIf
    EndIf
    PWM PORTC.0, DUTY, 250
    DelayMS 10
Loop
End
Abdullah

Ivano

Hi Abdullah, it's clear from the code you wrote that you're unfamiliar with Positron; you haven't read the Positron manual or the PIC datasheet.
The code you wrote can't work; that PWM port B looks like you made it up yourself.
The duty variable you calculate, after appropriate calculations, must be inserted into two PIC registers if they're 10 bits, or into one if you only need 8 bits. You also need to initialize other registers to choose the timer used, the CCP peripheral in PWM mode, calculate the period, and insert it into the appropriate register. I'm not familiar with the 16F676, but by reading the datasheet, you can find the names of the various registers and even the formulas for the period and duty. There are several examples; just search.
As someone said, before attempting a complex project, you need to learn the basics, creating simple projects and making small changes to see how it behaves. Gradually, you can move on to more complex projects,
in order to gain experience.
If you run into any difficulties, you'll definitely find help on the forum. Asking for ready-made code certainly doesn't help you learn, and if you encounter problems or make changes to the code, you'll never be able to make them. Good luck! Sorry for the English translation.

RGV250

#49
Hi,
As Ivano says, try to start with something simple. Have you looked at the samples as there are both HPWM and PWM both for a 16F series device. Les puts a lot of work into creating these samples so that is always my first place to look when I need to find out something. In fact the PWM example is almost what you are trying to do.
A couple of things about your code, the first and most important is there are no comments so people on the forum cannot see your thoughts which is very important.

Second, I wonder what happens when volt = 2.5 as both conditions will be ignored. One of them needs to be volt <= point or volt >= point but not both.
You could also make the second ElseIf just Else so it is on or the other.

If you are only going by +/-1 INC / DEC are neater commands.

Bob

Stephen Moss

Quote from: Abdullah on Nov 05, 2025, 05:01 AMHello sir, I have tried many times in 16F676. I want to control the duty cycle through simple PWM for feedback, but I have tried many times, the duty cycle does not work.
Pepe has already provided you with several version of code for doing PWM, which knowing Pepe I am sure will do what you have requested. I have already pointed you to the examples provided with the software, and indicated you should RTFM & the provided code in conjunction with the datasheets to learn what the code is doing, that is the only way you can alter anything to do what you want.
I am getting the impression that you simply can't be bothered to help yourself by trying to learn these things, we are all busy people and don't have time to write your code for you.

The manual states...
QuoteOverview
Output pulse-width-modulation on a pin, then return the pin to input state.
It seems unlikely that you would want the PWM signal to stop as it will when the pin returns to being an input, are you sure that is the correct command to be using rather then the HPWM command?

I have no idea what you are trying to achieve with...
If volt < Point Then
    If DUTY < 250 Then
       DUTY = DUTY + 1
    EndIf
ElseIf volt > Point Then
    If DUTY > 5 Then
       DUTY = DUTY - 1
    EndIf
EndIf
If your analogue input is a the PWM output converted to a voltage then as you are using a 10-bit ADC point (2.5, I presume volts) = 512, as you never set the duty above 250 the PWM (If DUTY < 250) the PWM output will never produce a voltages greater than 2.5.

Abdullah

Hello sir, I sincerely apologize for this. I know you all are busy people. Actually, the method that works on the 684 is not working on this one. One reason for the difficulty is that I don't understand English well, so I find it a bit hard to read the datasheet. Please just tell me the method — I'll try to handle the rest myself. If there's any command or method in Positron related to feedback, please let me know so I can study it.
Abdullah

Abdullah

#52
Device 16F676
Xtal 4
All_Digital True
Declare Adin_Res 10
Declare Adin_Tad FRC
Declare Adin_Stime 50
TRISC = 0
TRISA = %111111
ANSEL = %000001
CMCON = 7
Dim adc_value As Word
Dim voltage As Float
Dim DUTY As Byte
DUTY = 0
Main:
adc_value = ADIn(0)       
    DUTY = adc_value / 4
    PWM PORTC.0 , DUTY, 250
    voltage = (adc_value * 5) / 1023
    DelayMS 5
GoTo Main
End
Abdullah

Pepe

The 16f676 does not have hardware PWM; it can only be done via software. Attached is an example of a 250Hz PWM with a resolution of 156 steps.

Abdullah

Quote from: Pepe on Nov 05, 2025, 11:54 AMThe 16f676 does not have hardware PWM; it can only be done via software. Attached is an example of a 250Hz PWM with a resolution of 156 steps.
ok sir i need only 50hz max
Abdullah

Abdullah

Quote from: Pepe on Nov 05, 2025, 11:54 AMThe 16f676 does not have hardware PWM; it can only be done via software. Attached is an example of a 250Hz PWM with a resolution of 156 steps.
Sir I hope that I make an effort to understand this myself and won't ask about it again I'll work hard on my own
Abdullah

Pepe


Pepe

example of pwm with timer1 and 255-step resolution at 50Hz

Abdullah

Thank you very much Sir I expect it will take me about a week to understand it, but once i do,I'll try to creat different things with it
Abdullah

Ivano

I've never used the PWM command portc.0,..., and I didn't remember it was there. Sorry, I don't know if it still works on newer PICs. It certainly only generates a maximum of 255 pulses, but I don't think that's what you need. What you need is HPWM..... I don't remember all the parameters because I never use it, but as Pepe said, this PIC doesn't have hardware PWM. In another post, Les replied that the HPWM commands are now obsolete, also due to the different architecture of the newer PICs.
I solved the problem with procedures that modify the various registers appropriately. If you change PICs, check the datasheet to see if they have the same registers and, if necessary, adapt the code to the new PIC.
You can also generate the PWM using a timer and an interrupt routine to vary the duty cycle and frequency by incrementing or decrementing a variable.