Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Split Numbers

Status
Not open for further replies.

AtomSoft

Well-Known Member
Ok i finally got my DS1337 "I2C Serial Real-Time Clock" to work and send me info. i noticed that power has to be on constant or it wont keep track so i will adapt a small battery to keep it on track.

Um... my question is i get the data back as 0-80 for seconds and such which i really dont care about but am using it for testing.

How would i change the incoming data to text i can use on a LCD. I have it on LCD showing jibberish.

Any thoughts?

My Input is: 0x00, 0x01

how do i split that to like 00 and 01 for on screen?
 
This is far easier than you think. The data is returned in binary coded decimal (BCD) so you take the nibble (4 bit value) for each digit and add the ascii character value for 0 and you've got the ascii digit suitable for displaying. in C:
Code:
char sec_low, sec_high;
unsigned char seconds;

seconds = get_seconds();
sec_low = (seconds & 0xF) + '0';
sec_high = ((seconds>>4) & 0xF) + '0';
get_seconds() and sending the digits to the LCD are up to you.

You will need to mask out some bits like 12/24, am/pm, century.
 
And it's just as easy in asm,
Code:
	swapf	Seconds,W	;swap nibbles
	andlw	0x0f		;keep what was the high nibble
	addlw	'0'		;convert to ascii
	call	PutLCD		;display it
	movf	Seconds,W	;repeat for low nibble
	andlw	0x0f
	addlw	'0'
	call	PutLCD

Mike.
 
This is far easier than you think. The data is returned in binary coded decimal (BCD) so you take the nibble (4 bit value) for each digit and add the ascii character value for 0 and you've got the ascii digit suitable for displaying. in C:
Code:
char sec_low, sec_high;
unsigned char seconds;

seconds = get_seconds();
sec_low = (seconds & 0xF) + '0';
sec_high = ((seconds>>4) & 0xF) + '0';
get_seconds() and sending the digits to the LCD are up to you.

You will need to mask out some bits like 12/24, am/pm, century.

Wow thanks! And thanks pommie. Im on my cell right now so can't try it but it seems to make sense. Ill post a full code later. Thanks again.
 
The and 0x0f changes it to a number that is 0 to 9. By adding the ascii value for "0" which is 0x30 it changes it to 0x30 to 0x39 which are the required codes to display 0-9. Try sending 0x09 to your display and then send 0x39 and see what you get.

Mike.
 
Last edited:
Sorry didn't notice the single qoute...I see now

hi atom,
In case you havn't got to the 'clock setting' stage yet, the DS expects packed BCD
[ if its the same as the DS1307, which I assume it is] when setting the time/date.

I use a 3Volt, CR1/2AA Lithium back up battery, works fine.
 
Last edited:
i know lol its working great now. I finally have a expensive clock lol. Now i just need to write the current time.

Do i write the current time into the DS1337?

EDIT: Ericgibbs you beat me to the question. BCD = binary-coded decimal. How would i send that. I know how to send data in but like if its

7:09

I have to send:

Minutes = 0-000-1001
Code:
Bit7 = Always 0
Bits6:4 = 10 minutes
Bit3:0 = minutes
Hour = 0-1-0-0-0111
Code:
Bit7 = Always 0
bit6 = 12/24(high = 12hr)
bit5 = am/pm (since bit6 is high) else 10 hour
bit4 = 10 hour
bit 3:0 = hour

is that correct?
 
Last edited:
i know lol its working great now. I finally have a expensive clock lol. Now i just need to write the current time.

Do i write the current time into the DS1337?

hi,
For ref only see this pdf.

Very accurate with a pin for the backup battery.

As I use the DS1307, it requires the current time when initially using and of course when you adjust the clock.
Watch out for bit 7 'enable osc' else the clock will not run.
 

Attachments

  • DS3232S.pdf
    250.7 KB · Views: 230
Noticed a typo in my datasheet for DS1337:

"The DS1337 can be run in either 12-hour or 24-hour mode. Bit 6 of the hours register is defined as the 12- or 24-hour mode-select bit. When high, the 12-hour mode is selected."

Thats not true its opposite.
 
