当前位置:网站首页>[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边栏推荐
- 编译文件时报错:错误: 编码GBK的不可映射字符
- Pytorch multi card distributed training distributeddataparallel usage
- 2022年已过半,得抓紧
- 递归:快速排序,归并排序和堆排序
- 错误 C2694 “void Logger::log(nvinfer1::ILogger::Severity,const char *)”: 重写虚函数的限制性异常规范比基类虚成员函数
- Recursive use and multi-dimensional array object to one-dimensional array object
- 2022 Shandong Province safety officer C certificate examination questions and Shandong Province safety officer C certificate simulation examination question bank
- 释放数据力量的Ceph-尚文网络xUP楠哥
- 渤、黄海的潮汐特征
- [MySQL] the difference between left join, right join and join
猜你喜欢

Téléchargement et installation du client Filezilla

What is pytorch? Is pytorch a software?

编译文件时报错:错误: 编码GBK的不可映射字符

Nanning water leakage detection: warmly congratulate Guangxi Zhongshui on winning the first famous brand in Guangxi

Elsevier latex submitted the article pdftex def Error: File `thumbnails/cas-email. jpeg‘ not found: using draf

SAP ui5 application development tutorial 105 - detailed introduction to the linkage effect implementation of SAP ui5 master detail layout mode

Web会话管理安全问题

Download and install captura and configure ffmpeg in captura

MongoDB安装 & 部署

用Three.js做一個簡單的3D場景
随机推荐
Lvgl usage experience
可分离债券与可转债
2022年已过半,得抓紧
Stepping on pits and solutions when using inputfilter to limit EditText
What is pytorch? Is pytorch a software?
在 .NET 6 项目中使用 Startup.cs
学会pytorch能干什么?
Separable bonds and convertible bonds
用Three.js做一個簡單的3D場景
leetcode:动态规划模板
shardingsphere动态数据源
Bid farewell to artificial mental retardation: Mengzi open source project team received RMB 100 million financing to help NLP develop
Using jasmine to monitor constructors - spying on a constructor using Jasmine
Basic operations of mongodb [add, delete, modify, query]
[combinatorics] brief introduction to generating function (definition of generating function | Newton binomial coefficient | commonly used generating function | correlation with constant | correlation
[mathematical logic] propositions and connectives (propositions | propositional symbolization | truth connectives | no | conjunction | disjunction | non truth connectives | implication | equivalence)
For instruction, uploading pictures and display effect optimization of simple wechat applet development
IPv6过渡技术-6to4手工隧道配置实验--尚文网络奎哥
Applet (continuous update)
[leetcode question brushing day 34] 540 Unique element in array, 384 Disrupt array, 202 Happy number, 149 Maximum number of points on a line