pic16f887 + pwm + rpm

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
gionnut
Posts: 2
Joined: 07 Jun 2010 11:42

pic16f887 + pwm + rpm

#1 Post by gionnut » 07 Jun 2010 20:51

hy
i'm using a pic16f887 with 20mhz quartz
i'm using RC1 pin for pwm to control a pc fan
what i want is how to read the RPM from the pc fan
the pc fan has 4 wires: gnd, vcc, pwm and hall sensor
i read in datasheet of the pic that i can use RC1 or RC2 in capture mode
can someone help me with a program that reads the RPM on RC2 pin and sends it to the terminal

unsigned int duty = 0;
void main() {
PR2 = 255;
CCP2CON = 0x0f;
CCPR2L = (unsigned char) ((duty*PR2)/100);

T2CON = 0x07;
TMR2 = 0;
TRISC.F1=0;

while (1)
{
duty=90;
CCPR2L = (unsigned char) ((duty*PR2)/100);
}
}

this is the program that i used for pwm

User avatar
tihomir.losic
mikroElektronika team
Posts: 2138
Joined: 02 Dec 2009 14:16
Location: Serbia
Contact:

Re: pic16f887 + pwm + rpm

#2 Post by tihomir.losic » 08 Jun 2010 16:37

Hello,

please, follow this link:
http://www.mikroe.com/esupport/index.ph ... icleid=144

PWM Library

mikroC PRO for PIC provides library which simplifies using PWM HW Module.
Some MCUs have multiple CCP modules. In order to use the desired CCP library routine,
simply change the number 1 in the prototype with the appropriate module number, i.e. PWM2_Start();

Also, on this page, you have described all Library Routines and Example which comes with our PWM Library.

I hope that this will help you. For any further informations, please, create Support Ticket on:
http://www.mikroe.com/esupport
and contact us, in order to help you to finish your project.

Thanks.

Best regards,

Losic Tihomir
mikroElektronika [Support team]

gionnut
Posts: 2
Joined: 07 Jun 2010 11:42

Re: pic16f887 + pwm + rpm

#3 Post by gionnut » 08 Jun 2010 18:19

i know how to generate pwm, the program above created by me uses only one duty cycle
what i dont know is how to capture RPM from a pc fan

User avatar
tihomir.losic
mikroElektronika team
Posts: 2138
Joined: 02 Dec 2009 14:16
Location: Serbia
Contact:

Re: pic16f887 + pwm + rpm

#4 Post by tihomir.losic » 10 Jun 2010 14:10

Hello,

please, use following code in order to capture RPM form a PC fan.
This program counts pulses on T1CKI pin for 1 Second Sampling Period.
I used our EasyPIC6 and PIC16F887 with oscillator of 20MHz.

Code: Select all

  // LCD module connections
  sbit LCD_RS at RB4_bit;
  sbit LCD_EN at RB5_bit;
  sbit LCD_D4 at RB0_bit;
  sbit LCD_D5 at RB1_bit;
  sbit LCD_D6 at RB2_bit;
  sbit LCD_D7 at RB3_bit;

  sbit LCD_RS_Direction at TRISB4_bit;
  sbit LCD_EN_Direction at TRISB5_bit;
  sbit LCD_D4_Direction at TRISB0_bit;
  sbit LCD_D5_Direction at TRISB1_bit;
  sbit LCD_D6_Direction at TRISB2_bit;
  sbit LCD_D7_Direction at TRISB3_bit;
  // End LCD module connections

  const unsigned short FILLER = 256-132;    // = 132 * 128us = 16896us
  unsigned int FREQ;
  unsigned char TALLY;
  unsigned int TIMER1 absolute 0x0E;
  char HertzStr[12];

void interrupt(){
  TALLY--;
  if (TALLY == 0){
    TMR2 = TMR2 + FILLER;
    TALLY = 31;
  }
  if (TALLY == 30){
    TMR1ON_bit = 0;
    FREQ = TIMER1;
    TIMER1 = 0;
    TMR1ON_bit = 1;
    TMR2 = 0;
  }
  TMR2IF_bit = 0;
}

void SetupT1asCounter(){

  T1CON = 0b00000110;
  TIMER1 = 0x0000;
  TMR1ON_bit = 1;
  TMR1CS_bit = 1;
  TALLY = 30;
}

void SetupT2asTimer(){
  TMR2 = 0;
  T2CON =  0b01111110;
  PIR1 = 0x00;
  PIE1 = 0x02;
  INTCON = 0b11000000;
}

void General_Init(){

  TRISA = 0;
  PORTA = 0;
  TRISB = 0;
  PORTB = 0;
  TRISC = 1;
  PORTC = 0;
  TRISD = 0;
  PORTD = 0;

  ANSEL = 0;
  ANSELH = 0;
  
  C1ON_bit = 0;
  C2ON_bit = 0;

  CCP1CON = 0;
  FREQ = 0;

  Lcd_Init();
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Cmd(_LCD_CURSOR_OFF);

  SetupT1asCounter();
  SetupT2asTimer();

}
void main(){

  General_Init();

  while(1){

    Delay_ms(50);
    WordToStr(FREQ, HertzStr);

    Lcd_Out(1, 1, "HERTZ");
    Lcd_Out(2, 1, HertzStr);
  }
}
Best regards,

Losic Tihomir
mikroElektronika [Support team]

emanuel
Posts: 15
Joined: 09 Jun 2010 21:43

Re: pic16f887 + pwm + rpm

#5 Post by emanuel » 18 Jun 2010 14:13

Dear Losic
Could you please explain a litle the program because I need red rpm with an 18F4520?
and my proyect has an encoder with 15 captures so my idea is when I have 1 Hz in the machine in the output sensor will read 15 Hz after that I do 2 shift and I take the rpm
Best regards

User avatar
tihomir.losic
mikroElektronika team
Posts: 2138
Joined: 02 Dec 2009 14:16
Location: Serbia
Contact:

Re: pic16f887 + pwm + rpm

#6 Post by tihomir.losic » 07 Jul 2010 12:58

emanuel wrote:Dear Losic
Could you please explain a litle the program because I need red rpm with an 18F4520?
and my proyect has an encoder with 15 captures so my idea is when I have 1 Hz in the machine in the output sensor will read 15 Hz after that I do 2 shift and I take the rpm
Best regards
Hello,

unfortunately, we do not have enough resources to help our users by writing them custom projects. We are planning to expand our support by having a mikroE Workshop,
as a kind of project section, that will develop all kinds of useful projects, along with source codes and documentations,
but it will take some time until we can go into realization of this idea. In the meantime, we will do our best to expand our collection of examples,
so you will have a better staring reference for coding.
Anyway, thank you for your suggestions. Use forum (my code from previous reply) as a great staring reference where you can pick up lots of useful ideas for it's realization.
If you are stuck up with something, feel free to contact us again, and we will do our best to help you with your code, or to give you proper guidance for development.

Sorry for the inconvenience.

Best regards,

Losic Tihomir
mikroElektronika [Support team]

Post Reply

Return to “mikroC PRO for PIC General”