【单片机】用iic做电池充放电

iic.c

#include 
#include "./lcd/lcd.h"
#include "./delay/delay.h"

sbit SCL = P2^2;
sbit SDA = P2^3;

bit ack;

/*
================
功能:起始信号
================
*/
void iic_start()
{
    SDA = 1;
	SCL = 1;
	delay_us(1);
	SDA = 0;
	delay_us(1);
	SCL = 0;
}

/*
================
功能:终止信号
================
*/
void iic_stop()
{
    SDA = 0;
	SCL = 1;
	delay_us(1);
	SDA = 1;
	delay_us(1);
	SCL = 0;
}

/*
====================
功能:发送一个字节
====================
*/
bit iic_send_byte(unsigned char byte)
{
    unsigned char i;
	
	for(i = 0; i < 8; i++)
	{
	    SDA = byte & 0x80;
        SCL = 1;
        delay_us(1);
		SCL = 0;
		byte <<= 1;
	}
	
	SCL = 1;
	SDA = 1;
	delay_us(1);
	
	if(0 == SDA)
	{
	    ack = 1;
	}
	else
	{
	    ack = 0;
	}
	
	SCL = 0;
	return ack;
}

/*
====================
功能:接收一个字节
====================
*/
unsigned char iic_receive_byte()
{
    unsigned char i;
	unsigned char a;
	unsigned char temp = 0;
	
	SDA = 1;
	
	for(i = 0; i < 8; i++)
	{
	    SCL = 0;
		delay_us(1);
		SCL = 1;
		
		if(SDA)
		{
		    a = 1;
		}
		else
		{ 
		    a = 0;
		}
		
		temp |= (a << (7 - i));
		delay_us(1);
	}
	
	SCL = 0;
	return temp;
}

#if 0
/*
====================
功能:应答
====================
*/
void iic_ack()
{
    SDA = 0;
	SCL = 1;
	delay_us(1);
	SCL = 0;
}
#endif

/*
====================
功能:无应答
====================
*/
void iic_noack()
{
    SDA = 1;
	SCL = 1;
	delay_us(1);
	SCL = 0;
}

#if 0
/*
====================
功能:发送字符串
====================
*/
bit iic_send_str(unsigned char sla, unsigned char suba, unsigned char *str, unsigned char len)
{
    unsigned char i;
	
	iic_start();
	
	iic_send_byte(sla);
	if(0 == ack)
	{
	    return 0;
	}
	
	iic_send_byte(suba);
	if(0 == ack)
	{
	    return 0;
	}
	
	for(i = 0; i < len; i++)
	{
	    iic_send_byte(*str);
		delay_us(1);
		
		if(0 == ack)
		{
		    return 0;
		}
		
		str++;
	}
	
	iic_stop();
	return 1;
}

/*
====================
功能:接收字符串
====================
*/
bit iic_receive_str(unsigned char sla, unsigned char suba, unsigned char *str, unsigned char len)
{
    unsigned char i;
	
	iic_start();
	
	iic_send_byte(sla);
	if(0 == ack)
	{
	    return 0;
	}
	
	iic_send_byte(suba);
	if(0 == ack)
	{
	    return 0;
	}
	
	iic_start();
	iic_send_byte(sla + 1);
	if(0 == ack)
	{
	    return 0;
	}
	
	for(i = 0; i < len - 1; i++)
	{
	    *str = iic_receive_byte();
		iic_ack();
		str++;
	}
	
	*str = iic_receive_byte();
	iic_noack();
	iic_stop();
	
	return 1;
}
#endif

unsigned char AD_read()
{
    unsigned char temp;

    iic_start();
	
	iic_send_byte(0x90);
	if(0 == ack)
	{
	    return 0;
	}
	
	iic_send_byte(0x40);    //设置通道和工作方式
	if(0 == ack)
	{
	    return 0;
	}
	
	iic_start();
	iic_send_byte(0x90 + 1);
	if(0 == ack)
	{
	    return 0;
	}
	
	temp = iic_receive_byte();
	iic_noack();
	iic_stop();
	
	return temp;
}

void main()
{
    unsigned char test;
    unsigned char n;
    float per;
    float number;

    lcd_init();
	
    while(1)
    {
	test = AD_read();
	delay_ms(20);
		
	number = (float)test / 255 * 5;

	n = (float)test / 255 * 7;
		
	per = (float)test * 100 / 255;
	
	lcd_dis_power(n);
		
	lcd_write_byte(1,3,(unsigned char)(number) % 10 + 0x30);
	lcd_write_byte(1,4,'.');
	lcd_write_byte(1,5,(unsigned char)(number * 10) % 10 + 0x30);
	lcd_write_byte(1,6,(int)(number * 100) % 10 + 0x30);
		
	lcd_write_byte(2,1,(unsigned char)(per) / 100 + 0x30);		
	lcd_write_byte(2,2,(unsigned char)(per) % 100 / 10 + 0x30);
	lcd_write_byte(2,3,(unsigned char)(per) % 10 + 0x30);
	lcd_write_byte(2,4,'%');
		
	delay_ms(5);
    }
}

