PWM for PIC16F1615 not working

General discussion on mikroC.
Post Reply
Author
Message
saharul
Posts: 489
Joined: 08 Jul 2010 08:11

PWM for PIC16F1615 not working

#1 Post by saharul » 28 Mar 2017 07:33

Hi

I tried below code. the push button code works fine, the ADC also works fine, but the PWM not working at all.
can someone share knowledge what went wrong with the code.

Code: Select all

#include <built_in.h>
bit oldstate;
void main() {

OSCCON =0xFB;
  TRISA = 0x0E;           // set direction to be output
  ANSELA=0x04;
  TRISC = 0xF0;           // set direction to be output
  PWM1_Remappable_Init(1000);
  PWM1_Remappable_Set_Duty(200);  PWM1_Remappable_Start();
  ADC_Init();

  LATC = 0x00;
  do {   //if (Button(&PORTA, 1, 1, 1)) {               // Detect logical one
      //oldstate = 1;                              // Update flag
      if (ADC_Read(2) >512){
    LATA = 0x00;       // Turn OFF LEDs on PORTA
    //       // Turn OFF LEDs on PORTC
    Delay_ms(1000);    // 1 second delay
    LATA = 0x1;       // Turn ON LEDs on PORTA
    //LATC = 0xFF;       // Turn ON LEDs on PORTC
    Delay_ms(1000);    // 1 second delay
 } } while(1);          // Endless loop}
}

User avatar
darko.ilijevski
Posts: 581
Joined: 21 Mar 2017 16:57

Re: PWM for PIC16F1615 not working

#2 Post by darko.ilijevski » 28 Mar 2017 16:54

Hello,

I see you were trying to use remappable version for the PWM output, but you haven't declared the output pin.
Did you try simple PWM1_Init(), with the PPS library included in the project (as requested by the compiler) ?
It should be using the default PWM1 pin, like on the datasheet.

Regards
BR,
Darko

saharul
Posts: 489
Joined: 08 Jul 2010 08:11

Re: PWM for PIC16F1615 not working

#3 Post by saharul » 28 Mar 2017 22:58

Thanks Darko,

i tried with your suggestion as below code..but no success.

Code: Select all

#include <built_in.h>
bit oldstate;
void main() {

OSCCON =0xFB;
  TRISA = 0x0E;           // set direction to be output
  ANSELA=0x04;

  TRISC = 0x00;           // set direction to be output  
  LATC = 0x00;
  ANSELC=0x00;//
  

  PWM1_Init(1000);
  PWM1_Set_Duty(20);
  Delay_ms(100);
  PWM1_Start();
  PWM2_Init(1000);
  PWM2_Set_Duty(10);
  PWM2_Start();
  ADC_Init();

  //
  do {   PWM2_Set_Duty(10);
          PWM1_Set_Duty(10);
    //if (Button(&PORTA, 1, 1, 1)) {               // Detect logical one
      //oldstate = 1;                              // Update flag
      if (Button(&PORTA, 1, 1, 1)||ADC_Read(2) >512){
    LATA = 0x00;       // Turn OFF LEDs on PORTA
    //       // Turn OFF LEDs on PORTC
    Delay_ms(1000);    // 1 second delay
    LATA = 0x1;       // Turn ON LEDs on PORTA
    //LATC = 0xFF;       // Turn ON LEDs on PORTC
    Delay_ms(1000);    // 1 second delay
 } } while(1);          // Endless loop}
}

User avatar
darko.ilijevski
Posts: 581
Joined: 21 Mar 2017 16:57

Re: PWM for PIC16F1615 not working

#4 Post by darko.ilijevski » 30 Mar 2017 17:08

Hi,

We are experiencing some difficulties with the library routines for the PWM and the PPS for that particular MCU.
I will test it some more during the next few days and hopefully we will get it working. Sorry for the inconvenience...

As a workaround, you could try manually setting the necessary bits, according to the datasheet.

Best regards.
BR,
Darko

saharul
Posts: 489
Joined: 08 Jul 2010 08:11

Re: PWM for PIC16F1615 not working

#5 Post by saharul » 31 Mar 2017 00:06

Thank you darko,

Really Hope for the success..

User avatar
darko.ilijevski
Posts: 581
Joined: 21 Mar 2017 16:57

Re: PWM for PIC16F1615 not working

#6 Post by darko.ilijevski » 04 Apr 2017 10:43

Hello again,

I have made a patch for the library for PIC16F1614 and 15. Unzip that in your mikroC PRO for PIC installation folder, and keep the folder structure as is in the .zip archive.
It's just a temporal fix, till the official update is made. Sorry for the inconvenience and thanks for your patience.

I hope this helps, best regards.
Attachments
PIC16F1614_15_temp_fix.zip
(18.28 KiB) Downloaded 290 times
BR,
Darko

saharul
Posts: 489
Joined: 08 Jul 2010 08:11

Re: PWM for PIC16F1615 not working

#7 Post by saharul » 05 Apr 2017 11:43

Thank you very much darko,

But i just don't get what you meant by
and keep the folder structure as is in the .zip archive
..

Sorry.

User avatar
darko.ilijevski
Posts: 581
Joined: 21 Mar 2017 16:57

