zoals de titel al luidt. Ik wil hiermee PWM maken. Ik begin met 20KHz en met S1 en S1 (dus met PORTB.0 en PORTB.1) kan ik de frequentie bepalen door te drukken. Waarom werkt dit niet?

DEVICE 16F628A
CONFIG INTRC_OSC_NOCLKOUT, WDT_OFF, PWRTE_ON, LVP_OFF, MCLRE_OFF
ALL_DIGITAL TRUE

DIM frequentiehoog AS BYTE
DIM frequentielaag AS BYTE

SYMBOL S1 = PORTB.0
SYMBOL S2 = PORTB.1

PORTB_PULLUPS ON

frequentiehoog = 0
frequentielaag = 0

frequentiehoog = frequentiehoog + 25
frequentielaag = frequentielaag + 25

Opnieuw:

IF S1 = 0 THEN
frequentiehoog = frequentiehoog + 1
frequentielaag = frequentielaag + 1
ENDIF

IF S1 = 0 THEN
frequentiehoog = frequentiehoog - 1
frequentielaag = frequentielaag - 1
ENDIF

PORTA.1 = 1
DELAYUS frequentiehoog
PORTA.1 = 0
DELAYUS frequentielaag

GOTO Opnieuw

END

plaatje voor de verduidelijking:

http://members.lycos.nl/electrojan/PWM.GIF

je hebt 2 keer s1 = 0 ,bij een s1 =0 gaat hij -1 en bij s1=0 gaat hij ook s1 +1

kan het niet met het command pwm??

Op 24 september 2005 19:21:19 schreef chevcamaro:
je hebt 2 keer s1 = 0 ,bij een s1 =0 gaat hij -1 en bij s1=0 gaat hij ook s1 +1

kan het niet met het command pwm??

jawel maar ik ken deze niet goed. Ik heb wel gezocht met google, maar ik kom niet verder dan dit:

PWM pin,duty,cycles

alleen deze is wel van een basic stamp dus ik weet niet of het werkt. Ik zal eens kijken of dat lukt.

hierbij een stukje uit de help functie:

PWM

Syntax
PWM Pin , Duty , Cycles

Overview
Output pulse-width-modulation on a pin, then return the pin to input state.

Operators
Pin is a Port.Pin constant that specifies the I/O pin to use.
Duty is a variable, constant (0-255), or expression, which specifies the analogue level desired (0-5 volts).
Cycles is a variable or constant (0-255) which specifies the number of cycles to output. Larger capacitors require multiple cycles to fully charge. Cycle time is dependant on Xtal frequency. If a 4MHz crystal is used, then cycle takes approx 5 ms. If a 20MHz crystal is used, then cycle takes approx 1 ms.

Notes
PWM can be used to generate analogue voltages (0-5V) through a pin connected to a resistor and capacitor to ground; the resistor-capacitor junction is the analogue output (see circuit). Since the capacitor gradually discharges, PWM should be executed periodically to refresh the analogue voltage.

PWM emits a burst of 1s and 0s whose ratio is proportional to the duty value you specify. If duty is 0, then the pin is continuously low (0); if duty is 255, then the pin is continuously high. For values in between, the proportion is duty/255. For example, if duty is 100, the ratio of 1s to 0s is 100/255 = 0.392, approximately 39 percent.

When such a burst is used to charge a capacitor arranged, the voltage across the capacitor is equal to:-

(duty/ 255) * 5.

So if duty is 100, the capacitor voltage is

(100/255) * 5 = 1.96 volts.

This voltage will drop as the capacitor discharges through whatever load it is driving. The rate of discharge is proportional to the current drawn by the load; more current = faster discharge. You can reduce this effect in software by refreshing the capacitor's charge with frequent use of the PWM command. You can also buffer the output using an op-amp to greatly reduce the need for frequent PWM cycles.

See also : HPWM, PULSEOUT, SERVO.

Op 24 september 2005 19:21:19 schreef chevcamaro:
je hebt 2 keer s1 = 0 ,bij een s1 =0 gaat hij -1 en bij s1=0 gaat hij ook s1 +1

das 1 ding , maar dit is geen pwm regeling. het enige wat hier gebeurt is de frequentie aanpassen.
bij pwm zou je het volgende moeten doen : je frequentie blijft constant maar je duty cycle moet veranderen dus


periode =25
x=1
again:
hightime= periode-x  ' reken tijd uit dattie hoog is
lowtime= periode-hightime ' reken tijd uit dat ie laag is
if s1=0 then x=x-1 ' eentje eraf (duty cycle naar beneden)
if s2=0 then x=x+1 ' eentje erbij (duty cycle omhoog)
if x>periode then x= periode ' bescherming tegen overflow
if x<1 then x=1 ' bescherming tegen underflow

porta.1=1
delay hightime
porta.1=0
delay lowtime

goto again
end

tis zomaar uit de losse pols hoor. ik ken geen picbasic mer wel genoeg andere basic .

[Bericht gewijzigd door free_electron op ]

