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.

Source code for digital temperature monitoring system.

Status
Not open for further replies.

HerohetoChakma

Junior Member level 3
Joined
Apr 19, 2013
Messages
30
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
Chittagong, Bangladesh.
Activity points
2,338
heto.jpg
I have code for this program. But it is done in a SK40c Development board. I have done this in PCB board. Could anyone help me by changing the code? My code are given bellow:
#include <16f877a.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;
}
 

I don't want to change. But when i simulate this no output is show. There may be some programing problem. Now i need solution. Could anyone help me. In this circuit it is designed to make a digital temperature monitoring system to measured internal temperature and we can also set our set point by keypad, when temperature exit red light will on and buzzer will alarm. Please help me
 

Hello!

Your problem is apparently just a matter of adapting the new hardware, so if you change only the IO port definitions,
then it should work.
Now about your code:
1. - You wrote functions like clearrow1 to clearrow4. All these functions have no parameters
and their contents just look the same.
Why don't you write just a single function clear row(uint8 which_row); ?
2. - Same remark for scan column(uint8 which_column).
3. - Same remark for the blink led functions. Why not blink_led(uint8 led_mask); ?
4. - You have a function send_string. Why do you keep sending character by character in the first
level while of your main?
5. - You use, for instance:
hundred_thousand = data/100000;
data = data % 100000;
You sould be aware that division takes a lot of time, and so does %.
You should consider data -= hundred_thousand*100000 instead of data = data%100000.
6. - What would happen if you want to print 1000000? It simply wouldn't work. Here is why:
If you divide 1000000 by 100000, you obtain 10. 10 is not 0, so we are in the first case:
if(hundred_thousand > 0) because hundred_thousand is 10. It would print *000000
which is probably not what you want.
7. - You define a delay function, but you don't use it, for example, in blink led functions.
What's the point of rewriting the same thing?
8. - Your read_temp function is absolutely useless. Your temp variable is global, and so is result.
Worse than that: you define another temp variable in read_temp, which has the same name as
the global temp variable which is also valid in this context. Even if your compiler allows it to
work, it's simply looking for trouble. Why not using directly the result variable?
9. - All your delays are hardcoded. You have delay(500);, delay(600); delay(10000);
If you use another processor with a different frequency, there is a high chance that the delays
will be too long (waste of time) or too short (the peripheral devices may not work anymore.
You should define each delay (fore example LCD_DELAY) and use delay(LCD_DELAY) instead
of hardcoding it.
10.- Again about hardcoding: you write:
send_config(0b00000001);
send_config(0b00000010);
Therefore by reading your code, nobody can guess what it does. I guess it is a LCD config, but which one?
Why not defining #define MY_LCD_CONFIG 0b00000001 and use it in send_config(MY_LCD_CONFIG); ?
Of course, replace this by a meaningful string.

If you do this, then your source code will end up with about 125 ~ 150 lines instead of 500.

Divide your code length by 3, and you will divide the risk of errors by 3.

There are probably other details to be improved...

Dora.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top