CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Incremental rotary encoder interface

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
epicnote



Joined: 11 Oct 2009
Posts: 3

View user's profile Send private message

Incremental rotary encoder interface
PostPosted: Sun Oct 11, 2009 11:49 pm     Reply with quote

Hello guys, I am a newbie here. By the way, I have finished programming the encoder, relays and keypad. I attached the encoder Channel A to RB0 and Channel B to RB1 of pic16f877a. Then I only used key number 1, 2 and 3 of keypad module where the 3 columns connected to RB2, RB3 and RB4 while the row1 connected to RB5. For the relay, a SPST relay connected to RD7 and a DPDT relay connected to RD6. So this is my C program using CCS C compiler for the system. Hope you guys can help me in guiding the program to complete.
Code:

#include <16F877A.H>
#fuses HS, PUT, NOWDT, NOPROTECT, NOBROWNOUT
#use delay (clock=20000000) //MCU clock 20MHz
#use rs232 (UART1, baud=9600)

void main()
{
signed int encoder; //Encoder counter
int current_pos; //current_pos indicates the position
port_B_pullups(TRUE); //Port B pull up
set_tris_D(0x00); //Port D set as output
set_tris_B(0xC7); //Port B pin RB2 to RB4 as output and others as input

while(TRUE)
{
output_low(PIN_B2); //Clear RB2
if(!input(PIN_B5)) //If keypad 1 is pressed
{
output_high(PIN_B2);
delay_ms(25);
if(current_pos==0) //Check initial position
{
output_low(PIN_D7); //Motor stops
output_low(PIN_D6);
}
else if(current_pos==5) //Check initial position
{
output_high(PIN_D7); //Motor moves down(CCW)
output_low(PIN_D7);
if(!input(PIN_B0)&&!input(PIN_B1)) //Check if B0 and B1 is equal to 0
{
encoder--;
if(encoder==0)
{
output_low(PIN_D7); //Motor stops
output_low(PIN_D6);
current_pos=encoder; //Store last position
}
}
}
else if(current_pos==10) //Check initial position
{
output_high(PIN_D7); //Motor moves down(CCW)
output_low(PIN_D6);
if(!input(PIN_B0)&&!input(PIN_B1)) //Check if B0 and B1 equal to 0
{
encoder--;
if(encoder==0)
{
output_low(PIN_D7); //Motor stops
output_low(PIN_D6);
current_pos=encoder; //Store last position
}
}
}
}
output_low(PIN_B3); //Clear RB3
if(!input(PIN_B5)) //If keypad 2 is pressed
{
output_high(PIN_B3);
delay_ms(25);
if(current_pos==0) //Check initial position
{
output_high(PIN_D7); //Motor moves up(CW)
output_high(PIN_D6);
if(!input(pin_B0)&&!input(PIN_B1)) //Check if B0 and B1 equal to 0
{
encoder++;
if(encoder==5)
{
output_low(PIN_D7); //Motor stops
output_low(PIN_D6);
current_pos=encoder; //Store last position
}
}
}
if(current_pos==5) //Check initial position
{
output_low(PIN_D7); //Motor stops
output_low(PIN_D6);
}
if(current_pos==10) //Check initial position
{
output_high(PIN_D7); //Motor moves down(CCW)
output_low(PIN_D6);
if(!input(PIN_B0)&&!input(PIN_B1)) //Check if B0 and B1 equal to 0
{
encoder--;
if(encoder==5)
{
output_low(PIN_D7); //Motor stops
output_low(PIN_D6);
current_pos=encoder; //Store last position
}
}
}
}
output_low(PIN_B4); //Clear RB4
if(!input(PIN_B5)) //If keypad 3 is pressed
{
output_high(PIN_B4);
delay_ms(25);
if(current_pos==0) //Check initial position
{
output_high(PIN_D7); //Motor moves up(CW)
output_high(PIN_D6);
if(!input(PIN_B0)&&!input(PIN_B1)) //Check if B0 and B1 equal to 0
{
encoder++;
if(encoder==10)
{
output_low(PIN_D7); //Motor stops
output_low(PIN_D6);
current_pos=encoder; //Store last position
}
}
}
if(current_pos==5) //Chech last position
{
output_high(PIN_D7); //Motor moves up(CW)
output_high(PIN_D6);
if(!input(PIN_B0)&&!input(PIN_B1)) //Check if B0 and B1 equal to 0
{
encoder++;
if(encoder==10)
{
output_low(PIN_D7); //Motor stops
output_low(PIN_D6);
current_pos=encoder; //Store last position
}
}
}
if(current_pos==10) //Check initial position
{
output_low(PIN_D7); //Motor stops
output_low(PIN_D6);
}
}
}
}

Hence, my question is:

1) Is that relevant to create a program with so many IF statement?
2) Then is the code above can complete my task?
Steve H
Guest







PostPosted: Mon Oct 12, 2009 10:31 am     Reply with quote

You did not say if the code works or not - I will assume that it did. Your program is not so big that the multiple if statements are a real problem with readability.

You are building a state machine - you can search Google for "State machine C", here is a reasonable link I found. State machines are a simple way to code this kind of problem.

http://tactilicio.us/2008/02/12/some-code-snippets-for-a-simple-c-state-machine/

HTH - Steve H.
epicnote



Joined: 11 Oct 2009
Posts: 3

View user's profile Send private message

PostPosted: Sat Oct 17, 2009 10:41 am     Reply with quote

Guys, I have created a new program that using external source for timer0, become counter of channel B of the encoder. The program runs when I push key 1 of the keypad. Then the motor will move clockwise for 2 revolutions and stop. Once I run the program, the motor stops when finished 2 revolution. Then when I want to start again with pushing the key 1, the relays contact without stopping. So why does it happen?? Anybody can help me?? Here is my coding:
Code:

#include <16F877A.H>
#fuses HS, PUT, NOWDT, NOPROTECT, NOBROWNOUT
#use delay (clock=20000000) // MCU clock 20MHz
#use rs232 (UART1, baud=9600)

void setup_encoder();  //Read encoder
void scankeypad();   //Read keypad

unsigned int8 time;

void main()
{
 set_tris_D(0x00);     //port D set as output
 set_tris_B(0xFF);     //port B input
 set_tris_A(0b11111111);  //RA4 as input
  while(TRUE)
  {
    setup_encoder();
    scankeypad();
  }
}
void setup_encoder()
{
 setup_counters(RTCC_EXT_H_TO_L,RTCC_DIV_1);
 set_rtcc(0);
}
void scankeypad()
{
 while(TRUE)
 {
  output_low(pin_D5);
  if(!input(pin_B2)) //If keypad1 is pressed
   {
    output_high(pin_D7);    //Motor moves clockwise
    output_high(pin_D6);
    delay_ms(50);
    time=get_rtcc();
   }
  if(time>=96)
   {
    output_low(pin_D7);  //Motor stops
    output_low(pin_D6);
    set_rtcc(0);
   }
 }
}

ferrousduke



Joined: 31 Mar 2008
Posts: 29
Location: Mumbai, India

View user's profile Send private message Visit poster's website

PostPosted: Sat Oct 17, 2009 11:51 pm     Reply with quote

Do you have any schematics illusrating connections that you've made ? Explain abt the task too.
epicnote



Joined: 11 Oct 2009
Posts: 3

View user's profile Send private message

PostPosted: Sun Oct 18, 2009 1:55 am     Reply with quote

Sorry for unclear details. So this is my circuit from isis. Thanks for the respond Wink

Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group