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

What wrong in my code? send & receive serial data.

 
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

What wrong in my code? send & receive serial data.
PostPosted: Sat Feb 28, 2009 8:21 pm     Reply with quote

Please help!
Why not work?

Tx send

Code:

#include <16F886.h>
#device  ADC=10                   
#define  CLOCK_SP   4000000
#fuses   hs,NOLVP,WDT,NOPROTECT,NOPUT,NOBROWNOUT,NOCPD,NOWRT,NODEBUG
#use     delay (clock=CLOCK_SP)   
#use     fast_io(A)
#use     fast_io(B)
#byte    port_b=6                 
#byte    OSCCON = 0x08F

.
.
#define  SendData PIN_B2            // send serial data to 12F675
.
.

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

void tx1 (void)                  // send bit 1
{output_high (SendData);delay_us (500);output_low (SendData);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);                                        // preamble
    txbyte (0);
    tx1 ();                                            // start
    txbyte (0x2);                                      // code1
    txbyte (0x4);                                      // code2
    txbyte (0x6);                                      // code3
    txbyte (0x8);                                      // command
    tx0 ();                                            // end
}

Void     main()
{
 OSCCON=0x60;                                         // set int-osc 4 Mhz
 while ((OSCCON&0x4)!=0x4);                           // wait for clock stable
 setup_ccp1(CCP_CAPTURE_FE);                         
 setup_ccp2(CCP_OFF);
 enable_interrupts(INT_CCP1);                         
 enable_interrupts(GLOBAL);                           
 setup_adc_ports(sAN0 | sAN4);
 setup_adc(adc_clock_internal);
 setup_spi(FALSE);
 enable_interrupts(INT_TIMER0);                     
 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);             
 set_timer0(0);                                     
 setup_timer_1(T1_DISABLED);                       
 setup_timer_2(T2_DISABLED,0,1);                     
 set_tris_a(0b00110001);                             
 set_tris_b(0xFA);                                   
 set_tris_c(0b00001101);                             
 output_low(SendData);
 setup_wdt(WDT_2304MS);
 while(1)
      {
       .
       restart_wdt();txsend (0x6);restart_wdt();
       .   
      {
}


Tx receive

Code:

#include <12F675.h>
#device  adc=10
#FUSES NOWDT,INTRC_IO,NOCPD,NoPROTECT,NOMCLR,PUT,NOBROWNOUT
#use   delay(clock=4000000)       
#byte  OSCCON = 0x8F

.
.
#define Confirm  PIN_A2           // confirm can get data
#define Datain   PIN_A3           // serial data from 16F886
.
.

int16 rxbit (void)                // check bit cycle
{
    int16 x;
    x = 0;
    while (input (IRWork)==1) x++;
    while (input (IRWork)==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 i,j,a,x;
    rxpre ();
    i = rxbyte ();
    j = rxbyte ();
    a = rxbyte ();
    x = rxbyte ();
    if (i==0x2 && j==0x4 && a==0x6 ) return (x);    // match
    else return (0xff);                             // not match
}

void main()
{
 char    x;
 setup_adc_ports(NO_ANALOGS);     
 setup_adc(ADC_OFF);               
 disable_interrupts(GLOBAL);
 setup_adc(ADC_CLOCK_INTERNAL);   
 setup_counters (RTCC_INTERNAL,RTCC_DIV_1);
 set_tris_A(0x1B);
 output_low(Confirm);
 while(1)
      {
       .
       .
       x=rxrecv();
       if (x==0x8) output_high(Confirm); else  output_low(Confirm);
       delay_ms(1000);

       .
       .
      }
}


Regards
asmallri



Joined: 12 Aug 2004
Posts: 1635
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Sun Mar 01, 2009 1:25 pm     Reply with quote

On the TX side you have set the HS fuse but the clock parameter says you are only running at 4MHz.

So are you running at 4MHz or are you using HS?
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
Ttelmah
Guest







PostPosted: Sun Mar 01, 2009 2:58 pm     Reply with quote

Worse, he appears to be trying to use the internal RC oscillator.
If you want to use this, use the INTRC fuse, and stop fiddling with the OSCCON register. Doing this will override the oscillator adjustment factor automatically loaded from the chip at boot, and the oscillator will never start, since HS, won't run without an external resonator. I wonder if in fact he has deleted the adujstment factor in the past so INTRC won't work?. If so, do a search here on how to reload this with a #rom statement.

Best Wishes
sorasit46



Joined: 07 May 2007
Posts: 69

View user's profile Send private message

PostPosted: Sun Mar 01, 2009 8:29 pm     Reply with quote

Ttelmah wrote:
Worse, he appears to be trying to use the internal RC oscillator.
If you want to use this, use the INTRC fuse, and stop fiddling with the OSCCON register. Doing this will override the oscillator adjustment factor automatically loaded from the chip at boot, and the oscillator will never start, since HS, won't run without an external resonator. I wonder if in fact he has deleted the adujstment factor in the past so INTRC won't work?. If so, do a search here on how to reload this with a #rom statement.

Best Wishes


12F675 use INTRC (4MHz). 16F886 use HS (4MHz) .
You mean my phoblem is 12F675 use INTRC. but 12F886 use HS.
regards
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