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

Please help me about one wire serial communication.

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







Please help me about one wire serial communication.
PostPosted: Tue Feb 22, 2005 10:25 am     Reply with quote

i'm very confuse in this code. When i'm not use procedure between //====start crc16 ====== to //====end crc16=====
i will receive data ATR is ok. but when i use procedure between //====start crc16 ====== to //====end crc16===== Data ATR
will fail all the time why???? Should i do for my code.
//==============================================
#include <16F877.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, float_high, bits=8, xmit=PIN_B0, rcv=PIN_B0 ,STREAM=MCU_DATA)

#define PBUS_RAM_SIZE 16

BYTE pbus_ram[PBUS_RAM_SIZE];

enum pbus_states { PBUS_IDLE =0x80,
PBUS_NEED_ATR =0X81,
PBUS_NEED_NAD =0X82,
PBUS_NEED_PCB =0X83,
PBUS_NEED_LEN =0X84,
PBUS_NEED_CLA =0X85,
PBUS_NEED_INS =0X86,
PBUS_NEED_P1 =0X87,
PBUS_NEED_P2 =0X88,
PBUS_NEED_LC =0X89,
PBUS_NEED_DATA =0X90,
PBUS_NEED_LE =0X91,
PBUS_NEED_CRC_HI=0X92,
PBUS_NEED_CRC_LO=0X93,
PBUS_SEND_START_SESSION=0X94
} ;

BYTE pbus_state=PBUS_IDLE;
BYTE pbus_next_loc,pbus_next_value;

char dat[20];

char buffin[20];
int next_in=0;
int next_out=0;

#bit intf = 11.1

#define CARD_IN !input(PIN_B1)

int CIN=0;

void clear_buffin(void)
{
int a;
for(a=0;a<=20;a++)
{
buffin[a]='\0';
}
}

#int_ext
void pbus_isr()
{
BYTE data;

if(kbhit())
{
dat[next_in]=Fgetc(MCU_DATA);
if(++next_in==20)
{
next_in=0;
}
}
}

byte get_buf(void)
{
byte retval;

while(next_in==next_out);

retval=dat[next_out];
if(++next_out==20)
{
next_out=0;
}
return retval;
}



/*
//=======================================START CRC16========================================
const unsigned int CRC16_LookupHigh[16]={
0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70,
0x81, 0x91, 0xA1, 0xB1, 0xC1, 0xD1, 0xE1, 0xF1
};

const unsigned int CRC16_LookupLow[16] ={
0x00, 0x21, 0x42, 0x63, 0x84, 0xA5, 0xC6, 0xE7,
0x08, 0x29, 0x4A, 0x6B, 0x8C, 0xAD, 0xCE, 0xEF
};

unsigned char CRC16_High, CRC16_Low;

void CRC16_Init( void )
{
CRC16_High = 0xff; CRC16_Low = 0xff;
}

void CRC16_Update4Bits( unsigned char val )
{
unsigned char t;

t = CRC16_High >> 4;
t = t ^ val;
CRC16_High = (CRC16_High << 4) | (CRC16_Low >> 4);
CRC16_Low = CRC16_Low << 4;

CRC16_High = CRC16_High ^ CRC16_LookupHigh[t];
CRC16_Low = CRC16_Low ^ CRC16_LookupLow[t];
}

void CRC16_Update( unsigned char val )
{
CRC16_Update4Bits( val >> 4 ); // High nibble first
CRC16_Update4Bits( val & 0x0F ); // Low nibble
}

int16 generate_16bit_crc(char* datach,int16 lendata)
{
int colen;

CRC16_Init();

for(colen=0;colen<lendata;colen++)
{
CRC16_Update(datach[colen]);
}

return make16(CRC16_High,CRC16_LOW);
}

//=======================================END CRC16==========================================

*/

#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7 , stream=PC_DATA)

void Receive_ATR(void)
{
const byte atrdata[13] = { 0x3b,0xa7,0x00,0x80,0x10,0x20,'K','U','_','V','1','.','0' };

byte countatr;
int numatr;

numatr=0;

for(countatr=0;countatr<=12;countatr++) { buffin[countatr]=get_buf(); }

for(countatr=0;countatr<=12;countatr++)
{
if( buffin[countatr]==atrdata[countatr] )
{
numatr++;
}
else
{
fputc(buffin[countatr],PC_DATA);
fputc('\n',PC_DATA);
fputc('\r',PC_DATA);
}
}

fprintf(PC_DATA,buffin);

if(numatr==13) { fprintf(PC_DATA,"ATR OK\n\r"); pbus_state=PBUS_SEND_START_SESSION; }
else { fprintf(PC_DATA,"ATR NOT OK\n\r"); pbus_state=PBUS_IDLE; }
}


#include <input.c>

void main() {

BYTE to,i,value,CIN;


char xd[2]="A";
int16 CRC_16;
int8 CRC_H,CRC_L;

fprintf(PC_DATA,"\r\nPress S to send.\r\n");

ext_int_edge( h_to_l );
disable_interrupts(global);
disable_interrupts(int_ext);

clear_buffin();

do {
if(CARD_IN)
{
IF(CIN==0)
{
fprintf(PC_DATA,"CARD IN.\r\n");

OUTPUT_LOW(PIN_A5); //reset
DELAY_MS(10); //reset
OUTPUT_HIGH(PIN_A5); //reset

CIN=1;
clear_buffin();
pbus_state=PBUS_NEED_ATR;

enable_interrupts(global);
enable_interrupts(int_ext);
}

switch (pbus_state)
{
case PBUS_NEED_ATR : { Receive_ATR(); break; }
/* case PBUS_SEND_START_SESSION : {

disable_interrupts(global);
disable_interrupts(int_ext);

CRC16_Init();
CRC_16=generate_16bit_crc(xd,1);
CRC_H=CRC_16>>8;
CRC_L=CRC_16;

fprintf(pc_data," %X\r\n",CRC_H);
fprintf(pc_data," %X\r\n",CRC_L);

enable_interrupts(int_ext);
enable_interrupts(global);
break;
}
*/
case PBUS_SEND_START_SESSION : { fprintf(PC_DATA,"HEXXX\r\n"); break; }
default : break;
}
}
else
{
cardout:
disable_interrupts(global);
disable_interrupts(int_ext);
CIN=0;
clear_buffin();
next_in=0;
next_out=0;
pbus_state=PBUS_IDLE;
}
} while (TRUE);
}
ckielstra



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

View user's profile Send private message

PostPosted: Tue Feb 22, 2005 6:04 pm     Reply with quote

Please be more specific. What do you mean with your application failing?
I mean: Tell us if the program stops? Or do you get unexpected results from the CRC16 routine? Or what other behaviour do you see?

From a quick look at you code I don't see any obvious syntax errors.
a
Guest







PostPosted: Tue Feb 22, 2005 6:44 pm     Reply with quote

Program is not stop but when i write code crc16 in program is receive data error but when i'm not write code crc16 in program is work(receive data ok). i'm very confuse about it.
Or anyone have way to receive data from 1 wire (not in ex_CRC). Please help me Sad i will try other way
ckielstra



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

View user's profile Send private message

PostPosted: Wed Feb 23, 2005 3:25 pm     Reply with quote

I don't think your problem is in the CRC16 routine as you are not checking the result code but only printing.
The other difference between your two code versions is the disabling of interrupts... You are printing the results at the same data rate as the input data rate. I don't know the specifications of your protocol, but very likely the printing of the results takes too much time which causes you to miss some incomming data.

The normal approach to solve this problem is to buffer the incomming and/or outgoing data. These buffers can then be flushed through interrupts. Search this forum for the keywords 'circular buffer' for code examples.
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