单片机AD采集的C程序

单片机AD采集的C程序,第1张

AD0809的采集程序

//---A/D转换---

//-----头文件引用------

#include <Reg51h>

#include <absacch>

#include <intrinsh>

typedef unsigned char BYTE; /自定义字节类型/

#define Set_Bit(BIT) (BIT = 1) /定义置1函数/

#define Clear_Bit(BIT) (BIT = 0) /定义清0函数/

//

void Write_Hd7279(BYTE,BYTE); /定义HD7279写函数/

BYTE Read_Hd7279(BYTE); /定义HD7279读函数/

void Send_Byte(BYTE); /定义HD7279发送字节函数/

BYTE Receive_Byte(void); /定义HD7279接收字节函数/

void Short_Delay(void); /定义短延时函数/

void Long_Delay(void); /定义长延时函数/

void Mcu_Init(void); /定义MCU初始化函数/

void Delay_200_mS(void); /定义200ms延时函数/

sbit Hd7279_Clk=P1^6; /定义HD7279时钟硬件连接/

sbit Hd7279_Data=P1^5; /定义HD7279数据硬件连接/

sbit cs=P1^7;

void Short_Delay(void) /短延时函数/

{

BYTE i;

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

}

//

void Long_Delay(void) /长延时函数/

{

BYTE i;

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

}

//

void Write_Hd7279(BYTE Command,BYTE Data) /HD7279写函数/

{

Send_Byte(Command);

Send_Byte(Data);

}

//

void Send_Byte(BYTE Data_Out) /HD7279发送字节函数/

{

BYTE i;

cs=0;

Long_Delay();

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

{

if(Data_Out&0x80) Set_Bit(Hd7279_Data);

else Clear_Bit(Hd7279_Data);

Set_Bit(Hd7279_Clk);

Short_Delay();

Clear_Bit(Hd7279_Clk);

Short_Delay();

Data_Out=Data_Out<<1;

}

Clear_Bit(Hd7279_Data);

}

//-----宏声明-----

#define A_DPORT XBYTE[0xFef3]//0809通道0地址

#define uchar unsigned char

//-----变量定义-----

bit bdata bz=0;//定义标志

uchar val;

//-----初始化-----

void first(void)

{

P1=0xff;

P2=0xff;

P3=0xff;

P0=0xff;

Send_Byte(0xa4);

IT1=1;

EX1=1;

EA=1; //INT0 允许

}

//-----中断-----

void int_0(void) interrupt 2

{

val=A_DPORT; //读 A_D 数据

bz=1; //置读数标志

}

//-----主程序-----

main()

{

first(); //初始化

while(1)

{

A_DPORT=val; //启动 A_D

while(bz==0); //等待 A_D 转换结束

// val=~A_DPORT;

//P1=val; //数据输出

Write_Hd7279(0xc8,val&0x0f);

Write_Hd7279(0xc9,val>>4);

Write_Hd7279(0x92,0x00);

Write_Hd7279(0x93,0x00);

Write_Hd7279(0x94,0x00);

Write_Hd7279(0x95,0x00);

Write_Hd7279(0xce,0x0d);

Write_Hd7279(0xcf,0);

bz=0; //清读数标志

}

}

这就是C的程序

>

应该不是判断忙碌或者不只是判断忙碌,这个语句应该是送了一串命令进去

//HD44780 LCD DRIVER

#include <AT89X52H>

#include <ctypeh>

//LCD Commands

#define LCD_CLS 1 //Clears entire display and sets DDRAM address 0

#define LCD_HOME 2 //Sets DDRAM address 0 in address counter

#define LCD_SETMODE 4 //Sets cursor move direction and specifies display shift

#define LCD_SETVISIBLE 8 //Sets entire display (D) on/off,cursor on/off (C), and blinking of cursor position character (B)

#define LCD_SHIFT 16 //Moves cursor and shifts display without changing DDRAM contents

#define LCD_SETFUNCTION 32 //Sets interface data length (DL), number of display lines (N), and character font (F)

#define LCD_SETCGADDR 64 //Sets CGRAM addressCGRAM data is sent and received after this setting

#define LCD_SETDDADDR 128 //Sets DDRAM address DDRAM data is sent and received after this setting

#define TURE 1

#define FORSE 0

#define TMH 0xf8 //delay timeh 2ms

#define TML 0xCD //delay timel

//------------------------------------------

#define MAX_DISPLAY_CHAR 2

//-----------------------------------------

