How to write a code for PWM for microcontroller ?

greetings all

i’d like to create a rule for micro-controller pertaining to converting analog that will digital and make use of the out put to build PWM signal to regulate a motor

for the reason that analog input raise the speed belonging to the motor will need to increase by means of increasing the positive job cycle.
i completed the A/D converter but i dont have learned to generate PWM indicate
may anyone assistance me PLZZZZZZZZZZ

#include
#pragma config OSC = HS /* intended for 4 MHz amazingly */
#pragma config WDT = OFF
#pragma config LVP = OFF

void main (void)

TRISB=0x00; // produce portb output
TRISA=0xff;
TRISC=0x00;
PORTC=0x00;

ADCON0= 0x01; //POWER ON
ADCON1= 0x0E; // Vref- = vss, Vref+ = vdd, AN0 should be only analog
ADCON2= 0x04; // left justify plus 4*Tosc

// Initialization intended for PWM

although (1)

// commence of A/D converter *************************************
Delay1KTCYx(25); //delay with 25ms

ADCON0bits.GO=1;
although (ADCON0bits.GO! = 0);
PORTB=ADRESH;
Delay1KTCYx(25);
// Conclusion of A/D Converter*******************************
// Commence of PWM*************************************
PR2= 0x00; //for discovering the pwm period
CCPR1L=0xFA;
CCP1CONbits.DC1B0=1;
CCP1CONbits.DC1B1=1;
T2CON=0x05;
CCP1CON=0x9E;

i reached about here will it be write or even

THNX

I can’t chose the break or perhaps terminating affliction for though (1).
I here’s afraid you may be stuck with an unlimited loop.

Leave a Reply