0% found this document useful (0 votes)
17 views1 page

Unsigned Short Unsigned Int Void

This code initializes two pulse width modulation (PWM) modules at 35 kHz and uses an analog-to-digital converter (ADC) input to adjust the duty cycles of the PWM outputs over time. It initializes the input/output pins on multiple ports, starts both PWM modules, and enters a loop to continuously read the ADC value and update the duty cycles of PWM1 and PWM2 based on the ADC reading.

Uploaded by

Maruthi Jacs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views1 page

Unsigned Short Unsigned Int Void

This code initializes two pulse width modulation (PWM) modules at 35 kHz and uses an analog-to-digital converter (ADC) input to adjust the duty cycles of the PWM outputs over time. It initializes the input/output pins on multiple ports, starts both PWM modules, and enters a loop to continuously read the ADC value and update the duty cycles of PWM1 and PWM2 based on the ADC reading.

Uploaded by

Maruthi Jacs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

unsigned short current_duty1,current_duty2;

unsigned int out;


void InitMain()
{
TRISA = 0xff;
// configure PORTA pins as input
PORTB = 0;
// set PORTB to 0
TRISB = 0;
// designate PORTB pins as output
PORTC = 0;
// set PORTC to 0
TRISD = 0xff;
TRISC = 0;
// designate PORTC pins as output
PWM1_Init(35000);
// Initialize PWM1 module at 35KHz
PWM2_Init(35000);
// Initialize PWM1 module at 35KHz
}
void main()
{
InitMain();
current_duty2 = 128;
// initial value for current_duty
current_duty1 = 100;
// initial value for current_duty
PWM1_Start();
// start PWM1
PWM1_Set_Duty(current_duty1);
// Set current duty for PWM1
//current_duty = 5;
// initial value for current_duty
PWM2_Start();
// start PWM1
PWM2_Set_Duty(current_duty2);
// Set current duty for PWM1
while (1)
{
out=ADC_Read(0);out=out/20;
PWM1_Set_Duty(current_duty1);
// Set current duty for PWM1
PWM2_Set_Duty(current_duty2);
// Set current duty for PWM1
if(out>=19)
{
/*current_duty1++;
// increment current_duty
if(current_duty1>=210)
{current_duty1=210;}*/
current_duty2++;
if(current_duty2>=210)
{current_duty2=210;}
//PWM1_Set_Duty(current_duty1);
PWM2_Set_Duty(current_duty2);
// Set current duty for PWM1
Delay_ms(100);
out=ADC_Read(0);out=out/20;
}
if(out<=22)
{
// button on RA1 pressed
/*current_duty2++;
// increment current_duty
if(current_duty2>=210)
{current_duty2=210;}*/
current_duty2--;
if(current_duty2<=30)
{current_duty2=30;}
PWM2_Set_Duty(current_duty2);
//PWM1_Set_Duty(current_duty1);
// Set current duty for PWM1
Delay_ms(100);
out=ADC_Read(0);out=out/20;}
}
}

You might also like