Convert C program from MPLAB to Micro C 8.2.0.0

General discussion on mikroC.
Post Reply
Author
Message
mrerdy
Posts: 1
Joined: 04 Feb 2010 16:22

Convert C program from MPLAB to Micro C 8.2.0.0

#1 Post by mrerdy » 04 Feb 2010 16:30

hye..gud day to all
i have problem to understand how to understand coding C from MPLAB because i want to build it using Micro C,can anyone help me please..



// Project :(Keypad door security)
// Project description :PIC18F877A + 4x3 keypad + LCD are used to build a keypad door
// security system which will activate the relay and buzzer after
// a preset 6-digit password is entered.
// LCD will display ****** when keypad is pressed.
// preset password for this program is 123456
//========================================================================================


//========================================================================================
// include
//=========================================================================================
#include <pic.h>

//=========================================================================================
// configuration
//=========================================================================================
__CONFIG ( 0x3F32 );

//==========================================================================================
// define
//==========================================================================================
#define rs RC0
#define e RC1
#define led_red RC3
#define led_yellow RC2
#define lcd_data PORTD
#define relay RB1
#define buzzer RB2

//===========================================================================================
// function prototype
//===========================================================================================
void delay(unsigned long data);
void send_config(unsigned char data);
void send_char(unsigned char data);
void e_pulse(void);
void lcd_goto(unsigned char data);
void lcd_clr(void);
void send_string(const char *s);
void clearcolumn1(void);
void clearcolumn2(void);
void clearcolumn3(void);
void clearcolumn4(void);
void scanrow1(void);
void scanrow2(void);
void scanrow3(void);
void scanrow4(void);
void beep_once(void);
void beep_twice(void);

//============================================================================================
// global variable
//============================================================================================
unsigned char password_count=0;
unsigned char keyin_char[6]; // Declare an array to stall the 6-digit key in password
unsigned char stalled_char[6]="123456"; // Declare an array to stall the 6-digit desired password

//============================================================================================
// main function
//============================================================================================
void main(void)
{
ADCON1=0b00000110; //set all portA pins as digital I/O
TRISA=0b11001111; //clear bit 4&5 portA as output and set the rest as input
TRISB=0b00000000; //set portB as output
TRISD=0b00000000; //set portD as output
TRISC=0b11110000; //set bit4-7 portC as input(connected to 4 row of keypad)
TRISE=0b00000000; //set portE as output

PORTC=0;
PORTD=0;
relay=0;
buzzer=0;
led_yellow=0;
led_red=0;

send_config(0b00001001); //clear display at lcd
send_config(0b00000010); //Lcd Return to home
send_config(0b00000110); //entry mode-cursor increase 1
send_config(0b00001100); //diplay on, cursor off and cursor blink off
send_config(0b00111000); //function

lcd_clr();
delay(1000);
lcd_goto(0); //initial display
send_string("PLEASE ENTER");
lcd_goto(20);
send_string("6-DIGIT PASSWORD");


while(1)
{ //keypad scanning algorithm
clearcolumn1(); //Clear 1st output pin and set the others
scanrow1(); //scan row
clearcolumn2(); //Clear 2nd output pin and set the others
scanrow2(); //scan row
clearcolumn3(); //Clear 3rd output pin and set the others
scanrow3(); //scan row
clearcolumn4(); //Clear 4th output pin and set the others
scanrow4(); //scan row

if(password_count==6)
{
password_count=0;
if((keyin_char[0]==stalled_char[0])&&(keyin_char[1]==stalled_char[1])&&
(keyin_char[2]==stalled_char[2])&&(keyin_char[3]==stalled_char[3])&&
(keyin_char[4]==stalled_char[4])&&(keyin_char[5]==stalled_char[5])) //compare the keyin value with stalled value to test whether password is correct
{
lcd_clr(); //clear lcd
lcd_goto(0);
send_string("SUCCESS!"); //display SUCCESS
led_yellow=1; //green light on
relay=1; //relay on
beep_once(); //beep one time
while(1); //infinity loop
}
else
{
lcd_clr(); //clear lcd
lcd_goto(0);
send_string("ERROR!"); //display ERROR!
led_red=1; //red light on
beep_twice(); //beep two time
while(1); //infinity loop
}
}
}
}

//=======================================================================================
// scanning functions
//=======================================================================================
void clearcolumn1(void) //clear the 1st column and set the others
{
RE1=0; //RE1,RE0, RA5 and RA4 are the output pins from PIC which connect to 4 pins of keypad
RE0=1;
RA5=1;
RA4=1;
}

void clearcolumn2(void) //clear the 2nd column and set the others
{
RE1=1;
RE0=0;
RA5=1;
RA4=1;
}

