eng/ IBRAHIM
Joined: 14 Aug 2007 Posts: 31
|
tacho of motor |
Posted: Sun Oct 18, 2009 9:03 am |
|
|
Hi all
Anyone knows why this code not works.
I use 16f877 to control of motor speed using feedback of tachometer signal.
Code: | #use delay(clock=4000000,restart_wdt)
#ROM 0x3ff = {0x3478}
#define Triac PIN_b1 // Triac
#define ZCD PIN_b0 // Zero Cross
#ZERO_RAM
int j=1; int phaseangle=123; int tacho;
void init()
{
disable_interrupts(GLOBAL);
disable_interrupts(INT_EXT);
disable_interrupts(INT_TIMER0);
output_low(Triac); // triac low
delay_ms(10);
}
#INT_TIMER0
void tempo()
{
output_high(Triac); // triac high
}
#INT_EXT
void externa()
{
set_timer0(phaseangle);
output_low(Triac); // triac low
enable_interrupts(INT_TIMER0);
if (j==0)
{
j=1;
ext_int_edge(H_TO_L);
}
else if(j==1)
{
j=0;
ext_int_edge(L_TO_H);
}
}
long rise,fall,pulse_width;
#int_ccp2
void isr()
{
rise = CCP_1;
fall = CCP_2;
pulse_width = fall - rise; // CCP_1 is the time the pulse went high
}
void main()
{
//init();
enable_interrupts(INT_EXT);
ext_int_edge(L_TO_H);
setup_timer_0(RTCC_INTERNAL | RTCC_DIV_64);
enable_interrupts(GLOBAL);
while(1)
{
int32 freq;
setup_ccp1(CCP_CAPTURE_RE); // Configure CCP1 to capture rise
setup_ccp2(CCP_CAPTURE_FE); // Configure CCP2 to capture fall
setup_timer_1(T1_INTERNAL); // Start timer 1
enable_interrupts(INT_CCP2); // Setup interrupt on falling edge
enable_interrupts(GLOBAL);
while(TRUE) {
delay_ms(1000);
freq=(1000000/pulse_width);
output_d(freq);
}
if(freq<0x59)
{
phaseangle++;
delay_ms(10);
}
if(freq>0x59)
{
phaseangle--;
delay_ms(10);
}
enable_interrupts(INT_EXT);
}
} |
|
|