oTaRu
Junior Member level 2
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*/
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*/