PWM, Switching from 100% Duty Cycle, to 20% Duty Cycle
Posted: 15 Feb 2019 15:02
I am trying to, upon pressing a button (RPB0) output a PWM to one of the LEDs on the fusion 7 board ( located at RPD01 ). I use pascal all the time, but I can't seem to get this working with the following code. Can anyone please help me out with what might be wrong. BTW, the LED at RPD01 lights up, but doesn't change in brightness like I would expect when the RPB0 button is pressed.
Code: Select all
program PWM_Spike_And_Hold;
{ Declarations }
var PWM_Period, PWM_Duty_Cycle, Current_Duty_Cycle : word;
case_index : integer;
begin { Main program }
// Configure the analog pins to Digital I/O
ANSELB := 0x0000;
ANSELD := 0x0000;
// set PORTA to be digital output
TRISB := 0x0000;
TRISD := 0x0000;
// Initialize all PORTB values to 0
PORTB := 0x0000;
// Map the timer1 output to PORTD bit 0
PPS_Mapping(_RPD1, _OUTPUT, _OC1);
// Create PWM period
PWM_Period := PWM_Init(1000,1,1,2);
// Start the pulse width modulation
PWM_Start(1);
// Set the PWM duty cycle
PWM_Set_Duty(PWM_Period,1);
// Initialize the case state variable to 0
case_index := 0;
// forever loop
while (True) do
if ((RB0_bit = 1) and (case_index = -1)) then
begin
case_index := 0;
end;
begin
case case_index of
0:begin
Delay_ms(1000);
Current_Duty_Cycle := PWM_Period;
PWM_Set_Duty(Current_Duty_Cycle, 1);
case_index := 1;
end;
1:begin
Delay_ms(1000);
Current_Duty_Cycle := PWM_Period/2;
PWM_Set_Duty(Current_Duty_Cycle, 1);
case_index := 2;
end;
2: case_index := -1;
end;
end;
end.