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.