/Designs/MRAKOMER2/SW/Ondrejov/teplomer/irmrak.h |
---|
0,0 → 1,5 |
#include <16F88.h> |
#device adc=8 |
#fuses NOWDT,INTRC_IO, NOPUT, NOMCLR, NOBROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG, NOPROTECT, NOFCMEN, NOIESO, CCPB3 |
#use delay(clock=8000000) |
/Designs/MRAKOMER2/SW/Ondrejov/teplomer/irmrak4.PJT |
---|
0,0 → 1,45 |
[PROJECT] |
Target=irmrak4.HEX |
Development_Mode=2 |
Processor=0x688F |
Processor_Text=PIC16F88 |
ToolSuite=CCS |
[Directories] |
Include= |
Library= |
LinkerScript= |
[Target Data] |
FileList=C:\Dokumenty\MLAB\Designs\MRAKOMER2\SW\Ondrejov\teplomer\irmrak4.c |
BuildTool=C-COMPILER |
OptionString=+FM |
AdditionalOptionString= |
BuildRequired=1 |
[irmrak4.c] |
Type=4 |
Path= |
FileList= |
BuildTool= |
OptionString= |
AdditionalOptionString= |
[mru-list] |
1=irmrak4.c |
[Windows] |
0=0000 irmrak4.c 0 0 796 451 3 0 |
[Opened Files] |
1=irmrak4.c |
2=smb.c |
3=irmrak4.h |
4=..\..\..\..\..\..\..\Program Files\PICC\devices\16F88.h |
5=..\..\..\..\..\..\..\Program Files\PICC\drivers\string.h |
6=..\..\..\..\..\..\..\Program Files\PICC\drivers\stddef.h |
7=..\..\..\..\..\..\..\Program Files\PICC\drivers\ctype.h |
8= |
[Units] |
Count=1 |
1=irmrak4 (main) |
/Designs/MRAKOMER2/SW/Ondrejov/teplomer/irmrak4.c |
---|
0,0 → 1,148 |
/**** IR Mrakomer - special version for BART ****/ |
#define VERSION "2.2" |
#define ID "$Id: irmrak4.c 1306 2009-01-17 12:25:40Z kakl $" |
#include "irmrak4.h" |
#bit CREN = 0x18.4 // USART registers |
#bit SPEN = 0x18.7 |
#bit OERR = 0x18.1 |
#bit FERR = 0x18.2 |
#include <string.h> |
#CASE // Case sensitive compiler |
#define MAXHEAT 30 // Doba po kterou se topi v [s] |
#define HEATING PIN_B3 // Heating for defrosting |
char VER[4]=VERSION; // Buffer for concatenate of a version string |
int8 heat; // Status variables |
void delay(int16 cycles) // Vlastni pauza, zbylo to z ovladani fototranzistoru z MM4 |
{ |
int16 i; |
for(i=0; i<cycles; i++) {delay_us(100);} |
} |
void welcome(void) // Welcome message |
{ |
char REV[50]=ID; // Buffer for concatenate of a version string |
if (REV[strlen(REV)-1]=='$') REV[strlen(REV)-1]=0; |
printf("\n\r\n\r# Mrakomer %s (C) 2007 KAKL\n\r",VER); // Welcome message |
printf("#%s\n\r",&REV[4]); |
printf("#\n\r"); |
printf("# h - Switch On Heating for 30s.\n\r"); |
printf("# f - Freezing. Switch Off Heating.\n\r"); |
printf("# i - Print this Information.\n\r"); |
printf("# 0..9 - Single measure at given angle.\n\r"); |
printf("# m - Measure at three space points.\n\r"); |
printf("#\n\r"); |
printf("$<Angle> <Ambient Temperature> <Space Temperature> ..."); |
printf("\n\r\n\r"); |
//---WDT |
restart_wdt(); |
} |
#include "smb.c" // System Management Bus driver |
// Read sensor's RAM |
// Returns temperature in °K |
int16 ReadTemp(int8 addr, int8 select) |
{ |
unsigned char arr[6]; // Buffer for the sent bytes |
int8 crc; // Readed CRC |
int16 temp; // Readed temperature |
addr<<=1; |
SMB_STOP_bit(); //If slave send NACK stop comunication |
SMB_START_bit(); //Start condition |
SMB_TX_byte(addr); |
SMB_TX_byte(RAM_Access|select); |
SMB_START_bit(); //Repeated Start condition |
SMB_TX_byte(addr); |
arr[2]=SMB_RX_byte(ACK); //Read low data,master must send ACK |
arr[1]=SMB_RX_byte(ACK); //Read high data,master must send ACK |
temp=make16(arr[1],arr[2]); |
crc=SMB_RX_byte(NACK); //Read PEC byte, master must send NACK |
SMB_STOP_bit(); //Stop condition |
arr[5]=addr; |
arr[4]=RAM_Access|select; |
arr[3]=addr; |
arr[0]=0; |
if (crc != PEC_calculation(arr)) temp=0; // Calculate and check CRC |
return temp; |
} |
/*-------------------------------- MAIN --------------------------------------*/ |
void main() |
{ |
unsigned int16 timer, temp, tempa; |
signed int16 ta, to; |
output_low(HEATING); // Heating off |
delay_ms(500); |
restart_wdt(); |
heat=0; |
timer=0; |
welcome(); |
tempa=ReadTemp(SA, RAM_Tamb); // Dummy read |
temp=ReadTemp(SA, RAM_Tobj1); |
delay_ms(500); |
while(TRUE) // Main Loop |
{ |
char ch; |
//---WDT |
restart_wdt(); |
if(kbhit()) |
{ |
ch=getc(); // Precti znak od radice motoru |
CREN=0; CREN=1; // Reinitialise USART |
tempa=ReadTemp(SA, RAM_Tamb); // Read temperatures from sensor |
temp=ReadTemp(SA, RAM_Tobj1); |
ta=tempa*2-27315; // °K -> °C |
to=temp*2-27315; |
switch (ch) |
{ |
case 'H': |
heat=MAXHEAT; // Need heating |
break; |
case 'F': |
heat=0; // Freeze |
break; |
case 'I': |
welcome(); // Information about version, etc... |
break; |
} |
printf("%c %Ld %Ld ", ch, ta, to); |
if (('A'!=ch)&&('B'!=ch)&&('C'!=ch)&&('S'!=ch)) printf("H %u\r\n", heat); // Vzdycky se konci natocenim na Ground |
} |
delay_ms(1); |
if (timer>0) {timer--;} else {timer=1000;} |
if (heat>0) {if(1000==timer) heat--; output_high(HEATING);} else { output_low(HEATING); } |
} |
} |
/Designs/MRAKOMER2/SW/Ondrejov/teplomer/irmrak4.h |
---|
0,0 → 1,19 |
#include <16F88.h> |
#device adc=8 |
#FUSES NOWDT //!!! //Watch Dog Timer |
#FUSES INTRC_IO //Internal RC Osc, no CLKOUT |
#FUSES PUT //Power Up Timer |
#FUSES NOMCLR //Master Clear pin used for I/O |
#FUSES NOBROWNOUT //No brownout reset |
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O |
#FUSES NOCPD //No EE protection |
#FUSES NOWRT //Program memory not write protected |
#FUSES NODEBUG //No Debug mode for ICD |
#FUSES NOPROTECT //Code not protected from reading |
#FUSES NOFCMEN //Fail-safe clock monitor disabled |
#FUSES NOIESO //Internal External Switch Over mode disabled |
#use delay(clock=8000000) |
#use rs232(baud=2400,parity=N,xmit=PIN_B5,rcv=PIN_B2,bits=8) |
/Designs/MRAKOMER2/SW/Ondrejov/teplomer/irmrak4.hex |
---|
0,0 → 1,182 |
:1000000004308A008D2C00008A068A0623104D39A0 |
:10001000E135EF366539A0127310A8212910321886 |
:10002000B01BA025C1254C050D0023106810201021 |
:10003000A016A029F734F43168104F372024E5309A |
:10004000F434EE3320336F39A019B0392E050D008A |
:10005000231066102010A0162023F232653D693768 |
:100060006717A029F734F43168104F336610C8328F |
:10007000613A693767178A0600012310691020105A |
:10008000A0162028F2346E3A203AE834731049372B |
:10009000E637F236613AE9376E178A060001231017 |
:1000A0003017AE1CA016A029693767366510ED32EF |
:1000B000E13975396510613AA033693B6537A03085 |
:1000C000EE33EC322E050D0023106D102010A0161B |
:1000D000A026E530F33AF232A03074107434F232D4 |
:1000E00065107338E1316510F0376937F4392E0542 |
:1000F0000D00241E41376736651F201EC136E234CD |
:1001000065377410D4326D386539613A7539651FB9 |
:10011000201E5338E1316510D4326D386539613AAB |
:100120007539651F20172E1700018A068A06000000 |
:1001300033308400831300080319AB280230F80021 |
:10014000F701F70BA128F80BA0289730F700F70B61 |
:10015000A728800B9E2808006508E7006408E600D1 |
:100160006708FA006608840083137A1883178008EA |
:100170000319BE28E60A0319E70AB02864086602D4 |
:10018000F7006708FA006508031C650FFA02770894 |
:10019000F8000800831603178C170C1400000000E9 |
:1001A0000318FA2883120C087F390313E50003179C |
:1001B0000D080313E60003170F080313E700650893 |
:1001C0000C1EE0289900660803178D0003136708CA |
:1001D00003178F0083168C170C1400000000831285 |
:1001E0000313E40BF428F72883160317FA281A29B7 |
:1001F0008316031783120C0D0E0D7F390313E500D0 |
:1002000003170D080313E60003170F080313E70095 |
:1002100065080C1E09299900660803178D00031351 |
:10022000670803178F008D0A03198F0A0310031341 |
:10023000E40BCA280800800803193129E5010408E5 |
:10024000E4006510831B651400080C1E2529990025 |
:1002500064088400831365188317840A0319850AC8 |
:100260001B290800831603178C170C1400000000CC |
:1002700083120C087F39031978290313E40003174C |
:100280000D080313E50003170F080313E6006408C5 |
:100290000C1E48299900650803178D000313660892 |
:1002A00003178F0083168C170C14000000008312B4 |
:1002B0000C0D0E0D7F39031978290313E400031781 |
:1002C0000D080313E50003170F080313E600640885 |
:1002D0000C1E68299900650803178D000313660832 |
:1002E00003178F008D0A03198F0A0313322903178E |
:1002F000031308002430B2004930B3006430B40066 |
:100300003A30B5002030B6006930B7007230B8001E |
:100310006D30B9007230BA006130BB006B30BC0088 |
:100320003430BD002E30BE006330BF002030C0002E |
:100330003130C1003330C2003030C3003630C40029 |
:100340002030C5003230C6003030C700C800393018 |
:10035000C9002D30CA003030CB003130CC002D30F8 |
:10036000CD003130CE003730CF002030D0003130DA |
:10037000D1003230D2003A30D3003230D4003530A0 |
:10038000D5003A30D6003430D7003030D8005A305B |
:10039000D9002030DA006B30DB006130DC006B30DC |
:1003A000DD006C30DE002030DF002430E000E101B1 |
:1003B000E5013230E400AC2001307802323E8400A6 |
:1003C00083130008243C031DEF29E5013230E400CB |
:1003D000AC2001307802323E840083138001043067 |
:1003E00003178D0000308F0003100F300313E4005B |
:1003F000CA202230840083131B210C3003178D0088 |
:1004000000308F00031410300313E400CA2023309F |
:100410000C1E082A99003630840083131B210A30F1 |
:100420000C1E102A99000D300C1E142A990023303E |
:100430000C1E182A99000A300C1E1C2A99000D3037 |
:100440000C1E202A9900153003178D0000308F00F4 |
:1004500003133221283003178D0000308F0003135F |
:1004600032213D3003178D0000308F0003133221FD |
:100470004F3003178D0000308F000313322164309A |
:1004800003178D0000308F000313322123300C1E20 |
:10049000472A99000A300C1E4B2A99000D300C1E79 |
:1004A0004F2A9900793003178D0000308F00031315 |
:1004B0003221953003178D0000308F000313322155 |
:1004C00064000800831686140610831206100D308F |
:1004D000F700F70B692A83168610831286100D30F9 |
:1004E000F700F70B712A831606140D30F700F70B8F |
:1004F000772A861483120800831686140D30F700BD |
:10050000F70B802A06140D30F700F70B852A8610AA |
:10051000831286100D30F700F70B8C2A8316061015 |
:10052000831206100D30F700F70B942A0800C2085A |
:10053000031D9F2A8316861083128610A22A831613 |
:1005400086148312A32AA42AA52A831606141A3015 |
:10055000F700F70BA92A0000061083120610B02A34 |
:100560004230F700F70BB22A0000B62A08008316C3 |
:10057000861406141A30F700F70BBC2A0000861404 |
:100580008312861CC62A0130C200C72AC201831604 |
:10059000061083120610CC2A4230F700F70BCE2A41 |
:1005A0000000D22A4208F80008000830BF00BF0847 |
:1005B0000319E72ABE1FDF2A0130C100E02AC1016A |
:1005C0004108C20097220310BE0DBF03D72AB722ED |
:1005D0007808C0004008F80008000830BF00BF08D5 |
:1005E0000319FF2AB722F8080319FA2A0310BE0DCF |
:1005F0003E14FD2A0310BE0D3E10BF03EF2A3D0836 |
:10060000C20097223E08F80008000310B20D6222D3 |
:100610007C223208BE00D5223308BD00BE00D522A0 |
:100620007C223208BE00D522BD01ED227808B6003A |
:10063000BD01ED227808B5003508BC003608BB00C6 |
:100640000130BD00ED227808BA0062223208B900FC |
:100650003308B8003208B700B401BE013430BD0021 |
:100660002F30C500C401C301C201C1010130C00067 |
:100670000730BF002F30C500C6010530C700C801D4 |
:1006800047083D07840083133E1883170008CA00FB |
:100690008030F7004808F8000319522B0310F70CBC |
:1006A000F80B4E2B77084A05031D642BC708031966 |
:1006B000642BC5034808063C031C602BC80A622B48 |
:1006C000C801C703632B402B08304502C600C6088B |
:1006D0000319902B0530C700470F6F2B8D2B01306E |
:1006E00047023F3E84008313801F7C2BC7080319F9 |
:1006F0007C2B0130C9007D2BC9013F3047078400A6 |
:1007000083130310000D80003F30470784008313DC |
:10071000490800078000C7036C2BC6038F2B672B8B |
:10072000C7014708053C031CB32B47083D07F800E9 |
:100730003E08FA000318FA0A7808840083137A182E |
:1007400083170008CC003F3047078400831300085C |
:100750004C06CC007808840083137A1883174C0861 |
:100760008000C70A912B4508083C031C322B3D082A |
:10077000840083133E1883170008F80078083A02B3 |
:100780000319C42BBC01BB013B08F8003C08F9006D |
:100790000800BA010408B9003A10831B3A14B31FC9 |
:1007A000D92BB917391EB90AB209B309B20A03190C |
:1007B000B30A330EF038B500B507E23EB600323E5C |
:1007C000B80033080F39B607B607B807E93EB700D7 |
:1007D000B707B707320E0F39B707B807B70DB80D0F |
:1007E000B809B80D32080F39B807B50D0730B40095 |
:1007F0000A30B807B703031CF92BB707B603031C6D |
:10080000FD2BB607B503031C012CB507B403031C6D |
:10081000052C3430840083130730390539138403E1 |
:100820003905031D1A2C391A840A391A1A2C20305A |
:10083000F700372C840738300402031939170008F1 |
:10084000F700031D2B2C391B2B2C391A3B2CB91903 |
:100850002B2C2030362CB91F332C2D30F70084037D |
:100860003913B913372CB91539123030F707770817 |
:100870000C1E382C9900840A391F1B2C08003508DF |
:10088000F80134020318472C3408F700532CF70101 |
:100890000830B600B40DF70D350877020318F700DD |
:1008A000F80DB60B4A2C080078083208B400643002 |
:1008B000B5003F247708B20078083030031D672C5C |
:1008C000B31C6F2CB3196F2C331A20306A2CB31160 |
:1008D00033123314F80778080C1E6C2C9900320878 |
:1008E000B4000A30B5003F247708B20078083030F1 |
:1008F000031D802CB319852C331C852C331A203012 |
:10090000F80778080C1E822C99003030B2073208A4 |
:100910000C1E882C99008A115F2D840183131F30CF |
:100920008305723083168F000F0833309900A23090 |
:10093000980090308312980083161F129F121B0894 |
:1009400080399B0007309C008312A001A101323046 |
:10095000A2002E30A3003230A400A5018316861118 |
:10096000831286110230B200FA30B3009820B20B25 |
:10097000B42C6400A601A801A7017A21B2010630B7 |
:10098000B30005237908AC007808AB00B20107304A |
:10099000B30005237908AA007808A9000230B20044 |
:1009A000FA30B3009820B20BD02C64008C1E672D57 |
:1009B0008C1ED82C1A08B10018121816B201063075 |
:1009C000B30005237908AC007808AB00B20107300A |
:1009D000B30005237908AA007808A90003102B0D9D |
:1009E000B2002C0DB300B3303202F7003308FA0026 |
:1009F0006A30031C6B30FA027708AD007A08AE004B |
:100A00000310290DB2002A0DB300B3303202F700F3 |
:100A10003308FA006A30031C6B30FA027708AF0023 |
:100A20007A08B0003108483A03191D2D0E3A03190F |
:100A3000202D0F3A0319222D242D1E30A600242D1F |
:100A4000A601242D7A21242D31080C1E252D990074 |
:100A500020300C1E292D9900103084002E08B30080 |
:100A60002D08B200C92320300C1E342D99001030FF |
:100A700084003008B3002F08B200C92320300C1EB8 |
:100A80003F2D99003108413C0319672D3108423C44 |
:100A90000319672D3108433C0319672D3108533C76 |
:100AA0000319672D48300C1E532D990020300C1E61 |
:100AB000572D99002608B2001B30B300542C0D307E |
:100AC0000C1E602D99000A300C1E642D9900013017 |
:100AD000B3009820A708031D702DA8080319752DD1 |
:100AE00027080319A803A703792D0330A800E830CD |
:100AF000A700A6080319892D2708E83C031D842DAB |
:100B00002808033C0319A603831686118312861551 |
:0E0B10008D2D8316861183128611D52C63005D |
:04400E00103FFC3F24 |
:00000001FF |
;PIC16F88 |
;CRC=FAA7 CREATED="21-VIII-10 08:50" |
/Designs/MRAKOMER2/SW/Ondrejov/teplomer/smb.c |
---|
0,0 → 1,261 |
/************ SMB driver ************/ |
#define SA 0x00 // Slave Address (0 for single slave / 0x5A<<1 default) |
#define RAM_Access 0x00 // RAM access command |
#define RAM_Tobj1 0x07 // To1 address in the RAM |
#define RAM_Tamb 0x06 // Ta address in the RAM |
// High and Low level of clock |
#define HIGHLEV 40 // max. 50us |
#define LOWLEV 100 // max. 30ms |
#define TBUF 20 |
// SMBus control signals |
#define SCL PIN_B0 |
#define SDA PIN_B1 |
#define mSDA_HIGH() output_float(SDA); // SDA float |
#define mSDA_LOW() output_low(SDA); // SDA low |
#define mSCL_HIGH() output_float(SCL); // SCL float |
#define mSCL_LOW() output_low(SCL); // SCL low |
#define ACK 0 |
#define NACK 1 |
//********************************************************************************************** |
// START CONDITION ON SMBus |
//********************************************************************************************** |
//Name: START_bit |
//Function: Generate START condition on SMBus |
//Parameters: No |
//Return: No |
//Comments: Refer to "System Managment BUS(SMBus) specification Version 2.0" |
// or AN"SMBus communication with MLX90614" on the website www.melexis.com |
//********************************************************************************************** |
void SMB_START_bit(void) |
{ |
// disable_interrupts(GLOBAL); |
mSDA_HIGH(); // Set SDA line |
delay_us( TBUF ); // Wait a few microseconds |
mSCL_HIGH(); // Set SCL line |
delay_us( TBUF ); // Generate bus free time between Stop |
// and Start condition (Tbuf=4.7us min) |
mSDA_LOW(); // Clear SDA line |
delay_us( TBUF ); // Hold time after (Repeated) Start Condition. |
// After this period, the first clock is generated. |
// (Thd:sta=4.0us min) |
mSCL_LOW(); // Clear SCL line |
// enable_interrupts(GLOBAL); |
delay_us( TBUF ); // Wait a few microseconds |
} |
//********************************************************************************************* |
// STOP CONDITION ON SMBus |
//********************************************************************************************* |
//Name: STOPbit |
//Function: Generate STOP condition on SMBus |
//Parameters: No |
//Return: No |
//Comments: Refer to "System Managment BUS(SMBus) specification Version 2.0" |
// or AN"SMBus communication with MLX90614" on the website www.melexis.com |
//********************************************************************************************* |
void SMB_STOP_bit(void) |
{ |
// disable_interrupts(GLOBAL); |
mSDA_HIGH(); |
mSCL_LOW(); // Clear SCL line |
delay_us( TBUF ); // Wait a few microseconds |
mSDA_LOW(); // Clear SDA line |
delay_us( TBUF ); // Wait a few microseconds |
mSCL_HIGH(); // Set SCL line |
delay_us( TBUF ); // Stop condition setup time(Tsu:sto=4.0us min) |
mSDA_HIGH(); // Set SDA line |
// enable_interrupts(GLOBAL); |
} |
void SMB_send_bit(unsigned char bit_out) |
{ |
// disable_interrupts(GLOBAL); |
if(bit_out==0) {mSDA_LOW();} |
else {mSDA_HIGH();} |
delay_us(3); |
mSCL_HIGH(); // Set SCL line |
delay_us( HIGHLEV ); // High Level of Clock Pulse |
mSCL_LOW(); // Clear SCL line |
//!!! toggle_dome(); |
delay_us(1); |
delay_us( LOWLEV ); // Low Level of Clock Pulse |
// mSDA_HIGH(); // Master release SDA line , |
// enable_interrupts(GLOBAL); |
//!!! toggle_dome(); |
delay_us(1); |
return; |
} |
unsigned char SMB_Receive_bit(void) |
{ |
unsigned char Ack_bit; |
// disable_interrupts(GLOBAL); |
mSDA_HIGH(); //_SDA_IO=1; // SDA-input |
mSCL_HIGH(); // Set SCL line |
delay_us( HIGHLEV ); // High Level of Clock Pulse |
if(input(SDA)) Ack_bit=1; // \ Read acknowledgment bit, save it in Ack_bit |
else Ack_bit=0; // / |
mSCL_LOW(); // Clear SCL line |
//!!! toggle_dome(); |
delay_us(1); |
delay_us( LOWLEV ); // Low Level of Clock Pulse |
// enable_interrupts(GLOBAL); |
//!!! toggle_dome(); |
delay_us(1); |
return Ack_bit; |
} |
//********************************************************************************************* |
// TRANSMIT DATA ON SMBus |
//********************************************************************************************* |
//Name: TX_byte |
//Function: Send a byte on SMBus |
//Parameters: TX_buffer ( the byte which will be send on the SMBus ) |
//Return: Ack_bit ( acknowledgment bit ) |
//Comments: Sends MSbit first |
//********************************************************************************************* |
unsigned char SMB_TX_byte(unsigned char Tx_buffer) |
{ |
unsigned char Bit_counter; |
unsigned char Ack_bit; |
unsigned char bit_out; |
for(Bit_counter=8; Bit_counter; Bit_counter--) |
{ |
if(Tx_buffer&0x80) bit_out=1; // If the current bit of Tx_buffer is 1 set bit_out |
else bit_out=0; // else clear bit_out |
SMB_send_bit(bit_out); // Send the current bit on SDA |
Tx_buffer<<=1; // Get next bit for checking |
} |
Ack_bit=SMB_Receive_bit(); // Get acknowledgment bit |
return Ack_bit; |
} |
//********************************************************************************************* |
// RECEIVE DATA ON SMBus |
//********************************************************************************************* |
//Name: RX_byte |
//Function: Receive a byte on SMBus |
//Parameters: ack_nack (ackowlegment bit) |
//Return: RX_buffer(Received byte) |
//Comments: MSbit is received first |
//********************************************************************************************* |
unsigned char SMB_RX_byte(unsigned char ack_nack) |
{ |
unsigned char RX_buffer; |
unsigned char Bit_Counter; |
for(Bit_Counter=8; Bit_Counter; Bit_Counter--) |
{ |
if(SMB_Receive_bit()) // Get a bit from the SDA line |
{ |
RX_buffer <<= 1; // If the bit is HIGH save 1 in RX_buffer |
RX_buffer |=0b00000001; |
} |
else |
{ |
RX_buffer <<= 1; // If the bit is LOW save 0 in RX_buffer |
RX_buffer &=0b11111110; |
} |
} |
SMB_send_bit(ack_nack); // Sends acknowledgment bit |
return RX_buffer; |
} |
unsigned char PEC_calculation(unsigned char pec[]) // CRC calculation |
{ |
unsigned char crc[6]; |
unsigned char BitPosition=47; |
unsigned char shift; |
unsigned char i; |
unsigned char j; |
unsigned char temp; |
do |
{ |
crc[5]=0; /* Load CRC value 0x000000000107 */ |
crc[4]=0; |
crc[3]=0; |
crc[2]=0; |
crc[1]=0x01; |
crc[0]=0x07; |
BitPosition=47; /* Set maximum bit position at 47 */ |
shift=0; |
//Find first 1 in the transmited message |
i=5; /* Set highest index */ |
j=0; |
while((pec[i]&(0x80>>j))==0 && i>0) |
{ |
BitPosition--; |
if(j<7) |
{ |
j++; |
} |
else |
{ |
j=0x00; |
i--; |
} |
//!!! toggle_dome(); |
delay_us(1); |
} |
shift=BitPosition-8; /*Get shift value for crc value*/ |
//Shift crc value |
while(shift) |
{ |
for(i=5; i<0xFF; i--) |
{ |
if((crc[i-1]&0x80) && (i>0)) |
{ |
temp=1; |
} |
else |
{ |
temp=0; |
} |
crc[i]<<=1; |
crc[i]+=temp; |
} |
shift--; |
//!!! toggle_dome(); |
delay_us(1); |
} |
//Exclusive OR between pec and crc |
for(i=0; i<=5; i++) |
{ |
pec[i] ^=crc[i]; |
} |
} while(BitPosition>8); |
return pec[0]; |
} |