Created
August 15, 2012 13:24
-
-
Save francistm/3360096 to your computer and use it in GitHub Desktop.
51Hei SCM C Libs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.o | |
.swp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* for 1602 lcd display control | |
*/ | |
#include <at89x52.h> | |
#include <intrins.h> | |
#include "types.h" | |
#ifndef __1602LCDLIB_H__ | |
#define __1602LCDLIB_H__ | |
#define LCD_DATA P0 | |
#define LCD_EP P3_4 | |
#define LCD_RS P3_5 | |
#define LCD_RW P2_7 | |
void lcd_init (uchar mode); | |
void lcd_delay (); | |
void lcd_writecmd (char cmd); | |
void lcd_writedata (char dta); | |
char lcd_readdata (); | |
char lcd_readstatus (); | |
void lcd_init(uchar mode) | |
{ | |
lcd_writecmd(0x38); | |
lcd_writecmd(mode); | |
lcd_writecmd(0x01); | |
} | |
void lcd_delay() | |
{ | |
uint i; | |
for(i = 0; i < 125; i++) | |
_nop_(); | |
} | |
void lcd_writecmd(char cmd) | |
{ | |
LCD_RS = 0; | |
LCD_RW = 0; | |
LCD_EP = 0; | |
LCD_DATA = cmd; | |
lcd_delay(); | |
LCD_EP = 1; | |
lcd_delay(); | |
LCD_EP = 0; | |
} | |
void lcd_writedata(char dta) | |
{ | |
LCD_RS = 1; | |
LCD_RW = 0; | |
LCD_EP = 0; | |
LCD_DATA = dta; | |
lcd_delay(); | |
LCD_EP = 1; | |
lcd_delay(); | |
LCD_EP = 0; | |
} | |
char lcd_readdata() | |
{ | |
char status; | |
LCD_RS = 1; | |
LCD_RW = 1; | |
LCD_EP = 1; | |
lcd_delay(); | |
status = LCD_DATA; | |
LCD_EP = 0; | |
return status; | |
} | |
char lcd_readstatus() | |
{ | |
char status; | |
LCD_RS = 0; | |
LCD_RW = 1; | |
LCD_EP = 1; | |
lcd_delay(); | |
status = LCD_DATA; | |
LCD_EP = 0; | |
return status; | |
} | |
void lcd_print(char *str) | |
{ | |
while(*str != '\0') { | |
lcd_writedata(*str); | |
str++; | |
} | |
} | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __74HC164_H__ | |
#define __74HC164_H__ | |
#include <at89x52.h> | |
#include <intrins.h> | |
#include "types.h" | |
#define DATA_74HC164 P2_5 | |
#define CLCK_74HC164 P2_4 | |
void push_to_74hc164_register (char dta); | |
void push_to_74hc164_register(char dta) | |
{ | |
uchar i; | |
for(i = 0; i < 8; i++) { | |
DATA_74HC164 = dta & 0x80; | |
CLCK_74HC164 = 0; | |
CLCK_74HC164 = 1; | |
dta <<= 1; | |
} | |
} | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __DS1302_H__ | |
#define __DS1302_H__ | |
#include <at89x52.h> | |
#include "types.h" | |
#define RST_DS1302 P2_0 | |
#define SCLK_DS1302 P1_0 | |
#define IO_DS1302 P1_1 | |
void set_init_data (char *dta); | |
void push_to_ds1302_register (char dta); | |
void push_to_ds1302_address (char add, char dta); | |
void read_current_data_to_buffer (char *buff); | |
char read_from_ds1302_register (void); | |
char read_from_ds1302_address (char add); | |
void push_to_ds1302_register(char dta) | |
{ | |
char i; | |
for(i = 0; i < 8; i++) { | |
IO_DS1302 = dta & 0x01; | |
dta >>= 1; | |
SCLK_DS1302 = 0; | |
SCLK_DS1302 = 1; | |
} | |
} | |
void push_to_ds1302_address(char add, char dta) | |
{ | |
RST_DS1302 = 0; | |
SCLK_DS1302 = 0; | |
RST_DS1302 = 1; | |
push_to_ds1302_register(add); | |
push_to_ds1302_register(dta); | |
RST_DS1302 = 0; | |
IO_DS1302 = 1; | |
} | |
void set_init_data(char *dta) | |
{ | |
char i = 0; | |
char start_add = 0x80; | |
push_to_ds1302_address(0x8e, 0x00); | |
while(i < 7) { | |
push_to_ds1302_address(start_add, dta[i]); | |
start_add += 2; | |
i++; | |
} | |
push_to_ds1302_address(0x8e, 0x80); | |
} | |
char read_from_ds1302_register() | |
{ | |
char i; | |
uchar data_buff; | |
for(i = 0; i < 8; i++) { | |
if(IO_DS1302 == 1) data_buff += 0x80; | |
data_buff >>= 1; | |
SCLK_DS1302 = 1; | |
SCLK_DS1302 = 0; | |
} | |
return data_buff; | |
} | |
char read_from_ds1302_address(char add) | |
{ | |
uchar data_buff; | |
uchar data_h, data_l; | |
RST_DS1302 = 0; | |
SCLK_DS1302 = 0; | |
RST_DS1302 = 1; | |
push_to_ds1302_register(add); | |
data_buff = read_from_ds1302_register(); | |
RST_DS1302 = 0; | |
data_h = data_buff >> 4; | |
data_l = data_buff & 0x0f; | |
return data_h * 10 + data_l; | |
} | |
void read_current_data_to_buffer(char *buffer) | |
{ | |
char i, start_add = 0x81; | |
for(i = 0; i < 8; i++) { | |
buffer[i] = read_from_ds1302_address(start_add); | |
start_add += 2; | |
} | |
} | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __EEPROM_H__ | |
#define __EEPROM_H__ | |
#include <at89x52.h> | |
#include <intrins.h> | |
#include "types.h" | |
#include "i2c.h" | |
bool eeprom_write_char (char dev_add, char dta_add, char dta); | |
bool eeprom_write_chars(char dev_add, char dta_add, char *dta, uchar leng); | |
char eeprom_read_char (char dev_add, char dta_add); | |
void eeprom_read_chars (char dev_add, char dta_add, char *dta, uchar leng, char *buff); | |
bool eeprom_write_char(char dev_add, char dta_add, char dta) | |
{ | |
char status; | |
_i2c_write_adds = dev_add << 1; | |
_i2c_read_adds = (dev_add << 1) + 1; | |
_i2c_init(); | |
_i2c_write_data(_i2c_write_adds); | |
_nop_(); | |
if(_i2c_receved_ack()) { | |
_i2c_write_data(dta_add); | |
_nop_(); | |
if(_i2c_receved_ack()) { | |
_i2c_write_data(dta); | |
_nop_(); | |
if(_i2c_receved_ack()) { | |
status = 1; | |
_i2c_end(); | |
return status; | |
} | |
} | |
} | |
return 0; | |
} | |
bool eeprom_write_chars(char dev_add, char dta_add, char *dta, uchar leng) | |
{ | |
char status; | |
_i2c_write_adds = dev_add << 1; | |
_i2c_read_adds = (dev_add << 1) + 1; | |
_i2c_init(); | |
_i2c_write_data(_i2c_write_adds); | |
_nop_(); | |
if(_i2c_receved_ack()) { | |
_i2c_write_data(dta_add); | |
_nop_(); | |
if(_i2c_receved_ack()) { // data address confirmed | |
while(leng > 0) { | |
_i2c_write_data(*dta); | |
_nop_(); | |
if(!_i2c_receved_ack()) // break out if no ack singal | |
return 0; | |
dta++; | |
leng--; | |
} | |
_i2c_end(); // all data sent | |
return 1; | |
} | |
} | |
return 0; | |
} | |
char eeprom_read_char(char dev_add, char dta_add) | |
{ | |
char dta_buff; | |
_i2c_write_adds = dev_add << 1; | |
_i2c_read_adds = (dev_add << 1) + 1; | |
_i2c_init(); | |
_i2c_write_data(_i2c_write_adds); | |
_nop_(); | |
if(_i2c_receved_ack()) { | |
_i2c_write_data(dta_add); | |
_nop_(); | |
if(_i2c_receved_ack()) { // data address written | |
_i2c_init(); // a new init signal sending | |
_i2c_write_data(_i2c_read_adds); | |
_nop_(); | |
if(_i2c_receved_ack()) { | |
dta_buff = _i2c_read_data(); | |
_i2c_end(); // data receved, send end signal | |
return dta_buff; | |
} | |
} | |
} | |
return 0x00; | |
} | |
void eeprom_read_chars(char dev_add, char dta_add, char *dta, uchar leng, char *buff) | |
{ | |
char dta_buff; | |
_i2c_write_adds = dev_add << 1; | |
_i2c_read_adds = (dev_add << 1) + 1; | |
_i2c_init(); | |
_i2c_write_data(_i2c_write_adds); | |
_nop_(); | |
if(_i2c_receved_ack()) { | |
_i2c_write_data(dta_add); | |
_nop_(); | |
if(_i2c_receved_ack()) { // data address written | |
_i2c_init(); // a new init signal sending | |
_i2c_write_data(_i2c_read_adds); | |
_nop_(); | |
if(_i2c_receved_ack()) { // ready to receve data | |
while(leng > 0) { | |
*buff = _i2c_read_data(); | |
_i2c_send_ack(); | |
_nop_(); | |
buff++; | |
leng--; | |
} | |
_i2c_end(); | |
} | |
} | |
} | |
return 0x00; | |
} | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __I2C_H__ | |
#define __I2C_H__ | |
#include <at89x52.h> | |
#include <intrins.h> | |
#include "types.h" | |
#define SCL_I2C P2_1 | |
#define SDA_I2C P2_0 | |
data char _i2c_read_adds; | |
data char _i2c_write_adds; | |
void _i2c_init() | |
{ | |
SDA_I2C = 1; | |
SCL_I2C = 1; | |
SDA_I2C = 0; | |
SCL_I2C = 0; | |
} | |
void _i2c_end() | |
{ | |
SCL_I2C = 0; | |
SDA_I2C = 0; | |
SCL_I2C = 1; | |
SDA_I2C = 1; | |
} | |
char _i2c_read_data() | |
{ | |
char i, buff; | |
for(i = 0; i < 8; i++) { | |
SCL_I2C = 1; | |
_nop_(); | |
buff <<= 1; | |
if(SDA_I2C == 1) buff += 1; | |
SCL_I2C = 0; | |
} | |
return buff; | |
} | |
void _i2c_write_data(char dta) | |
{ | |
char i; | |
for(i = 0; i < 8; i++) { | |
SDA_I2C = dta & 0x80; | |
SCL_I2C = 1; | |
_nop_(); | |
SCL_I2C = 0; | |
dta <<= 1; | |
} | |
} | |
void _i2c_send_ack() | |
{ | |
SDA_I2C = 0; | |
SCL_I2C = 1; | |
SCL_I2C = 0; | |
SDA_I2C = 1; | |
} | |
void _i2c_send_no_ack() | |
{ | |
SDA_I2C = 1; | |
SCL_I2C = 1; | |
SCL_I2C = 0; | |
} | |
bool _i2c_receved_ack() | |
{ | |
bit ack; | |
SDA_I2C = 1; | |
SCL_I2C = 1; | |
ack = SDA_I2C; | |
SCL_I2C = 0; | |
return ack == 0; | |
} | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* for 8-bit nixie tube display section & position library | |
*/ | |
#include "types.h" | |
#ifndef __LEDLIB_H__ | |
#define __LEDLIB_H__ | |
/* selection will be marked as 0 */ | |
code unsigned char led_sections[] = { | |
0xff, | |
0xfe, 0xfd, 0xfb, 0xf7, | |
0xef, 0xdf, 0xbf, 0x7f, | |
}; | |
/* positions will be marked as 1 */ | |
code unsigned char led_positions[] = { | |
0x3f, 0x06, 0x5b, 0x4f, 0x66, | |
0x6d, 0x7d, 0x07, 0x7f, 0x6f, | |
0x00, | |
}; | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __SERIAL_H__ | |
#define __SERIAL_H__ | |
#include <at89x52.h> | |
void serial_send_chars(char *str) | |
{ | |
bit es_enabled; | |
if(ES) es_enabled = 1; | |
if(es_enabled) ES = 0; | |
while(*str != '\0') { | |
SBUF = *str; | |
while(!TI); | |
TI = 0; | |
str++; | |
} | |
if(es_enabled) ES = 1; | |
} | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __TYPES_H__ | |
#define __TYPES_H__ | |
typedef bit bool; | |
typedef unsigned int uint; | |
typedef unsigned char uchar; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment