Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics 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.

PIC16F877 to control L293D for the motor- time control issue

Status
Not open for further replies.

oTaRu

Junior Member level 2
Joined
May 21, 2009
Messages
21
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,473
Time control issue...

Hi, I am currently using PIC16F877 to control L293D for the motor. The issue is I was unable to set the time control to stop the motor when the time is up... No matter how big the value in my C programming. It will still stop less than 1sec time. Please advice. Thanks!

Below is my coding:

#ifndef __CPU_16F877__
#error "This program is tailored for PIC16F877 controller"
#endif

#include "io16f877.h" //the hardware register definition file.

int timer_value=0xEA60; //decimal 60000.

void DelayUs(int count)
{
int i;
int j;
for(i=0;i<count;i++)
{
for(j=0;j<500000000;j++);
//This for loop has 5 NOPs & wastes 1 uS for our PIC clock frequency of 20MHz.
}
}

void initialize_IO_ports(void)
{
//set the digital IO ports as per requirement.
TRISB = 0x01 ; //portA as input.
TRISD = 0x00 ; //portD as output.

//clear the output ports at the beginning.
PORTD = 0x00 ; //clear portD.
}


void initialize_timer1(void)
{
TMR1CS=0;
// set prescalar value of 1:8 i.e. timer1 count=8x200ns=1600nS.
T1CKPS1=1;
T1CKPS0=1;
//Refer to the datasheet for the organization of interrupts.
GIE=1; //global interrupt enabled.
PEIE=1; //peripheral interrupt enabled.
TMR1IE=1; //enable timer1 interrupt.
}


// This function loads timer_value in timer1, & enables it.
void load_timer1(int timer_value)
{
TMR1ON=0; //disable timer1 before loading the values.
TMR1IF=0; //timer1 flag cleared.
TMR1H=(0xFFFF-timer_value)>>8; //load timer1 high register.
TMR1L=0xFFFF-timer_value; //load timer1 low register.
TMR1ON=1; //enable timer1.
}

int main()
{
int motor_switch=0;
int i;
RB0 = motor_switch;
initialize_IO_ports();
initialize_timer1();
load_timer1(timer_value);

if (RB0==1)
{
RD0=1;
for(i=10;i>0;i--)
{
DelayUs(10000);
}
}
else
RD0=0;
}

/*end of program*/
 

Re: Time control issue...

One thing I have noticed is that j, a 16-bit integer can only have a maximum value of 65535.


Code:
void DelayUs(int count) 
  { 
  int i; 
  int j; 
  for(i = 0; i < count; i++) 
    { 
    for(j = 0; j < 500000000; j++); 
    //This for loop has 5 NOPs & wastes 1 uS for our PIC clock frequency of 20MHz. 
    } 
  }
 

Time control issue...

oh... so i should edit it to 65535 for the maximum?


void DelayUs(int count)
{
int i;
int j;
for(i = 0; i < count; i++)
{
for(j = 0; j < 65535; j++);
//This for loop has 5 NOPs & wastes 1 uS for our PIC clock frequency of 20MHz.
}
}
 

Re: Time control issue...

Give it a go.
Let me know what happens.
Are you using mplab sim? It is very good, if it works in the sim, it will work in the hardware.
 

Time control issue...

Thanks! I had tried with it. But nothing has changed. I did not use mplab sim to try as my lab do not have this software.

I have noticed that my output (portD) does not have any voltage flow between the PIC controller and the IC chip. I have been trying to solve this problem but no good result appear.
 

Re: Time control issue...

We cannot use "for loops" if exact time delay is required moreover in embedded designs, as we don't know the exact time a loop takes to execute.

Instead use timers to give exact delay you are wishing for.

Small doubt...you initialised timer, but not using for any specific purpose I feel.
Use this timer inside the if condition to give delay instead of the forloop.

Hope this helps you...
 

Re: Time control issue...

you are using int j;
and a value of 65535 , its wrong by default if you are not specifying the compiler takes it as signed so the limit is -32768 to +32767

so either reduce the value to 32767 or use a unsigned int j;
 

what software u r using for compiling?
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top