CSS:
hi
So I am working on a project that aims to control the speed of a single phase induction motor, the ones that you find in your home fans.
Entire idea is to create an low cost IR remote with buttons for ON-OFF, but let's not bring that up now.
Stage one of the project is getting a microcontroller to get your tasks (of increasing/decreasing as well as turning on/off) programmed.
Okay so my progress so far:
1) Used Proteus to build the circuit (attached below) for Simulation
2) Wrote the C program (also attached below)
3) Compiled it using Keil Uvision 5
The problem is that while the compiler gave no errors, I am unable to see the results as required.
I'm pretty sure the fault is with the code.
Kindly have a look and revert if you find anything.
Thanks in advance.
Best Regards,
Vedant.
Mod edit: add source code with code tags.
Edit1: As suggested by Mod, added source code with code tags
Code:
#include<reg51.h>
//project is to control speed of single phase AC motor operating at 230V, 50Hz
unsigned int speed; // for the 4 speeds : 0(switched off),1,2,3 and 4(max, no delay)
code unsigned char timer_h[3]={0xe8,0xf0,0xf8};
code unsigned char timer_l[3]={0x8f,0x5f,0x2f};
//delay value calculations
//Supply frequency= 50Hz, time period=20ms
// one step takes apprx. 1us to be executed. Therefore
// speed=3 2ms (10%delay) = 2000us = 2000 steps = load F82F in timer
// speed=2 4ms (20%delay) = 4000us = 4000 steps = load F05F in timer
// speed=1 6ms (30%delay) = 6000us = 6000 steps = load E88F in timer
void delay_ms (unsigned int);
sbit led1 = P3^7;
sbit led2 = P3^6;
sbit s1 = P1^0; //switch one for ON/OFF operation
sbit s2 = P1^1; //switch two for increasing speed
sbit s3 = P1^2; //switch three for decreasing speed
sbit output =P0^0;
bit flag=0;
void decrease(void) // subroutine for decreasing speed
{
if(speed>0)
{speed--;
}
//else, message = Can't reduce speed further
}
void increase(void) // subroutine for increasing speed
{
if (speed<2)
{speed++;
}
//else, message = Can't increase speed further
}
void int0(void) interrupt 0 // external interrupt 0 subroutine
{ // Called whenever zero crossing detected
if(flag==1) //indicates that Switch 1 has been pressed
{ // to turn on
if(speed==0) //Motor turned on but speed still to be kept 0
{ // Off the triac permanently
output=1;
}
else if(speed==4) //Motor turned on and speed is to be made max
{ // on the triac permanently
output=0;
}
else
{ //indicates either SW2 or SW3 has been pressed
output=1;
TL0 = timer_l[speed-1]; //setting new speed to variables
TH0 = timer_h[speed -1];
TR0 = 1; //starting timer again
}
}
else
output=1;
}
void timer0 (void) interrupt 1 //called whenver timer operation finishes or TF=1
{
TR0=0; //stop timer
output=0; //fire triac
}
void int1(void) interrupt 2 // external interrupt 1 subroutine
{ //happens when button is pressed
if(s1==0) // for first key
flag=~flag; // on/off operation
else if(s2==0) // for second key
{increase(); // increase phase angle decrease power
}
else if(s3==0) // for third key
{decrease(); // decrease phase angle increase power
}
delay_ms(1); // 1 ms delay
}
void delay_ms (unsigned int j)
{ int i;
for(i=j*250;i>0;i--); //approximate delay in milliseconds
} //considering 1 iteration in FOR loop takes 4 steps i.e.
//four microseconds
void main(void)
{
TMOD=0x01; // initialize timer0 as 16 bit timer
IT0=IT1=1; // making edge trigger
EX0=EX1=ET0=1; // enable interrupts of timer0 and external interrupt 0 & 1
EA=1;
TL0=TH0=TR0=TF0=0;
if(flag==0)
led2=1;
else
led2=0; //Glow LED if Motor is turned ON
}
Attachments
-
28.6 KB Views: 45
-
3.4 KB Views: 33
Last edited: