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

Bit Bang serial comm

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



Joined: 09 Aug 2005
Posts: 32

View user's profile Send private message

Bit Bang serial comm
PostPosted: Mon Aug 25, 2008 10:10 am     Reply with quote

I am writing a routine to get data from a thumbwheel switch. I can't get the right thumbwheel switch.


This is th original code written in Hi-tech to send out the thumbwheel pin to another processor.

Code:


/************************************************************************
// MODULE              : SendID
//************************************************************************/
void SendID( void )
{
    unsigned long ulID;
    unsigned long ulMask;

   
   unsigned long ulLSD, ulCD, ulMSD;
   
   // initialize the value we will be sending
    ulID = 0;
      
   
   ulMSD = bHighDigit;
   ulMSD = ( ulMSD << 4 ) & 0x00000f00;
   
   ulCD = bMidDigit;
   ulCD = ulCD & 0x000000f0;
   
   ulLSD = ( bLowDigit >> 4 ) & 0x0000000f;
   
   ulID = ulMSD + ulCD + ulLSD;
   
   

   // For debug only.
   //ulID = 0xaaaaaa;         

    // set mask for msb of ID
    ulMask = 0x0000800;

    // let the blk box know we are ready
    RA4 = LO;
   RA0 = HI;
   

    while( ulMask > 0 )
    {
        // throw the dog a bone
        HitWD();

      
       
        // wait for blk box to start clocking data (shouldn't be for more than 5 uS)
        while( !RA3 )
        {
            // loop until RA3 goes HI
            // DelayUs( WAIT_FOR_SIGNAL_DELAY );

        }
      
      
        // throw the dog a bone
        HitWD();

        // outbit bit value
        if( ( ulID & ulMask ) == 0 )
        {
            // set bit to LO
            RA2 = LO;
        }
        else
        {
            // set bit HI
            RA2 = HI;
        }

        while( RA3 == HI )
        {
            // loop until RA3 goes LO (shouldn't be for more than 5 uS)
            // DelayUs( WAIT_FOR_SIGNAL_DELAY );

        }

        // throw the dog a bone
        HitWD();

        ulMask = ulMask >> 1;
    }

    RA4 = HI;
    RA0 = LO;
}



I suppose to write a receiver side to get the pin what already sent from the previous routine using CCS.

Here is my code
Code:

#include <prototype.h>

//unsigned char uchrReceivedPINNumberArray[PINNUBERSIZE];



main()
{
    
 counter = 0;
 while(1)
   {   
   
   
      output_high(TWHEEL_POWER);
      if(!input(pin_b5))
      {
         output_low(PIN_b3);
         data = input(pin_b1);
         counter++;
         hNumber = data;
         data = input(pin_b1);
         counter++;
         mNumber = data;
         data = input(pin_b1);
      //   counter++;
      }
      else
      {

      }



   

   printf("\n\r%u %u %u ",hNumber,mNumber,lNumber);
//   delay_ms(10);
   }

   
}
         

   


Can anybody help me out on my code?

Thanks.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Aug 25, 2008 10:50 am     Reply with quote

First, when posting a question in this forum we always like to know:
- Your compiler version.
- The PIC model you are using.
- The clock speed your PIC is running.

Then, for your application I'd like to know:
- Do you have the PIC connected to a real thumb wheel switch? If yes, do you have a type number and data sheets?
- How is your PIC connected to the other device? Which pins go where?

The most obvious error in your program is that the thumb wheel expects a clock signal but you don't provide such a signal (all outputs are static, none is going high/low/high/... etc).
newpic



Joined: 09 Aug 2005
Posts: 32

View user's profile Send private message

Bit Bang Serial Comm
PostPosted: Mon Aug 25, 2008 11:32 am     Reply with quote

Ok here my prototype.h

Code:
#include <16f877a.h>
#device ICD=TRUE ADC=10
#fuses XT, NOLVP,NOWDT,PUT
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)



The Thumbwheel is connect to a PIC16F628A (which is I call the thumbwheel) There is 3 digits thumbwheel.
The thumbwheel get power on pin RB2 of main station(my board)
4Mhz

Thumbwheel
5-pins connect my 16f877a(main station) as follow:

Thumbwheel Main Station

RA5 ----------------------RB2 (use as VCC or CS for thumbwheel)
RA4-----------------------RB5 (use to send signal that thumbwheel is ready to send data)
RA3-----------------------RB0 (use to as clock)
RA2-----------------------RB1 (use for data transmittion pin)


the main code of the thumbwheel read the thumbwheel switch and put together into 3 digit hNumber, mNumber, lNumber. then transmit to the main station.

So my board is the main station and design to read the thumbwheel

The original thumwheel software was written from somebody else.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Aug 25, 2008 11:55 am     Reply with quote