void clearcolumn3(void) //clear the 3rd column and set the others
{
RE1=1;
RE0=1;
RA5=0;
RA4=1;
}

void clearcolumn4(void) //clear the 4th column and set the others
{
RE1=1;
RE0=1;
RA5=1;
RA4=0;
}

void scanrow1(void)
{
if(RA3==0) //if key '#' is being pressed
{
while(RA3==0)continue; //waiting the key to be released
if(password_count==0)lcd_clr(); //Clear the LCD if the key is the 1st password
lcd_goto(password_count); //The cursor of LCD points to the column equivalent to the value of password_count variable
send_char('*'); //Display the symbol '*' at LCD
keyin_char[password_count]='#'; //Stall the '#' value at the keyin_char array
password_count+=1; //increase the Password_count variable's value by 1 and the result stall back to the variable
}

}

void scanrow2(void)
{
if(RA3==0) //if key '3' is being pressed
{
while(RA3==0)continue; //waiting the key to be released
if(password_count==0)lcd_clr(); //Clear the LCD if the key is the 1st password
lcd_goto(password_count); //The cursor of LCD points to the column equivalent to the value of password_count variable
send_char('*'); //Display the symbol '*' at LCD
keyin_char[password_count]='3'; //Stall the '3' value at the keyin_char array
password_count+=1; //increase the Password_count variable's value by 1 and the result stall back to the variable
}
else if(RA2==0) //if key '2' is being pressed
{
while(RA2==0)continue; //waiting the key to be released
if(password_count==0)lcd_clr(); //Clear the LCD if the key is the 1st password
lcd_goto(password_count); //The cursor of LCD points to the column equivalent to the value of password_count variable
send_char('*'); //Display the symbol '*' at LCD
keyin_char[password_count]='2'; //Stall the '2' value at the keyin_char array
password_count+=1; //increase the Password_count variable's value by 1 and the result stall back to the variable
}
else if(RA1==0) //if key '1' is being pressed
{
while(RA1==0)continue; //waiting the key to be released
if(password_count==0)lcd_clr(); //Clear the LCD if the key is the 1st password
lcd_goto(password_count); //The cursor of LCD points to the column equivalent to the value of password_count variable
send_char('*'); //Display the symbol '*' at LCD
keyin_char[password_count]='1'; //Stall the '1' value at the keyin_char array
password_count+=1; //increase the Password_count variable's value by 1 and the result stall back to the variable
}
else if(RA0==0) //if key '0' is being pressed
{
while(RA0==0)continue; //waiting the key to be released
if(password_count==0)lcd_clr(); //Clear the LCD if the key is the 1st password
lcd_goto(password_count); //The cursor of LCD points to the column equivalent to the value of password_count variable
send_char('*'); //Display the symbol '*' at LCD
keyin_char[password_count]='0'; //Stall the '0' value at the keyin_char array
password_count+=1; //increase the Password_count variable's value by 1 and the result stall back to the variable
}
}

void scanrow3(void)
{
if(RA3==0) //if key '7' is being pressed
{
while(RA3==0)continue; //waiting the key to be released
if(password_count==0)lcd_clr(); //Clear the LCD if the key is the 1st password
lcd_goto(password_count); //The cursor of LCD points to the column equivalent to the value of password_count variable
send_char('*'); //Display the symbol '*' at LCD
keyin_char[password_count]='7'; //Stall the '7' value at the keyin_char array
password_count+=1; //increase the Password_count variable's value by 1 and the result stall back to the variable
}
else if(RA2==0) //if key '6' is being pressed
{
while(RA2==0)continue; //waiting the key to be released
if(password_count==0)lcd_clr(); //Clear the LCD if the key is the 1st password
lcd_goto(password_count); //The cursor of LCD points to the column equivalent to the value of password_count variable
send_char('*'); //Display the symbol '*' at LCD
keyin_char[password_count]='6'; //Stall the '6' value at the keyin_char array
password_count+=1; //increase the Password_count variable's value by 1 and the result stall back to the variable
}
else if(RA1==0) //if key '5' is being pressed
{
while(RA1==0)continue; //waiting the key to be released
if(password_count==0)lcd_clr(); //Clear the LCD if the key is the 1st password
lcd_goto(password_count); //The cursor of LCD points to the column equivalent to the value of password_count variable
send_char('*'); //Display the symbol '*' at LCD
keyin_char[password_count]='5'; //Stall the '5' value at the keyin_char array
password_count+=1; //increase the Password_count variable's value by 1 and the result stall back to the variable
}
else if(RA0==0) //if key '4' is being pressed
{
while(RA0==0)continue; //waiting the key to be released
if(password_count==0)lcd_clr(); //Clear the LCD if the key is the 1st password
lcd_goto(password_count); //The cursor of LCD points to the column equivalent to the value of password_count variable
send_char('*'); //Display the symbol '*' at LCD
keyin_char[password_count]='4'; //Stall the '4' value at the keyin_char array
password_count+=1; //increase the Password_count variable's value by 1 and the result stall back to the variable
}
}

