Mình đang làm mạch đếm xung 24V hiên thị 10 số trên LCD. Lưu được dữ liệu khi mất điện.
mình làm xong phần cứng rùi. còn phần code thì hơi có vấn đề 1 chút. mình chỉ lưu được đến 255 là dùng thui. không lưu được số có 10 chữ số. nhờ các pro giúp với!
code:
#include <REGX52.H>
#include "24xx512.h"
#include "lcd.h"
unsigned int x,k,dat;
unsigned char x1,x2,x3,x4,x5,x6,x7,x8,x9,x10;
void init()
{
unsigned char inner, outer;
for (outer = 0x00; outer < 0x10; outer++)
{
for (inner = 0x00; inner < 0xFF; inner++);
}
LCD_init();
for (inner = 0; inner < 10; inner++)
LCD_delay(2);
}
/***************************** Main function *************************************/
void main(void)
{
IE=0x85;// su dung ngat ngoai chan INT0
IT0=1;// ngat suon âm
IT1=1;
init();
while(1)
{
LCD_row1(); LCD_puts("so xung la: ");
dat = EEPROM_get(0xffff);// doc san pham da luu hien thi len LCD
LCD_row2(); LCD_command(0xc0+2);LCD_number(dat);
x=dat;// doc lai gia tri bien x khi mat dien
}
} /* End main */
void ngat0(void) interrupt 0
{
x++;
EEPROM_set(0xffff,x);// luu bien san pham eeprom
if(x==999)x=0;
}
void ngat1(void) interrupt 2
{
x=0;
EEPROM_set(0xffff,x);
}
file: 24xx512.h:
#include "i2c.h" /* Need i2c bus */
#define EEPROMS_ID 0xA0 /* Microchip 24xx512 */
unsigned char EEPROM_get(unsigned int addr)
{
unsigned char dat;
I2C_start(); /* Start i2c bus */
I2C_write(EEPROMS_ID); /* Connect to EEPROM */
I2C_write(addr>>8); /* Request RAM address (Hight byte) */
I2C_write(addr&0x00FF); /* Request RAM address (Low byte) */
I2C_start(); /* Start i2c bus */
I2C_write(EEPROMS_ID+1);/* Connect to EEPROM for Read */
dat = I2C_read(); /* Receive data */
I2C_noack();
I2C_stop(); /* Stop i2c bus */
return dat;
}
void EEPROM_set(unsigned int addr, unsigned char val)
{
I2C_start();
I2C_write(EEPROMS_ID); /* Connect to EEPROM */
I2C_write(addr>>8); /* Request RAM address (Hight byte) */
I2C_write(addr&0x00FF); /* Request RAM address (Low byte) */
I2C_write(val); /* Write sec on RAM specified address */
I2C_stop(); /* Stop i2c bus */
}
file: i2c.h:
#define SDA P2_7 /* Set P2.7 = SDA */
#define SCL P2_6 /* Set P2.6 = SCL */
#define I2C_DELAY 0x0F /* For delay i2c bus */
void I2C_delay(void)
{
unsigned char i;
for(i=0; i<I2C_DELAY; i++);
}
void I2C_clock(void)
{
I2C_delay();
SCL = 1; /* Start clock */
I2C_delay();
SCL = 0; /* Clear SCL */
}
void I2C_start(void)
{
if(SCL)
SCL = 0; /* Clear SCL */
SDA = 1; /* Set SDA */
SCL = 1; /* Set SCL */
I2C_delay();
SDA = 0; /* Clear SDA */
I2C_delay();
SCL = 0; /* Clear SCL */
}
void I2C_stop(void)
{
if(SCL)
SCL = 0; /* Clear SCL */
SDA = 0; /* Clear SDA */
I2C_delay();
SCL = 1; /* Set SCL */
I2C_delay();
SDA = 1; /* Set SDA */
}
bit I2C_write(unsigned char dat)
{
bit data_bit;
unsigned char i;
for(i=0;i<8;i++) /* For loop 8 time(send data 1 byte) */
{
data_bit = dat & 0x80; /* Filter MSB bit keep to data_bit */
SDA = data_bit; /* Send data_bit to SDA */
I2C_clock(); /* Call for send data to i2c bus */
dat = dat<<1;
}
SDA = 1; /* Set SDA */
I2C_delay();
SCL = 1; /* Set SCL */
I2C_delay();
data_bit = SDA; /* Check acknowledge */
SCL = 0; /* Clear SCL */
I2C_delay();
return data_bit; /* If send_bit = 0 i2c is valid */
}
unsigned char I2C_read(void)
{
bit rd_bit;
unsigned char i, dat;
dat = 0x00;
for(i=0;i<8;i++) /* For loop read data 1 byte */
{
I2C_delay();
SCL = 1; /* Set SCL */
I2C_delay();
rd_bit = SDA; /* Keep for check acknowledge */
dat = dat<<1;
dat = dat | rd_bit; /* Keep bit data in dat */
SCL = 0; /* Clear SCL */
}
return dat;
}
//void I2C_ack()
//{
// SDA = 0; /* Clear SDA */
// I2C_delay();
// I2C_clock(); /* Call for send data to i2c bus */
// SDA = 1; /* Set SDA */
//}
void I2C_noack()
{
SDA = 1; /* Set SDA */
I2C_delay();
I2C_clock(); /* Call for send data to i2c bus */
SCL = 1; /* Set SCL */
}
file LCD.h:
#define LCD_en P2_4
#define LCD_rs P2_5
#define LCD_DELAY 1535 /* Delay for 1 ms */
#define LCD_clear() LCD_command(0x1) /* Clear display LCD */
#define LCD_origin() LCD_command(0x2) /* Set to origin LCD */
#define LCD_row1() LCD_command(0x80) /* Begin at Line 1 */
#define LCD_row2() LCD_command(0xC0) /* Begin at Line 2 */
/************************************************** *
* Prototype(s) *
************************************************** */
void LCD_delay(unsigned char ms);
void LCD_enable();
void LCD_command(unsigned char command);
void LCD_putc(unsigned char ascii);
void LCD_puts(unsigned char *lcd_string);
void LCD_init();
/************************************************** *
* Sources *
************************************************** */
void LCD_delay(unsigned char ms)
{
unsigned char n;
unsigned int i;
for (n=0; n<ms; n++)
{
for (i=0; i<LCD_DELAY; i++); /* For 1 ms */
}
}
void LCD_enable()
{
LCD_en = 0; /* Clear bit P2.4 */
LCD_delay(1);
LCD_en = 1; /* Set bit P2.4 */
}
void LCD_command(unsigned char command)
{
LCD_rs = 0; /* Clear bit P2.5 */
P2 = (P2 & 0xF0)|((command>>4) & 0x0F);
LCD_enable();
P2 = (P2 & 0xF0)|(command & 0x0F);
LCD_enable();
LCD_delay(1);
}
void LCD_putc(unsigned char ascii)
{
LCD_rs = 1; /* Set bit P2.5 */
P2 = (P2 & 0xF0)|((ascii>>4) & 0x0F);
LCD_enable();
P2 = (P2 & 0xF0)|(ascii & 0x0F);
LCD_enable();
LCD_delay(1);
}
void LCD_number (unsigned int d)
{
unsigned char a1,a2,a3,a4,a5,a6,a7,a8,a9,a10;
a1=d%10;
d=d/10;
a2=d%10;
d=d/10;
a3=d%10;
d=d/10;
a4=d%10;
d=d/10;
a5=d%10;
d=d/10;
a6=d%10;
d=d/10;
a7=d%10;
d=d/10;
a8=d%10;
d=d/10;
a9=d%10;
d=d/10;
a10=d%10;
LCD_putc(48+a10);
LCD_putc(48+a9);
LCD_putc(48+a8);
LCD_putc(48+a7);
LCD_putc(48+a6);
LCD_putc(48+a5);
LCD_putc(48+a4);
LCD_putc(48+a3);
LCD_putc(48+a2);
LCD_putc(48+a1);
}
void LCD_puts(unsigned char *lcd_string)
{
while (*lcd_string)
{
LCD_putc(*lcd_string++);
}
}
void LCD_init()
{
LCD_en = 1; /* Set bit P2.4 */
LCD_rs = 0; /* Clear bit P2.5 */
LCD_command(0x33);
LCD_command(0x32);
LCD_command(0x28);
LCD_command(0x0C);
LCD_command(0x06);
LCD_command(0x01); /* Clear */
LCD_delay(256);
}
mình làm xong phần cứng rùi. còn phần code thì hơi có vấn đề 1 chút. mình chỉ lưu được đến 255 là dùng thui. không lưu được số có 10 chữ số. nhờ các pro giúp với!
code:
#include <REGX52.H>
#include "24xx512.h"
#include "lcd.h"
unsigned int x,k,dat;
unsigned char x1,x2,x3,x4,x5,x6,x7,x8,x9,x10;
void init()
{
unsigned char inner, outer;
for (outer = 0x00; outer < 0x10; outer++)
{
for (inner = 0x00; inner < 0xFF; inner++);
}
LCD_init();
for (inner = 0; inner < 10; inner++)
LCD_delay(2);
}
/***************************** Main function *************************************/
void main(void)
{
IE=0x85;// su dung ngat ngoai chan INT0
IT0=1;// ngat suon âm
IT1=1;
init();
while(1)
{
LCD_row1(); LCD_puts("so xung la: ");
dat = EEPROM_get(0xffff);// doc san pham da luu hien thi len LCD
LCD_row2(); LCD_command(0xc0+2);LCD_number(dat);
x=dat;// doc lai gia tri bien x khi mat dien
}
} /* End main */
void ngat0(void) interrupt 0
{
x++;
EEPROM_set(0xffff,x);// luu bien san pham eeprom
if(x==999)x=0;
}
void ngat1(void) interrupt 2
{
x=0;
EEPROM_set(0xffff,x);
}
file: 24xx512.h:
#include "i2c.h" /* Need i2c bus */
#define EEPROMS_ID 0xA0 /* Microchip 24xx512 */
unsigned char EEPROM_get(unsigned int addr)
{
unsigned char dat;
I2C_start(); /* Start i2c bus */
I2C_write(EEPROMS_ID); /* Connect to EEPROM */
I2C_write(addr>>8); /* Request RAM address (Hight byte) */
I2C_write(addr&0x00FF); /* Request RAM address (Low byte) */
I2C_start(); /* Start i2c bus */
I2C_write(EEPROMS_ID+1);/* Connect to EEPROM for Read */
dat = I2C_read(); /* Receive data */
I2C_noack();
I2C_stop(); /* Stop i2c bus */
return dat;
}
void EEPROM_set(unsigned int addr, unsigned char val)
{
I2C_start();
I2C_write(EEPROMS_ID); /* Connect to EEPROM */
I2C_write(addr>>8); /* Request RAM address (Hight byte) */
I2C_write(addr&0x00FF); /* Request RAM address (Low byte) */
I2C_write(val); /* Write sec on RAM specified address */
I2C_stop(); /* Stop i2c bus */
}
file: i2c.h:
#define SDA P2_7 /* Set P2.7 = SDA */
#define SCL P2_6 /* Set P2.6 = SCL */
#define I2C_DELAY 0x0F /* For delay i2c bus */
void I2C_delay(void)
{
unsigned char i;
for(i=0; i<I2C_DELAY; i++);
}
void I2C_clock(void)
{
I2C_delay();
SCL = 1; /* Start clock */
I2C_delay();
SCL = 0; /* Clear SCL */
}
void I2C_start(void)
{
if(SCL)
SCL = 0; /* Clear SCL */
SDA = 1; /* Set SDA */
SCL = 1; /* Set SCL */
I2C_delay();
SDA = 0; /* Clear SDA */
I2C_delay();
SCL = 0; /* Clear SCL */
}
void I2C_stop(void)
{
if(SCL)
SCL = 0; /* Clear SCL */
SDA = 0; /* Clear SDA */
I2C_delay();
SCL = 1; /* Set SCL */
I2C_delay();
SDA = 1; /* Set SDA */
}
bit I2C_write(unsigned char dat)
{
bit data_bit;
unsigned char i;
for(i=0;i<8;i++) /* For loop 8 time(send data 1 byte) */
{
data_bit = dat & 0x80; /* Filter MSB bit keep to data_bit */
SDA = data_bit; /* Send data_bit to SDA */
I2C_clock(); /* Call for send data to i2c bus */
dat = dat<<1;
}
SDA = 1; /* Set SDA */
I2C_delay();
SCL = 1; /* Set SCL */
I2C_delay();
data_bit = SDA; /* Check acknowledge */
SCL = 0; /* Clear SCL */
I2C_delay();
return data_bit; /* If send_bit = 0 i2c is valid */
}
unsigned char I2C_read(void)
{
bit rd_bit;
unsigned char i, dat;
dat = 0x00;
for(i=0;i<8;i++) /* For loop read data 1 byte */
{
I2C_delay();
SCL = 1; /* Set SCL */
I2C_delay();
rd_bit = SDA; /* Keep for check acknowledge */
dat = dat<<1;
dat = dat | rd_bit; /* Keep bit data in dat */
SCL = 0; /* Clear SCL */
}
return dat;
}
//void I2C_ack()
//{
// SDA = 0; /* Clear SDA */
// I2C_delay();
// I2C_clock(); /* Call for send data to i2c bus */
// SDA = 1; /* Set SDA */
//}
void I2C_noack()
{
SDA = 1; /* Set SDA */
I2C_delay();
I2C_clock(); /* Call for send data to i2c bus */
SCL = 1; /* Set SCL */
}
file LCD.h:
#define LCD_en P2_4
#define LCD_rs P2_5
#define LCD_DELAY 1535 /* Delay for 1 ms */
#define LCD_clear() LCD_command(0x1) /* Clear display LCD */
#define LCD_origin() LCD_command(0x2) /* Set to origin LCD */
#define LCD_row1() LCD_command(0x80) /* Begin at Line 1 */
#define LCD_row2() LCD_command(0xC0) /* Begin at Line 2 */
/************************************************** *
* Prototype(s) *
************************************************** */
void LCD_delay(unsigned char ms);
void LCD_enable();
void LCD_command(unsigned char command);
void LCD_putc(unsigned char ascii);
void LCD_puts(unsigned char *lcd_string);
void LCD_init();
/************************************************** *
* Sources *
************************************************** */
void LCD_delay(unsigned char ms)
{
unsigned char n;
unsigned int i;
for (n=0; n<ms; n++)
{
for (i=0; i<LCD_DELAY; i++); /* For 1 ms */
}
}
void LCD_enable()
{
LCD_en = 0; /* Clear bit P2.4 */
LCD_delay(1);
LCD_en = 1; /* Set bit P2.4 */
}
void LCD_command(unsigned char command)
{
LCD_rs = 0; /* Clear bit P2.5 */
P2 = (P2 & 0xF0)|((command>>4) & 0x0F);
LCD_enable();
P2 = (P2 & 0xF0)|(command & 0x0F);
LCD_enable();
LCD_delay(1);
}
void LCD_putc(unsigned char ascii)
{
LCD_rs = 1; /* Set bit P2.5 */
P2 = (P2 & 0xF0)|((ascii>>4) & 0x0F);
LCD_enable();
P2 = (P2 & 0xF0)|(ascii & 0x0F);
LCD_enable();
LCD_delay(1);
}
void LCD_number (unsigned int d)
{
unsigned char a1,a2,a3,a4,a5,a6,a7,a8,a9,a10;
a1=d%10;
d=d/10;
a2=d%10;
d=d/10;
a3=d%10;
d=d/10;
a4=d%10;
d=d/10;
a5=d%10;
d=d/10;
a6=d%10;
d=d/10;
a7=d%10;
d=d/10;
a8=d%10;
d=d/10;
a9=d%10;
d=d/10;
a10=d%10;
LCD_putc(48+a10);
LCD_putc(48+a9);
LCD_putc(48+a8);
LCD_putc(48+a7);
LCD_putc(48+a6);
LCD_putc(48+a5);
LCD_putc(48+a4);
LCD_putc(48+a3);
LCD_putc(48+a2);
LCD_putc(48+a1);
}
void LCD_puts(unsigned char *lcd_string)
{
while (*lcd_string)
{
LCD_putc(*lcd_string++);
}
}
void LCD_init()
{
LCD_en = 1; /* Set bit P2.4 */
LCD_rs = 0; /* Clear bit P2.5 */
LCD_command(0x33);
LCD_command(0x32);
LCD_command(0x28);
LCD_command(0x0C);
LCD_command(0x06);
LCD_command(0x01); /* Clear */
LCD_delay(256);
}
Comment