In the RS232 setup add the ERRORS keyword. This will prevent the UART from locking up if you are slow in reading the incoming data.

Code:
output_low(PIN_b3);
Why is this line in your code if nothing is connected to it?

I think you can write the code yourself, it is not very difficult. Two things are missing in your code:
1) The clock signal. Your program has to create a clock signal on the B0 pin. (a high/low pulse before every reading of the pin B1 level).
2) You are overwriting the value in 'data' with every bit you read. Checkout the bit shift instruction '<<' (shift left).

Note that the example program is shifting 12 bits, so your 'data' variable must be at least an int16 size.

Try this and if it doesn't work post your program here again so we can have a look at it.
newpic



Joined: 09 Aug 2005
Posts: 32

View user's profile Send private message

Bit Bang serial comm
PostPosted: Mon Aug 25, 2008 2:21 pm     Reply with quote

Here is my new code to use the RB0 as clock

and it still not working. My hyperterminal just show [0 0 0 0]
it does not read any switch from the thumbwheel.

Code:

#include <16f877a.h>
#device ICD=TRUE ADC=10
#fuses XT, NOLVP,NOWDT,PUT
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)


#define CLK  PIN_B0
#define DOUT PIN_B1
#define CS   PIN_B2

unsigned int uintTempLB, uintTempCB,uintTempUB;
long intResult;
// This routine reads the thumbwheel switches. It return an integer that is 3-digit thumbwheel switch reading
int ReadThumbwheelSwitches(void)
{
   long int lintReadBuffer;
   
   int intBitCounter;
   unsigned int uintTempLB, uintTempCB,uintTempUB;

   lintReadBuffer = 0;
   intResult = 0;

   // Set Clock Low
   output_low(CLK);
   
   // We must first power-up the thumbwheel board and wait for the enable line to go high
   output_high(CS);
   delay_ms(100);

   // Ok, now we can start reading the data from the thumbwheel board.
   output_low(CLK);    //Make sure clock is low
   
   // Now get 3 binary numbers from the thumbwheel board.
   for(intBitCounter = 0; intBitCounter <13; intBitCounter++)
   {
      delay_us(7);
      output_high(CLK);
      delay_us(7);
      

      lintReadBuffer = input(DOUT);
      lintReadBuffer = (lintReadBuffer << 1);

      //Set clock low   
      output_low(CLK);

   // Now power down the thumbwheel board
//   output_low(CS);

   // Now format it into an integer result
   lintReadBuffer = (lintReadBuffer >> 1) & 0x00000fff;
   uintTempLB = lintReadBuffer & 0x0000000f;
   uintTempCB = (lintReadBuffer & 0x000000f0) >> 4;
   uintTempUB = (lintReadBuffer & 0x00000f00) >> 8;

   intResult = (uintTempUB * 100) + (uintTempCB * 10) + uintTempLB;

   // Make sure we have a good number or return 1000.
   if(intResult >999 || (intResult < 0))
   {
      intResult = 0;
   }
   return intResult;
}
      
}


//**************main program start here *********************

void main()
{

   while(1)
   {
      
      ReadThumbwheelSwitches();
      printf("\n\r%u  %u  %u %lu ",uintTempUB,uintTempCB,uintTempLB,intResult);
   }


   
}


Can anybody help?
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Aug 25, 2008 5:50 pm     Reply with quote

- You didn't add the ERRORS keyword to the #use RS232 line as I recommended you to do.

Code:
   unsigned int uintTempLB, uintTempCB,uintTempUB;
This line of code is two times in your program. Now you have local _and_ global variables with the same name. Get rid of the local variables (the line inside the function ReadThumbwheelSwitches().

Code:
   for(intBitCounter = 0; intBitCounter <13; intBitCounter++)
   {
...
   if(intResult >999 || (intResult < 0))
   {
      intResult = 0;
   }
   return intResult;
}                           <<------------ this one should be about 15 lines higher !!!!!!
     
}
The second last curly brace is at the wrong position. Now your loop only executes one time.


Code:
   // Now format it into an integer result
   lintReadBuffer = (lintReadBuffer >> 1) & 0x00000fff;
Here you do a correction of one too many shifts. This is not an error, but if you reverse the two lines below than this whole correction is not required any more.
Code:
      lintReadBuffer = input(DOUT);
      lintReadBuffer = (lintReadBuffer << 1);



Code:
   // Now format it into an integer result
   lintReadBuffer = (lintReadBuffer >> 1) & 0x00000fff;
   uintTempLB = lintReadBuffer & 0x0000000f;
   uintTempCB = (lintReadBuffer & 0x000000f0) >> 4;
   uintTempUB = (lintReadBuffer & 0x00000f00) >> 8;
lintReadBuffer is a 16 bit variable but but your AND of 0x0000000f is 32-bit. A waste of resources. Anding with 0x000F would suffice.
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