蓝桥杯pcf8591读和写

Posted 一心向月

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了蓝桥杯pcf8591读和写相关的知识,希望对你有一定的参考价值。

读取的是通道3,通过电位器2调节电压。然后输出的是5V电压,用万能表测量。
main.c文件

#include "stc15f2k60s2.h"
#include "iic.h"

typedef unsigned char u8;
typedef unsigned int u16;

u8 code duan[12]=0xc0,0xf9,0xa4,0xb0,0x99,0x92,
				  0x82,0xf8,0x80,0x90,0xff,0xbf;

u16 Rb2=0;

void delay(u16 i)

	while(i--);


void Chan_hc573(chose)

	switch(chose)
	
		case 4:
			P2 = ( P2 & 0x1f ) | 0x80; break;
		case 5:
			P2 = ( P2 & 0x1f ) | 0xa0; break;
		case 6:
			P2 = ( P2 & 0x1f ) | 0xc0; break;
		case 7:
			P2 = ( P2 & 0x1f ) | 0xe0; break;
	
	P2 = ( P2 & 0x1f ) | 0x00;


void Smg_display(u8 com,u8 dat)

	P0=0xff;
	Chan_hc573(7);
	P0=0x01<<com;
	Chan_hc573(6);
	P0=dat;
	Chan_hc573(7);


void Close_smg()

	P0=0x00;
	Chan_hc573(6);
	P0=0xff;
	Chan_hc573(7);


void Display_temp()

	Smg_display(0,duan[10]); delay(500);
	Smg_display(1,duan[10]); delay(500);
	Smg_display(2,duan[10]); delay(500);
	Smg_display(3,duan[10]); delay(500);
	Smg_display(4,duan[10]); delay(500);
	Smg_display(5,duan[Rb2/100]+0x80); delay(500);
	Smg_display(6,duan[Rb2/10%10]); delay(500);
	Smg_display(7,duan[Rb2%10]); delay(500);
	Close_smg();


void Init_system()

	P0=0x00;
	Chan_hc573(5);
	P0=0xff;
	Chan_hc573(4);


void main()

	Init_system();
	while(1)
	
		Rb2=read_pcf8591(3)/0.51;
		wire_pcf8591(255);
		Display_temp();
	

iic.c文件

#include "reg52.h"
#include "intrins.h"
#include "iic.h"

#define DELAY_TIME 5

#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1

//×ÜÏßÒý½Å¶¨Òå
sbit SDA = P2^1;  /* Êý¾ÝÏß */
sbit SCL = P2^0;  /* ʱÖÓÏß */

void IIC_Delay(unsigned char i)

    do_nop_();
    while(i--);        

//×ÜÏßÆô¶¯Ìõ¼þ
void IIC_Start(void)

    SDA = 1;
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SDA = 0;
    IIC_Delay(DELAY_TIME);
    SCL = 0;	

//×ÜÏßÍ£Ö¹Ìõ¼þ
void IIC_Stop(void)

    SDA = 0;
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SDA = 1;
    IIC_Delay(DELAY_TIME);

//·¢ËÍÓ¦´ð
void IIC_SendAck(bit ackbit)

    SCL = 0;
    SDA = ackbit;  					// 0£ºÓ¦´ð£¬1£º·ÇÓ¦´ð
    IIC_Delay(DELAY_TIME);
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SCL = 0; 
    SDA = 1;
    IIC_Delay(DELAY_TIME);

//µÈ´ýÓ¦´ð
bit IIC_WaitAck(void)

    bit ackbit;
	
    SCL  = 1;
    IIC_Delay(DELAY_TIME);
    ackbit = SDA;
    SCL = 0;
    IIC_Delay(DELAY_TIME);
    return ackbit;

//ͨ¹ýI2C×ÜÏß·¢ËÍÊý¾Ý
void IIC_SendByte(unsigned char byt)

    unsigned char i;

    for(i=0; i<8; i++)
    
        SCL  = 0;
        IIC_Delay(DELAY_TIME);
        if(byt & 0x80) SDA  = 1;
        else SDA  = 0;
        IIC_Delay(DELAY_TIME);
        SCL = 1;
        byt <<= 1;
        IIC_Delay(DELAY_TIME);
    
    SCL  = 0;  

//´ÓI2C×ÜÏßÉϽÓÊÕÊý¾Ý
unsigned char IIC_RecByte(void)

    unsigned char i, da;
    for(i=0; i<8; i++)
       
    	SCL = 1;
	IIC_Delay(DELAY_TIME);
	da <<= 1;
	if(SDA) da |= 1;
	SCL = 0;
	IIC_Delay(DELAY_TIME);
    
    return da;    


unsigned char read_pcf8591(unsigned char addr)

	unsigned char dat;
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(addr);
	IIC_WaitAck();
	IIC_Stop();
	
	IIC_Start();
	IIC_SendByte(0x91);
	IIC_WaitAck();
	dat=IIC_RecByte();
	IIC_SendAck(1);
	IIC_Stop();
	return dat;


void wire_pcf8591(unsigned char dat)

	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(0x40);
	IIC_WaitAck();
	IIC_SendByte(dat);
	IIC_WaitAck();


iic.h文件

#ifndef _IIC_H
#define _IIC_H
void wire_pcf8591(unsigned char dat);
unsigned char read_pcf8591(unsigned char addr);
	
#endif

以上是关于蓝桥杯pcf8591读和写的主要内容,如果未能解决你的问题,请参考以下文章

蓝桥杯pcf8591读和写

蓝桥杯13——PCF8591的应用(AD转换)

蓝桥杯万能模板

蓝桥杯万能模板

蓝桥杯万能模板

蓝桥杯15——PCF8591与24C02综合应用:智能照明自动控制器