当前位置:网站首页>[Blue Bridge Road - bug free code] pcf8591 - code analysis of AD conversion
[Blue Bridge Road - bug free code] pcf8591 - code analysis of AD conversion
2022-07-03 03:49:00 【The journey of a bald girl is the sea of stars】
Read Rb2 And display the voltage value on the nixie tube , Range 0.00~5.00V, By changing the sliding rheostat Rb2 The resistance value of can change the voltage value
main.c
#include"stc15f2k60s2.h"
#include"iic.h"
#include"intrins.h"
typedef unsigned char uchar;
typedef unsigned int uint;
uchar dsp_code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar dsp_ADC[8]={0xff,0xff,0xff,0xff,0xff,0xff};
uint value;
uchar count_adc;
void Timer_Init()
{
AUXR|= 0x80;
TMOD&= 0xf0;
TL0=0xcd;
TH0=0xd4;
TR0=1;
ET0=1;
EA=1;
}
void serviceTimer() interrupt 1
{
static uchar dsp_com=0;
P0=0;P2=0xc0;P2=0;
P0=dsp_ADC[dsp_com];P2=0xe0;P2=0;
P0=1<<dsp_com;P2=0xc0;P2=0;
if(++dsp_com==8)dsp_com=0;
++count_adc;
}
void Initsystem()
{
P0=0xff;P2=0x80;P2=0;
P0=0x00;P2=0xa0;P2=0;
}
void main()
{
char i=0;
Initsystem();
for(i=0;i<5;i++)
{
value=read_adc(0x03)*1.96+0.5;
}
Timer_Init();
while(1)
{
if(count_adc>199)
{
count_adc=0;
EA=0;
value=read_adc(0x03)*1.96+0.5;
EA=1;
}
dsp_ADC[5]=dsp_code[value/100]&0x7f;
dsp_ADC[6]=dsp_code[value/10%10];
dsp_ADC[7]=dsp_code[value%10];
}
}iic.c
/*
Program description : IIC Bus driver
Software environment : Keil uVision 4.10
Hardware environment : CT107 SCM comprehensive training platform 8051,12MHz
Japan period : 2011-8-9
*/
#include "reg52.h"
#include "intrins.h"
#define DELAY_TIME 5
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
// Bus pin definition
sbit SDA = P2^1; /* cable */
sbit SCL = P2^0; /* Clock line */
void IIC_Delay(unsigned char i)
{
do
{
_nop_();
}
while(i--);
}
// Bus start condition
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}
// Bus stop condition
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
}
// Waiting for an answer
bit IIC_WaitAck(void)
{
bit ackbit;
SCL=1;
IIC_Delay(DELAY_TIME);
ackbit=SDA;
SCL=0;
IIC_Delay(DELAY_TIME);
return ackbit;
}
// adopt I2C The bus sends data
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;
IIC_WaitAck();
}
// from I2C Data is received on the bus
unsigned char IIC_RecByte(void)
{
unsigned char da;
unsigned char i;
for(i=0;i<8;i++)
{
SCL = 1;
IIC_Delay(DELAY_TIME);
da <<= 1;
if(SDA)
da |= 0x01;
SCL = 0;
IIC_Delay(DELAY_TIME);
}
IIC_WaitAck();
return da;
}
void IIC_SendAck(bit ackbit)
{
SCL=0;
SDA=ackbit;
IIC_Delay(DELAY_TIME);
SCL=1;
IIC_Delay(DELAY_TIME);
SCL=0;
IIC_Delay(DELAY_TIME);
SDA=1;
IIC_Delay(DELAY_TIME);
}
unsigned char read_adc(unsigned char add)
{
unsigned char val;
IIC_Start();
IIC_SendByte(0x90);
IIC_SendByte(add);
IIC_Start();
IIC_SendByte(0x91);
val=IIC_RecByte();
IIC_Stop();
return val;
}iic.h
#ifndef _IIC_H
#define _IIC_H
// Function declaration
void IIC_Start(void);
void IIC_Stop(void);
void IIC_SendAck(bit ackbit);
void IIC_SendByte(unsigned char byt);
bit IIC_WaitAck(void);
unsigned char IIC_RecByte(void);
unsigned char read_adc(unsigned char add);
#endif边栏推荐
- [mathematical logic] propositions and connectives (propositions | propositional symbolization | truth connectives | no | conjunction | disjunction | non truth connectives | implication | equivalence)
- CEPH Shangwen network xUP Nange that releases the power of data
- Shardingsphere dynamic data source
- leetcode:动态规划模板
- NPM: the 'NPM' item cannot be recognized as the name of a cmdlet, function, script file, or runnable program. Please check the spelling of the name. If the path is included, make sure the path is corr
- Recursive use and multi-dimensional array object to one-dimensional array object
- Table structure of Navicat export database
- js中#号的作用
- [daily question] dichotomy - find a single dog (Bushi)
- 递归:深度优先搜索
猜你喜欢

Error in compiled file: error: unmapped character encoding GBK

docker安装及启动mysql服务

Download and install captura and configure ffmpeg in captura

For instruction, uploading pictures and display effect optimization of simple wechat applet development

释放数据力量的Ceph-尚文网络xUP楠哥

MongoDB安装 & 部署

Makefile demo

Pytorch multi card distributed training distributeddataparallel usage

900w+ data, from 17s to 300ms, how to operate

简易版 微信小程序开发之for指令、上传图片及展示效果优化
随机推荐
MySQL MAC download and installation tutorial
TCP/IP模型中的重磅嘉宾TCP--尚文网络奎哥
Open Visual Studio 2010 hangs when opening a SQL file sql file
【全民编程】《软件编程-讲课视频》【零基础入门到实战应用】
Captura下载安装及在Captura配置FFmpeg
MongoDB复制集【主从复制】
Recursion: quick sort, merge sort and heap sort
FileZilla Client下载安装
Web会话管理安全问题
C语言HashTable/HashSet库汇总
机械臂速成小指南(八):运动学建模(标准DH法)
[mathematical logic] predicate logic (first-order predicate logic formula | example)
Wechat applet + Alibaba IOT platform + Hezhou air724ug built with server version system analysis
pytorch是什么?pytorch是一个软件吗?
Error c2694 "void logger:: log (nvinfer1:: ilogger:: severity, const char *)": rewrite the restrictive exception specification of virtual functions than base class virtual member functions
Stepping on pits and solutions when using inputfilter to limit EditText
Change and access of median value of listening object
如何迈向IPv6之IPv6过渡技术-尚文网络奎哥
MongoDB簡介
pytorch项目怎么跑?