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

Unexpected Result using 16F84...Please review

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







Unexpected Result using 16F84...Please review
PostPosted: Mon May 03, 2004 5:42 pm     Reply with quote

I'm trying to read a single switch using a 16F84.
As you can see in the code below I'm only sending 2 bytes of data if the state
of the switch changes.

However, I'm getting the following 3 bytes:

If RB6 is LOW, it sends: 0xFE 0x1E 0x00
If RB6 is High it sends: 0xFE 0x9E 0x00

I was expecting the following:
If RB6 is LOW, it sends: 0xBF 0x01
If RB6 is High it sends: 0xBF 0x00

The variables nTemp and nFinal holds the polled and valid state of the switch, respectively.
The MSB represents the switch state, and the 2 LSB is the counter.

I know something is wrong here. Perhaps, people in this forum can help me on this. Thank you in advance.

I'm using CCS 3.173
Also, I'm not using a max232, hence, an INVERT parameter in the #use rs232.

Code:

#include <16f84.h>
#fuses HS, PUT, NOWDT
#use delay (clock=10000000)

#use rs232 (baud=9600, xmit=PIN_A0, rcv=PIN_A1, INVERT)




//------------------------------------------------------------------------------
//declarations
#define  COUNTER     0x03
#define  BOARD_ID    0xBF
         
int nTemp, nFinal, buf, z;
void get_input(void);
void send_out(int, int);


//------------------------------------------------------------------------------
main() 
{

   get_input();
   nFinal = nTemp |  COUNTER;
   buf = nFinal;

   while(1)
   {
      if( (nTemp & 0x80) != (nFinal & 0x80) ) 
      {
         nFinal--;                                  //state changed, so decrement counter
         delay_ms(5); 
       
         if( (nFinal & COUNTER) == 0x00 )           //counter now zero?
         {
            z = (nTemp == 0x80) ? 1 : 0;

            nFinal = nTemp | COUNTER;               //store final state
       buf = nFinal;                           //make a copy
            send_out(BOARD_ID, z);                 
         }
      }
     
      else                                         
      {
            nFinal = buf;                           //no change, so restore previous valid state   
      }
                     
      get_input();
     
   }

}


//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

void get_input(void)
{
   if(input(PIN_B6))
   {
      nTemp = 0x80; 
   }
   else
   {
      nTemp = 0x00;
   }
}


//------------------------------------------------------------------------------

void send_out(int board_ch, int value)
{
   putc(board_ch);
   putc(value);
}

//------------------------------------------------------------------------------
Guest_X44
Guest







PostPosted: Mon May 03, 2004 6:15 pm     Reply with quote

I got it.
Thanks anyway.
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