View previous topic :: View next topic |
Author |
Message |
ooines
Joined: 06 Feb 2009 Posts: 2
|
Counting the encoder signal pulse in PIC16F877A |
Posted: Tue Feb 10, 2009 8:43 am |
|
|
Hi,I am a beginner in CCS & PIC.
I am doing my final year project about the DC motor control using the PIC16F877A. I try to program it with the following source codes and run the experiments. But during the experiments,when the duty cycle is increased till 58 and so on...the count_pulse keep displaying 0 results.
I have made sure the motor & encoder are still running and being tested the output signals via oscilloscope to see the waveform within the frequency is increasing during increasing the duty cycle.
So, can anyone help me to find out is it any problems in my source codes?
Thx alot.
Code: |
#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#device adc=10
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, BRGH1OK)
#include <stdlib.h>
#include <input.c>
// define
//==========================================================================
#define CCW pin_B5
#define CW pin_B6
#define timevalue 3036
// function prototype
//==========================================================================
void delay(unsigned long data);
// global variable
unsigned char temp=50;
boolean flag;
unsigned long int count,count_next;
unsigned long int actual_speed;
char oper;
unsigned long int duty_set;
#int_TIMER1
TIMER1_isr() {
count_next=count; //transfer what ever for next calculation
count=0; // reset count for next sample
flag=true;
set_timer1(timevalue); // reset timer value
actual_speed=count_next*10;
}
#int_EXT //interrupt by the encoder
void ext_isr(void)
{
count++;
}
// main function
void main(void)
{
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(A_Analog); //Set analog input
set_TRIS_B(0b00000001); //Set pin RB5 & RB6 as input
set_TRIS_C(0b11000000); //Set pin RC2 as output for PWM
setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
//PWM frequecy set as 4.88KHz
setup_timer_2(T2_DIV_BY_4, 255, 1); //Timer2 On, prescale 4
duty_set=0;
set_pwm1_duty(duty_set);
port_b_pullups(TRUE);
ext_int_edge(0,H_TO_L);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
enable_interrupts(INT_EXT);
enable_interrupts(INT_TIMER1);
enable_interrupts(global);
flag=true;
count=0; // reset countings for next count
set_timer1(timevalue); // this ensure timer roll up when 0.1s
while(true)
{
if (flag==true){
printf("\r\n %ld ",duty_set);
printf("\rCount_pulse = %ld",actual_speed);
flag=true;
}
if(kbhit())
{ oper=getc();
if(oper=='a') // CCW
{
printf("\ndetect CCW");
output_high(CW);
output_low(CCW);
oper='x';
}
if(oper=='b') // CW
{
printf("\ndetect CW");
output_high(CCW);
output_low(CW);
oper='x';
}
if((oper=='c')&&(duty_set<1000)) // up
{
duty_set=duty_set+1;
set_pwm1_duty(duty_set);
oper='x';
}
if ((oper=='d')&&(duty_set!=0)) //down
{
duty_set=duty_set-1;
set_pwm1_duty(duty_set);
//delay_us(20);
oper='x';
}
if((oper=='s')&&(duty_set<1000)) // stop
{
printf("\ndetect stop");
duty_set=0;
set_pwm1_duty(duty_set);
//delay_us(20);
oper='x';
}
}
}
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Feb 10, 2009 3:01 pm |
|
|
Quote: | #int_EXT //interrupt by the encoder
void ext_isr(void)
{
count++;
} |
What is the frequency of the signal that is connected to the External
interrupt pin ? What are the peak-to-peak voltage levels of the signal ?
What is the duty cycle ? |
|
|
ooines
Joined: 06 Feb 2009 Posts: 2
|
|
Posted: Wed Feb 11, 2009 2:33 am |
|
|
the max output frequency is 100kHz, Peak voltage is 0-5 v max and the duty cycle is 50%.
again,i am doubting that isit the pic cant read the encoder output signals becoz of the speed of the motor is too high?
my motor spec is :
500 pulses per revolution
positioning resolutions up to 2000 counts per rev
the datasheet is as below
http://docs-asia.electrocomponents.com/webdocs/001a/0900766b8001acf7.pdf
|
|
|
Guest
|
|
Posted: Sat Feb 14, 2009 8:10 am |
|
|
thx anyway...PCM programmer!
i think the problem is from my encoder that can positioning resolutions up to 2000 counts per revolution only.
n the duty cycle of my frequency is changing manually for increasing n decreasing speed of my motor. |
|
|
|