Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

Fix me code with CRC algorithm

Status
Not open for further replies.

member_tdh

Member level 5
Joined
Feb 6, 2006
Messages
86
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
2,187
Hi all members!
In my project, i want to sent data between 2 AVR via RS232. I using CRC algorithm to corrected data, but my program is running wrong. I have not understand detail about CRC method yet. I post my code received and transmition, anybody pls help me...!?
Thanks all friends !

//This's Transmit Data Routine:

/*****************************************************
Chip type : ATmega8515
Program type : Application
Clock frequency : 8.000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 128
*****************************************************/

#include <mega8515.h>
#include <delay.h>
#include <stdlib.h>

// Standard Input/Output functions
#include <stdio.h>

#define BYTE unsigned char
#define WORD unsigned int

void Init();

BYTE DiemGui, ViTriLed;
WORD msec;

bit bTransToPc=0;

interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
TCNT1H=0xFF; //interrupt 1ms
TCNT1L=0x83;

if (++msec==1000)
{
msec=0;
if (++DiemGui>10) DiemGui=5;
if (++ViTriLed>6) ViTriLed=1;
bTransToPc=1;
// putchar('B');
// putchar(DiemGui);
// putchar(ViTriLed);
}

}


void Init()
{
PORTA=0xFF;
DDRA=0xFF;

PORTB=0xFF;
DDRB=0xFF;

PORTC=0xFF;
DDRC=0xFF;

PORTD=0xFF;
DDRD=0xFF;

PORTE=0xFF;
DDRE=0xFF;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 125.000 kHz
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x03;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x80;

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud rate: 9600
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;

// Global enable interrupts
#asm("sei")
}

// Declare your global variables here

void main(void)
{
BYTE b1,b2,b3,b4,checksum;
Init();

ViTriLed=1;
DiemGui=5;


while (1)
{
b1=(DiemGui%10)+0x30;
b2=(DiemGui/10)+0x30;
b3=(ViTriLed%10)+0x30;
b4=(ViTriLed/10)+0x30;
checksum=b1^b2^b3^b4;
putchar('B');
putchar(b1);
putchar(b2);
putchar(b3);
putchar(b4);
putchar(checksum);

};
}


//This's Received Data Routine:

BYTE buffer[10];

void main()
{
BYTE i,crc8;

Init();

//received 5-Bytes
for(i=0;i<5;i++) crc8=crc8_calc(buffer, 5);

while(1)
{


}

}


BYTE crc8_calc(BYTE *byt, WORD size )
{
/* Calculate CRC-8 value; uses The CCITT-8 polynomial,
expressed as X^8 + X^5 + X^4 + 1 */

BYTE crc = (BYTE) 0xff;
WORD index;
BYTE b;

for( index=0; index<size; index++)
{
crc ^= byt[index];
for( b=0; b<8; ++b )
{
if( crc & 0x80 )
crc = (crc << 1) ^ 0x31;
else
crc = (crc << 1);
}
}
return crc;
}

Pls anybody explain how is CRC8 work...?
Thanks all!
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top