51单片机ReadAD函数c语言,AD7193驱动代码 C语言
时间: 2024-01-20 19:04:29 浏览: 158
以下是51单片机ReadAD函数c语言和AD7193驱动代码C语言的示例:
51单片机ReadAD函数c语言示例:
```c
#include <reg52.h>
/*
* 函数名称:ReadAD
* 函数功能:读取AD转换值
* 函数输入:无
* 函数输出:AD转换值
*/
unsigned int ReadAD()
{
unsigned int ad_value;
//设置AD口为模拟输入
P1ASF = 0x01;
//开始AD转换
ADC_CONTR = 0x90;
//等待转换完成
while (!(ADC_CONTR & 0x20));
//读取转换结果
ad_value = ADC_RES;
ad_value |= ADC_RESL << 8;
return ad_value;
}
```
AD7193驱动代码C语言示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>
#define SPI_DEVICE "/dev/spidev0.0"
#define SPI_MODE SPI_MODE_3
#define SPI_BITS_PER_WORD 8
#define SPI_SPEED 1000000
/*
* 函数名称:AD7193_Init
* 函数功能:初始化AD7193
* 函数输入:无
* 函数输出:0--成功,其他--失败
*/
int AD7193_Init()
{
int fd;
unsigned char tx_data[6], rx_data[6];
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)tx_data,
.rx_buf = (unsigned long)rx_data,
.len = 6,
.delay_usecs = 0,
.speed_hz = SPI_SPEED,
.bits_per_word = SPI_BITS_PER_WORD,
};
//打开SPI设备
fd = open(SPI_DEVICE, O_RDWR);
if (fd < 0) {
printf("Failed to open SPI device\n");
return -1;
}
//设置SPI模式
ioctl(fd, SPI_IOC_WR_MODE, &SPI_MODE);
//设置SPI数据位数
ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &SPI_BITS_PER_WORD);
//设置SPI时钟速度
ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &SPI_SPEED);
//软复位AD7193
tx_data[0] = 0xFF;
tx_data[1] = 0xFF;
tx_data[2] = 0xFF;
tx_data[3] = 0xFF;
tx_data[4] = 0xFF;
tx_data[5] = 0xFF;
if (ioctl(fd, SPI_IOC_MESSAGE(1), &tr) < 0) {
printf("Failed to reset AD7193\n");
close(fd);
return -1;
}
//等待AD7193初始化完成
usleep(5000);
//设置AD7193配置寄存器
tx_data[0] = 0x10;
tx_data[1] = 0x00;
tx_data[2] = 0x00;
tx_data[3] = 0x00;
tx_data[4] = 0x00;
tx_data[5] = 0x00;
if (ioctl(fd, SPI_IOC_MESSAGE(1), &tr) < 0) {
printf("Failed to set AD7193 config register\n");
close(fd);
return -1;
}
//设置AD7193模式寄存器
tx_data[0] = 0x08;
tx_data[1] = 0x00;
tx_data[2] = 0x00;
tx_data[3] = 0x00;
tx_data[4] = 0x00;
tx_data[5] = 0x00;
if (ioctl(fd, SPI_IOC_MESSAGE(1), &tr) < 0) {
printf("Failed to set AD7193 mode register\n");
close(fd);
return -1;
}
//关闭SPI设备
close(fd);
return 0;
}
/*
* 函数名称:AD7193_ReadData
* 函数功能:读取AD7193转换值
* 函数输入:无
* 函数输出:AD转换值
*/
unsigned int AD7193_ReadData()
{
int fd;
unsigned char tx_data[7], rx_data[7];
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)tx_data,
.rx_buf = (unsigned long)rx_data,
.len = 7,
.delay_usecs = 0,
.speed_hz = SPI_SPEED,
.bits_per_word = SPI_BITS_PER_WORD,
};
unsigned int ad_value;
//打开SPI设备
fd = open(SPI_DEVICE, O_RDWR);
if (fd < 0) {
printf("Failed to open SPI device\n");
return -1;
}
//设置SPI模式
ioctl(fd, SPI_IOC_WR_MODE, &SPI_MODE);
//设置SPI数据位数
ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &SPI_BITS_PER_WORD);
//设置SPI时钟速度
ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &SPI_SPEED);
//读取AD7193转换值
tx_data[0] = 0x58;
tx_data[1] = 0x00;
tx_data[2] = 0x00;
tx_data[3] = 0x00;
tx_data[4] = 0x00;
tx_data[5] = 0x00;
tx_data[6] = 0x00;
if (ioctl(fd, SPI_IOC_MESSAGE(1), &tr) < 0) {
printf("Failed to read AD7193 conversion value\n");
close(fd);
return -1;
}
//计算AD转换值
ad_value = rx_data[3];
ad_value = (ad_value << 8) | rx_data[4];
ad_value = (ad_value << 8) | rx_data[5];
ad_value = (ad_value >> 8) & 0xFFFFFF;
//关闭SPI设备
close(fd);
return ad_value;
}
```
阅读全文