Hi i am newbie and i am just learning how to write programs in c for PIC 16f877a
I have written a code for rotating a 2 stepper motor forward and backward. (Sun-tracker but does not track the sun just a timer based on the sun movement.)
One stepper motor move the panel from morning 6 to evening 6 that is i want the motor to rotate 180 degrees in the entire 12 hour period and then quickly return to back its original position that is rotate reverse by 180 degrees quickly and wait till 6 next morning to start rotating again.
And the other will should move after a period of 4 days by 2 degrees and about half a month start moving backwards.
Also what is the minimum delay i can implement on a stepper motor??
And i want to implement a delay of about 12 hours what code should i use or can i use a code to turn it off for 12 hours??
Can i run the timer programs for both motors simultaneously cause one timer will be in terms of minutes and the other in terms of days..
Could you'll please help me.
Sorry for asking so many questions but i would really help me to understand better.
Any suggestions or help..
Thanks..
I uploaded the simple program i wrote to rotate a stepper motor but this is only for the one motor. I dunno how to interface two motors simultaneously and run separate timers for each of them..
And i use MPLAB for programming
I wouldn't bother with a second timer. Just move the second motor 1/2 degree every time you use the fast return cycle. Or, use a counter and move the second motor 2 degrees every fourth time you use the fast return cycle.
Thanks for your reply.
Sorry for the delay. Was having exams.
I tried writing the code you said but i have a problem.
Just see what is the problem.
I am using isis stimulation and have written the program where after a count of 2 it rotates the motor by 1 step.
but after it rotates by one step the loop should start again at the firtst motor by counting from 0 till 2 but it doesn't stop at 2 and goes into a continous loop.
Please help me
I have put a video file along with it
It never gets to two!!! after the second if statement you increment count... Then if count doesn't equal 2 it is then cleared before returning to the forever loop... It will NEVER reach 2..
C:
#include <htc.h>
#define _XTAL_FREQ 4000000
void main()
{
TRISB=0X00;
PORTB=0X00;
TRISD=0X00;
PORTD=0X00;
unsigned int i;
unsigned int j;
unsigned int k;
int count=0;
int count1=0;
for(;;)
{
for(i=0;i<1;i++)
{
PORTB=0X04;
__delay_ms(500);
PORTB=0X02;
__delay_ms(500);
PORTB=0X08;
__delay_ms(500);
PORTB=0X01;
__delay_ms(500);
}
for(j=0;j<1;j++)
{
PORTB=0X08;
__delay_ms(500);
PORTB=0X02;
__delay_ms(500);
PORTB=0X04;
__delay_ms(500);
PORTB=0X01;
__delay_ms(500);
count++;
}
if(count==2) // H[B]ere!! if the statement is false[/B]
{
switch(count1)
{
case 0:
PORTD=0X04;
count1++;
break;
case 1:
PORTD=0X02;
count1++;
break;
case 2:
PORTD=0X08;
count1++;
break;
case 3:
PORTD=0X01;
count1==0;
break;
}
}
count=0; // [B]You then clear count!!![/B]
}
while(1);
}
Then how am i supposed to get the 2nd motor to rotate and then reset the count to 0 for the first??
I understood the problem that i had to remove that count=0 cause it was setting it to 0 when the if condition was not satisfied but still cant get it to work
Thanks for the quick response..
count == 0; This is an equality operator!!! It's asking "Is count equal to 0"
You need..
count = 0;
Here is the modified code... I needed to place the count = 0 in before the switch statement!!
There was no need for the last while(1); statement as the code never gets to it
C:
#include <htc.h>
#define _XTAL_FREQ 4000000
void main()
{
TRISB=0X00;
PORTB=0X00;
TRISD=0X00;
PORTD=0X00;
unsigned int i;
unsigned int j;
unsigned int k;
int count=0;
int count1=0;
for(;;)
{
for(i=0;i<1;i++)
{
PORTB=0X04;
__delay_ms(500);
PORTB=0X02;
__delay_ms(500);
PORTB=0X08;
__delay_ms(500);
PORTB=0X01;
__delay_ms(500);
}
for(j=0;j<1;j++)
{
PORTB=0X08;
__delay_ms(500);
PORTB=0X02;
__delay_ms(500);
PORTB=0X04;
__delay_ms(500);
PORTB=0X01;
__delay_ms(500);
count++;
}
if(count==2){
count = 0;
switch(count1){
case 0:
PORTD=0X04;
count1++;
break;
case 1:
PORTD=0X02;
count1++;
break;
case 2:
PORTD=0X08;
count1++;
break;
case 3:
PORTD=0X01;
count1=0;
break;
}
}
}
}
I got it..
Now looking forward to finish programming..
Thanks Ian... U were of great help..
Glad i posted my query here..
If i have any problem will notify again..
Hi there again..
Stumbled upon a problem..
Hoping ull help me again..
The problem i have now is that after the 2nd motor moves 5 times in one direction i want it to move backward..Then forward again in the loop..
The problem is after 5 times it doesn't move the other direction..
Just see if you can see the problem..
Thanks again..
Awating for reply..
Hi there, i finished my main code..
Now i want to interface an lcd in the program..
Now i want to know the code how can i display a statement and a value eg. count from the above in one line..
Eg. Days passed: 5 where 5 is the count value and updates after each loop..
I interfaced the lcd but the above thing is only the problem.
I have used lcd LM044L..
Thanks!! and Merry Christmas!!
Hi again ..
I have modified the code and written this..
The problem i find here is that if i write it as int am1 i get output as 1.0 (I want the output to show 1.8)
if i write it as double am1 i get an error saying operator not applicable to this operands
If i write it as long am1 it does not rotate properly i.e it turns 90 degrees and then 180* in the opposite direction (Considering Step Angle is 90*)
And can you tell me what does Lcd_Chr(1,10,48+am1); the 48+ part mean??
By the way i have shifted the code to mikroC as the lcd code was easily avaliable.
Thanks again for your time.
Sorry for piling on the questions..
I tried adding a if statement with switch to rotate the secondary motor if a condition is not fullfilled..
Here after case 3 i want it to return to case 0 but it does not and goes out of the loop.
What is the problem you think??
thanks..
C:
int i;
int j;
int k;
int m;
int l;
unsigned char ch;
int count=0;
int count1=0;
int count2=18;
int count3=0;
int count4=0;
int count5=0;
int count6=0;
int count7=0;
int count8=13;
int am1;
char txt1[6];
sbit LCD_RS at RC2_bit;
sbit LCD_EN at RC3_bit;
sbit LCD_D7 at RC7_bit;
sbit LCD_D6 at RC6_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D4 at RC4_bit;
sbit LCD_RS_Direction at TRISC2_bit;
sbit LCD_EN_Direction at TRISC3_bit;
sbit LCD_D7_Direction at TRISC7_bit;
sbit LCD_D6_Direction at TRISC6_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D4_Direction at TRISC4_bit;
void main()
{
TRISB=0X00;
PORTB=0X00;
TRISD=0X00;
PORTD=0X00;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
delay_ms(500); //First delay when program is started
if (count2>13){
switch(count7){
case 0:
{
PORTD=0X04;
delay_ms(500);
count8++;
if(count8==count2){
count7++ ;
break;
}
}
case 1:
{
PORTD=0X02;
delay_ms(500);
count8++;
if(count8==count2){
count7++;
break;
}
}
case 2:
{
PORTD=0X08;
delay_ms(500);
count8++;
if(count8==count2){
count7++;
break;
}
}
case 3:{
PORTD=0X01;
delay_ms(500);
count8++;
if(count8==count2){
count7=0;
break;
}
}
count7=0;
}
}
Look into the itoa() function in the stdio library.... You convert the number by multiplying the float by 10 then converting to a string and adding the decimal point yourself..