Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

Wheel Encoder, PIC and interrupts

Status
Not open for further replies.

alirezan

Newbie level 4
Joined
Mar 11, 2007
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,333
encoder con pic

Hi all,

I'm trying to use wheel encoders on my robot. I am using PIC18F1320 and programming in C.

I used PIC to generate the PWM and set the PORTs to set the direction of movement. I attached the wheel encoder hardware (which works just fine) and let the PIC get an interrupt as the sensors see different color patch when the wheel rotates (used PORTB pin change interrupt).

I have a very simple program but a very strange problem. When I simulate in MPLAB SIM it works fine.

Here's the problem. In my encoder, white patch generates a 0 and black a 1. Whenever I start the robot, the wheel moves just a little bit and stops. I found out that as the encoder hits the black patch, it stops completely. When I turn the wheel manually to make the sensor see a white patch then the wheel starts to move and again, when it hits the black patch, it stops!

Does anybody know why this happens?

Here's my code:

Thanks

Code:
/********/

#pragma chip PIC18F1320        //CC8E compiler directive to use 18F1320.H
void Start_PWM (void);
void Stop_PWM (void);


/*********************************************************************************************/
// 1 MACRO DEFINITIONS AND CONSTANTS  /
//***********************************`

//Logical Definitions:
//====================

//PIC18F1320 Definitions:
//=======================
#define F_OSC 8000000        //Frequency (Hz) of the oscillator (Fosc) for currently used clock
//ROBOT_PIC Definitions:
//======================

int distance_to_move;
int rotation_constant; // should be 1.754
int time_to_turn;
unsigned int counter;



#pragma origin 0x8    //Specify where in program memory to place the following code
       //(0x8 corresponds to the High-Priority Interrupt Vector)
interrupt int_server(void)  //The interrupt service routine is a special function. See CC8E User Manual for details
{  
int_save_registers   //CC8E Macro to save W, STATUS, and BSR registers
 
if (RBIF)   //At least one of the RB4:RB7 is changed
{
 if (!PORTB.5)  // PB5 has received an interrupt
 {
  PORTB.2 = 1;
  counter++;
 }

 RBIF = 0;
 
}

if (TMR2IF)
{
 TMR2IF = 0;
}

int_restore_registers //CC8E Macro to restore W, STATUS, and BSR registers
}



/*********************************************************************************************/
// MAIN FUNCTION  /
//*****************`

void main(void)
{ //Oscillator Initialization:
//==========================
SWDTEN = 0;
OSCCON = 0b01110000;      // Configure for: !IDLEN, 8MHz INTOSC Frequency, PRI_RUN power mode
OSCTUNE = 0x10;        //This register used to compensate for oscilator frequency drift error

//Port A,B Initialization:
//========================
ADON = 0;
ADCON1 = 0b01111111;
TRISA = 0b10000010;
TRISB = 0b00100000;

PORTA = 0x00;         //Initialize Ports A,B to 0
PORTB = 0x00;
PORTA.6 = 0;
PORTB.6 = 0;
RBIE = 1;
RBIF = 0;

GIE = 1;
PEIE = 1;
Start_PWM();


while (1);
}

void Start_PWM (void)
{
CCP1CON = 0;
PR2 = 199;//249;  //199 for DC=25%, 249 for DC=75%
CCPR1L = 49;//186;  //49 for DC=25%, 186 for DC=75%
TRISB.3 = 0;
T2CON = 0x01;
CCP1CON = 0x3C;
TMR2 = 0;
TMR2ON = 1;
TMR2IE = 1;
}

void Stop_PWM (void)
{
TMR2ON = 0;
}

//END OF FILE
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top