|
|
View previous topic :: View next topic |
Author |
Message |
xixorro
Joined: 27 Jun 2012 Posts: 4
|
need some help with LCD |
Posted: Wed Jun 27, 2012 11:18 am |
|
|
Hi.
I'm making a school project in which I use a pic 16f874A and RTC ds1307 and an 8bit LCD.
I get the the time and date from the RTC and i want to write it to the LCD,
The problem is that the date won't show right, the time info is ok but not the date. It shows numbers that are not the ones I have entered.
I also use RS232 for showing the time and date and it all seems fine.
Thanks to all of you.
I will post all my code here.
main.c :
Code: | #include <16F874A.h>
#device PASS_STRINGS=IN_RAM
#device adc=10 //ADC de 10 bits
#include <math.h>
#use delay(clock=20000000)
#fuses NOWDT,HS, NOPUT, NOPROTECT, NODEBUG, NOLVP, NOCPD, NOWRT, BROWNOUT
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,parity=N, bits=8,ERRORS)
#include <funcoes.c>
#include <ds1307.c>
void main(){
int k;
float corrente,i;
char mystring[4]="";
char data[]="";
char tempo[]="";
int sec=100,min,hora,dia,mes,ano;
//Inicializacoes do ADC
setup_adc_ports(AN0); //Define qtos canais analogicos se quer
setup_adc(ADC_CLOCK_INTERNAL);
port_b_pullups(0xF0);
set_tris_b(0xF0);
set_tris_d(0x00);
while (sec>59 || min>59 || hora>23 || dia>31 || mes>12 || ano>100)
{
reset_lcd();
transfere_string("segundos:");
sec=recebe_dados();
reset_lcd();
transfere_string("minutos:");
min=recebe_dados();
reset_lcd();
transfere_string("horas:");
hora=recebe_dados();
reset_lcd();
transfere_string("dia:");
dia=recebe_dados();
reset_lcd();
transfere_string("mes:");
mes=recebe_dados();
reset_lcd();
transfere_string("ano:");
ano=recebe_dados();
reset_lcd();
if (sec>59 || min>59 || hora>23 || dia>31 || mes>12 || ano>99)
{
reset_lcd();
transfere_string("DADOS INVALIDOS");
delay_ms(1000);
}
}
delay_ms(50);
init_ds1307(); // initial DS1307
sec=read_ds1307(0);
write_ds1307(0,bin2bcd(sec) & 0x7F); // enable oscillator(bit 7 =0)
write_ds1307 (0x01,bin2bcd(min));
write_ds1307 (0x02,bin2bcd(hora));
write_ds1307 (0x04,bin2bcd(dia));
write_ds1307 (0x05,bin2bcd(mes));
write_ds1307 (0x06,bin2bcd(ano));
while(1)
{
sec=read_ds1307(0); // read second
min=read_ds1307(1); // read minute
hora=read_ds1307(2); // read hour
dia=read_ds1307(4); // read date
mes=read_ds1307(5); // read month
ano=read_ds1307(6); // read year
printf("Date : %02X/%02X/20%02X\r\n",dia,mes,ano);
printf("Time : %02X:%02X:%02X\r\n",hora,min,sec);
set_adc_channel(0); //Inicia a conversao do Canal AN 0
delay_us(20); //Atraso para fazer a conversao,pk nada eh instantaneo
corrente=0;
for (k=0;k<20;k++){
i = read_adc();
delay_ms(100);
i=i-512;
corrente=corrente+(i*0.04878);
}
corrente=corrente/20;
if (input(pin_A1)==1){
reset_lcd();
sec=read_ds1307(0); // read second
min=read_ds1307(1); // read minute
hora=read_ds1307(2); // read hour
dia=read_ds1307(4); // read date
mes=read_ds1307(5); // read month
ano=read_ds1307(6); // read year
sprintf(tempo,"Time: %i:%i:%i",bcd2bin(hora),bcd2bin(min),bcd2bin(sec));//puts time data in a string
transfere_string(tempo); //writes the time in the lcd
transfere_inst(192); //ldc to 2nd line
[color=red] sprintf(data,"Date: %i/%i/%i",bcd2bin(dia),bcd2bin(mes),bcd2bin(ano));//puts date data in a string[/color]
transfere_string(data);//writes the date in the lcd
//transfere_string("Time : %02X:%02X:%02X\r\n",hora,min,sec);
//transfere_string("Date : %02X/%02X/20%02X\r\n",dia,mes,ano);
}
if (input(pin_A1)==0) {
reset_lcd();
transfere_string("CORRENTE=");
sprintf(mystring,"%f",corrente);
transfere_string(mystring);
}
}
}
|
My functions:
Code: | #define RS PIN_E0
#define ENABLE PIN_E1
#include <string.h>
char tecla;
void activa()
{
output_high(ENABLE);
delay_ms(5);
output_low(ENABLE);
}
void transfere_inst(int inst)
{
output_low(RS);
output_d(inst);
activa();
}
int conversao_char_int(char c){
char carac[10]={'0','1','2','3','4','5','6','7','8','9'};
int num,i;
num=0;
for(i=0;i<10;i++){
if(c==carac[i])
num=i;
}
return num;
}
char bin2bcd(char binary_value) {
char temp;
char retval;
temp = binary_value;
retval = 0;
while(1)
{
// Get the tens digit by doing multiple subtraction
// of 10 from the binary value.
if(temp >= 10)
{
temp -= 10;
retval += 0x10;
}
else // Get the ones digit by adding the remainder.
{
retval += temp;
break;
}
}
return(retval);
}
void transfere_carac(int8 carac)
{
output_high(RS);
output_d(carac); //É no porto D que é feito a transferencia
activa();
}
char bcd2bin(char bcd_value)
{
char temp;
temp = bcd_value;
// Shifting upper digit right by 1 is same as multiplying by 8.
temp >>= 1;
// Isolate the bits for the upper digit.
temp &= 0x78;
// Now return: (Tens * 8) + (Tens * 2) + Ones
return(temp + (temp >> 2) + (bcd_value & 0x0f));
}
void transfere_string(char string[]){
int8 i,j;
j=strlen(string);
for (i=0;i<j ;i++){
transfere_carac(string[i]);
}
}
ler_tecla(){
tecla = '*';
output_low(pin_b0);
output_high(pin_b1);
output_high(pin_b2);
output_high(pin_b3);
while((input(pin_b4)==0) && (input(pin_b4)==0) && (input(pin_b4)==0)){
tecla='1';
}
while((input(pin_b5)==0) && (input(pin_b5)==0) && (input(pin_b5)==0)){
tecla='4';
}
while((input(pin_b6)==0) && (input(pin_b6)==0) && (input(pin_b6)==0)){
tecla='7';
}
while((input(pin_b7)==0) && (input(pin_b7)==0) && (input(pin_b7)==0)){
tecla='A';
}
output_high(pin_b0);
output_low(pin_b1);
output_high(pin_b2);
output_high(pin_b3);
while((input(pin_b4)==0) && (input(pin_b4)==0) && (input(pin_b4)==0)){
tecla='2';
}
while((input(pin_b5)==0) && (input(pin_b5)==0) && (input(pin_b5)==0)){
tecla='5';
}
while((input(pin_b6)==0) && (input(pin_b6)==0) && (input(pin_b6)==0)){
tecla='8';
}
while((input(pin_b7)==0) && (input(pin_b7)==0) && (input(pin_b7)==0)){
tecla='0';
}
output_high(pin_b0);
output_high(pin_b1);
output_low(pin_b2);
output_high(pin_b3);
while((input(pin_b4)==0) && (input(pin_b4)==0) && (input(pin_b4)==0)){
tecla='3';
}
while((input(pin_b5)==0) && (input(pin_b5)==0) && (input(pin_b5)==0)){
tecla='6';
}
while((input(pin_b6)==0) && (input(pin_b6)==0) && (input(pin_b6)==0)){
tecla='9';
}
while((input(pin_b7)==0) && (input(pin_b7)==0) && (input(pin_b7)==0)){
tecla='B';
}
output_high(pin_b0);
output_high(pin_b1);
output_high(pin_b2);
output_low(pin_b3);
while((input(pin_b4)==0) && (input(pin_b4)==0) && (input(pin_b4)==0)){
tecla='F';
}
while((input(pin_b5)==0) && (input(pin_b5)==0) && (input(pin_b5)==0)){
tecla='E';
}
while((input(pin_b6)==0) && (input(pin_b6)==0) && (input(pin_b6)==0)){
tecla='D';
}
while((input(pin_b7)==0) && (input(pin_b7)==0) && (input(pin_b7)==0)){
tecla='C';
}
}
int8 recebe_dados(){
delay_ms(50);
int j;
char p[2];
int8 p11 ,pin01;
for(j=0;j<2;j++){
ler_tecla();
while(tecla == '*' && tecla == '*' && tecla == '*'){
ler_tecla();
// if (tecla=='A'){
// break;
// }
}
p11=conversao_char_int(tecla); delay_ms(10);
transfere_carac(tecla); delay_ms(10);
p[j]=p11; delay_ms(60);
}
pin01=(p[0]*10)+p[1]; delay_ms(5);
return pin01;
}
void reset_lcd(){
int instrucao;
instrucao=56;
transfere_inst(instrucao); //A instrução é um comando
instrucao=1; //Comando que Apaga Display
transfere_inst(instrucao);
instrucao=12; //Define o Cursor como Invisivel
transfere_inst(instrucao);
instrucao=2; // colocação do cursor na 1º linha
transfere_inst(instrucao);
} |
the RTC:
Code: | #define DS1307_SDA PIN_C4
#define DS1307_SCL PIN_C3
#use i2c(master, sda=DS1307_SDA, scl=DS1307_SCL)
void init_DS1307()
{
output_float(DS1307_SCL);
output_float(DS1307_SDA);
}
void write_DS1307(byte address, BYTE data)
{
short int status;
i2c_start();
i2c_write(0xd0);
i2c_write(address);
i2c_write(data);
i2c_stop();
i2c_start();
status=i2c_write(0xd0);
while(status==1)
{
i2c_start();
status=i2c_write(0xd0);
}
}
//==========================
// read data one byte from DS1307
//==========================
BYTE read_DS1307(byte address)
{
BYTE data;
i2c_start();
i2c_write(0xd0);
i2c_write(address);
i2c_start();
i2c_write(0xd1);
data=i2c_read(0);
i2c_stop();
return(data);
}
|
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed Jun 27, 2012 2:57 pm |
|
|
I'm not going to wade through loads of code trying to eliminate distractions.
You need to do that.
Post code which is as SHORT as possible, and highlights your problem.
Mike |
|
|
xixorro
Joined: 27 Jun 2012 Posts: 4
|
|
Posted: Wed Jun 27, 2012 6:16 pm |
|
|
Mike Walne wrote: | I'm not going to wade through loads of code trying to eliminate distractions.
You need to do that.
Post code which is as SHORT as possible, and highlights your problem.
Mike |
Sorry. I thought I had highlighted the problem.
Code: |
if (input(pin_A1)==1){
reset_lcd();
sec=read_ds1307(0); // read second
min=read_ds1307(1); // read minute
hora=read_ds1307(2); // read hour
dia=read_ds1307(4); // read date
mes=read_ds1307(5); // read month
ano=read_ds1307(6); // read year
sprintf(tempo,"Time: %i:%i:%i",bcd2bin(hora),bcd2bin(min),bcd2bin(sec));//puts time data in a string
transfere_string(tempo); //writes the time in the lcd
transfere_inst(192); //ldc to 2nd line
[color=red] sprintf(data,"Date: %i/%i/%i",bcd2bin(dia),bcd2bin(mes),bcd2bin(ano));//puts date data in a string[/color]
transfere_string(data);//writes the date in the lcd
|
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Thu Jun 28, 2012 10:42 am |
|
|
OK, so you have not posted code which is complete and compilable so we can test it.
However, I suggest you look at the values stored in each of your variables sec, min, hora, dia, mes, ano, and your two string arrays tempo, and data. You may very well find a clue there.
The CCS forum guide gives hints on how to get help.
Mike |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19590
|
|
Posted: Thu Jun 28, 2012 2:56 pm |
|
|
Code: |
sprintf(tempo,"Time: %2x:%2x:%2x"hora,min,sec);//puts time data in a string
|
%i, is not a legitimate specifier for printf.
You also don't need to convert the values back to binary. %x, which is designed to print hexadecimal, also prints BCD directly. BCD, is a 'subset' of hex numbers, omitting the values where the digits are above '9'.
Best Wishes |
|
|
xixorro
Joined: 27 Jun 2012 Posts: 4
|
|
Posted: Fri Jun 29, 2012 7:37 am |
|
|
Ttelmah wrote: | Code: |
sprintf(tempo,"Time: %2x:%2x:%2x"hora,min,sec);//puts time data in a string
|
%i, is not a legitimate specifier for printf.
You also don't need to convert the values back to binary. %x, which is designed to print hexadecimal, also prints BCD directly. BCD, is a 'subset' of hex numbers, omitting the values where the digits are above '9'.
Best Wishes |
unfortunately that did not work at all,the lcd shows some letters ans all, if i do that print to the serial port it works well, but not to a string so I can put it in the LCD |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19590
|
|
Posted: Fri Jun 29, 2012 8:43 am |
|
|
Whoa a second. If the print works to the serial port, then it'll also work to the display. I'd say you have a problem with your LCD string display routine....
It looks rather complex anyway. Are you using a standard text LCD?. If so, why not use the standard drivers (either FLEX_LCD, or LCD.c)?. If your display driver supports receiving standard text characters (transfere_inst possibly), then use the ability to send the printf output - which will be exactly the same characters as go to the RS232 - directly to the display. So:
printf(transfere_inst,"Time: %2x:%2x:%2x"hora,min,sec);//send to LCD
Best Wishes |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Fri Jun 29, 2012 8:44 am |
|
|
xixorro wrote: | Ttelmah wrote: | Code: |
sprintf(tempo,"Time: %2x:%2x:%2x"hora,min,sec);//puts time data in a string
|
%i, is not a legitimate specifier for printf.
You also don't need to convert the values back to binary. %x, which is designed to print hexadecimal, also prints BCD directly. BCD, is a 'subset' of hex numbers, omitting the values where the digits are above '9'.
Best Wishes |
unfortunately that did not work at all,the lcd shows some letters ans all, if i do that print to the serial port it works well, but not to a string so I can put it in the LCD |
You're missing a comma after your end quote for the string and the word hora. _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Fri Jun 29, 2012 9:45 am |
|
|
I really hate those moments when you find the obvious "You're missing a comma after your end quote for the string and the word hora." - I had the same thing in Visual Basic the other day. Simple program to play sound. Would not work. 3 of us could not find the obvious error. Duh - the API call declaration was supposed to be "byval variable" - I had somehow dropped the space and it was "byvalvariable". No errors, simply didn't work. That is why we have switched to the new flatscreen monitors - it doesn't hurt nearly as much when we bang our heads against it as the old CRT's did :-)
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
xixorro
Joined: 27 Jun 2012 Posts: 4
|
|
Posted: Fri Jun 29, 2012 1:19 pm |
|
|
I managed to solve the problem,the istruction to put the lcd in the 2nd line was messing up my date readings somehow.
I read the date after the lcd instruction and it all went ok, but i will that code with the comma later.
Thanks to all for the help, the project is almost finished but i am sure i will run into some troubles. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|