lcd.c
#include 
#include "../delay/delay.h"
#include "lcd.h"

unsigned char disdata[7][8] = 
{
0x0e,0x1b,0x11,0x11,0x11,0x11,0x11,0x1f,
0x0e,0x1b,0x11,0x11,0x11,0x11,0x1f,0x1f,
0x0e,0x1b,0x11,0x11,0x11,0x1f,0x1f,0x1f,
0x0e,0x1b,0x11,0x11,0x1f,0x1f,0x1f,0x1f,
0x0e,0x1b,0x11,0x1f,0x1f,0x1f,0x1f,0x1f,
0x0e,0x1b,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,
0x0e,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,
};

void lcd_write(unsigned char byte, unsigned char flag)
{
    if(flag)
    {
	RS = 1;
    }
    else
    {
	RS = 0;
    }
	
    RW = 0;
    E = 1;
    LCDPORT = byte;
    delay_us(5);
    E = 0;
}

void lcd_init()
{
    delay_ms(15);
    lcd_write(0x38,LCD_WRITE_COM);
    delay_ms(5);
    lcd_write(0x38,LCD_WRITE_COM);
    delay_ms(5);
    lcd_write(0x38,LCD_WRITE_COM);
    delay_ms(5);
    lcd_write(0x38,LCD_WRITE_COM);
    delay_ms(5);
    lcd_write(0x08,LCD_WRITE_COM);
    delay_ms(5);
    lcd_write(0x01,LCD_WRITE_COM);
    delay_ms(5);
    lcd_write(0x06,LCD_WRITE_COM);
    delay_ms(5);
    lcd_write(0x0c,LCD_WRITE_COM);
    delay_ms(5);
}

#if 0
void lcd_dis_str(unsigned char x, unsigned char y, unsigned char *disdata)
{
    unsigned char add;
	
    if(((0 == x) || (x > 2)) || ((0 == y) || (y > 16)))
    {
	return ;
    }
	
    add = 0x80 + (x - 1) * 0x40 + (y - 1);
    lcd_write(add,LCD_WRITE_COM);
	
    while(*disdata != '\0')
    {	    
	lcd_write(*disdata,LCD_WRITE_DATA);
	disdata++;
    }
}
#endif

void lcd_write_byte(unsigned char x, unsigned char y, unsigned char byte)
{
    unsigned char add;
	
    if(((0 == x) || (x > 2)) || ((0 == y) || (y > 16)))
    {
	return ;
    }
	
    add = 0x80 + (x - 1) * 0x40 + (y - 1);
	
    lcd_write(add,LCD_WRITE_COM);	     
    lcd_write(byte,LCD_WRITE_DATA);
}

void lcd_dis_power(unsigned char num)
{
	unsigned char i;
	unsigned char j;
	
	for(i = 0; i < num; i++)
	{	
	    lcd_write(0x40,LCD_WRITE_COM);
		
	    for(j = 0; j < 8; j++)
	    {
		lcd_write(disdata[i][j],LCD_WRITE_DATA);
	    }
		
	    lcd_write(0x80,LCD_WRITE_COM);
	    lcd_write(0x00,LCD_WRITE_DATA);
		
	}
}

delay.c
/*
=============================
延时毫秒:time = 2 * t + 12
=============================
*/
void delay_us(unsigned char t)
{
    while(--t);
}

/*
=================
功能:延时微秒
=================
*/
void delay_ms(unsigned char t)
{
    while(t--)
    {
	delay_us(245);
	delay_us(245);
    }
}

/*
=================
功能:延时秒
=================
*/
#if 0
void delay_s(unsigned char t)
{
    while (t--)
    {
	delay_ms(200);
	delay_ms(200);
	delay_ms(200);
	delay_ms(200);
	delay_ms(200);
    }
}
#endif

lcd.h
#ifndef _LCD_H_
#define _LCD_H_

#define LCDPORT P0
#define LCD_WRITE_DATA 1
#define LCD_WRITE_COM 0

sbit RS = P2^4;
sbit RW = P2^5;
sbit E = P2^6;

extern void lcd_write(unsigned char byte, unsigned char flag);
extern void lcd_init();
extern void lcd_dis_str(unsigned char x, unsigned char y, unsigned char *disdata);
extern void lcd_dis_byte(unsigned char x, unsigned char y, unsigned char byte);
extern void lcd_write_byte(unsigned char x, unsigned char y, unsigned char byte);
extern void lcd_dis_power(unsigned char num);

#endif

delay.h
#ifndef _DELAY_H_
#define _DELAY_H_

extern void delay_us(unsigned char t);

extern void delay_ms(unsigned char t);

extern void delay_s(unsigned char t);

#endif


你可能感兴趣的:(单片机)