当前位置:网站首页>[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
边栏推荐
- 如何迈向IPv6之IPv6过渡技术-尚文网络奎哥
- FileZilla Client下载安装
- 渤、黄海的潮汐特征
- Shardingsphere dynamic data source
- Download and install node, NPM and yarn
- Recursion: quick sort, merge sort and heap sort
- Nanning water leakage detection: warmly congratulate Guangxi Zhongshui on winning the first famous brand in Guangxi
- Numpy warning visibledeprecationwarning: creating an ndarray from ragged needed sequences
- [national programming] [software programming - Lecture Video] [zero foundation introduction to practical application]
- Lvgl usage experience
猜你喜欢
pytorch开源吗?
Makefile demo
PHP generates PDF tcpdf
Hutool动态添加定时任务
Mysql Mac版下载安装教程
docker安装及启动mysql服务
How to move towards IPv6: IPv6 Transition Technology - Shangwen network quigo
ffmpeg之 一张/多张图片合成视频
2022 polymerization process examination questions and polymerization process examination skills
Wechat applet + Alibaba IOT platform + Hezhou air724ug build a serverless IOT system (III) -- wechat applet is directly connected to Alibaba IOT platform aliiot
随机推荐
[set theory] partial order relation (partial order relation definition | partial order set definition | greater than or equal to relation | less than or equal to relation | integer division relation |
编译文件时报错:错误: 编码GBK的不可映射字符
Latest version of NPM: the "NPM" item cannot be recognized as the name of a cmdlet, function, script file, or runnable program. Please check
Using jasmine to monitor constructors - spying on a constructor using Jasmine
【全民编程】《软件编程-讲课视频》【零基础入门到实战应用】
Use three JS make a simple 3D scene
QQ小程序开发之 一些前期准备:预约开发账号、下载安装开发者工具、创建qq小程序
C语言HashTable/HashSet库汇总
Lvgl usage experience
Simple wechat applet development page Jump, data binding, obtaining user information, obtaining user location information
npm : 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
FileZilla Client下载安装
In Net 6 project using startup cs
Téléchargement et installation du client Filezilla
Dynamic programming: longest common substring and longest common subsequence
MongoDB复制集【主从复制】
Is pytorch difficult to learn? How to learn pytorch well?
Mongodb installation & Deployment
Web session management security issues
Download and install node, NPM and yarn