Ja richtige angeschlossen ist es...jetzt kann ich wenigsten schon in die erste Zeile schreiben, nur die 2 zeile lässt sich durch das PIC Bord nicht benutzen. Hat auch komischen Reset, PIC Board RST hift gar nicht ..muss erst Spannung wegschalten vom Bord, ehe dasdisplay nach Neuprogrammierung des PICs einigermassen geht. Benutze PICKit 3.
Habe nun die LCD am Arduino versucht , da geht alles wunderbar alle beide Zeilen.
Hier nun der gesamt Code, es ist eine Demo um das Datum und Zeit von einem am Board befindlichem DS1302 auszugeben:
Code:
// Source Code : http://pastebin.com/febf0f69
// DS1302 Zeitausgabe an LCD 16x2
// CONFIG
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#define _XTAL_FREQ 4000000 //EasyPIC-40 FQ= 4.0MHz
#include <xc.h>
#include <stdlib.h>
void lcd_line1(void); //function prototypes
void lcd_line2(void);
void init(); //I/O init
void lcd_init(); //LCD init
void write(char x); //display one byte
void lcd_enable(); //LCD display setting.
void set_time(); //set time subroutine.
void delay(void);
void clk_write(unsigned char);
unsigned char clk_read(void);
void clkburst(void);
void datafix(void);
#define RS RA1
#define RW RA2
#define E RA3
#define RST RB5
#define DATA RB4
#define SCLK RB0
#define DATA_Tris TRISB4
char number[16] = " : : ";
char daydate[16] = " : :20 ";
char sec[] = " ";
//define the time: sec,min,hour,day,month,weekday,year,control word.
//zum Zeit setzen : einzelne Werte in table[] demendsprechend aendern
const char table[]={0x00,0x47,0x17,0x18,0x3,0x06,0x17,0x00};
void main(void)
{
char x;
unsigned char a,b;
a=0;
number[2] = number[5] = 0x3a;
daydate[2] = daydate[5] = 0x3a;
daydate[6] = 0x32;
daydate[7] = 0x30;
TRISB = 0;
TRISD = 0;
RST = 0; //1302 reset low
SCLK = 0; //1302 clock low
RW = 0; //set lcd R/W low
E = 0; //set lcd E low
delay();
init(); //I/O init
lcd_init(); //LCD init
// set_time(); // zum Zeit setzen Zeile aktivieren !!!
while(1)
{
RST = 1; //read hours
clk_write(0xbf); //burst read
sec[0] = clk_read(); //sec
sec[1] = clk_read(); //min
sec[2] = clk_read(); //hour
sec[3] = clk_read(); //date
sec[4] = clk_read(); //month
sec[5] = clk_read(); //day
sec[6] = clk_read(); //year
sec[7] = clk_read(); //control
RST = 0;
datafix(); //convert for display
lcd_line1(); //display time and date
// Zeile 2 Pos.0
/* PORTD=0xC0; //set the 2nd line display address Pos.0
lcd_enable(); //LCD display setting command.
lcd_line2(); //display date
*/
}
}
void datafix(void)
{
char temp,x;
char *foo = sec;
char bleh[] = " ";
for(x=0;x<14;x+=2)
{
temp = *foo;
temp &+ 0xf0;
temp >>= 4;
bleh[x] = temp + 0x30;
temp = *foo;
temp &= 0x0f;
bleh[x+1] = temp + 0x30;
foo++;
}
number[6]=bleh[0]; //sec
number[7]=bleh[1];
number[3]=bleh[2]; //min
number[4]=bleh[3];
number[0]=bleh[4]; //hour
number[1]=bleh[5];
daydate[0]=bleh[6]; //day
daydate[1]=bleh[7];
daydate[3]=bleh[8]; //month
daydate[4]=bleh[9];
daydate[8]=bleh[12]; //year
daydate[9]=bleh[13];
}
void clk_write(unsigned char dat)
{
char x;
for(x=0;x<8;x++)
{
if(dat & 0x01) //put bit on data pin
{
DATA = 1;
}
else DATA = 0;
SCLK = 1; //toggle clock to latch bit
SCLK = 0;
dat >>= 1; //move next bit into place
}
}
unsigned char clk_read(void)
{
char x;
unsigned char dat,tmpdat;
dat = 0;
DATA_Tris = 1; //make DATA input
for(x=0;x<8;x++)
{
if(DATA) //read a bit
{
tmpdat |= 1;
} else {
tmpdat |= 0;
}
tmpdat <<= 7;
dat >>= 1;
dat |= tmpdat;
SCLK = 1; //toggle clock to latch bit
SCLK = 0;
}
DATA_Tris = 0;
return(dat);
}
void lcd_16number(int num)
{
number[0]=(char)(abs(num/10000)+0x30);
num=num-abs(num/10000)*10000;
number[1]=(char)(abs(num/1000)+0x30);
num=num-abs(num/1000)*1000;
number[2]=(char)(abs(num/100)+0x30);
num=num-abs(num/100)*100;
number[3]=(char)(abs(num/10)+0x30);
num=num-abs(num/10)*10;
number[4]=(char)(num+0x30);
}
//---------------------------------------
//I/O init
void init()
{
ADCON1=0x07; //a port as ordinary i/o.
TRISA=0x00; //a port as output.
TRISD=0x00; //d port as output.
return;
}
//---------------------------------------
//LCD init
void lcd_init()
{
PORTD=0x01; //clr screen
lcd_enable();
PORTD=0x38; //8 bits 2 lines 5*7 mode.
lcd_enable();
PORTD=0x0C; //display on,cursor on,blink on.
lcd_enable();
PORTD=0x06; //character not move,cursor rotate right.
lcd_enable();
PORTD=0x80; //??WWW.PIC16.COM"
lcd_enable();
return;
}
//--------------------------------------
//write a byte to lcd.
void write(char x)
{
PORTD=x; //data send to PORTD
RS = 1; //is data not command
RW = 0; //is write not read
E = 0; //pull low enable signal
delay();//for a while
E = 1; //pull high to build the rising edge.
return;
}
//--------------------------------------
//lcd display setting
void lcd_enable()
{
RS=0; //is command not data
RW=0; //is write not read.
E=0; //pull low enable signal.
delay(); //for a while.
E=1; //pull high to build the rising edge
return;
}
void lcd_line1()
{
int i;
for(i=0;i<16;++i) //total 16 bytes to display.
{
write(number[i]); //search table to display
}
return;
}
void lcd_line2()
{
int i;
for(i=0;i<16;++i) //total 16 bytes to display.
{
write(daydate[i]); //search table to display
}
return;
}
void delay(void)
{
long int y;
for(y=0;y<100;y++){} // 100 Orginal
}
//set time.
void set_time()
{
int i; //define the loop counter.
RST=1; //enable DS1302
clk_write(0xbe); //
for(i=0;i<8;i++) //continue to write 8 bytes.
{
clk_write(table[i]); //write one byte
}
RST=0; //reset
}
- - - Aktualisiert - - -
Zitat von
HeXPloreR
Probiere doch mal aus in dem Delay(10) zu warten. Wenn das immer noch nicht hinhaut, kopiere es auch in die Ini mit rein.
Hoffe es hilft.
Viele Grüße
Jörg
Hallo Jörg,
Das delay() ist im prog eine eigenständige Prozedur...siehe Code oben..habe ich schon experimentiert von vorn bis hinten
ja vileicht sollte ich mal _delay_ms(xx) oder delay_us(xx) probieren..bin noch Neuling bei den PICs.
Hier kam es mir nur auf das NICHT Funtionieren der LCDs an....
Vielen Dank..
Gerhard
Lesezeichen