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 CCS Technical Support

Problems reading PORTB inputs

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



Joined: 04 Nov 2004
Posts: 33

View user's profile Send private message Send e-mail

Problems reading PORTB inputs
PostPosted: Wed Nov 10, 2004 11:42 am     Reply with quote

Connected to RB6 and RB7 are Normally Closed (NC) switches. So normally, RB6 and RB7 should be reading GND, and when the switches are set both should be reading +5V. However, when we read the PORTB register, setting the switch on RB7 actually sets bit 6 of register PORTB instead of bit 7, and setting the switch on RB6 does nothing. What is going on? Here is our complete program. It is actually five separate files but I've put them into one file so I could post it... I have comments where each file ends and begins.

We are using PIC16F627A
Our compiler is the PCW Compiler, IDE version 3.30, PCB version 3.155, PCM version 3.155, and PCH version 3.155

Code:

/*************************************************/
//  THIS IS A SEPARATE FILE - AUTOSENSOR.H
#include <16F627A.h>
#use delay(clock=4000000)
#fuses INTRC_IO,NOWDT

/************    END AUTOSENSOR.H   **************/





/**************************************************/
//  THIS IS A SEPARATE FILE - PROJECT_DEFS.H
// Buttons --- BT = Button
#define BT_RET_Pushed         !input(PIN_A0)
#define BT_RET_NOT_Pushed     input(PIN_A0)

#define BT_EL_Pushed          !input(PIN_A4)
#define BT_EL_NOT_Pushed      input(PIN_A4)

#define BT_TURN_Pushed        !input(PIN_B1)
#define BT_TURN_NOT_Pushed    input(PIN_B1)

#define BT_POWER_Pushed        !input(PIN_B0)
#define BT_POWER_NOT_Pushed    input(PIN_B0)


//LEDs
#define Motion_LED_On         output_high(PIN_A7)
#define Motion_LED_Off        output_low(PIN_A7)

#define Raised_LED_On         output_high(PIN_A3)
#define Raised_LED_Off        output_low(PIN_A3)
                             
#define Travel_LED_On         output_high(PIN_B2)
#define Travel_LED_Off        output_low(PIN_B2)


//Limit Switches --- LS = Limit Switch
#define Down_LS_SET           input(PIN_B3)
//#define Down_LS_NOTSET        !input(PIN_B3)

#define END_LS_SET            input(PIN_B6)
//#define END_LS_NOTSET         !input(PIN_B6)

#define START_LS_SET         input(PIN_B7)
//#define START_LS_NOTSET      !input(PIN_B7)


//Motor Controls
#define Enable_On             output_high(PIN_A6)
#define Enable_Off            output_low(PIN_A6)

#define Phase_On              output_high (PIN_B5)
#define Phase_Off             output_low (PIN_B5)


//Puts entire system to sleep
#define Sleep_Off             output_high (PIN_B4)
#define Sleep_On              output_low (PIN_B4)

/************    END PROJECT_DEFS.H   **************/





/****************************************************/
// THIS IS A SEPARATE FILE - DEBOUNCE.C

void debounce(void)
{
         delay_ms(250);
         while(BT_POWER_Pushed || BT_EL_Pushed)
            delay_ms(20);
}

/************    END DEBOUNCE.C   **************/




/****************************************************/
// THIS IS A SEPARATE FILE - MOTOR.C

/*******   Motor Truth Table   ***********

   Phase    Enable    Sleep    Function
   -----    ------    -----    --------
     1        1         1      Forward
     0        1         1      Reverse
     X        0         1      Brake
     X        X         0      Sleep Mode

*****************************************/
 
void motor_CCW(void)
{
         Motion_LED_On;
         Phase_On;
         Enable_On;
}


void motor_CW(void)
{
         Motion_LED_On;
         Phase_Off;
         Enable_On;
}


void motor_STOP(void)
{
         Motion_LED_Off;
         Enable_Off;
}
/************    END MOTOR.C   **************/





/*************************************************************
                           MAIN PROGRAM
*************************************************************/
                         
#include "AutoSensor.h"
#include "project_defs.h"
#include "debounce.c"
#include "motor.c"

#ZERO_RAM

boolean POWER_ON = FALSE;   //Power button

/************************************************************
                       Interrupt Function
************************************************************/

#int_EXT
EXT_isr() {

   debounce();
   if(POWER_ON)
      POWER_ON = FALSE;
   else
      POWER_ON = TRUE;

     if (POWER_ON)   //Power On
      Sleep_Off;

   else
        Sleep_On;  //Puts everything to sleep

}

/************************************************************
                       Main Function
************************************************************/

