发送16进制的数据,例如:
SBUF=ADC1//ADC1是AD转换的结果,16进制数据
发送ASC码的格式数据,例如:
SBUF=0X30+ADC1/100//发送百位数字
SBUF=0X30+(ADC1%100)/10//发送十位数字
SBUF=0X30+(ADC1%100)%10//发送个位数字
stm32单片机有ad转换芯片。AD5328是ADI的一款DAC芯片,可输出8路模拟量,SPI通讯,自己根据AD5328数据手册,基于STM32编写了AD5328驱动程序,并且经过项目测试,使用无问题,可单独对通道进行开关,可单独设置各通道输出电压值。#include "reg51.h"#define data_point P0
sbit EOC=P2^0
sbit ADDA=P2^1
sbit ADDB=P2^2
sbit ADDC=P2^3
sbit OE=P2^5
sbit START=P2^6
sbit CLK=P2^7
sbit ALE=P2^6
unsigned char disp[3]={0,0,0}
char code dispcode[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}
unsigned char t0count=0
unsigned int temp
double sum
unsigned char val_Integer //整数
unsigned int val_Decimal //小数
sbit k1 = P1^0
sbit k2 = P1^1
sbit k3 = P1^2
sbit k4 = P1^3
void delay(unsigned char ms)
{
unsigned char i
while(ms--)
for(i=0i<125i++)
}
void display()
{
disp[0]=disp[0]&0x7f
P3= disp[0]
k1 = 1
delay(2)
k1 = 0
P3= disp[1]
k2 = 1
delay(2)
k2 = 0
P3= disp[2]
k3 = 1
delay(2)
k3 = 0
P3= disp[3]
k4 = 1
delay(2)
k4 = 0
}
unsigned char ADC0808()
{
unsigned char d
ADDC=0
ADDB=0
ADDA=0
TR1=1
ALE=1ALE=0
START=1START=0
while(EOC==0)
OE=1
d=data_point
OE=0
TR1=1
return d
}
void covert(unsigned char x)
{
sum=x*0.0201378
val_Integer=(unsigned char)sum
val_Decimal=(unsigned int)((sum-val_Integer)*1000)
disp[3]=dispcode[val_Decimal%10]
disp[2]=dispcode[val_Decimal/10%10]
disp[1]=dispcode[val_Decimal/100]
disp[0]=dispcode[val_Integer]
}
void main()
{
TMOD=0x21
TH0=(65536-10000)/256
TL0=(65536-10000)%256
TH1=256-2
ET0=1
ET1=1
EA=1
TR0=1
OE=0
START=0
EOC=1
while(1)
{
display()
}
}
void time0() interrupt 1
{
TH0=(65536-10000)/256
TL0=(65536-10000)%256
t0count++
if(t0count==100)
{
t0count=0
covert(ADC0808())
}
}
void time1() interrupt 3
{
CLK=~CLK
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)