Mani_91
Newbie level 3
- Joined
- Feb 1, 2013
- Messages
- 4
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,310
Hello...!
I got stuck in a problem if anyone here can help me..!
I am unable to pass array to the function as if i don't use this way the program becomes too big so if any of you have command over it i will be so thankful to u...! My work is on PIC18F452 and here is its mikroC code
I got stuck in a problem if anyone here can help me..!
I am unable to pass array to the function as if i don't use this way the program becomes too big so if any of you have command over it i will be so thankful to u...! My work is on PIC18F452 and here is its mikroC code
Code:
// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
void Lcd_Chr_Cp(char);
// End LCD module connections
unsigned int CRC_L_R, CRC_H_R; //variables to mask Received CRC
unsigned char CRC_L_S, CRC_H_S; // variables to mask CRC to be send
unsigned char SlaveId, FunctionCode, Data_H, Data_L, loop_Out, loop_Inner, i, Check; //Other variables
unsigned char Print1 [] = "INVALID REQUEST"; //Display in case of Wrong CRC or Slave id mismatch or Functioncode mismatch
unsigned char Cal[6]; // Register to store received 6 data bytes
unsigned int CRC_Rec_R,CRC_Rec,CRC_Cal,CRC_Cal_R; //Registers for saving and reversing CRC
unsigned char Temp=100,Humidity=100,Temp_Pre=155,Humid_Pre=120; //Variables shows Present teperature Humidity and their setpoint
unsigned int reverseBits(unsigned int num);
unsigned int Received_CRC_FETCH(unsigned char* A[2]);
unsigned int Calculator(unsigned char *C[4]);
unsigned char Tester(unsigned char* Check1,unsigned char* Check2,unsigned char* B[4]);
unsigned int reverseBits(unsigned int num) //Function work for Bitreversal
{
unsigned int NO_OF_BITS = sizeof(num) * 8;
unsigned int reverse_num = 0;
for (i = 0; i < NO_OF_BITS; i++)
{
if((num & (1 << i)))
reverse_num |= 1 << ((NO_OF_BITS - 1) - i);
}
return reverse_num;
}
unsigned int Received_CRC_FETCH(unsigned char * A) { // Function to interpret the CRC from Bit reversed CRC
CRC_L_R = A[0] & 0x00FF ;
CRC_L_R = CRC_L_R << 8 ;
CRC_H_R=0x00FF & A[1];
CRC_Rec_R=CRC_L_R|CRC_H_R;
CRC_Rec = reverseBits(CRC_REC_R);
}
*unsigned int Calculator(unsigned char * C) //Work to calculate CRC Using SlaveId,FunctionCode and two Data bytes
{
unsigned int CRC = 0xFFFF;
for (loop_Out=0;loop_Out<4;loop_Out++)
{
CRC = CRC ^ C[Loop_Out];
CRC = CRC >> 1;
for (loop_Inner = 0; loop_Inner < 7; loop_Inner++)
{
if ( ((CRC & 0x0001) ^ 0x0001) ==1 ) //This condition checks Th LSB either it is 0 or 1
{
CRC = CRC ^ 0xA001;
CRC = CRC >> 1;
}
else
{
CRC = CRC >> 1;
} }
}
return CRC; } */
//Function to tackle with data corruption and SlaveId + FunctionCode
unsigned char Tester(unsigned char * B)
{
if ((B[0]==0x01)&(B[1]==0x01)) {
CRC_Cal = Calculator(&B[0],&B[1],&B[2],&B[3]);
CRC_Rec = Received_CRC_FETCH(&B[4],&B[5]); //Returns 1 to Check if Function code is 1 and
if (CRC_Cal==CRC_Rec) return 1 ; // This FuntionCode is for setting point for Temperature
else return 0; } // and Humidity
else if ((B[0]==0x01)&(B[1]==0x00)) {
CRC_Cal = Calculator(&B[0]); //Returns 2 to Check if Function code is 2 and
CRC_Rec = Received_CRC_FETCH(&B[4],&B[5]); //This function code is for getting data from MCU
if (CRC_Cal==CRC_Rec) return 2; }
else return 0;
}
void main() {
UART1_Init(9600); // Initialize UART module at 9600 bps
Delay_ms(100); // Wait for UART module to stabilize
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
while (1) {
for(i=0;i<6;i++) { //Receives 6 Bytes SlaID,FuncCode,DataH,DataL,CRC (2BYTES) MSB<-------->LSB
if (UART1_Data_Ready()) { // If data is received,
Cal[i] = UART1_Read(); // read the received data,
UART1_Write(Cal[i]);
Lcd_Chr_Cp(Cal[i]); }
}
Check = Tester(Cal); //TEST the error and Slaveid+function code
if(Check==0) { //If Error and Invalid slaveId or Funtion
Lcd_Chr_Cp(Print1[]);
}
if(Check==2) { //Sends data means current temperature and Humidity status to HMI
CRC_Cal = Calculator(0x00,0x00,Temp_Pre,Humid_Pre);
CRC_Cal_R = reverseBits(CRC_Cal); //Making it ready as CRC is send in Reversed order || MSB leaves first
CRC_H_S = ((CRC_Cal_R & 0xFF00) >> 8 ; //Break it into 8-bit or 8 byte using masking
CRC_L_S = ( CRC_Cal_R & 0x00FF );
if (UART1_Tx_Idle()) { // If Transmission line is clear
UART1_Write(0x00); // Data packets holding Temp and Humidity and 0 in place of Slave id and FunctionCode
UART1_Write(0x00);
UART1_Write(Temp_Pre);
UART1_Write(Humid_Pre);
UART1_Write(CRC_H_S); //Transmission of bitreversed CRC
UART1_Write(CRC_L_S); }
}
else { //If tester function returns 1 then take Set point
Temp = Cal[2];
Humidity = Cal[3];
}
delay_ms(500);
}
}
Attachments
Last edited by a moderator: