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

why! Tx - Rx source code not work

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



Joined: 07 May 2007
Posts: 69

View user's profile Send private message

why! Tx - Rx source code not work
PostPosted: Mon Dec 06, 2010 5:36 pm     Reply with quote

I've searched, using the search tool and have not found a solution.
Please help.
Rx code
Code:
/* Recieved */
#include <12f675.h>
#device  adc=10
#fuses   INTRC_IO                   
#fuses   NOWDT                     
#fuses   PUT                       
#fuses   NOMCLR                     
#fuses   NOBROWNOUT                 
#fuses   NOCPD                     
#fuses   NOPROTECT                 
#use     delay (clock=4000000)
#byte    OSCCON = 0x8F

#define DATA     PIN_A5           // input
#define Led      PIN_A4           // output

int16 rxbit (void)                // check bit cycle
      {
       int16 x;
       x = 0;
       while (input (DATA)==1) {x++;}
       while (input (DATA)==0) {x++;}
       return (x);
      }

void rxpre (void)                 // check preamble
     {
      int16 x;
      char c;
      c = 0;
      while (c<5)                 // check bit 0
            {
             x = rxbit ();
             if (x>=33 && x<=37) c++; else c = 0;
            }
      while (x<53) {x = rxbit ();}     // check bit 1           
     }

char rxbyte (void)                // read one byte
     {
      char x,i;
      x = 0;
      for (i=0;i<=7;i++)
          {
           x >>= 1;
           if (rxbit ()>53) x |= 0x80;
          }
      return (x);
     }

char rxrecv (void)
     {
      char c,x ;                         
      rxpre ();c = rxbyte ();x = rxbyte ();
      if (c==0x240 ) return (x);  // match         
     }

void main (void)
     {
      char x;
      disable_interrupts(GLOBAL);
      setup_adc_ports(NO_ANALOGS);
      setup_adc(ADC_OFF);     
      setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
      setup_timer_1(T1_DISABLED);
      setup_adc(ADC_CLOCK_INTERNAL);   
      port_a_pullups(true);
      set_tris_A(0b11101111);
      OSCCON = 0x60;delay_ms(100);
      output_low (Led);
      while (true)
            {
             x = rxrecv ();
             if (x==0x1)
                {
                 output_high(Led);delay_ms(100);
                 output_low(Led);delay_ms(50);
                }
            }
}


Tx code
Code:
/* SEND */
#include <12f675.h>
#fuses INTRC_IO                 
#fuses NOWDT                     
#fuses PUT                       
#fuses NOMCLR                   
#fuses NOBROWNOUT                 
#fuses NOCPD                     
#fuses NOPROTECT                 
#use   delay (clock=4000000)
#byte  OSCCON = 0x8F

#define DATA     PIN_A4           // output
#define LED      PIN_A0           // output
#define Send     PIN_A1           // input

void tx0 (void)                   // send bit 0
     {
      output_high (DATA);delay_us (250);
      output_low (DATA);delay_us (250);
     }

void tx1 (void)                   // send bit 1
     {
      output_high (DATA);delay_us (500);
      output_low (DATA);delay_us (500);
     }

void txbyte (char x)              // send one byte
     {
      char i;
      for (i=0;i<=7;i++)
          {
           if ((x & 1)==1) tx1 (); else tx0 ();
           x >>= 1;
          }
     }

void txsend (char x)
     {
      txbyte (0);txbyte (0);      // preamble
      tx1 ();                     // start
      txbyte (0x240);             // code 1
      txbyte (x);                 // command
      tx0 ();                     // end
      output_high (LED);delay_ms (100);
      output_low (LED);
     }

void main (void)
     {
      disable_interrupts(GLOBAL);
      setup_adc_ports(NO_ANALOGS);
      setup_adc(ADC_OFF);
      setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
      setup_timer_1(T1_DISABLED);
      set_tris_A(0b11101110);               
      setup_adc(ADC_CLOCK_INTERNAL); 
      port_a_pullups(true);
      output_low (LED);
      OSCCON = 0x60;
      delay_ms(20);
      while (true)
            {if (input (Send)==0) {txsend (0x1);delay_ms(500);}}
}


