AD/DA conversion (program)

Publisher:科技思想家Latest update time:2022-04-14 Source: eefocusKeywords:conversion Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#include

#include "./delay/delay.h"

 

 

#define LCD_WRITE_DATA 1

#define LCD_WRITE_COM 0

#define LCDPORT P0

#define SUCC 0

#define ERR 1

 

 

sbit RS = P2^4;

sbit RW = P2^5;

sbit E = P2^6;

 

 

sbit SCL = P1^0;

sbit SDA = P1^1;

bit ack = 0;

 

 

void iic_start()

{

SDA = 1;

SCL = 1;

delay_us(1);

SDA = 0;

delay_us(1);

SCL = 0;

}

 

 

void iic_stop()

{

SDA = 0;

SCL = 1;

delay_us(1);

SDA = 1;

delay_us(1);

SCL = 0;

 

 

}

 

 

bit iic_send_byte(unsigned char byte)

{

unsigned char i;

SDA = 1;

for(i = 0;i < 8;i++)

{

SDA = byte & 0x80;

SCL = 1;

delay_us(1);

SCL = 0;

byte <<= 1;

}

SCL = 1;

SDA = 1;

delay_us(1);

if(0 == SDA)

{

ack = 1;

}

else

{

ack = 0;

}

SCL = 0;

return ack;

}

 

 

void iic_noack()

{

SDA = 1;

SCL = 1;

delay_us(1);

SCL = 0;

}

 

 

void iic_ack()

{

SDA = 0;

SCL = 1;

delay_us(1);

SCL = 0;

}

 

 

unsigned char iic_rcv_byte()

{

unsigned char i;

unsigned char temp = 0;

unsigned char a;

SDA = 1;

for(i = 0;i < 8;i++)

{

SCL = 0;

delay_us(1);

SCL = 1;

if(SDA == 1)

{

a = 0x01;

}

else

{

a = 0x0;

}

temp |= (a<<(7-i));

delay_us(1);

}

SCL = 0;

return temp;

}

 

 

unsigned char AT24C02_send_str(unsigned char devaddr,unsigned char romaddr,unsigned char *s,unsigned char num)

{

unsigned char i;

iic_start();

iic_send_byte(devaddr);

if(0 == ack) return ERR;

iic_send_byte(romaddr);

if(0 == ack) return ERR;

for(i = 0;i < num;i++)

{

iic_send_byte(*s);

if(0 == ack) return ERR;

s++;

}

iic_stop();

return SUCC;

 

 

}

 

 

unsigned char AT24C02_rcv_byte(unsigned char devaddr,unsigned char romaddr,unsigned char *s,unsigned char num)

{

unsigned char i;

iic_start();

iic_send_byte(devaddr);

if(0 == ack) return ERR;

iic_send_byte(romaddr);

if(0 == ack) return ERR;

iic_start();

iic_send_byte(devaddr + 1);

for(i = 0;i < num - 1;i++)

{

*s = iic_rcv_byte();

iic_ack();

s++;

 

 

}

*s = iic_rcv_byte();

iic_noack();

iic_stop();

return SUCC;

}

 

 

 

 

void lcd1602_write_data(unsigned char byte,unsigned char flag)

{

if(flag)

{

RS = 1;

}

else

{

RS = 0;

}

RW = 0;

E = 1;

LCDPORT = byte;

delay_us(5);

E = 0;

}

 

 

void lcd_init()

{

delay_ms(15);

lcd1602_write_data(0x38,LCD_WRITE_COM);

delay_ms(5);

lcd1602_write_data(0x38,LCD_WRITE_COM);

delay_ms(5);

lcd1602_write_data(0x38,LCD_WRITE_COM);

delay_ms(5);

lcd1602_write_data(0x38,LCD_WRITE_COM);

delay_ms(5);

lcd1602_write_data(0x08,LCD_WRITE_COM);

delay_ms(5);

lcd1602_write_data(0x01,LCD_WRITE_COM);

delay_ms(5);

lcd1602_write_data(0x06,LCD_WRITE_COM);

delay_ms(5);

lcd1602_write_data(0x0c,LCD_WRITE_COM);

delay_ms(5);

}

 

 

void lcd_write_str(unsigned char x,unsigned char y,unsigned char *p)

{

if((x > 15) || (y > 1))

{

return;

}

if(0 == y)

{

lcd1602_write_data(0x80+x,LCD_WRITE_COM);

}

else

{

lcd1602_write_data(0x80+0x40+x,LCD_WRITE_COM);

}

 

 

while(*p != '')

{

  lcd1602_write_data(*p,LCD_WRITE_DATA);

p++;

}

}

 

 

unsigned char AD_read()

