Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics 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.

This is a program for PIC16F877A .How can i run this program in MPLAB?PLS details.

Status
Not open for further replies.

HerohetoChakma

Junior Member level 3
Junior Member level 3
Joined
Apr 19, 2013
Messages
30
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
Chittagong, Bangladesh.
herohetoc.webs.com
Activity points
2,338
#include <pic.h>
#include <math.h>
#include "config.h"
//===============configuration==============================
__CONFIG (0x3F32);
//===============define IO port=============================
#define lcd PORTD
#define RS RB4
#define E RB5
#define led1 RB6
#define redled RB7
#define CHANNEL0 0b10000001
#define CHANNEL1 0b10001001
#define buzzer RB2
#define orangeled RB3
#define greenled RB0
//==============FUNCTION PTOTOTYPE=========================
void e_pulse(void);
void delay(unsigned short i);
void send_char(unsigned char data);
void send_string(const char *s);
void send_config(unsigned char data);
void lcd_goto(unsigned char data);
void lcd_clr(void);
void dis_num(unsigned long data);
void increment(unsigned long data);
void read_adc(void);
unsigned short read_temp(void);
void blink_LED1(unsigned char n);
void blink_redled(unsigned char n);
void blink_orangeled(unsigned char n);
void clearrow1(void);
void clearrow2(void);
void clearrow3(void);
void clearrow4(void);
void scancolumn1(void);
void scancolumn2(void);
void scancolumn3(void);
void scancolumn4(void);
void beep_once(void);
//====================MAIN================================
unsigned short result;
unsigned short temp;
float tempB,tempA;
unsigned char keyin[3];
unsigned char num_count=0;
void main(void)
{
ADRESH=0;
ADRESL=0;

ADCON1=0b11000000;
TRISA=0b11111111;
TRISB=0b00000000;
TRISC=0b00001111;
TRISD=0b00000000;
TRISE=0b00000000;
PORTA=0;
PORTB=0;

CCP1CON==0b00001100;
PR2=0xFF;
T2CON=0b00000101;
CCPR1L=0;

send_config(0b00000001);
send_config(0b00000010);
send_config(0b00000110);
send_config(0b00001100);
send_config(0b00111000);
lcd_clr();
delay(1000);
lcd_goto(0);
send_string("PLEASE ENTER");
lcd_goto(20);
send_string("2 DIGIT SPEC");
while(1)
{
clearrow1();
scancolumn1();
clearrow2();
scancolumn2();
clearrow3();
scancolumn3();
clearrow4();
scancolumn4();
if(num_count==2)
{
delay(8000);
lcd_goto(0);
//display character on LCD
send_char(' ');
send_char('S');
send_char('P');
send_char('E');
send_char('C');
send_char('.');
send_char('.');
send_char('=');
lcd_goto(20);
//display character on LCD
send_char(' ');
send_char('M');
send_char('E');
send_char('A');
send_char('S');
send_char('U');
send_char('R');
send_char('E');
send_char('D');
send_char('=');
while(1)
{
//keypad value
ADCON0=CHANNEL1;
lcd_goto(9);
keyin[2]=0;
tempA=(keyin[0]-0x30)*10+(keyin[1]-0x30);
dis_num((unsigned long)round(tempA));
send_char(0b11011111);
send_char('C');
send_char(' ');
send_char(' ');
//sensor B the LM35 sensor
ADCON0=CHANNEL0;
lcd_goto(31);
read_adc();
temp=read_temp();
tempB=(((float)temp)*0.48876);
dis_num((unsigned long)round(tempB));
send_char(0b11011111);
send_char('C');
send_char(' ');
send_char(' ');
if(tempB<tempA)
{
if((tempB+5)>=tempA)
{
led1=1;
blink_LED1(10);
greenled=1;
orangeled=1;
blink_orangeled(3);
redled=0;
buzzer=0;
}
else
{
led1=1;
blink_LED1(10);
greenled=1;
orangeled=0;
redled=0;
buzzer=0;
}
}
else if(tempB>tempA)
{
led1=1;

blink_LED1(3);
greenled=0;
orangeled=1;
blink_orangeled(3);
redled=1;

blink_redled(3);
buzzer=1;
beep_once();
}
delay(2000);
}
}
}
}
//=============================================================================
// Keypad scanning functions
//=============================================================================
void clearrow1(void)
{
RC7=0;
RC6=1;
RC5=1;
RC4=1;
}
void clearrow2(void)
{
RC7=1;
RC6=0;
RC5=1;
RC4=1;
}
void clearrow3(void)
{
RC7=1;
RC6=1;
RC5=0;
RC4=1;
}
void clearrow4(void)
{
RC7=1;
RC6=1;
RC5=1;
RC4=0;
}
void scancolumn1(void)
{
if(RC0==0)
{
while(RC0==0)continue;
if(num_count==0)lcd_clr();
lcd_goto(num_count);
send_char('1');
keyin[num_count]='1';
num_count+=1;
}
else if(RC1==0)
{
while(RC1==0)continue;
if(num_count==0)lcd_clr();
lcd_goto(num_count);
send_char('2');
keyin[num_count]='2';
num_count+=1;
}
else if(RC2==0)
{
while(RC2==0)continue;
if(num_count==0)lcd_clr();
lcd_goto(num_count);
send_char('3');
keyin[num_count]='3';
num_count+=1;
}
}
void scancolumn2(void)
{
if(RC0==0)
{
while(RC0==0)continue;
if(num_count==0)lcd_clr();
lcd_goto(num_count);
send_char('4');
keyin[num_count]='4';
num_count+=1;
}
else if(RC1==0)
{
while(RC1==0)continue;
if(num_count==0)lcd_clr();
lcd_goto(num_count);
send_char('5');
keyin[num_count]='5';
num_count+=1;
}
else if(RC2==0)
{
while(RC2==0)continue;
if(num_count==0)lcd_clr();
lcd_goto(num_count);
send_char('6');
keyin[num_count]='6';
num_count+=1;
}
}
void scancolumn3(void)
{
if(RC0==0)
{
while(RC0==0)continue;
if(num_count==0)lcd_clr();
lcd_goto(num_count);
send_char('7');
keyin[num_count]='7';
num_count+=1;
}
else if(RC1==0)
{
while(RC1==0)continue;
if(num_count==0)lcd_clr();
lcd_goto(num_count);
send_char('8');
keyin[num_count]='8';
num_count+=1;
}
else if(RC2==0)
{
while(RC2==0)continue;
if(num_count==0)lcd_clr();
lcd_goto(num_count);
send_char('9');
keyin[num_count]='9';
num_count+=1;
}
}
void scancolumn4(void)
{
if(RC0==0)
{
while(RC0==0)continue;
if(num_count==0)lcd_clr();
cd_goto(num_count);
send_char('*');
keyin[num_count]='*';
num_count+=1;
}
else if(RC1==0)
{
while(RC1==0)continue;
if(num_count==0)lcd_clr();
lcd_goto(num_count);
send_char('0');
keyin[num_count]='0';
num_count+=1;
}
else if(RC2==0)
{
while(RC2==0)continue;
if(num_count==0)lcd_clr();
lcd_goto(num_count);
send_char('#');
keyin[num_count]='#';
num_count+=1;
}
}
//==================subroutine LCD setting ==========================
void send_config(unsigned char data)
{
RS=0;
lcd=data;
delay(500);
e_pulse();
}
void e_pulse(void)
{
E=1;
delay(500);
E=0;
delay(500);
}
void send_char(unsigned char data)
{
RS=1;
lcd=data;
delay(500);
e_pulse();
}
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)
{
RS=0;
send_config(0x01);
delay(600);
}
void send_string(const char *s)
{
unsigned char i=0;
while (s && *s)send_char (*s++);
}
void dis_num(unsigned long data)
{
unsigned char hundred_thousand;
unsigned char ten_thousand;
unsigned char thousand;
unsigned char hundred;
unsigned char tenth;
hundred_thousand = data/100000;
data = data % 100000;
ten_thousand = data/10000;
data = data % 10000;
thousand = data / 1000;
data = data % 1000;
hundred = data / 100;
data = data % 100;
tenth = data / 10;
data = data % 10;
if(hundred_thousand>0)
{
send_char(hundred_thousand + 0x30);
send_char(ten_thousand + 0x30);
send_char(thousand + 0x30);
send_char(hundred + 0x30);
send_char(tenth + 0x30);
send_char(data + 0x30);
}
else if(ten_thousand>0)
{
send_char(ten_thousand + 0x30);
send_char(thousand + 0x30);
send_char(hundred + 0x30);
send_char(tenth + 0x30);
send_char(data + 0x30);
}
else if(thousand>0)
{
send_char(thousand + 0x30);
send_char(hundred + 0x30);
send_char(tenth + 0x30);
send_char(data + 0x30);
}
else if(hundred>0)
{
send_char(hundred + 0x30);
send_char(tenth + 0x30);
send_char(data + 0x30);
}
else if(tenth>0)
{
send_char(tenth + 0x30);
send_char(data + 0x30);
}
else send_char(data + 0x30);
}
void increment(unsigned long data)
{
unsigned short j;
for(j=10;j>0;j--)
{ lcd_goto(32);
data=data+1;
dis_num(data);
delay(10000);
}
}
//==================subroutine ADC=========================
void read_adc(void)
{
unsigned short i;
unsigned long result_temp=0;
for(i=2000;i>0;i-=1)
{
ADGO = 1;
while(ADGO==1);
result=ADRESH;
result=result<<8;
result=result|ADRESL;
result_temp+=result;
}
result = result_temp/2000;
}
unsigned short read_temp(void)
{
unsigned short temp;
temp=result;
return temp;
}
//==================subroutine DELAY==========================
void delay(unsigned short i)
{
for(;i>0;i--);
}
//==================led blinking============================
void blink_LED1(unsigned char n)
{
//loop for n times
for(n+=1;n>0;n-=1)
{
//set LED1 to 1
led1=1;
//short delay
for(unsigned int i=0;i<20000;i+=1);
//set LED1 to 1
led1=0;
//short delay
for(unsigned int i=0;i<20000;i+=1);
}
}
//function to blink redled for n times
void blink_redled(unsigned char n)
{
//loop for n times
for(n+=1;n>0;n-=1)
{
//set redled to 1
redled=1;
//short delay
for(unsigned int i=0;i<20000;i+=1);
//set redled to 1
redled=0;
//short delay
for(unsigned int i=0;i<20000;i+=1);
}
}
//function to blink redled for n times
void blink_orangeled(unsigned char n)
{
//loop for n times
for(n+=1;n>0;n-=1)
{
//set redled to 1
orangeled=1;
//short delay
for(unsigned int i=0;i<20000;i+=1);
//set redled to 1
orangeled=0;
//short delay
for(unsigned int i=0;i<20000;i+=1);
}
}
//==================Buzzer Beeping============================
void beep_once(void)
{
buzzer=1;
delay(10);
buzzer=0;
delay(20);
buzzer=1;
delay(10);
buzzer=0;
}
 

Well, the first thing you need is a 'C' compiler. Start MPLAB and configure the tools to include the compiler. Then start a new project (use the Wizard if you like) and open a new file. Copy the code into the new file and save it with ".c" at the end of the name. Then add the file to the project and press the F10 key. It should compile OK but depending on which compiler it was originally written for, you may have to make a few small changes.

Brian.
 
Thank You. But now i have and another problem.Actually this is a program for digital temperature monitoring system where keypaid, LCD, Buzzer, LED,coller are interface. But my question is that how can i burn this code to the microcontroller. I have a PIC burner. Please inform me details.
 

Compile your codes,the important output file is .hex file ,then with the downloader softwares of your burner download the .hex file into your Microcontroller.
Hope this help.!!
 
I have done it. Its ok. but what i have to do to burn this program into the microcontroller. or what have to do after this step?? Please help me.

Hello guy,how man programs (softwares) are u using?
Let give u an exampkle me I am using PIC PGM programmer (burner) for downloading .hex file in the pic. I use also ccs c compiler as code editor so after writing the codes with ccs C Icompile and Build it then It creates for me the .hex file which now I use the pic downloader Ver 4.0 for downloading my .hex file in My microcontroller.

Hope thus helps.
 
You need hardware to move the data in the hex file to the silicon inside the PIC. There are many programming units on the market or you can build your own. I recommend the Pickit 2 or Pickit 3 from Microchip. The hex file is actually a plain text file with information on your program and the addresses inside the PIC where it is to be saved. If you Google for "intel hex format" it should give you details.

Brian.
 
Just do what betwixt has told you ,tell us if you have a device which might help you to upload your .hex file in your microcontroller.
 
Then all you have to do is connect the Pickit 2 to the PIC using the pins named in the data sheet, select the device in the Pickit software and load the hex file. When you click the "Write" button it will decode the hex file and put the data into the PIC for you. Simple as that!

Brian.
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top