本人小白,初次接触msp430f5438a,我们老师布置的任务,让用5438a连接ad7076,以前没接触过,上课没听懂,网上找的代码原本是51的,有没有大神教一下如何把51的改成f5438a的连接,我们老师说改个脚输出输入就可以,但是我还是没弄明白,书上都说的用f5438a自带的ADC12就可以,但是我们老师不让使用那个,希望有大神可以赐教,领着入个门,告诉我一下如何改变输出输入什么都如何连接,真的是十分感谢,以后会好好学习的,再次谢谢大佬了。。。。
51的例程:/*
* This is a program for testing AD7606 used STC89C52RC
* The line connecting AD7606 with STC89C52RC is:
* STC89C52RC | AD7606
* P0 | DB[0:7]
* P2 | DB[8:15]
* OS0 | P1^0
* OS1 | P1^1
* OS2 | P1^2
* rage | P1^3
* convst | P1^4
* busy | P1^5
* rst | P1^6
* rd_and_cs | P1^7
* green_led | P3^6
* red_led | P3^7
* GND | GND
* +5V | VCC
* green_led | P3_6
* red_led | P3_7
* Author : jinming chen
* Compang : Hongwei in dongguan
* Date : Sep 1 ,2015
*/
#include
#include
#include
#include "AD7076.h"
#include "other.h"
//uchar introduce1[] = "This is a program ";
//uchar introduce2[] = "for testing AD7606\r\n";
uchar sampling[] = "The sampling data is : ";
uchar newline[] = "\r\n";
uchar space[] = " ";
uchar *DB_str[16] = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
/*
uchar *data_to_string(uchar Data)
{
uchar tmp;
tmp = Data & 0x0f;
str[1] =DB_str[binary_to_decimal(tmp)];
tmp = Data>>4;
str[0] =DB_str[binary_to_decimal(tmp)];
return str;
}
*/
void serial_init(void)
{
SCON = 0x50; //UART为模式一,8位数据,允许接收
TMOD |= 0x20; //定时器1为模式二,8位自动重装
PCON |= 0x80; //SMOD=1
TH1 =0xfd; //波特率为19200,晶振频率为11.0592MHz
IE = 0x00;
TR1 = 1;
TI = 1;
}
/*
//向串口发送一个函数
void serial_send_byte(uchar ch)
{
SBUF=ch;
while (TI== 0);
TI = 0 ;
}
*/
//向串口发送一串字符数据
void serial_send_string(uchar *p)
{
while(*p != '\0')
{
SBUF = *p;
while(TI == 0);
TI = 0;
p++;
}
}
int main()
{
struct DB_data_struct *DB_data;
uchar k,tmp;
uchar DB_data_cmp[8];
serial_init();
AD7606_init();
while(1)
{
// DB_data = DB_str;
// serial_send_string(introduce1);
// serial_send_string(introduce2);
AD7606_startconvst();
while((busy == 1)) //当busy为低电平时,数据转换完毕,此时可以读取数据
{
// serial_send_byte(busy);
// delay_ms(500);
;
}
DB_data = AD7606_read_data();
serial_send_string(sampling);
for(k=0;k<8;k++)
{
tmp = (DB_data->DB_data_H>>4) & 0x0f;
serial_send_string(DB_str[tmp]);
tmp = (DB_data->DB_data_H) & 0x0f;
serial_send_string(DB_str[tmp]);
tmp = (DB_data->DB_data_L>>4) & 0x0f;
serial_send_string(DB_str[tmp]);
tmp = (DB_data->DB_data_L) & 0x0f;
serial_send_string(DB_str[tmp]);
serial_send_string(space);
DB_data_cmp[k] = DB_data->DB_data_H;
DB_data += 1;
}
if((DB_data_cmp[0]>0x7D)&&(DB_data_cmp[1]>0x7D)&&(DB_data_cmp[2]>0x7D)&&(DB_data_cmp[3]>0x7D) \
&&(DB_data_cmp[4]>0x7D)&&(DB_data_cmp[5]>0x7D)&&(DB_data_cmp[6]>0x7D)&&(DB_data_cmp[7]>0x7D))
{ //电压检测为预定值,则绿灯亮
red_led = 0;
green_led = 1;
}
else
{ //电压检测为非预定值,则红灯亮
green_led = 0;
red_led = 1;
}
serial_send_string(newline);
serial_send_string(newline);
serial_send_string(newline);
delay_ms(300);
}
return 0;
}
--------------------------------------------------------------------
#include "AD7076.h"
/*
* 名称:AD7606_startconvst()
* 功能:启动转换
*/
void AD7606_startconvst(void)
{
convst = 0;
delay_us(1);
convst = 1;
}
/*
* 名称:AD7606_reset()
* 功能:复位模块
*/
void AD7606_reset(void)
{
rst = 0;
rst = 1;
// delay_us(1);
rst = 0;
}
/*
* 名称:AD7606_setinputvoltage()
* 功能:设置测试电压范围
* rage=1为-10V~+10V
* rage=0位-5V~+5V
*/
void AD7606_setinputvoltage(uchar vol)
{
if(vol ==1)
{
rage = 1;
}else{
rage = 0;
}
}
/*
* 名称:AD7606_setOS()
* 功能:设置过采样频率
*/
void AD7606_setOS(uchar uCoS)
{
switch(uCoS)
{
case sampling_0times: //无过采样
OS0 = 0;
OS1 = 0;
OS2 = 0;
break;
case sampling_2times: //2倍过采样
OS0 = 1;
OS1 = 0;
OS2 = 0;
break;
case sampling_4times: //4倍过采样
OS0 = 0;
OS1 = 1;
OS2 = 0;
break;
case sampling_8times: //8倍过采样
OS0 = 1;
OS1 = 1;
OS2 = 0;
break;
case sampling_16times: //16倍过采样
OS0 = 0;
OS1 = 0;
OS2 = 1;
break;
case sampling_32times: //32倍过采样
OS0 = 1;
OS1 = 0;
OS2 = 1;
break;
case sampling_64times: //64倍过采样
OS0 = 0;
OS1 = 1;
OS2 = 1;
break;
default:
break;
}
}
/*
* 名称:AD7606_read_data()
* 功能:读取数据
* 返回值:返回一个结构体指针,该指针为指向结构体数组的首地址
*/
struct DB_data_struct *AD7606_read_data(void)
{
uchar i;
// uchar DB_data_H,DB_data_L;
struct DB_data_struct DB_data[8]; //申请一个结构体数组,用于存储8个通道的16位数据
for(i=0;i<8;i++)
{
rd_and_cs = 0; //在并行模式下,RD和CS同时置低,将使得数据更新在并行总线上
DB_data[i].DB_data_L = P0;
DB_data[i].DB_data_H = P2;
rd_and_cs = 1;
// DB_data[i] = (DB_data_H<<8)|DB_data_L;
}
return DB_data;
}
/*
* 名称:AD7606_init()
* 功能:初始化AD7606模块
* 初始化为无过采样,即采样频率为200ksps,采样电压范围为-5V~+5V
*/
void AD7606_init(void)
{
AD7606_setOS(sampling_0times);
AD7606_setinputvoltage(0);
AD7606_reset();
AD7606_startconvst();
}
------------------------------------------------------
#include
#include "other.h"
void delay_ms(uint timer) //毫秒延时程序
{
uchar i;
while(timer--)
{
for(i=200;i>0;i--);
for(i=120;i>0;i--);
}
}
void delay_us(uchar timer) //微妙延时程序
{
while(timer--);
}
/*
* 名称:binary_to_decimal()
* 功能:二进制数据转换为十进制数据
*/
/*
uchar binary_to_decimal(uchar binary)
{
uchar decimal,i,j,binary_tmp;
uchar sumtmp = 1;
for(i=4;i>0;i--)
{
binary_tmp = binary&0x08;
if(binary_tmp)
{
if(i>1)
{
for(j=i;j>1;j--)
{
sumtmp = sumtmp*2;
}
decimal += sumtmp;
sumtmp = 1;
}
else if(i == 1)
{
decimal += 1;
}
else
{
;
}
}
binary = binary<<1;
}
return decimal;
}
*/
|