#include
#include
#define uint unsigned int
#define uchar unsigned char
#define addw 0x90
#define addr 0x91
sbit scl=P2^0;
sbit sda=P2^1;
sbit rs=P2^6;
sbit rw=P2^5;
sbit en=P2^7;
float Ch0value,Ch1value;
void delayms(int i)
{ int x,y;
for (x=i;x>0;x--)
for(y=110;y>0;y--);
}
void usdelay()
{
_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();
}
void start() //Start signal
{//When the clock line is at a high level, the data line changes from high to low, which is the start signal
sda=1;
usdelay();
scl=1;
usdelay();
sda=0; //The data line changes from high to low, which is the start signal
usdelay();
scl=0;
}
void stop() //End signal
{//When the clock line is at a high level, the data line changes from low to high, which is the end signal
sda=0;
usdelay();
scl=1;
usdelay();
sda=1;
usdelay();
sda=0; //This sentence is optional
scl=0; //This sentence is optional
}
void ack()//应答信号
{ uchar i;
scl=1;
usdelay();
while((sda==1)&&(i<200))i++;
scl=0;
usdelay();
}
void Noack() //No response
{
sda=1; //When the clock line is high, the data line is high, which means no response
usdelay();
scl=1;
usdelay();
scl=0;
usdelay();
}
void send_byte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
scl=0;
usdelay();
sda=(bit)(dat&0x80); //Data transmission is from high to low
//(bit) is the forced conversion command, which changes the data into a one-bit database of either 0 or 1
usdelay();
scl=1;
usdelay();
dat<<=1;
}
scl=0;
usdelay();
sda=1;//Release the bus
usdelay();
}
fly read_byte()
{
uchar i,k;
scl=0;
usdelay();
sda=1; //Release bus
usdelay();
for(i=0;i<8;i++)
{
scl=1;
usdelay();
k=(k<<1)|sda;//Judge 8 times, shift 7 times
scl=0;
usdelay();
}
return k;
}
[page]
uchar read_adc(uchar channel)
{
uchar temp;
start();
send_byte(addw);
ack();
send_byte(0x40|channel);
ack();
start();
send_byte(addr);
ack();
temp=read_byte();
Noack();
stop();
return temp;
}
void init()
{
scl=1;
sda=1;
}
/*********1602Program************************/
void write_com(suchar com)
{
rs=0;
rw=0;
en=0;
P0=com;
delayms(2);
en=1;
delayms(2);
en=0;
}
void write_dat(uchar dat)
{
rs=1;
rw=0;
en=0;
P0=dat;
delayms(2);
en=1;
delayms(2);
en=0;
}
void zfc(uchar *p)
{
while(*p!=0)
write_dat(*p++);
}
void init_1602()
{
write_com(0x38);
delayms(2);
write_com(0x0c);
delayms(2);
write_com(0x06);
delayms(2);
write_com(0x01);
write_com(0x80);
zfc("Channel 0 :");
write_com(0x80+0x40);
zfc("Channel 1 :");
}
void display()
{
uchar a0,a1,a2, b0,b1,b2;
a0=Ch0value*196/10000;
a1=Ch0value*196%10000/1000;
a2=Ch0value*196%1000/100;
b0=Ch1value*196/10000;
b1=Ch1value*196%10000/1000;
b2=Ch1value*196%1000/100;
write_com(0x80+11);
write_dat(0x30+a0);
write_dat('.');
write_dat(0x30+a1);
write_dat(0x30+a2);
write_dat('V');
write_com(0x80+0x40+11);
write_dat(0x30+b0);
write_dat('.');
write_dat(0x30+b1);
write_dat(0x30+b2);
write_dat('V');
}
void main()
{
init_1602();
init();
while(1)
{
Ch0value=read_adc(0);
delayms(50);
Ch1value=read_adc(1);
delayms(50);
display();
}
}
Previous article:DS1302 uses 1602 display and keypad to adjust time
Next article:Clock written with 32768Hz crystal oscillator of MSP430
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Dot matrix display design based on msp430
- The largest semiconductor deal in history is born! $40 billion acquisition of ARM
- PCB design experience - for reference
- Confused, what is an oscilloscope with a Z axis? Don't worry, here is an introduction
- STM32 Hefei Seminar
- "Recommend Chinese Chip" + Minsilicon (ARM core 32-bit MCU)
- Wireless communication, 5G, RF, antenna and several concepts and indicators
- Happy Lantern Festival! Guess the lantern riddles and have fun
- How to make an automatic switch when there are 3 5V input ports on a circuit board?
- "Operational Amplifier Parameter Analysis and LTspice Application Simulation" Reading Notes Part 3 - Noise