void main() {

   Sleep_On;
   port_b_pullups(TRUE);
   setup_counters(RTCC_INTERNAL,WDT_2304MS);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_A1_A2);
   setup_vref(FALSE);
   enable_interrupts(global);
   enable_interrupts(INT_EXT);


  while(TRUE){
      while (POWER_ON)
      {

          // if neither button is pushed, Brake
                if (BT_TURN_NOT_Pushed && BT_RET_NOT_Pushed)
                     motor_STOP();

            // if Turn is pushed, rotate counterclockwise
                while (BT_TURN_Pushed)
                       {
         
                          if (END_LS_SET)
                           motor_STOP();
                        else
                           motor_CCW();
                       }

            // if RETURN is pushed, rotate clockwise
                while (BT_RET_Pushed)
                       {
                       
                          if (START_LS_SET)
                             {
                               motor_STOP();
                              Raised_LED_On;
                           }
                          else
                           motor_CW();
                           
                       }
        }


      /***********************************************************
                                Sleep Mode
       setting PIN_A2 high gives lowest sleep current
      ***********************************************************/

      setup_comparator(NC_NC_NC_NC); // disable comparator
      output_high (PIN_A2);      
      #ASM
         SLEEP
         NOP
      #ENDASM 
      output_low (PIN_A2);
      setup_comparator(NC_NC_A1_A2);
           
   }
}
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Wed Nov 10, 2004 12:47 pm     Reply with quote

Quote:

#fuses INTRC_IO,NOWDT


change to:
#fuses INTRC_IO,NOWDT,NOLVP,NOPROTECT

Humberto
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Nov 10, 2004 12:51 pm     Reply with quote

Are you using an ICD to read PORTB?
Sensor



Joined: 04 Nov 2004
Posts: 33

View user's profile Send private message Send e-mail

PostPosted: Wed Nov 10, 2004 12:55 pm     Reply with quote

that wasn't it. But thanx anyway Humberto

Last edited by Sensor on Wed Nov 10, 2004 1:01 pm; edited 1 time in total
Sensor



Joined: 04 Nov 2004
Posts: 33

View user's profile Send private message Send e-mail

PostPosted: Wed Nov 10, 2004 1:00 pm     Reply with quote

To Marks question....

We are using the MPLAB ICE 2000, with MPLAB version 6.60. We are reading the register PORTB under special function registers in MPLAB v6.60.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Nov 10, 2004 1:03 pm     Reply with quote

I would check the hardware.
Sensor



Joined: 04 Nov 2004
Posts: 33

View user's profile Send private message Send e-mail

PostPosted: Wed Nov 10, 2004 1:07 pm     Reply with quote

We think we exausted that possibility but we can do that again. The last physical connection (RB7) going into the emulator will behave as expected. i.e it WILL go high when we set the switch, but the PORTB register does not correspond to that, namely it registers it as a RB6 change.

Also the Input change on RB6 does register on the last physical connection to the emulator, but when we read the PORTB register, no changes are registered.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Nov 10, 2004 1:10 pm     Reply with quote

Have you tried a real chip? Its possible that the emulator pod is bad.
Sensor



Joined: 04 Nov 2004
Posts: 33

View user's profile Send private message Send e-mail

PostPosted: Wed Nov 10, 2004 1:14 pm     Reply with quote

We have not tried a real chip yet. However I want to mention this though:
If we do a simple command
Code:
output_high(PIN_B7);

we DO see a change in the register, namely it does go high on RB7
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Nov 10, 2004 1:16 pm     Reply with quote

The input buffers are different from the outputs. You can blow em up (inputs) and the outputs still work okay.
Sensor



Joined: 04 Nov 2004
Posts: 33

View user's profile Send private message Send e-mail

PostPosted: Wed Nov 10, 2004 1:19 pm     Reply with quote

is there any way to verify that
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Wed Nov 10, 2004 2:24 pm     Reply with quote

IMO you must reduce your code to minimum, at least to the level were you
feel confidence with the hardware, software and tools you are using.

Just an input switch and an output LED.

Code:

while(TRUE)
    {
      if (END_LS_SET)
          Led_OFF;
      else
          Led_ON;
    }


Then and only when the hardware does what you coded, you have a real case to post
the results or behaviour, meanwhile you are playing with "virtual" inputs and outputs.

HTH,

Humberto Very Happy
Sensor



Joined: 04 Nov 2004
Posts: 33

View user's profile Send private message Send e-mail

PostPosted: Mon Nov 15, 2004 10:54 am     Reply with quote

@ Mark

and everybody else...

Thanx. It turns out that our emulation kit is defective. We did sucessfully program the real chip and it is behaving as expected.

Thanx again for all of your help
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