小弟想用51单片机控制tlv5608输出模拟电压,可是遇到了问题,求解答
{
flag=0;
if(receive_table[3]!=receive_table[0]+receive_table[1]+receive_table[2])
{
send_UART(0xff);
}
else
{
send_UART(0x00);
这段是求和校验,为什么只是返回ff,即使校验成功。还有模拟电压输出也有问题,有大神了解的求指点,
QQ1533477443(有偿)
有没有人呀,有没有人呀,有没有人呀......
没有前后程序,没法做出判断~~~~
#include <STC12C5A60S2.h>
#include "delay.h"
#define RELOAD_COUNT 248 //11.0592Mhz, 256-reload_count=8, baud=115200
#include <STC12C5A60S2.h>
#include "delay.h"
#define RELOAD_COUNT 248 //11.0592Mhz, 256-reload_count=8, baud=115200
#define uchar unsigned char //单字节
#define uint unsigned int //双字节
#define N 4 //接收字节数
#define M 1 //发送字节数
#define S_max 20 //最高速度档位
sbit LED = P1^3;
sbit CTRL1=P0^4;
sbit CTRL2=P0^5;
sbit CTRL3=P0^6;
sbit CTRL4=P0^7;
sbit CTRL5=P4^6;
sbit CTRL6=P4^1;
uchar transmit_table[M]; //发送数组
uchar receive_table[N]; //接收数组
uint i=0; //发送数组变量
uint j=0; //接收数组变量
uint flag=0; //接收标志位
// sbit SPI_status=SPSTAT.7;
//spi波特率为四分之一晶振源,数据先准备好然后随着上升沿更新。
uchar L_WORD=0xaa;
uchar H_WORD=0x0f; // 0000
//void delay(uint k);
void send_UART(uchar l);
void UART_Interrupt_Receive(void);
void serial_port_initial();
void send_TLV5608();
void main()
{
send_TLV5608();
P0=0;
serial_port_initial();
while(1)
{
LED=1;
CTRL6=0;
//delay(50000);
LED=0;
CTRL6=1; //单片机测试LED
//delay(50000);
if(flag==1)
{
flag=0;
if(receive_table[3]!=receive_table[0]+receive_table[1]+receive_table[2])
{
send_UART(0xff);
}
else
{
send_UART(0x00);
/*
L_WORD=(int)(((float)receive_table[1]/5)*(0x3FF))&(0xff); //DA转换器低字节
H_WORD=(((int)(((float)receive_table[1]/5)*(0x3FF))>>8)|receive_table[0])&(0xff); // DA转换器高字节
switch(receive_table[0])
{
case 0x00:if(receive_table[2]==0x01) {CTRL1=1;send_TLV5608(); break;} else if(receive_table[2]==0x10){CTRL1=0;break;} else { send_UART(0xff);break;}
case 0x01:if(receive_table[2]==0x01) {CTRL2=1;send_TLV5608(); break;} else if(receive_table[2]==0x10){CTRL2=0;break;} else { send_UART(0xff);break;}
case 0x02:if(receive_table[2]==0x01) {CTRL3=1;send_TLV5608(); break;} else if(receive_table[2]==0x10){CTRL2=0;break;} else { send_UART(0xff);break;}
case 0x03:if(receive_table[2]==0x01) {CTRL4=1;send_TLV5608(); break;} else if(receive_table[2]==0x10){CTRL2=0;break;} else { send_UART(0xff);break;}
case 0x04:if(receive_table[2]==0x01) {CTRL5=1;send_TLV5608(); break;} else if(receive_table[2]==0x10){CTRL2=0;break;} else { send_UART(0xff);break;}
case 0x05:if(receive_table[2]==0x01) {CTRL6=1;send_TLV5608(); break;} else if(receive_table[2]==0x10){CTRL2=0;break;} else { send_UART(0xff);break;}
default: send_UART(0xff);break;
}
ES=1;
*/ //开启串口中断
}
}
}
// if(flag==1)
// {
//
// ES=0;
// for(i=0;i<M;i++)
// {
// SBUF=transmit_table;
// while(!TI);
// TI=0;
// }
// ES=1;
// flag=0;
//
// }
}
void send_TLV5608()
{
SPCTL=0xF8;
SPSTAT=0xC0;//clear SPIF,WCOL
SPDAT=L_WORD;
while((SPSTAT^7)==0)
{
continue;
}
SPDAT=H_WORD;
}
void serial_port_initial() //115200bps@11.0592MHz
{
PCON |= 0x80; //使能波特率倍速位SMOD
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x04; //独立波特率发生器时钟为Fosc,即1T
BRT = 0xFA; //设定独立波特率发生器重装值
AUXR |= 0x01; //串口1选择独立波特率发生器为波特率发生器
AUXR |= 0x10; //启动独立波特率发生器
EA=1;
ES=1;
}
//void serial_port_initial()
// {
// SCON = 0X50; //方式1,串口1,可变波特率,8位数据,无校验位
// BRT = RELOAD_COUNT; //串口1使用独立波特率发生器
// AUXR = 0X11; //串口1波特率发生器选择为独立波特率发生器,开启独立波特率发生器,12时钟计数1次,Fosc/12/(256-BRT)
// EA = 1;
// ES = 1;
// }
void send_UART(uchar l)
{
ES = 0;
TI = 0;
SBUF = l;
while(!TI);
TI = 0;
ES = 1;
}
void UART_Interrupt_Receive(void) interrupt 4
{
if(RI==1)
{
RI=0;
receive_table[j++]=SBUF;
if(j>=N)
{
flag=1; //接收数组满标志位
j=0;
}
}
else
{
TI=0;
}
}
这是一个16位数字转模拟电压的程序,麻烦大神给瞧瞧,谢谢!