unsigned char code text[6]="hellow";

static unsigned char data counter,a;

void wcchar(unsigned char d); //write a command char

void wdchar(unsigned char i); //write a data char

char wtbusy(); //wait busy sign

void clrscr(void); //clear screen

void initime0(void); //initial time0

void delay(unsigned char i); //delay

//-------------------------------

unsigned char crc8(unsigned char pData,unsigned char count);

unsigned char pData[]={0x33,0x12,0x34,0x56,0x78,0x33,0x12,0x23,0x45,0x56};

unsigned char data crc;

static char outputbuffer[MAX_DISPLAY_CHAR];

char calc_decascii(char num);

//-------------------------------

void ISR_Timer0(void) interrupt 1

{

TL0=TML;

TH0=TMH;

counter--;

}

void ISR_Int0(void) interrupt 0

{

wdchar(text[a++]); //display "hellow"

if (a>=6)

{a=0;wdchar(' ');}

}

//main program display "hellow"

main(void)

{

unsigned char data k;

initime0();

wcchar(0x38); //initial lcd

wcchar(LCD_SETVISIBLE+6); //set lcd visible

clrscr(); //clear lcd

while(1)

{

crc8(&pData,10);

calc_decascii(crc);

wdchar (outputbuffer[1]);

wdchar (outputbuffer[2]);

wdchar (' ');

for(k=0;k<=6;k++)

{

// wdchar(text[k]); //display "hellow"

P1_2=0;

delay(55); //delay

P1_2=1;

delay(55); //delay

}

}

}

//sub function || display a char

void wdchar(unsigned char i) //write a char

{

unsigned char xdata j;

P1_0=1;

P1_1=0;

j=0x8000;

j=i;

while (wtbusy())

{}

}

void wcchar(unsigned char d) //write a command char

{

unsigned char xdata j;

P1_0=0;

P1_1=0;

j=0x8000;

j=d;

while (wtbusy())

{}

}

char wtbusy() //wait busy sign

{

unsigned char xdata j;

P1_0=0;

P1_1=1;

j=0x8000;

if(j&0x80)

{

return TURE;

}

else

{

return FORSE;

}

}

//clear screen

void clrscr()

{

wcchar(LCD_CLS);

}

//initial time0

void initime0()

{

TL0=TML;

TH0=TMH;

TR0=TURE;

IE=0x93; //Enable T0 and Serial Port

}

//sub function time delay

void delay(unsigned char i)

{

counter=i;

while (counter)

{}

}

unsigned char crc8(unsigned char pData,unsigned char count)

{

// unsigned char crc;

crc = 0;

while (count-- > 0)

{

crc ^= pData++;

}

return crc;

}

char calc_decascii(char num)

// A rather messy function to convert a floating

// point number into an ASCII string

{ long data temp = num;

char data arrayptr = &outputbuffer[MAX_DISPLAY_CHAR];

long data divisor = 10;

long data result;

char data remainder,asciival;

int data i;

// If the result of the calculation is zero

// insert a zero in the buffer and finish

if (!temp)

{ arrayptr = 48;

goto done;

}

// Handle Negative Numbers

if (temp < 0)

{ outputbuffer[0] = '-';

temp -= 2temp;

}

for (i=0 ; i < sizeof(outputbuffer) ; i++)

{ remainder = temp % divisor;

result = temp / divisor;

// If we run off the end of the number insert a space into

// the buffer

if ((!remainder) && (!result))

{ arrayptr = ' ';}

// We're in business - store the digit offsetting

// by 48 decimal to account for the ascii value

else

{ asciival = remainder + 48;

arrayptr = asciival;

}

temp /= 10;

// Save a place for a negative sign

if (arrayptr != &outputbuffer[1]) arrayptr--;

}

done: return outputbuffer;

}

1602液晶的程序我有,要做数字锁的话其实用到液晶也不多。

因为你显示密码是时候不应该都是现实吗?

你只需把键盘写入的数据存在一个数组中,然后跟密码数组对比就好了。

还有就是值得注意的是数据类型问题。

输入的键值看你处理的方式而定,密码存放的格式,还有输出显示的是ascll码。注意转换。

下面附带一段51的1602LCD的C程序,自己仔细琢磨。

#include<reg52h>

#define uchar unsigned char

#define uint unsigned int

uchar table[16]="abcdefghijklmnyz";

uchar table1[16]="0123456789abcdef";

sbit lcden=P2^0;

sbit lcdrs=P2^1;

sbit dula=P2^6;

