Guest_X44 Guest
|
Unexpected Result using 16F84...Please review |
Posted: Mon May 03, 2004 5:42 pm |
|
|
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);
}
//------------------------------------------------------------------------------
|
|
|