Re: PWM for PIC16F1615 not working

#8 Post by darko.ilijevski » 05 Apr 2017 14:35

saharul wrote:Thank you very much darko,

But i just don't get what you meant by
and keep the folder structure as is in the .zip archive
..

Sorry.
I have packaged a few files inside the folders in which they need to be unpacked:

PIC16F1614_15_temp_fix - there are folders called DEFS and USES, inside this .ZIP file. So you need to copy all the files in the DEFS folder from the .ZIP archive, to the installation folder of the mikroC for PIC / DEFS folder. Same thing with the USES folder.

There are 4 files needed so I've packed them in such a way that you could see which one goes in which directory inside the installation folder...

I hope it's more clear now...

Regards
BR,
Darko

saharul
Posts: 489
Joined: 08 Jul 2010 08:11

Re: PWM for PIC16F1615 not working

#9 Post by saharul » 06 Apr 2017 08:34

Thanks Darko,

I did what you told me and i tried below code.
i realized only PWM1 is working. the PWM1 is not at PORT C3 or C5 as i expected, but at Port C1.

Why?

Many Thanks... :D :D

Code: Select all

#include <built_in.h>
bit oldstate;
void main() {

OSCCON =0xF0;
      //Unlock_IOLOCK();

  TRISA = 0x0E;           // set direction to be output
  ANSELA=0x04;

  TRISC = 0x00;           // set direction to be output  
  LATC = 0x00;
  /*ANSELC=0x00;//
  CCP1CON=0x1F;
  CCP2CON=0x1F;
  CCP1PPS= 0x15;
  CCP2PPS=0x13;

  RC3PPS=0x0C;
  RC5PPS=0x0D; */



  PWM1_Init(1000);
  PWM1_Set_Duty(200);
  Delay_ms(100);
  PWM1_Start();
  PWM2_Init(1000);
  PWM2_Set_Duty(100);
  Delay_ms(100);
  PWM2_Start();
  ADC_Init();
  //Lock_IOLOCK();
  //
  do {   PWM2_Set_Duty(200);
          PWM1_Set_Duty(255);
    //if (Button(&PORTA, 1, 1, 1)) {               // Detect logical one
      //oldstate = 1;                              // Update flag
      if (Button(&PORTA, 1, 1, 1)||ADC_Read(2) >512){
    LATA = 0x00;       // Turn OFF LEDs on PORTA
    //       // Turn OFF LEDs on PORTC
    Delay_ms(1000);    // 1 second delay
    LATA = 0x1;       // Turn ON LEDs on PORTA
    //LATC = 0xFF;       // Turn ON LEDs on PORTC
    Delay_ms(1000);    // 1 second delay
 } } while(1);          // Endless loop}
}

User avatar
darko.ilijevski
Posts: 581
Joined: 21 Mar 2017 16:57

Re: PWM for PIC16F1615 not working

#10 Post by darko.ilijevski » 06 Apr 2017 12:27

Hi saharul

1.) The functions PWM1_Init() and PWM2_Init() actually do internal PPS (peripherial pin selection), and those PWM output pins are chosen arbitrary.
Although it seems that CCP1 out and CCP2 outs are mapped to the RC3 and RC5 by default - by looking at the datasheet, it's actually not the case:
CCP1 IN and CCP2 IN are mapped to RC3 and RC5 pins. Because CCP modules have inputs too - e.g. when they work in capture mode.

By default - CCP1 out and CCP2 out are not mapped anywhere. You need to map them somewhere - that's why RC1 and RC2 are the outputs.
If you want to change that, you need to use the remappable version of that function, which lets you specify where you want the PWM output to be.

So you if you want PWM1 out to be on the RC5 pin :

Code: Select all

PPS_Mapping(_RC5, _OUTPUT, CCP1) ;  // you don't have to lock/unlock, this function does all that internally.
PPS1_Remappable_Init(1000);  // you have to map it to some pin, with above function.          
PPS1_Remappable_Set_Duty(80);  // proceed as usual...
PPS1_Remappable_Start();  //
2.) In your open 'Edit Project', you need to clear so called 'one way lock' bit, as in the picture. I've explained already that PWM1_Init actually does internal PPS, so
with that bit, you'll be able to set only one PWM output - but not the other, when you use non-remappable PPS_Init functions.

3.) This is just a temporal fix for you to be able to use PWM until we release the official library - proceed with that in mind.

I hope it is now more clear.
Best regards.
Attachments
Capture.JPG
Capture.JPG (87.24 KiB) Viewed 8378 times
BR,
Darko

saharul
Posts: 489
Joined: 08 Jul 2010 08:11

Re: PWM for PIC16F1615 not working

#11 Post by saharul » 07 Apr 2017 02:02

Thank you very much and very good explanation Darko.. :D :D

I tried the code and work very well...

Many Thanks :D :D :D

User avatar
darko.ilijevski
Posts: 581
Joined: 21 Mar 2017 16:57

Re: PWM for PIC16F1615 not working

#12 Post by darko.ilijevski » 07 Apr 2017 14:13

I am glad I could help... and thanks for the notice about this bug.

Best regards
BR,
Darko

Post Reply

Return to “mikroC General”