蓝桥杯单片机必备知识—–(3)pcf8591–ADC

2021-04-15 15:31:54 浏览数 (1)

蓝桥杯单片机必备知识—–(3)pcf8591–ADC

IIC.H中需要添加的代码
代码语言:javascript复制
unsigned char  init_pcf8591(void);
unsigned char read_pcf8591(void);
IIC.C中需要添加的代码
代码语言:javascript复制
void init_pcf8591(void)
{
   
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(0x03);  	//ADC通道3
	IIC_WaitAck();
	IIC_Stop();
}

unsigned char adc_pcf8591(void)
{
   
	unsigned char temp;
	IIC_Start();			//总线启动条件
	IIC_SendByte(0x90);		//通过I2C总线发送数据
	IIC_WaitAck();			//等待应答
	IIC_SendByte(add);		//通过I2C总线发送数据
	IIC_WaitAck();			//等待应答
	IIC_Stop();				//总线停止条件
	
	
	IIC_Start();			//总线启动条件
	IIC_SendByte(0x91);		//通过I2C总线发送数据
	IIC_WaitAck();			//等待应答
	temp=IIC_RecByte();		//从I2C总线上接收数据
	IIC_Stop();				//总线停止条件
	
	return temp;			//返回值
}

主函数

代码语言:javascript复制
void main()
{
   
	cls_buzz(); 	//关闭buzz
	AUXR = 0x80;	//将定时器0设置为12分频,定时器时钟1T模式
	TMOD = 0x30;	//屏蔽定时器1,设置定时器0为定时模式
	TL0 = 0xcd;		//填入初始值,设置时间为1ms
	TH0 = 0xd4;
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
	ET0 = 1;		//允许定时器0中断
	EA = 1;			//开启CPU总中断允许
	init_pcf8591();	//添加pcf8591初始化函数
	while(1)
	{
   
		if(adc_flag)//读取ADC数值的标志位
		{
   
			adc_flag = 0;
			adc_value = read_pcf8591(); //读取ADC
		}
	}
}

中断函数

代码语言:javascript复制
void time0(void) interrupt 1
{
   
	if(  intr == 50) //50ms读取一次ADC的值
	{
   
		intr = 0;
		adc_flag = 1;
	}
	display();
}
测试:

明天放图

IIC.H
代码语言:javascript复制
#ifndef __IIC_H
#define __IIC_H

void IIC_Start(void); 
void IIC_Stop(void);  
bit IIC_WaitAck(void);  
void IIC_SendAck(bit ackbit); 
void IIC_SendByte(unsigned char byt); 
unsigned char IIC_RecByte(void); 

unsigned char  init_pcf8591(void);
unsigned char read_pcf8591(void);
#endif
IIC.C
代码语言:javascript复制
#include "reg52.h"
#include "intrins.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;    
}

void operate_delay(unsigned char t)
{
   
	unsigned char i;
	while(t--)
	{
   
		for(i = 0;i<112 ; i  );
	}
}

void init_pcf8591(void)
{
   
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(0x03);  //ADC通道3
	IIC_WaitAck();
	IIC_Stop();
	operate_delay(10);
}

unsigned char read_pcf8591(void)
{
   
	unsigned char temp;
	IIC_Start();
	IIC_SendByte(0x91);
	IIC_WaitAck();
	temp = IIC_RecByte();
	IIC_SendAck(1);
	IIC_Stop();
	
	return temp;
}
main.C
代码语言:javascript复制
#include <stc15f2k60s2.h>
#include "iic.h"

#define uchar unsigned char
#define uint unsigned int

code unsigned char tab[] = {
   0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
uchar dspbuf[8] = {
   10,10,10,10,10,10,10,10};
uchar adc_flag;
uchar intr = 0;
uchar adc_value;


void display();
void load();


void cls_buzz()
{
   
	P2 = (P2 & 0x1f) | 0xa0;
	P0 = 0x00;
	P2 = 0x1f;
	
	P2 = (P2 & 0x1f) | 0x80;
	P0 = 0xff;
	P2 = 0x1f;
	
}

void main()
{
   
	cls_buzz();
	AUXR = 0x80;
	TMOD = 0x00;
	TH0 = 0xcd;
	TL0 = 0xd4;
	TF0 = 0;
	TR0 = 1;
	ET0 = 1;
	EA = 1;
	init_pcf8591();
	while(1)
	{
   
		if(adc_flag)
		{
   
			adc_flag = 0;
			adc_value = read_pcf8591();
		}
	}
}

void time0(void) interrupt 1
{
   
	if(  intr == 50)
	{
   
		intr = 0;
		adc_flag = 1;
	}
	display();
}

void load()
{
   
	(adc_value>= 100)?(dspbuf[5] = adc_value/100):(dspbuf[5] = 10);       
			(adc_value>=10)?(dspbuf[6] = adc_value0/10):(dspbuf[6] = 10);       
			dspbuf[7] = adc_value; 
}

void display()
{
   
	static unsigned char dspcom = 0; 
	
	load();
	P2 = (P2 & 0x1f) | 0xe0;
	P0 = 0xff;
	P2 = 0x1f;
	
	P2 = (P2 & 0x1f) | 0xc0;
	P0 = 1<<dspcom;
	P2 = 0x1f;
	
	P2 = (P2 & 0x1f) | 0xe0;
	P0 = tab[dspbuf[dspcom]];
	P2 = 0x1f;
	
	if(  dspcom == 8) dspcom = 0;
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/100131.html原文链接:

0 人点赞