Thanks for the help!


Last edited by sorasit46 on Tue Dec 07, 2010 2:45 am; edited 2 times in total
vinniewryan



Joined: 29 Jul 2009
Posts: 154
Location: at work

View user's profile Send private message MSN Messenger

PostPosted: Mon Dec 06, 2010 7:10 pm     Reply with quote

What is your compiler version?
What isn't working?
Have you tried to run a simple test program using this mcu?
_________________
Vinnie Ryan
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Mon Dec 06, 2010 9:15 pm     Reply with quote

Hi,

First you need to have a strategy to debug stuff like this on your own. It's your project and no one is going to take a greater interest in it than you! Also, you've got all the hardware right in front of you, so you have a significant advantage over a bunch of random folks on an internet forum!

Having said that, you need a way to find out what is going on inside the PIC. I have found it handy to have a serial LCD that I can connect to a spare pin and use to display diagnostic messages. Sometimes this doesn't always work in time critical sections of the code, but it may be a start. You can also get creative with extra LEDs to help debug what is going on. You also really need to look at how your PIC chips are connected - is there a common GND between them, for instance. Next, take small steps in the problem (for example, can the Tx output a single bit and can it be received by the RX?) and build toward the final solution.

Frankly, your code needs a lot of work. Most of the setup stuff is unnecessary, as is the use of TRIS (you aren't using Fast IO). I also noticed that you define OSCCON differently in each program - 0x08F and 0x8F. Not sure why you are doing that in the first place, but it is a discrepancy.

John
sorasit46



Joined: 07 May 2007
Posts: 69

View user's profile Send private message

PostPosted: Tue Dec 07, 2010 2:57 am     Reply with quote

ezflyr wrote:
Hi,
.....
Frankly, your code needs a lot of work. Most of the setup stuff is unnecessary, as is the use of TRIS (you aren't using Fast IO). I also noticed that you define OSCCON differently in each program - 0x08F and 0x8F. Not sure why you are doing that in the first place, but it is a discrepancy.

John


Thanks
Now! I using Fast IO & define OSCCON in each program - 0x8F.
But It's not work.
My compiler is PCWHD IDE 4.057.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Tue Dec 07, 2010 3:00 am     Reply with quote

First problem.

I actually expect this does not even compile, you should post the compiler error messages and warnings if there are any!

Code:

char rxrecv (void)
     {
      char c,x ;                         
      rxpre ();
      c = rxbyte ();
      x = rxbyte ();
      if (c == 0x240)
            return (x);  // match         
     }


I have made it more readable by seperating the lines, Can YOU spot the error ?

This routine only returns a value if c == 0x240. You need another return(0); statement at the end.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Tue Dec 07, 2010 3:18 am     Reply with quote

And another problem Smile, this is funny Smile, an honest mistake...

Code:

char rxrecv (void)
     {
      char c,x ;                         
      rxpre ();c = rxbyte ();x = rxbyte ();
      if (c==0x240 ) return (x);  // match         
     }


The same problem lies in the tx routine.

What value is 0x240 ? lol. What size is c ?

I expect because the error is in both it would not affect the working of your code.
sorasit46



Joined: 07 May 2007
Posts: 69

View user's profile Send private message

PostPosted: Tue Dec 07, 2010 7:49 pm     Reply with quote

Thanks for the help!
I'm trying to recheck.
sorasit46



Joined: 07 May 2007
Posts: 69

View user's profile Send private message

PostPosted: Sat Dec 11, 2010 5:26 am     Reply with quote

My mistake in this code.
Quote:

void rxpre (void) // check preamble
{
int16 x;
char c;
c = 0;
while (c<5) // check bit 0
{
x = rxbit ();
if (x>=66 && x<=74) c++; else c = 0;
}
while (x<106) {x = rxbit ();} // check bit 1
}

char rxbyte (void) // read one byte
{
char x,i;
x = 0;
for (i=0;i<=7;i++)
{
x >>= 1;
if (rxbit ()>106) x |= 0x80;
}
return (x);
}
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