sbit wela=P2^7;

uchar num;

void delay(uint z)

{

uint x,y;

for(x=z;x>0;x--)

for(y=110;y>0;y--);

}

void write_com(uchar com)

{ wela=0;

lcdrs=0;

P0=com;

delay(5);

lcden=1;

delay(5);

lcden=0;

}

void write_data(uchar date)

{ wela=0;

lcdrs=1;

P0=date;

delay(5);

lcden=1;

delay(5);

lcden=0;

}

void init()

{

lcden=0;

write_com(0x38);

write_com(0x0e);

write_com(0x06);

write_com(0x01);

write_com(0x80);

}

void main()

{

init();

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

{

write_data(table[num]);

delay(20);

}

write_com(1);

write_com(0x80+0x40);

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

{

write_data(table1[num]);

delay(20);

}

while(1);

}

这是吴鉴鹰单片机开发板配套的例程。

/-----------------------------------------------

名称:IIC协议 PCF8591 AD/DA转换

内容:使用4路AD中的4路检测外部模拟量输入 使用液晶显示

------------------------------------------------/

#include <reg52h>

#include "i2ch"

#include "delayh"

#include "1602h"

#include <stdioh>

#define AddWr 0x90   //写数据地址

#define AddRd 0x91   //读数据地址

extern bit ack;

unsigned char ReadADC(unsigned char Chl);

bit WriteDAC(unsigned char dat);

/------------------------------------------------

主程序

------------------------------------------------/

main()

{

unsigned char num=0,i;

unsigned char temp[7];//定义显示区域临时存储数组

float Voltage;        //定义浮点变量

LCD_Init();           //初始化液晶

DelayMs(20);          //延时有助于稳定

LCD_Clear();          //清屏

while (1)             //主循环

{

for(i=0;i<5;i++)                  //连续读5次,取最后一次,以便读取稳定值

num=ReadADC(0);                //读取第1路电压值,范围是0-255

Voltage=(float)num5/256;         //根据参考电源VREF算出时间电压,float是强制转换符号,用于将结果转换成浮点型

sprintf(temp,"V0 %32f ",Voltage);//格式输出电压值,%32f 表示浮点输出,共3位数,小数点后2位

LCD_Write_String(0,0,temp);

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

num=ReadADC(1);

Voltage=(float)num5/256;

sprintf(temp,"V1 %32f ",Voltage);

LCD_Write_String(8,0,temp);

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

num=ReadADC(2);

Voltage=(float)num5/256;

sprintf(temp,"V2 %32f ",Voltage);

LCD_Write_String(0,1,temp);

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

num=ReadADC(3);

Voltage=(float)num5/256;

sprintf(temp,"V3 %32f ",Voltage);

LCD_Write_String(8,1,temp);

//主循环中添加其他需要一直工作的程序

DelayMs(200);

}

}

/------------------------------------------------

读AD转值程序

输入参数 Chl 表示需要转换的通道,范围从0-3

返回值范围0-255

*** 作分四步:

(1)、发送地址字节,选择该器件。

(2)、发送控制字节,选择相应通道。

(3)、重新发送地址字节,选择该器件的读写。

(4)、接收目标通道的数据。

------------------------------------------------/

unsigned char ReadADC(unsigned char Chl)

{

unsigned char Val;

Start_I2c();                 //启动总线

SendByte(AddWr);             //发送器件地址

if(ack==0)return(0);

SendByte(0x40|Chl);          //发送器件子地址

if(ack==0)return(0);

Start_I2c();

SendByte(AddWr+1);           //1001 0001 是读命令

if(ack==0)return(0);

Val=RcvByte();

NoAck_I2c();                 //发送非应位

Stop_I2c();                  //结束总线

return(Val);

}

首先要确定原因出在哪里,是输入的模拟电压为0?还是AD转换电路不对,转换结果为0,如果转换结果不为0,有数据,就是1602的显示问题。这要具体找出原因来,才好解决吗。

是实物开发板?还是仿真?先不用LCD1602显示,用数码管显示一下A/D转换的结果,看倒底有没有转换数据。

以上就是关于单片机AD采集的C程序全部的内容,包括:单片机AD采集的C程序、跪求,经过MAX197A/D转换、单片机AT89C51处理、1602编程显示, 显示电流、电压 功率、功率因数编程。、关于单片机液晶1602的程序片段问题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/9693802.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-01
下一篇2023-05-01

发表评论

登录后才能评论

评论列表(0条)

    保存