Here is my full code so far. Everything works nicely. I will use some bit manipulation code from another project for user input for the set time and stuff. also i know i can free some variables up but not just yet i might need them.
Enjoy!

Code:
#include <p18f4525.h>
#include <stdio.h>
#include <delays.h>

#pragma config WDT = OFF, OSC = HS, LVP = OFF

//Funtions
void main(void);
void delay_s(unsigned char);
void delay_ms(unsigned char);
void delay_us(unsigned char);

void i2c_byte(char addr);
void i2c_start(void);
void i2c_ack(void);
char i2c_input(void);
void i2c_clock(void);
char rtc_read(char offset);
char rtc_write(char offset, char data);
void i2c_stop(void);

void lcd_clr_line(char line);
void lcd_init(void);
void e_togg(void);
void lcd_cmd(unsigned char cmd);
void lcd_char(unsigned char tchar);
void lcd_string(char *);
void lcd_nybble(unsigned char data,char rs);

// IO definitions
#define SDA_PORT PORTCbits.RC4
#define SDA LATCbits.LATC4
#define SDA_TRIS TRISCbits.TRISC4

#define SCL LATCbits.LATC3

#define RTC_W 0b11010000
#define RTC_R 0b11010001

char slave_r = 0b11010001;
char slave_w = 0b11010000;
char seconds;
char minutes;
char hours;
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
char Line1 = 0x80;            // 0x80 is the command hex to move cursor to 0 (Line 1 first character)
char Line2 =  0xC0;            // 0xC0 is the command hex to move cursor to 64 (Line 2 first character)

#define lcd_rs LATBbits.LATB4          // define LCD RS - PIN
#define lcd_e LATBbits.LATB5           // define LCD E  - PIN

#define lcd_DB7 LATBbits.LATB0         // define LCD Data Bit 7
#define lcd_DB6 LATBbits.LATB1         // define LCD Data Bit 6
#define lcd_DB5 LATBbits.LATB2         // define LCD Data Bit 5
#define lcd_DB4 LATBbits.LATB3         // define LCD Data Bit 4
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
char time[7] = " ";

void main(void)
{
    char sec_low, sec_high;
    char min_low, min_high;
    char hr_low, hr_high;

    ADCON1 = 0b00001111;
    TRISC = 0x00;
    TRISB = 0x00;

    lcd_init();                             //Launch Initialize function

    rtc_write(0x00,0x00);
    rtc_write(0x01,0b00101000);
    rtc_write(0x02,0b00000111);

    while(1){
        time[2] = 0x3A;
        time[5] = 0x3A;

        seconds = rtc_read(0x00);
        minutes = rtc_read(0x01);
        hours = rtc_read(0x02);

        time[7] = sec_low = (seconds & 0x0F) + '0';
        time[6] = sec_high = ((seconds>>4) & 0x0F) + '0';

        time[4] = min_low = (minutes & 0x0F) + '0';
        time[3] = min_high = ((minutes>>4) & 0x0F) + '0';

        time[1] = hr_low = (hours & 0x0F) + '0';
        time[0] = hr_high = ((hours>>4) & 0x0F) + '0';
        
	    lcd_cmd(Line1);                         //Set to Line 1
        delay_ms(1);

	    lcd_string(time);                     //Send our string.
        delay_ms(100);
    }
}

////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////

void delay_s(unsigned char x)
{
    char var1;
	for(var1=0;var1<x;var1++)
	{
        Delay10KTCYx(500);    //5,000,000 cycles = 1 second
	}
}

void delay_ms(unsigned char x)
{
    char var1;
	for(var1=0;var1<x;var1++)
	{
        Delay1KTCYx(5);    //5,000 cycles = 1 us
	}
}