{

unsigned char temp;

iic_start();

iic_send_byte(0x90);

if(0 == ack) return ERR;

iic_send_byte(0x40);

iic_start();

iic_send_byte(0x90+1);

if(0 == ack) return ERR;

}

temp = iic_rcv_byte();

iic_noack();

iic_stop();

return temp;

 

 

 

 

void lcd_dis_char(unsigned char x, unsigned char y, unsigned char byte)

{

    if((x > 15) || (y > 1))

{

  return ;

}

if(0 == y)

{

  lcd1602_write_data(0x80 + x,LCD_WRITE_COM);

}

else

{

  lcd1602_write_data(0x80 + 0x40 + x,LCD_WRITE_COM);

}

lcd1602_write_data(byte,LCD_WRITE_DATA);

}

 

 

unsigned char DA_write(unsigned char num)

{

iic_start();

iic_send_byte(0x90);

if(0 == ack) return ERR;

iic_send_byte(0x40);

if(0 == ack) return ERR;

iic_send_byte(num);

if(0 == ack) return ERR;

iic_stop();

return SUCC;

}

 

 

void main()

{

unsigned char num;

unsigned char test;

unsigned char v;

lcd_init();

    while(1)

{

test = AD_read();

v = test*100 / 51; 

lcd_dis_char(0,0,(test/100)+0x30);

lcd_dis_char(1,0,(test%100/10)+0x30);

lcd_dis_char(2,0,(test%10)+0x30);

lcd_dis_char(0,1,(v/100)+0x30);

lcd_dis_char(1,1,'.');

lcd_dis_char(2,1,(v%100)/10+0x30);

lcd_dis_char(3,1,(v%10)+0x30);

DA_write(num);

num++;

delay_ms(2);

}

}

Keywords:conversion Reference address:AD/DA conversion (program)

Previous article:High impedance state and I/O port working state
Next article:80C51 serial port structure and principle (2-program)

Recommended ReadingLatest update time:2024-11-17 05:00

Design of multi-channel AD sampling controller based on MPC5634
  As the requirements and dependence of automobiles on control systems increase, the design module of AD sampling has become an important component of automobile controllers. The results of AD sampling are the basis for automobile controllers to control actuators, and its speed and accuracy play an important role in a
[Power Management]
Design of multi-channel AD sampling controller based on MPC5634
PCF8591 AD/DA conversion MSP430 program
PCF8591 also uses IIC bus. With the bus protocol written last night, it is not difficult. The next step is to consider making an imprecise signal generator. Go to bed! The source code of the iic.h file is here: http://www.51hei.com/mcu/2319.html   #ifndef __PCF8591_h #define __PCF8591_h #include "basic.h" #inclu
[Microcontroller]
Can Mitsubishi plcFx2N2AD connect two pressure sensors?
Mitsubishi PLC FX2N-2AD is a programmable logic controller with analog input function, which can receive analog signals and convert them into digital signals for further processing and control. In some application scenarios, it may be necessary to connect two pressure sensors at the same time to realize the monitori
[Embedded]
ADI introduces high power efficiency, zero drift instrumentation amplifier AD8237
Beijing, August 15, 2012 /PRNewswire-Asia/ -- Analog Devices, Inc. (NASDAQ: ADI), a leading global supplier of high-performance signal processing solutions, recently launched the AD8237, a micropower, zero-drift precision instrumentation amplifier, which provides a high-efficiency solution for precision signal and sen
[Analog Electronics]
A Design of Signal Generator Based on AD9857
1 Introduction Circuit system design and testing require a variety of signal sources, which are the basic building blocks of circuit experiments. The signal generators currently used in circuit design and testing are usually composed of hardware circuit modules. This type of signal generator is not only costly, but a
[Power Management]
A Design of Signal Generator Based on AD9857
Universal DA converter circuit diagram of single power supply
Universal DA converter circuit diagram of single power supply
[Analog Electronics]
Universal DA converter circuit diagram of single power supply
DA conversion-15~15v voltage source design
               #include         #include         #include            #define uchar unsigned char         #define uint unsigned int         float num=51;     float temp;             uchar val;        uchar aa ;                                                unsigned char code disp_code ={              
[Microcontroller]
DA conversion-15~15v voltage source design
STC12C5410AD AD conversion sample program
#include"STC12C5410AD.h"  #define uchar unsigned char  unsigned char aa,bb;  void delay(uchar dd)  {  int x,y;  for(x=dd;x>0;x--)  for(y=100;y>0;y--);  }  unsigned char Read_ADC(unsigned char adcvcc)  {  //Set P1.0--P1.1 to the mode suitable for AD conversion  ADC_CONTR = ADC_CONTR|0x80; //1000,0000 turn on
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号