简体   繁体   中英

How to print output data from DS1307 to LCD 16x2

I have a simple project. I have an ATmega32 connected to a 16x2 LCD and a DS13027 chip. I am using 8-Bit data with the 16x2 LCD. Below is the code.

#define F_CPU 8000000UL

#define LCD_DPRT  PORTA     
#define LCD_DDDR  DDRA      
#define LCD_DPIN  PINA      
#define LCD_CPRT  PORTB     
#define LCD_CDDR  DDRB      
#define LCD_CPIN  PINB      
#define LCD_RS  0           
#define LCD_RW  1           
#define LCD_EN  2

#include <avr/io.h>
#include <util/delay.h>

void i2c_stop(void);
void i2c_write(unsigned char data);
void i2c_start(void);
void i2c_init(void);
unsigned char i2c_read(unsigned char ackVal);
void rtc_init(void);
void rtc_setTime(unsigned char deviceRegister, unsigned char value);
unsigned char *rtc_getTime(unsigned char deviceRegister);
void lcdCommand( unsigned char cmnd );
void lcdData( unsigned char data );
void lcd_init();
void lcd_gotoxy(unsigned char x, unsigned char y);
void lcd_print( const char * str );

int main(void) {

    unsigned char *hours;

    rtc_init();
    rtc_setTime(0x02, 0x22);

    hours = rtc_getTime(0x02);

    lcd_init();
    lcd_gotoxy(1,1);
    lcd_print(hours[0]);
    lcd_gotoxy(2,1);
    lcd_print(hours[1]);

    while (1);
    return 0;
}

void i2c_stop(void){
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
}

void i2c_write(unsigned char data){
    TWDR = data;
    TWCR = (1<<TWINT) | (1<<TWEN);
    while(!(TWCR & (1<<TWINT)));
}

void i2c_start(void){
    TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
    while(!(TWCR & (1<<TWINT)));
}

void i2c_init(void){
    TWSR = 0x00;
    TWBR = 0x47;
    TWCR = 0x04;
}

unsigned char i2c_read(unsigned char ackVal){
    TWCR = (1<<TWINT) | (1<<TWEN) | (ackVal<<TWEA);
    while( !( TWCR & (1<<TWINT)) );
    return TWDR;
}

void rtc_init(void){
    i2c_init();
    i2c_start();
    i2c_write(0xD0);
    i2c_write(0x07);
    i2c_write(0x00);
    i2c_stop();
}

void rtc_setTime(unsigned char deviceRegister, unsigned char value){
    i2c_start();
    i2c_write(0xD0);
    i2c_write(deviceRegister);
    i2c_write(value);
    i2c_stop();
}

unsigned char *rtc_getTime(unsigned char deviceRegister){

    unsigned char *data;

    i2c_start();
    i2c_write(0xD0);
    i2c_write(deviceRegister);
    i2c_stop();

    i2c_start();
    i2c_write(0xD1);
    *data = i2c_read(0);
    i2c_stop();

    return data;
}

void lcdCommand( unsigned char cmnd ){
  LCD_DPRT = cmnd;          
  LCD_CPRT &= ~ (1<<LCD_RS);
  LCD_CPRT &= ~ (1<<LCD_RW);
  LCD_CPRT |= (1<<LCD_EN);  
  _delay_us(1);             
  LCD_CPRT &= ~ (1<<LCD_EN);
  _delay_us(100);           
}

void lcdData( unsigned char data ){
  LCD_DPRT = data;          
  LCD_CPRT |= (1<<LCD_RS);  
  LCD_CPRT &= ~ (1<<LCD_RW);
  LCD_CPRT |= (1<<LCD_EN);  
  _delay_us(1);             
  LCD_CPRT &= ~ (1<<LCD_EN);
  _delay_us(100);           
}

void lcd_init(){
  LCD_DDDR = 0xFF;
  LCD_CDDR = 0xFF;

  LCD_CPRT &=~(1<<LCD_EN);  
  _delay_us(2000);          
  lcdCommand(0x38);         
  lcdCommand(0x0E);         
  lcdCommand(0x01);         
  _delay_us(2000);          
  lcdCommand(0x06);         
}

void lcd_gotoxy(unsigned char x, unsigned char y){  
 unsigned char firstCharAdr[]={0x80,0xC0,0x94,0xD4};//table 12-5  
 lcdCommand(firstCharAdr[y-1] + x - 1);
 _delay_us(100);    
}

void lcd_print( const char * str ){
  unsigned char i = 0 ;
  while(str[i]!=0)
  {
    lcdData(str[i]);
    i++ ;
  }
}

When I run this program nothing appears on the LCD. I don't know why it isn't outputting the hours on the LCD.

  1. Why isn't the hours appearing on the LCD?
  2. How can I output the hours on the LCD (code wise)?

Might be the display init. If you have a display compatible with the standard controller HD44780, you may try the following (I wrote it in PIC assembly many years ago for a 1x16, so I won't post this code here directly):

  1. Put 0x30 to the LCD data bus, keep RW and RS line low (command mode)
  2. wait 25ms
  3. Strobe Enable, ie Ena=high, wait 1 or 2 µs, Ena=low
  4. Wait 4.1ms
  5. Strobe Enable
  6. Wait 140 µs
  7. Strobe Enable
  8. lcdCommand(0x38) //;Function set: Display in 8-Bit Mode,5x7 Font
  9. lcdCommand(0x0C) //0x0E as you do might be correct with yout lcd
  10. lcdCommand(1) //clear
  11. lcdCommand(0x06) //;Entry Mode set: Auto-Increment, no Shift
  12. lcd_gotoxy(1,1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM