PWM, Switching from 100% Duty Cycle, to 20% Duty Cycle

General discussion on mikroPascal PRO for PIC32.
Post Reply
Author
Message
ckelley
Posts: 9
Joined: 14 Feb 2019 22:28

PWM, Switching from 100% Duty Cycle, to 20% Duty Cycle

#1 Post by ckelley » 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.

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: PWM, Switching from 100% Duty Cycle, to 20% Duty Cycle

#2 Post by stefan.filipovic » 27 Feb 2019 12:56

Hi,

Welcome to the mikroE forum.

There are a few mistakes in your code. You did not set RB0 pin to INPUT, you forgot to put the begin keyword for the while loop. Also, I suggest you use div instead of / for dividing to get the word type number instead of the real type because PWM_Set_Duty expects the word type.
Please see the edited code below:

Code: Select all

program PWM_Spike_And_Hold;
{ Declarations }
var PWM_Period, Current_Duty_Cycle : word;
    case_index : short;

begin { Main program }
  JTAGEN_bit := 0;
// Configure the analog pins to Digital I/O
  ANSELB := 0x0000;
  ANSELD := 0x0000;

  TRISB := 0x0001;      // you should set RB0 to input
  TRISD := 0x0000;

// Initialize all PORTD values to 0
  PORTD := 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
    begin  // there should be begin of while loop
    if ((RB0_bit = 1) and (case_index = -1)) then
      begin
        case_index := 0;
      end;
       // begin of while was there
      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 div 2;  //use div instead of / to get word type number
            PWM_Set_Duty(Current_Duty_Cycle, 1);
            case_index := 2;
          end;

        2: case_index := -1;
      end;
    end;
end.
Kind regards,
Stefan Filipović

Post Reply

Return to “mikroPascal PRO for PIC32 General”