nu wil ik het pwm commando gebruiken ook:P

oke, klopt het als ik dit gebruik?

pin 9 = een pwm pin.
duty moet een percentage zijn dus ik neem 50 voor mooie vierkantjes. Ik weet wat duty in houd. Maar cycles snap ik niet helemaal. Ik weet wel dat dit een getal onder de 256 moet zijn maar wat dat voor de rest in houd weet ik niet. Wie weet dat wel?

DEVICE 16F628A
CONFIG INTRC_OSC_NOCLKOUT, WDT_OFF, PWRTE_ON, LVP_OFF, MCLRE_OFF
ALL_DIGITAL TRUE

WHILE 1 = 1
PWM 9,50,cycles
WEND

END

Op 24 september 2005 20:04:40 schreef Jan Bot:
nu wil ik het pwm commando gebruiken ook:P

oke, klopt het als ik dit gebruik?

pin 9 = een pwm pin.
duty moet een percentage zijn dus ik neem 50 voor mooie vierkantjes.

Als je eens wat beter gelezen had, zag je dat de duty-variabele van 0 tot 255 ging, en niet van 0 tot 100 zoals jij blijkbaar denkt. Dat wil dus zeggen: 0 = volledig uit, 255 = volledig aan. Mooie vierkantjes krijg je dus door de waarde 255/2 op te geven, of afgerond 128.

Ik weet wat duty in houd. Maar cycles snap ik niet helemaal. Ik weet wel dat dit een getal onder de 256 moet zijn maar wat dat voor de rest in houd weet ik niet. Wie weet dat wel?

Cycles is het aantal keer dat de periode (dus één pwm-puls) zich herhaalt. Dat het tussen 0 en 256 moet liggen, heeft puur te maken met het feit dat je met een byte (8 bits) werkt.

Picbasic is trouwens zo'n kinderlijk eenvoudige taal die bovendien nog eens goed gedocumenteerd is, als je dat dan nog niet snapt...

je hebt ook nog het commando hpwm mischien heb je hier iets aan.

HPWM

Syntax
HPWM Channel , Dutycycle , Frequency

Overview

Output a pulse width modulated pulse train using the CCP modules PWM hardware, available on some PICmicros. The PWM pulses produced can run continuously in the background while the program is executing other instructions.

Operators

Channel is a constant value that specifies specifies which hardware PWM channel to use. Some devices have 1, 2 or 3 PWM channels. On devices with 2 channels, the Frequency must be the same on both channels. It must be noted, that this is a limitation of the PICmicro not the compiler. The data sheet for the particular device used shows the fixed hardware pin for each Channel. For example, for a PIC16F877, Channel 1 is CCP1 which is pin PORTC.2. Channel 2 is CCP2 which is pin PORTC.1.

Dutycycle is a variable, constant (0-255), or expression that specifies the on/off (high/low) ratio of the signal. It ranges from 0 to 255, where 0 is off (low all the time) and 255 is on (high) all the time. A value of 127 gives a 50% duty cycle (square wave).

Frequency is a variable, constant (0-32767), or expression that specifies the desired frequency of the PWM signal. Not all frequencies are available at all oscillator settings. The highest frequency at any oscillator speed is 32767Hz. The lowest usable HPWM Frequency at each oscillator setting is shown in the table below: -

XTAL frequency Lowest useable PWM frequency
4MHz 145Hz
8MHz 489Hz
10MHz 611Hz
12MHz 733Hz
16MHz 977Hz
20MHz 1221Hz
24MHz 1465Hz
33MHz 2015Hz
40MHz 2442Hz

Example
DEVICE = 16F877
XTAL = 20
HPWM 1,127,1000 ' Send a 50% duty cycle PWM signal at 1KHz
DELAYMS 500
HPWM 1,64,2000 ' Send a 25% duty cycle PWM signal at 2KHz

STOP

BEDANKT!!!!
Waar haal je dat vandaan??:P

32767 Hz oftwel 32,767KHz is dat niet erg weinig? Voor mij in iedergeval genoeg. Maar toch...

@ Thomas, Dat klopt, je hebt helemaal gelijk. Maar als je je eraan ergert dan moet je maar niet antwoorden.

Wel bedankt voor je antwoord.

[Bericht gewijzigd door VWG3JB op ]

Ga eens naar www.picbasic.nl
Klik op Projecten.
Klik op "Handdoekrek met houten aanraaksensor".

Onderaan dat artikel is het PIC Basic programmaatje te downloaden dat bij de handdoekrek hoort.
Daar zit ook het PWM commando in verwerkt.
Misschien heb je er wat aan.

Het commando HPWM werkt alleen bij PIC's die hardwarematig PWM aan boord hebben.

In cursus deel 7 wordt PWM pas behandeld, kan nog enkele maanden duren.

Het commando hpwm werkt gewoon bij de pic16f628a hij heeft toch een ccp1???

Ik heb nergens geschreven dat een 16F628(A) dat niet zou hebben... toch...?

Nee dat klopt ;)wat ik bedoelde was meer een vraag.