当前位置:网站首页>[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边栏推荐
- 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
- Ffmpeg recording screen and screenshot
- Hutool动态添加定时任务
- FileZilla Client下載安裝
- Error c2694 "void logger:: log (nvinfer1:: ilogger:: severity, const char *)": rewrite the restrictive exception specification of virtual functions than base class virtual member functions
- Applet (continuous update)
- golang xxx. Go code template
- 可分离债券与可转债
- 简易版 微信小程序开发之for指令、上传图片及展示效果优化
猜你喜欢

Tidal characteristics of the Bohai Sea and the Yellow Sea

Web会话管理安全问题

Web session management security issues

2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video

如何迈向IPv6之IPv6过渡技术-尚文网络奎哥

numpy之 警告VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences

FileZilla Client下載安裝

机械臂速成小指南(八):运动学建模(标准DH法)

Lvgl usage experience

TCP/IP模型中的重磅嘉宾TCP--尚文网络奎哥
随机推荐
What is pytorch? Is pytorch a software?
Arlo's thinking about himself
MongoDB安装 & 部署
Bid farewell to artificial mental retardation: Mengzi open source project team received RMB 100 million financing to help NLP develop
Change and access of median value of listening object
[mathematical logic] propositions and connectives (propositions | propositional symbolization | truth connectives | no | conjunction | disjunction | non truth connectives | implication | equivalence)
ffmpeg之 一张/多张图片合成视频
In Net 6 project using startup cs
ffmpeg录制屏幕和截屏
pytorch难学吗?如何学好pytorch?
IPv6 transition technology-6to4 manual tunnel configuration experiment -- Kuige of Shangwen network
[mathematical logic] propositional logic (propositional logic reasoning | formal structure of reasoning | inference law | additional law | simplification law | hypothetical reasoning | refusal | disju
How does the pytorch project run?
2022 P cylinder filling examination content and P cylinder filling practice examination video
pytorch怎么下载?pytorch在哪里下载?
记一次 .NET 差旅管理后台 CPU 爆高分析
PHP generates PDF tcpdf
Message queue addition failure
Small guide for rapid formation of manipulator (VIII): kinematic modeling (standard DH method)
Docker install and start MySQL service