This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Help with PWM

Hello everybody, I need help with my school project. My task is to create a program on C8051F330 microchip using uVision. The program should regulate RPM of a pc fan, using PWM (4 pin (wire) PC fan, I have to generate a PWM signal on the fourth wire to regulate it). Since I dont have any opportunity to try my program at home, I want to ask for your help, if it will work or where do I make mistakes. Ok so here is my program:

#include <c8051f330.h>
#include <stdio.h>

extern void Init_Device();

#define SYSCLK  24500000
#define CLKDIV  12

sbit button_increaseRPM=P1^1;
sbit button_decreaseRPM=P1^2;


float f, fp;
unsigned int reg, required_RPM, current_RPM, save_DutyCycle, count_impulses;

void main(void)
{
        Init_Device();
        TI0 = 1;
        save_DutyCycle = 0;
        required_RPM = 400;     ///minimal RPM
        printf("Increase/decrease RPM with buttons\n");

while(1)
{
if (button_increaseRPM == 0x02)
{
                  required_RPM++;
                }

                if (button_decreaseRPM == 0x04)
                {
                   required_RPM--;
                }

                f = save_DutyCycle;       //return duty cycle value to f, because of later    overwritng in program

                //compare the required RPM with current RPM
                if (required_RPM != current_RPM)
                {
                        if(current_RPM < required_RPM)
                         {
                                save_DutyCycle++;          //increase duty cycle of PWM
                                f = save_DutyCycle;
                                f /= 100;
                                f = (1 - f) * 256;
                                PCA0CPH0 = f;
                        }
                        else
                        {
                                save_DutyCycle--;          //decrease duty cycle of PWM
                                f = save_DutyCycle;
                                f /= 100;
                                f = (1 - f) * 256;
                                PCA0CPH0 = f;
                        }
                }
        }
}

void INT0 (void) interrupt 0  //counting impulses
{
        count_impulses++;
}

void TMR2INT (void) interrupt 20         // 20 ms  , check counted impulses
{
        TF2H = 0; // Clear interrupt flag for timer 2 reload 16 bit mode
        current_RPM = ((count_impulses/0.02)/2); //timer2 20ms, and 2 impulses from the fan per rotation
        count_impulses = 0;
        //
}


Every other settings are in the config created with Configuration Wizard 2 (Silicon laboratories).

Thank You for your help!

0