void scanrow4(void)
{

if(RA1==0) //if key '9' is being pressed
{
while(RA1==0)continue; //waiting the key to be released
if(password_count==0)lcd_clr(); //Clear the LCD if the key is the 1st password
lcd_goto(password_count); //The cursor of LCD points to the column equivalent to the value of password_count variable
send_char('*'); //Display the symbol '*' at LCD
keyin_char[password_count]='9'; //Stall the '9' value at the keyin_char array
password_count+=1; //increase the Password_count variable's value by 1 and the result stall back to the variable
}
else if(RA0==0) //if key '8' is being pressed
{
while(RA0==0)continue; //waiting the key to be released
if(password_count==0)lcd_clr(); //Clear the LCD if the key is the 1st password
lcd_goto(password_count); //The cursor of LCD points to the column equivalent to the value of password_count variable
send_char('*'); //Display the symbol '*' at LCD
keyin_char[password_count]='8'; //Stall the '8' value at the keyin_char array
password_count+=1; //increase the Password_count variable's value by 1 and the result stall back to the variable
}

}

//===============================================================================================
// General Purpose functions
//===============================================================================================
void delay(unsigned long data)
{
for( ;data>0;data-=1);
}

void beep_once(void)
{
buzzer=1; //buzzer on
delay(8000);
buzzer=0; //buzzer off
}

void beep_twice(void)
{
buzzer=1; //buzzer on
delay(8000);
buzzer=0; //buzzer off
delay(13000);
buzzer=1; //buzzer on
delay(8000);
buzzer=0; //buzzer off
}

//========================================================================================
// LCD functions
//========================================================================================
void send_config(unsigned char data)
{
rs=0; //clear rs into config mode
lcd_data=data;
delay(50);
e_pulse();
}

void send_char(unsigned char data)
{
rs=1; //set rs into write mode
lcd_data=data;
delay(50);
e_pulse();
}

void e_pulse(void)
{
e=1;
delay(50);
e=0;
delay(50);
}

void lcd_goto(unsigned char data)
{
if(data<16)
{
send_config(0x80+data);
}
else
{
data=data-20;
send_config(0xc0+data);
}
}

void lcd_clr(void)
{
send_config(0x01);
delay(50);
}

void send_string(const char *s)
{
unsigned char i=0;
while (s && *s)send_char (*s++);

}
[/code]

User avatar
tihomir.losic
mikroElektronika team
Posts: 2138
Joined: 02 Dec 2009 14:16
Location: Serbia
Contact:

#2 Post by tihomir.losic » 05 Feb 2010 13:45

Hello,

first of all, I recommend you our new version of mikroC, the mikroC PRO for the PIC (http://www.mikroe.com/en/compilers/mikroc/pro/pic/).
Fully functional free Demo version of mikroC PRO for PIC 2009 is available for download (http://www.mikroe.com/download/c_pic_pro.php).
If you have registered mikroC, upgrade to new version is free.

In mikroC PRO:
- you don't need #include<pic.h>
- configuration bits you can set in compiler, in edit project (path is Project -> Edit Project or shortcut: Ctrl + Shift + E)
- I saw that you are using the commands for the LCD. Our compiler contains several different libraries. One of them is LCD library:
The mikroC PRO for PIC provides a library for communication with Lcds (with HD44780 compliant controllers) through the 4-bit interface.
Examples for using LCD display you can see at the our help file in mikroC PRO.
- RE1=0, RE0=1, etc... redefine to RE1_bit=0, RE0_bit=1, etc... (for PRO version)

I hope it helps, for any additional question, create support ticket:
http://www.mikroe.com/en/support and feel free to contact me.

Thanks.

Best regards,

Losic Tihomir
mikroElektronika [Support team]

chingfongkee
Posts: 13
Joined: 31 Jul 2010 14:35

Re: Convert C program from MPLAB to Micro C 8.2.0.0

#3 Post by chingfongkee » 15 Jan 2012 10:54

so means the mikroc pro can convert the coding from MPLAB to mikroc?

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Convert C program from MPLAB to Micro C 8.2.0.0

#4 Post by filip » 16 Jan 2012 12:07

Hi,

There is no straightforward procedure for this, you will need to do as tihomir.losic suggested earlier.

Regards,
Filip.

Post Reply

Return to “mikroC General”