树莓派i2c c语言,终于搞掂树莓派驱动I2C LCD1602 液晶 显示时间 附C 版源码

#include

#include

#include

#include

#include

#include

#include

#include

int xio;

void send_command(int comm)

{

// Send bit7-4 firstly

int buf;

buf = comm & 0xF0;

buf |= 0x04;               // RS = 0, RW = 0, EN = 1

wiringPiI2CWrite(xio,buf);

usleep(2000);

buf &= 0xFB;               // Make EN = 0

wiringPiI2CWrite(xio,buf);

// Send bit3-0 secondly

buf = (comm & 0x0F) << 4;

buf |= 0x04;               // RS = 0, RW = 0, EN = 1

wiringPiI2CWrite(xio,buf);

usleep(2000);

buf &= 0xFB;               // Make EN = 0

wiringPiI2CWrite(xio,buf);

}

void send_data(data)

{

// Send bit7-4 firstly

int buf;

buf = data & 0xF0;

buf |= 0x05;//               # RS = 1, RW = 0, EN = 1

wiringPiI2CWrite(xio,buf);

usleep(2000);

buf &= 0xFB;//               # Make EN = 0

wiringPiI2CWrite(xio,buf);

// Send bit3-0 secondly

buf = (data & 0x0F) << 4;

buf |= 0x05;//               # RS = 1, RW = 0, EN = 1

wiringPiI2CWrite(xio,buf);

usleep(2000);

buf &= 0xFB;//               # Make EN = 0

wiringPiI2CWrite(xio,buf);

}

void init_lcd(void)

{

send_command(0x33);// # Must initialize to 8-line mode at first

usleep(5000);

send_command(0x32);// # Then initialize to 4-line mode

usleep(5000);

send_command(0x28);// # 2 Lines & 5*7 dots

usleep(5000);

send_command(0x0C);// # Enable display without cursor

usleep(5000);

send_command(0x01);// # Clear Screen

}

void print_lcd(int x, int y, char* str)

{

int addr;

if( x < 0)

x = 0;

if(x > 15)

x = 15;

if(y <0)

y = 0;

if(y > 1)

y = 1;

//# Move cursor

addr = 0x80 + 0x40 * y + x;

send_command(addr);

while(*str)

send_data(*str++);

}

int main (void)

{

struct tm *t ;

time_t tim ;

char buf [32] ;

printf ("Raspberry Pi LCD test program\n") ;

xio = wiringPiI2CSetup (0x27);

if (xio < 0){

fprintf (stderr, "xio: Unable to initialise I2C: %s\n", strerror (errno));

return 1;

}

wiringPiI2CWriteReg8 (xio, 0x0a, 0x84) ;  // IOCON - set BANK bit

wiringPiI2CWriteReg8 (xio, 0x05, 0x84) ;  // IOCON - set ODR in bank 0

wiringPiI2CWriteReg8 (xio, 0x00, 0x00) ;  // Port A -> Outputs

init_lcd();

while(1)

{

tim = time (NULL) ;

t = localtime (&tim) ;

sprintf (buf, "    %02d:%02d:%02d    ", t->tm_hour, t->tm_min, t->tm_sec) ;

print_lcd(0, 0, "ucat.taobao.com");

print_lcd (0, 1, buf) ;

}

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值