|
AT24C02 C语言驱动程序—MAX7219 显示 -|zl0801 发表于 2005-8-25 9:19:00
//AT24C02 EEPROM drive program //for 51 mcu with max7219 as display //designed by zhaoliang //2005-6-15 14:23
#i nclude " reg51.h " #i nclude " intrins.h " /********************************************************************/ //common part #define HIGH 1 #define LOW 0 #define TRUE 1 #define ZERO 0 #define MSB 0x80 //at24c02 part #define WRITE24C02 0xA0 #define READ24C02 0xA1 //I2C part #define ACK 0 #define NO_ACK 1 #define MSB 0x80 //max7219 part #define DECODE_MODE 0x09 #define INTENSITY 0x0A #define SCAN_LIMIT 0x0B #define SHUT_DOWN 0x0C #define DISPLAY_TEST 0x0F /********************************************************************/ //pin defined /***********************************************************************/ //change this part at different board sbit LOAD=P1^2; //MAX7219 Load-Data Input: rising edge pin 12 sbit DIN=P1^1; //MAX7219 Serial-Data Input: rising edge pin 1 sbit CLK=P1^0; //MAX7219 Serial-Clock Input: maximum 10MHz pin 13
//function define /***********************************************************************/ void Write_7219(unsigned char address,unsigned char dat);//write max7219 command and data void Init_7219(void);//Initize max7219 void Write_7219_byte(unsigned char temp);//write max7219 a byte
sbit SDA=P2^3; //AT24C02 serial data pin 5 sbit SCLK=P2^2; //AT24C02 serial clock pin 6 /********************************************************************/ void Write_7219(unsigned char address,unsigned char dat);//write max7219 command and data void Init_7219(void);//Initize max7219 void Write_7219_byte(unsigned char temp);//write max7219 a byte /********************************************************************/ void I2C_delay(void);//I2C delay function void I2C_start(void);//I2C start function void I2C_stop(void);//I2C stop function void I2C_send_ack(bit k);//I2C send responsion function void I2C_write_byte(unsigned char dat);//I2C bus write byte function unsigned char I2C_read_byte(void);//I2C bus read byte function /********************************************************************/ void AT24C02_write(unsigned char address,unsigned char dat);//write 24c02 information function unsigned char AT24C02_read(unsigned char address);//read 24c02 information function /********************************************************************/ void Mcu_init(void);//system initize funcition void Display(void);//24c02 display function unsigned char count[2]; /********************************************************************/ void main() { Mcu_init(); while(1) { Display(); } } /***********************************************************************/ void timer0(void) interrupt 1 using 1 { TH0=-(12000/256); TL0=-(12000%256); count[0]=count[0]+1; if(count[0]==100) { count[0]=0; count[1]=count[1]+1; if(count[1]==99) count[1]=0; } } /***********************************************************************/ void Mcu_init(void) { TMOD=0x11; TH0=-(12000/256); TL0=-(12000%256); EA=HIGH; ET0=HIGH; TR0=HIGH; Init_7219(); } /***********************************************************************/ /******************************** I2C PART **************************/ void I2C_delay(void) { _nop_();_nop_();_nop_();_nop_(); } /***********************************************************************/ void I2C_start(void) { SDA=HIGH; _nop_(); SCLK=HIGH; _nop_(); SDA=LOW; _nop_(); SCLK=LOW; _nop_(); } /***********************************************************************/ void I2C_stop(void) { SDA=LOW; _nop_(); SCLK=HIGH; _nop_(); SDA=HIGH; _nop_(); SCLK=LOW; _nop_(); } /***********************************************************************/ void I2C_send_ack(bit k) { SDA=k; I2C_delay(); SCLK=HIGH; I2C_delay(); SCLK=LOW; } /***********************************************************************/ void I2C_write_byte(unsigned char dat) { unsigned char i; for (i=8;i>0;i--) { SCLK=LOW; I2C_delay(); SDA=(bit)(dat&MSB); dat<<=1; I2C_delay(); SCLK=HIGH; I2C_delay(); } SCLK=LOW; } /***********************************************************************/ unsigned char I2C_read_byte(void) { unsigned char i,dat; for (i=0;i<8;i++) { SCLK=LOW; I2C_delay(); SDA=HIGH; I2C_delay(); SCLK=HIGH; dat<<=1; I2C_delay(); if(SDA) dat++; } SCLK=LOW; return (dat); } /***********************************************************************/ /************************ 24C02 PART **********************************/ void AT24C02_write(unsigned char address,unsigned char dat) { unsigned char temp; temp=dat/10; temp<<=4; temp=dat%10+temp; I2C_start(); I2C_write_byte(WRITE24C02); I2C_send_ack(ACK); I2C_write_byte(address); I2C_send_ack(ACK); I2C_write_byte(temp); I2C_send_ack(NO_ACK); I2C_stop(); } /***********************************************************************/ unsigned char AT24C02_read(unsigned char address) { unsigned char temp,dat; I2C_start(); I2C_write_byte(WRITE24C02); I2C_send_ack(ACK); I2C_write_byte(address); I2C_send_ack(NO_ACK); I2C_stop(); I2C_start(); I2C_write_byte(READ24C02); I2C_send_ack(ACK); dat=I2C_read_byte(); I2C_send_ack(NO_ACK); I2C_stop(); temp=dat/16; dat=dat%16; dat=dat+temp*10; return (dat); } /***********************************************************************/ void Write_7219_byte(unsigned char temp) { unsigned char i; for (i=0;i<8;i++) { CLK=LOW; DIN=(bit)(temp&MSB); temp<<=1; CLK=HIGH; } } /***********************************************************************/ void Write_7219(unsigned char address,unsigned char dat) { LOAD=LOW; Write_7219_byte(address); Write_7219_byte(dat); LOAD=HIGH; } /***********************************************************************/ void Init_7219(void) { Write_7219(SHUT_DOWN, 0x01); //Normal Operation XXXXXXX1 Shutdown Mode XXXXXXXX0 Write_7219(DISPLAY_TEST,0x00); //Normal Operation XXXXXXX0 Display Test Mode XXXXXXXX1 Write_7219(DECODE_MODE, 0xfb); //Decode Mode Select D7~D0 1 B decode 0 No decode Write_7219(SCAN_LIMIT, 0x07); //SCAN LIMIT 0~7 0xX0~0xX7 Write_7219(INTENSITY, 0x04); //Set Intensity 0xX0~0xXf } /***********************************************************************/ void Display(void) { unsigned char temp; AT24C02_write(0x00,count[1]); temp=AT24C02_read(0x00); Write_7219(7,temp/10); Write_7219(8,temp%10); Write_7219(1,2); Write_7219(2,4); Write_7219(3,0x4e);//no decode mode 'c' Write_7219(4,0); Write_7219(5,2); Write_7219(6,0x0f); AT24C02_write(0x00,count[1]); }
|