jmlabuac
Newbie level 5
Hi guys... I really need some help...
I created the code below which just displays time and some amount which depends on the time lapsed.. My problem is that when I ran it in proteus, it went smoothly and produced the output I expected... But when I implemented it on the actual hardware, it all went gibberish.. The LCD keeps on updating every second since I set it to interrupt every second and display an updated value.. However on the hardware implementation the LCD keeps on putting characters that weren't even set....
I hope you could help me here....
#define _seconds 0x00
#define _minutes 0x01
#define _hours 0x02
#define _days 0x03
#define _date 0x04
#define _month 0x05
#define _year 0x06
#define _control 0x07
#define _Wr_1307 0b11010000 // write data to the rtc
# define _Rd_1307 0b11010001 // read data from the rtc
# define _config1 0b10010000 // 1hz output
unsigned int p_fee = 0; // parking fee
unsigned int counter = 0;
unsigned int seconds;
unsigned int minutes;
unsigned int hours;
unsigned int days;
unsigned int date;
unsigned int month;
unsigned int year;
char buff;
char update = 0;
// Lcd Initialization
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
//End of Lcd Initialization
char read_1307(char addr){
I2C1_Start();
I2C1_Wr(_Wr_1307);
I2C1_Wr(addr);
I2C1_Repeated_Start();
I2C1_Wr(_Rd_1307);
buff = I2C1_Rd(0);
I2C1_Stop();
return (buff);
}
void get_data(){
seconds = read_1307(_seconds);
minutes = read_1307(_minutes);
hours = read_1307(_hours);
date = read_1307(_date);
}
void interrupt(){
if (INTCON.INTF){
update = 1;
INTCON.INTF = 0; // clear interrupt flag.
}
if (counter < 300)
counter++;
else{
p_fee += 2;
counter = 0;
}
}
void initialize(){
INTCON.GIE = 1; //controls all possible interrupt sources simultaneously
INTCON.PEIE = 1; // acts similar to the GIE it, but controls interrupts enabled by peripherals
INTCON.INTE = 1; // Enable RB0/INT external interrupt
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1, 10, "Days");
Lcd_Chr(2,3,'A');
Lcd_Chr(2,4,'m');
Lcd_Chr(2,5,'o');
Lcd_Chr(2,6,'u');
Lcd_Chr(2,7,'n');
Lcd_Chr(2,8,'t');
TRISB = 0X01;
I2C1_Init(100000);
delay_ms(100);
}
void write_ds1307(unsigned short address,unsigned short w_data)
{
I2C1_Start(); // issue I2C start signal
//address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
I2C1_Wr(0xD0); // send byte via I2C (device address + W)
I2C1_Wr(address); // send byte (address of DS1307 location)
I2C1_Wr(w_data); // send data (data to be written)
I2C1_Stop(); // issue I2C stop signal
}
void settime(){
write_ds1307(0,0x80); //Reset second to 0 sec. and stop Oscillator
write_ds1307(1,0x27); //write min 27
write_ds1307(2,0x14); //write hour 14
write_ds1307(3,0x00); //write day of week 2:Monday
write_ds1307(4,0x17); // write date 17
write_ds1307(5,0x00); // write month 6 June
write_ds1307(6,0x00); // write year 8 --> 2008
write_ds1307(7,0x10); //SQWE output at 1 Hz
write_ds1307(0,0x00); //Reset second to 0 sec. and start Oscillator
}
unsigned int time_reduce(unsigned int x){
if((x>9)&&(x<32))
x = 1;
else if((x>28)&&(x<48))
x = 2;
else if((x>41)&&(x<64))
x = 3;
else if((x>57)&&(x<80))
x = 4;
else if((x>73)&&(x<90))
x = 5;
else
x = 0;
return x;
}
unsigned int correct_time(unsigned int x){
x = x - (time_reduce(x) * 6);
return x;
}
void compute_amount(){
unsigned int min_1 = 0, min = 0, hr = 0, dte = 0;
get_data();
min_1 = correct_time(minutes);
min = (min_1/5) * 3;
if ( (min_1 % 5) > 0 )
{ min = min + 3; }
hr = correct_time(hours) * 36;
dte = correct_time(date) * 864;
p_fee = min + hr + dte;
}
unsigned char BCD2UpperCh(unsigned char bcd)
{
return ((bcd >> 4) + '0');
}
unsigned char BCD2LowerCh(unsigned char bcd)
{
return ((bcd & 0x0F) + '0');
}
void display2(){
char data1[9];
char amount[7];
char data2[3];
get_data();
compute_amount();
data2[2] = '\0';
data2[1] = BCD2LowerCh(date);
data2[0] = BCD2UpperCh(date);
data1[7] = '\0';
data1[7] = BCD2LowerCh(seconds);
data1[6] = BCD2UpperCh(seconds);
data1[5] = ':';
data1[4] = BCD2LowerCh(minutes);
data1[3] = BCD2UpperCh(minutes);
data1[2] = ':';
data1[1] = BCD2LowerCh(hours);
data1[0] = BCD2UpperCh(hours);
IntToStr(p_fee,amount);
Lcd_Out(1,1,data1);
Lcd_Out(1,15, data2);
Lcd_Out(2,10,amount);
}
void main()
{
initialize();
settime();
while(1){
if (update)
{
display2();
Delay_ms(500);
update = 0;
}
}
}
I created the code below which just displays time and some amount which depends on the time lapsed.. My problem is that when I ran it in proteus, it went smoothly and produced the output I expected... But when I implemented it on the actual hardware, it all went gibberish.. The LCD keeps on updating every second since I set it to interrupt every second and display an updated value.. However on the hardware implementation the LCD keeps on putting characters that weren't even set....
I hope you could help me here....
#define _seconds 0x00
#define _minutes 0x01
#define _hours 0x02
#define _days 0x03
#define _date 0x04
#define _month 0x05
#define _year 0x06
#define _control 0x07
#define _Wr_1307 0b11010000 // write data to the rtc
# define _Rd_1307 0b11010001 // read data from the rtc
# define _config1 0b10010000 // 1hz output
unsigned int p_fee = 0; // parking fee
unsigned int counter = 0;
unsigned int seconds;
unsigned int minutes;
unsigned int hours;
unsigned int days;
unsigned int date;
unsigned int month;
unsigned int year;
char buff;
char update = 0;
// Lcd Initialization
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
//End of Lcd Initialization
char read_1307(char addr){
I2C1_Start();
I2C1_Wr(_Wr_1307);
I2C1_Wr(addr);
I2C1_Repeated_Start();
I2C1_Wr(_Rd_1307);
buff = I2C1_Rd(0);
I2C1_Stop();
return (buff);
}
void get_data(){
seconds = read_1307(_seconds);
minutes = read_1307(_minutes);
hours = read_1307(_hours);
date = read_1307(_date);
}
void interrupt(){
if (INTCON.INTF){
update = 1;
INTCON.INTF = 0; // clear interrupt flag.
}
if (counter < 300)
counter++;
else{
p_fee += 2;
counter = 0;
}
}
void initialize(){
INTCON.GIE = 1; //controls all possible interrupt sources simultaneously
INTCON.PEIE = 1; // acts similar to the GIE it, but controls interrupts enabled by peripherals
INTCON.INTE = 1; // Enable RB0/INT external interrupt
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1, 10, "Days");
Lcd_Chr(2,3,'A');
Lcd_Chr(2,4,'m');
Lcd_Chr(2,5,'o');
Lcd_Chr(2,6,'u');
Lcd_Chr(2,7,'n');
Lcd_Chr(2,8,'t');
TRISB = 0X01;
I2C1_Init(100000);
delay_ms(100);
}
void write_ds1307(unsigned short address,unsigned short w_data)
{
I2C1_Start(); // issue I2C start signal
//address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
I2C1_Wr(0xD0); // send byte via I2C (device address + W)
I2C1_Wr(address); // send byte (address of DS1307 location)
I2C1_Wr(w_data); // send data (data to be written)
I2C1_Stop(); // issue I2C stop signal
}
void settime(){
write_ds1307(0,0x80); //Reset second to 0 sec. and stop Oscillator
write_ds1307(1,0x27); //write min 27
write_ds1307(2,0x14); //write hour 14
write_ds1307(3,0x00); //write day of week 2:Monday
write_ds1307(4,0x17); // write date 17
write_ds1307(5,0x00); // write month 6 June
write_ds1307(6,0x00); // write year 8 --> 2008
write_ds1307(7,0x10); //SQWE output at 1 Hz
write_ds1307(0,0x00); //Reset second to 0 sec. and start Oscillator
}
unsigned int time_reduce(unsigned int x){
if((x>9)&&(x<32))
x = 1;
else if((x>28)&&(x<48))
x = 2;
else if((x>41)&&(x<64))
x = 3;
else if((x>57)&&(x<80))
x = 4;
else if((x>73)&&(x<90))
x = 5;
else
x = 0;
return x;
}
unsigned int correct_time(unsigned int x){
x = x - (time_reduce(x) * 6);
return x;
}
void compute_amount(){
unsigned int min_1 = 0, min = 0, hr = 0, dte = 0;
get_data();
min_1 = correct_time(minutes);
min = (min_1/5) * 3;
if ( (min_1 % 5) > 0 )
{ min = min + 3; }
hr = correct_time(hours) * 36;
dte = correct_time(date) * 864;
p_fee = min + hr + dte;
}
unsigned char BCD2UpperCh(unsigned char bcd)
{
return ((bcd >> 4) + '0');
}
unsigned char BCD2LowerCh(unsigned char bcd)
{
return ((bcd & 0x0F) + '0');
}
void display2(){
char data1[9];
char amount[7];
char data2[3];
get_data();
compute_amount();
data2[2] = '\0';
data2[1] = BCD2LowerCh(date);
data2[0] = BCD2UpperCh(date);
data1[7] = '\0';
data1[7] = BCD2LowerCh(seconds);
data1[6] = BCD2UpperCh(seconds);
data1[5] = ':';
data1[4] = BCD2LowerCh(minutes);
data1[3] = BCD2UpperCh(minutes);
data1[2] = ':';
data1[1] = BCD2LowerCh(hours);
data1[0] = BCD2UpperCh(hours);
IntToStr(p_fee,amount);
Lcd_Out(1,1,data1);
Lcd_Out(1,15, data2);
Lcd_Out(2,10,amount);
}
void main()
{
initialize();
settime();
while(1){
if (update)
{
display2();
Delay_ms(500);
update = 0;
}
}
}