void delay_us(unsigned char x)
{
    char var1;
	for(var1=0;var1<x;var1++)
	{
        Delay1TCY();    //5 cycle = 1 us
        Delay1TCY();
        Delay1TCY();
        Delay1TCY();
        Delay1TCY();
	}
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////

void i2c_clock(void)
{
    SCL = 1;
    delay_us(5);
    SCL = 0;
}
char rtc_write(char offset, char data){

    char temp;
    i2c_start();                //Start
    i2c_byte(slave_w);          //Slave Byte
    i2c_ack();                  //ACK
    i2c_byte(offset);           //Address Offset
    i2c_ack();                  //ACK
    i2c_byte(data);          //Slave Byte    
    i2c_ack();                  //ACK
    i2c_stop;                    //Stop
    return temp;
}
char rtc_read(char offset){

    char temp;
    i2c_start();                //Start
    i2c_byte(slave_w);          //Slave Byte
    i2c_ack();                  //ACK
    i2c_byte(offset);           //Address Offset
    i2c_ack();                  //ACK
    i2c_start();                //Start
    i2c_byte(slave_r);          //Slave Byte    
    i2c_ack();                  //ACK
    temp = i2c_input();         //Get Data
    i2c_ack();                  //ACK
    i2c_stop;                    //Stop
    return temp;
}

void i2c_start(void){
    SDA_TRIS=0;   //SDA Output
    SCL=0;        //Clock Low
//Start - 5us
    SDA=1;        //SDA High
    SCL=1;        //Clock high
    delay_us(4);
    SDA=0;        //SDA Low
    delay_us(1);
    SCL=0;        //Clock Low
}

void i2c_byte(char addr){
    char bl;
    for(bl=0;bl<8;bl++){

        if((addr & 0x80) != 0)
            SDA = 1;
        else
            SDA = 0;

    i2c_clock();
    addr=addr<<1;
    }
}

void i2c_ack(void){
    SDA_TRIS = 1;   //SDA Input
    i2c_clock();
    delay_us(5);
    //while(SDA);
    SDA_TRIS = 0;
}

char i2c_input(void){
    char temp;
    char i;

    SDA_TRIS = 1;

    temp=0;  
    i = 0;           
    for(i=0;i<8;i++){  
        temp=temp<<1;   

        if(SDA_PORT) 
            temp|=1;

        i2c_clock();
    }

    SDA_TRIS = 0;
    return temp;
}

void i2c_stop(void){
    SDA = 0;
    SCL = 1;
    delay_us(3);
    SDA = 1;
    SCL = 0; 
}
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
//                LCD Stuff
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
void lcd_clr_line(char line)
{
//This is our clear line command.
    char tline;                             //The line to clear variable
    char x;                                 //loop variable

    if(line==0)                             //set the variable value based on line
        tline=0x80;                         // 0 = Line 1
    else
        tline=0xA8;                         // 1 = Line 2

    lcd_cmd(tline);                         // Send command to jump to beggining of line (1/2)
    delay_ms(1);

    for(x=0;x<40;x++){                      // Loop through all 40 chars of line (even tho 16 are viewable)
        lcd_char(0x20);                     // Send Blank Character
        delay_us(500);                  
    }

    lcd_cmd(tline);                         // Go back to beggining of line
    delay_ms(1);
}


void lcd_init(void)
{                                       //This is our lcd initialization function
    delay_ms(20);                       // Wait at least 16 mS after powerup

	lcd_nybble(0x03,0);                 // send 0x03 cmd 3 times to initialize
	delay_us(160);

	e_togg();                       // Since 0x03 nybble is on the port already
	delay_us(160);                      // we just toggle 2 more times

	e_togg();
	delay_us(160);

	lcd_nybble(0x02,0);                 //Enable 4 bit mode
	delay_us(160);

	lcd_cmd(0x28);			    //set 4-bit mode and 2 lines @ 5x7
	delay_us(160);

	lcd_cmd(0x10);			    //cursor move & shift left
	delay_us(160);

	lcd_cmd(0x06);			    //entry mode = increment
	delay_us(160);

	lcd_cmd(0x0d);			    //display on - cursor blink on
	delay_us(160);

	lcd_cmd(0x01);			    //clear display
	delay_us(160);

    delay_ms(5);
}

void e_togg(void){
    lcd_e = 1;
    lcd_e = 0;
}

void lcd_cmd(unsigned char letter)
{
//This is our Command Function
//The RS is set to 0 to signify this is a command
	unsigned char temp;             //Our temp Variable

	temp=letter;                    //move letter to temp
	temp=temp>>4;                   //shift temp to right by 4
	lcd_nybble(temp,0);             //send the 4 out

	temp=letter;                    //move letter to temp
	temp=temp&0x0f;                 //and out first 4
	lcd_nybble(temp,0);             //send out last 4
}

void lcd_char(unsigned char letter)
{
//This is the same as the lcd_cmd function except
//that is sets RS to 1 telling the lcd this is characters
	unsigned char temp;

	temp=letter;
	temp=temp>>4;
	lcd_nybble(temp,1);

	temp=letter;
	temp=temp&0x0f;
	lcd_nybble(temp,1);
}

void lcd_string(char *senpoint)
{
	while(*senpoint != '\0')            //While we havent seen a \0 (esc) go on
	{
	    lcd_char(*senpoint);            //send 1st char to our char function
	    senpoint++;                     //send next
	}
}
void lcd_nybble(unsigned char nyb,char rs)
{
	char i;
        char x;

        lcd_rs = rs;                    //Set RS Pin (defined in header file)

	for(i=0;i<4;i++){		//Loop through nybble

        if((nyb & 0x08) != 0)           //AND the nybble to 8
	    x=1;                        //if the AND == 1 set x
	else
 	    x=0;                        //if the AND == 0 clear x

        if(i==0)                    //select RA3:RA0 and set/clear pins
            lcd_DB7=x;

        if(i==1)
            lcd_DB6=x;

        if(i==2)
            lcd_DB5=x;

        if(i==3)
            lcd_DB4=x;


        nyb=nyb<<1;                     //shift nybble to the left by 1 (4 times total)
	}

	e_togg();                   //toggle E pin (defined in header file)
}
 
how would i combine 2 bytes?

I tried a shift and &...

byte3 = (byte1<<4) & byte2

any thoughts( i know its simple but i forget alot lol)

Tried a + and almost looks like it worked lol

EDIT: OK HERE IT IS:
Code:
tmp2 = (s_time[3]<<4) + s_time[4];
Seems to work i had to
Code:
s_time[x] -= 0x30;
In order to send to ds1337
 
Last edited:
EDIT: Ericgibbs you beat me to the question. BCD = binary-coded decimal. How would i send that. I know how to send data in but like if its

7:09

hi,
Just seen your edit,
send 09 to addr 01h
send 07 to addr 02h

That is packed BCD,,, the upper nibble is the 'tens' and the lower nibble the 'units'

17:59 hrs
would be 17 to addr 02 [ 00010111]
and 59 to addr 01 [ 01011001]
 
This is my complete code. I got to skim through it to remove rough edges but it works fine for now...

Code:
#include <p18f4525.h>
#include <stdio.h>
#include <delays.h>

#pragma config WDT = OFF, OSC = HS, LVP = OFF

//Funtions
void main(void);
void delay_s(unsigned char);
void delay_ms(unsigned char);
void delay_us(unsigned char);

void i2c_byte(char addr);
void i2c_start(void);
void i2c_ack(void);
char i2c_input(void);
void i2c_clock(void);
char rtc_read(char offset);
char rtc_write(char offset, char data);
void i2c_stop(void);

void lcd_clr_line(char line);
void lcd_init(void);
void e_togg(void);
void lcd_cmd(unsigned char cmd);
void lcd_char(unsigned char tchar);
void lcd_string(char *);
void lcd_nybble(unsigned char data,char rs);

void set_time(void);
void get_buttons(char hm);

// IO definitions
#define SDA_PORT PORTCbits.RC4
#define SDA LATCbits.LATC4
#define SDA_TRIS TRISCbits.TRISC4

#define SCL LATCbits.LATC3

char slave_r = 0b11010001;
char slave_w = 0b11010000;



////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
char Line1 = 0x80;            // 0x80 is the command hex to move cursor to 0 (Line 1 first character)
char Line2 =  0xC0;            // 0xC0 is the command hex to move cursor to 64 (Line 2 first character)

#define lcd_rs LATBbits.LATB4          // define LCD RS - PIN
#define lcd_e LATBbits.LATB5           // define LCD E  - PIN

#define lcd_DB7 LATBbits.LATB0         // define LCD Data Bit 7
#define lcd_DB6 LATBbits.LATB1         // define LCD Data Bit 6
#define lcd_DB5 LATBbits.LATB2         // define LCD Data Bit 5
#define lcd_DB4 LATBbits.LATB3         // define LCD Data Bit 4

#define up_btn PORTAbits.RA0
#define set_btn PORTAbits.RA1
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
char time[9] = " ";
char s_time[9] = " ";

char seconds;
char minutes;
char hours;

char g_hr_high, g_hr_low;
char g_min_high, g_min_low;

char stringA[5] = {'H','o','u','r',0}; 
char stringB[8] = {'M','i','n','u','t','e','s',0};

void main(void)
{
    char sec_low, sec_high;
    char min_low, min_high;
    char hr_low, hr_high;
    char tmp,tmp2;


    ADCON1 = 0b00001111;
    TRISC = 0x00;
    TRISB = 0x00;
    TRISA = 0x00;
    TRISAbits.TRISA0 = 1;
    TRISAbits.TRISA1 = 1;

    lcd_init();                             //Launch Initialize function
 
    while(1){
        for(tmp=0;tmp<9;tmp++){
            time[tmp] = 0x30;
            s_time[tmp] = 0x30;
        }
    
        time[2] = 0x3A;
        time[5] = 0x3A;
        time[8] = 0;

        s_time[1] = 0x31;
        s_time[2] = 0x3A;
        s_time[5] = 0x3A;
        s_time[8] = 0;

        seconds = rtc_read(0x00);
        minutes = rtc_read(0x01);
        hours = rtc_read(0x02);

        time[7] = sec_low = (seconds & 0x0F) + '0';
        time[6] = sec_high = ((seconds>>4) & 0x0F) + '0';

        time[4] = min_low = (minutes & 0x0F) + '0';
        time[3] = min_high = ((minutes>>4) & 0x0F) + '0';

        time[1] = hr_low = (hours & 0x0F) + '0';
        time[0] = hr_high = ((hours>>4) & 0x0F) + '0';
        
	    lcd_cmd(Line1);                         //Set to Line 1
        delay_ms(1);

	    lcd_string(time);                     //Send our string.
        delay_ms(50);
    
        if(set_btn){
            set_time();
            delay_ms(50);

            rtc_write(0x00,0x00);
            s_time[0]-= 0x30;
            s_time[1]-= 0x30;
            s_time[3]-= 0x30;
            s_time[4]-= 0x30;

            tmp2 = (s_time[3]<<4) + s_time[4];
            rtc_write(0x01,tmp2);

            tmp2 = (s_time[0]<<4) + s_time[1];
            rtc_write(0x02,tmp2);

            lcd_clr_line(1);
            delay_ms(10);
            lcd_clr_line(0);
            delay_ms(10);
        }
   }

}

////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////

void delay_s(unsigned char x)
{
    char var1;
	for(var1=0;var1<x;var1++)
	{
        Delay10KTCYx(500);    //5,000,000 cycles = 1 second
	}
}

void delay_ms(unsigned char x)
{
    char var1;
	for(var1=0;var1<x;var1++)
	{
        Delay1KTCYx(5);    //5,000 cycles = 1 us
	}
}

void delay_us(unsigned char x)
{
    char var1;
	for(var1=0;var1<x;var1++)
	{
        Delay1TCY();    //5 cycle = 1 us
        Delay1TCY();
        Delay1TCY();
        Delay1TCY();
        Delay1TCY();
	}
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////

void i2c_clock(void)
{
    SCL = 1;
    delay_us(5);
    SCL = 0;
}
char rtc_write(char offset, char data){

    char temp;
    i2c_start();                //Start
    i2c_byte(slave_w);          //Slave Byte
    i2c_ack();                  //ACK
    i2c_byte(offset);           //Address Offset
    i2c_ack();                  //ACK
    i2c_byte(data);          //Slave Byte    
    i2c_ack();                  //ACK
    i2c_stop;                    //Stop
    return temp;
}
char rtc_read(char offset){

    char temp;
    i2c_start();                //Start
    i2c_byte(slave_w);          //Slave Byte
    i2c_ack();                  //ACK
    i2c_byte(offset);           //Address Offset
    i2c_ack();                  //ACK
    i2c_start();                //Start
    i2c_byte(slave_r);          //Slave Byte    
    i2c_ack();                  //ACK
    temp = i2c_input();         //Get Data
    i2c_ack();                  //ACK
    i2c_stop;                    //Stop
    return temp;
}

void i2c_start(void){
    SDA_TRIS=0;   //SDA Output
    SCL=0;        //Clock Low
//Start - 5us
    SDA=1;        //SDA High
    SCL=1;        //Clock high
    delay_us(4);
    SDA=0;        //SDA Low
    delay_us(1);
    SCL=0;        //Clock Low
}

void i2c_byte(char addr){
    char bl;
    for(bl=0;bl<8;bl++){

        if((addr & 0x80) != 0)
            SDA = 1;
        else
            SDA = 0;

    i2c_clock();
    addr=addr<<1;
    }
}

void i2c_ack(void){
    SDA_TRIS = 1;   //SDA Input
    i2c_clock();
    delay_us(5);
    //while(SDA);
    SDA_TRIS = 0;
}

char i2c_input(void){
    char temp;
    char i;

    SDA_TRIS = 1;

    temp=0;  
    i = 0;           
    for(i=0;i<8;i++){  
        temp=temp<<1;   

        if(SDA_PORT) 
            temp|=1;

        i2c_clock();
    }

    SDA_TRIS = 0;
    return temp;
}

void i2c_stop(void){
    SDA = 0;
    SCL = 1;
    delay_us(3);
    SDA = 1;
    SCL = 0; 
}

//////////////////////////////////////////////////
//////////////////////////////////////////////////
//              Time Functions
//////////////////////////////////////////////////
//////////////////////////////////////////////////

void set_time(void){

    lcd_clr_line(0);
    delay_ms(100);

    lcd_clr_line(1);
    delay_ms(100);
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
    lcd_cmd(Line1);                         //Set to Line 1
    delay_ms(100);

    lcd_string(stringA);                     //Send our string.
    delay_ms(100);

    while(!set_btn){
        delay_ms(50);
        if(up_btn)
            get_buttons(1);
    }
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
    lcd_clr_line(0);
    delay_ms(100);

    lcd_clr_line(1);
    delay_ms(100);
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
    lcd_cmd(Line1);                         //Set to Line 1
    delay_ms(100);

    lcd_string(stringB);                     //Send our string.
    delay_ms(100);

    while(!set_btn){
        delay_ms(50);

        if(up_btn){
            get_buttons(0);
        }    

    }
}

void get_buttons(char hm){
if (hm == 1){
          
    if(s_time[0] == 0x31){
        if(s_time[1] < 0x32){
             s_time[1]++;
        } else {
             s_time[1] = 0x31;
             s_time[0] = 0x30;
        }
    } else {

        s_time[1]++;
        if(s_time[1] > 0x39){
            s_time[1] = 0x31;
            s_time[0]++;

            if(s_time[0] == 0x32)
                s_time[0] = 0x30;

        }
    }



} else {

   s_time[4]++;
           
    if(s_time[4] > 0x39){
        s_time[4] = 0x30;

        if(s_time[3] == 0x35)
            s_time[3] = 0x30;
        else
            s_time[3]++;

    }
}

    lcd_clr_line(1);
    delay_ms(50);

    lcd_cmd(Line2);                         //Set to Line 1
    delay_ms(50);

    lcd_string(s_time);                     //Send our string.
    delay_ms(60);  
        

}
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
//                LCD Stuff
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
void lcd_clr_line(char line)
{
//This is our clear line command.
    char tline;                             //The line to clear variable
    char x;                                 //loop variable

    if(line==0)                             //set the variable value based on line
        tline=0x80;                         // 0 = Line 1
    else
        tline=0xA8;                         // 1 = Line 2

    lcd_cmd(tline);                         // Send command to jump to beggining of line (1/2)
    delay_ms(1);

    for(x=0;x<40;x++){                      // Loop through all 40 chars of line (even tho 16 are viewable)
        lcd_char(0x20);                     // Send Blank Character
        delay_us(500);                  
    }

    lcd_cmd(tline);                         // Go back to beggining of line
    delay_ms(1);
}


void lcd_init(void)
{                                       //This is our lcd initialization function
    delay_ms(20);                       // Wait at least 16 mS after powerup

	lcd_nybble(0x03,0);                 // send 0x03 cmd 3 times to initialize
	delay_us(160);

	e_togg();                       // Since 0x03 nybble is on the port already
	delay_us(160);                      // we just toggle 2 more times

	e_togg();
	delay_us(160);

	lcd_nybble(0x02,0);                 //Enable 4 bit mode
	delay_us(160);

	lcd_cmd(0x28);			    //set 4-bit mode and 2 lines @ 5x7
	delay_us(160);

	lcd_cmd(0x10);			    //cursor move & shift left
	delay_us(160);

	lcd_cmd(0x06);			    //entry mode = increment
	delay_us(160);

	lcd_cmd(0x0d);			    //display on - cursor blink on
	delay_us(160);

	lcd_cmd(0x01);			    //clear display
	delay_us(160);

    delay_ms(5);
}

void e_togg(void){
    lcd_e = 1;
    lcd_e = 0;
}

void lcd_cmd(unsigned char letter)
{
//This is our Command Function
//The RS is set to 0 to signify this is a command
	unsigned char temp;             //Our temp Variable

	temp=letter;                    //move letter to temp
	temp=temp>>4;                   //shift temp to right by 4
	lcd_nybble(temp,0);             //send the 4 out

	temp=letter;                    //move letter to temp
	temp=temp&0x0f;                 //and out first 4
	lcd_nybble(temp,0);             //send out last 4
}

void lcd_char(unsigned char letter)
{
//This is the same as the lcd_cmd function except
//that is sets RS to 1 telling the lcd this is characters
	unsigned char temp;

	temp=letter;
	temp=temp>>4;
	lcd_nybble(temp,1);

	temp=letter;
	temp=temp&0x0f;
	lcd_nybble(temp,1);
}

void lcd_string(char *senpoint)
{
	while(*senpoint != '\0')            //While we havent seen a \0 (esc) go on
	{
	    lcd_char(*senpoint);            //send 1st char to our char function
	    senpoint++;                     //send next
	}
}
void lcd_nybble(unsigned char nyb,char rs)
{
	char i;
        char x;

        lcd_rs = rs;                    //Set RS Pin (defined in header file)

	for(i=0;i<4;i++){		//Loop through nybble

        if((nyb & 0x08) != 0)           //AND the nybble to 8
	    x=1;                        //if the AND == 1 set x
	else
 	    x=0;                        //if the AND == 0 clear x

        if(i==0)                    //select RA3:RA0 and set/clear pins
            lcd_DB7=x;

        if(i==1)
            lcd_DB6=x;

        if(i==2)
            lcd_DB5=x;

        if(i==3)
            lcd_DB4=x;


        nyb=nyb<<1;                     //shift nybble to the left by 1 (4 times total)
	}

	e_togg();                   //toggle E pin (defined in header file)
}

EDIT:
How do i split this file into various c files like :

1. i2c.c
2. rtc.c
3. lcd.c
 
Last edited:
hi Atom,
Like to help but I dont do 'C' I'm a Assemblersaurus.:)
 
Last edited:
I am assuming you are using the microchip IDE.

You split the file apart in to .c files using your favorite text editor and then add each file to the project. mplab knows to compile each file and add them to the link line. It's pretty easy. Sorry I can't give you the exact directions - I don't have it on this machine.

For the header stuff (prototypes, defines, ...), I stick all those in a .h file and use include. Add the .h files to your project so it will know to recompile the .c files when they change. You might need to use externs to access global variables across modules. It's not hard at all and makes it easier to share code among different projects.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top