//===================================================================================//
// 程序所用到的头文件
//===================================================================================//
#include <pic.h>
#include <math.h>
#include <stdlib.h>
//#include <stdio.h>
//#include <string.h>
//函数定义
void initport(void);
void initspi(void);
void mainloop(void);
void delay(unsigned char time);
void delay10ms(unsigned char); //delay n*10ms
void commspi(unsigned char cmd,unsigned char cmd1);
unsigned char commspi1(unsigned char cmd);
void setup_tlc2578(unsigned char cmd1,unsigned char cmd2);
void read_tlc2578(unsigned char cmd1,unsigned char cmd2);
void SCI_Init(void);
void Serialsenddata(unsigned char data);
void keydeal(void);
void ADdeal(unsigned char channel);
void SerialSendString(unsigned char *data,unsigned char len);
void SerialDeal(void);
void interrupt AD_int(void);
//变量定义
unsigned char led0,led1,led2,led3,led4,led5,led6;
unsigned int advalue,tempvol,SdataLength=0;
float voltage;
unsigned char adh,adl,SerialReceiveFlag;
unsigned char x0,x1,x2,x3,x4,Sdata[10];
//I/O口定义
#define tlc2578_cs RA5
#define tlc2578_eoc RB0
#define tlc2578_sclk RC3
#define tlc2578_sdo RC4
#define tlc2578_sdi RC5
__CONFIG(WDTDIS & LVPDIS & HS & PWRTDIS & BORDIS);//设置配置位
//WDTDIS:disable watchdog timer
//LVPDIS:low voltage programming disabled
//HS:high speed crystal/resonator
//PWRTDIS:disable power up timer
//BORDIS:disable brown out reset
//===================================================================================//
// 主程序
//===================================================================================//
void main(void)
{
initport();
SCI_Init();
initspi();
setup_tlc2578(0xa0,0x00);//初始化2578
setup_tlc2578(0xaa,0x04);//设置采样模式,重置FIFO,短采样12SCLKs,时钟源SCLK/64
while(1)
{
keydeal();
SerialDeal();
// ADdeal(0x10);
}
}
void DataConverter(void)
{
unsigned char len=0;
voltage = advalue;
voltage=((4.096*voltage)/4095-2.048)*5;
if(voltage >= 0)
{
tempvol=(unsigned int)(voltage*100);
x0='0';
}
else
{
tempvol=(unsigned int)(fabs(voltage)*100);
x0='-';
}
x1=(unsigned char)(tempvol/1000+0x30);
x2=(unsigned char)((tempvol%1000-tempvol%100)/100+0x30);
x3=(unsigned char)((tempvol%100-tempvol%10)/10+0x30);
x4=(unsigned char)(tempvol%10+0x30);
Serialsenddata(x0);
Serialsenddata(x1);
Serialsenddata(x2);
Serialsenddata('.');
Serialsenddata(x3);
Serialsenddata(x4);
Serialsenddata(0x0a);
}
void ADdeal(unsigned char channel)
{
setup_tlc2578(channel,0x00);
read_tlc2578(0xe0,0x00);
DataConverter();
//delay10ms(10);
}
void initport(void)
{
OPTION=0x8F; //WDT FRE=1:128
ADCON1=0x07;
TRISA=0x08; //TRISA5=0,OUTPUT
PORTA=0x2C; //RA5=1
TRISB=0xcf; // 设置相应口的输入输出方式
PORTB=0x21;
TRISC=0Xd3;
PORTC=0X00;
TRISD=0xf0;
TXIE = 1; //开发送中断
RCIE = 1; //开接收中断
PEIE = 1; //开外设中断
GIE = 1; //开总中断
TXEN = 1; //发送使能
CREN = 1; //接收使能
}
void initspi(void)
{
SSPSTAT=0x40; //SMP=0,CKE=1
tlc2578_cs=1;
SSPCON=0x32; //CKP=0,SSPEN=1,FOSC/64
}
// ************************************************************
// * send 16bit command byte to tlc2578 *
// ************************************************************
void commspi(unsigned char cmd,unsigned char cmd1)
{
//unsigned char ad=0;
SSPBUF=cmd;
while(STAT_BF==0)
{
;
}
SSPBUF=cmd1;
while(STAT_BF==0)
{
;
}
//ad=SSPBUF;
//return(ad);
}
// ************************************************************
// * send 16bit command byte to tlc2578 *
// ************************************************************
unsigned char commspi1(unsigned char cmd)
{
unsigned char ad=0;
SSPBUF=cmd;
while(STAT_BF==0)
{
;
}
ad=SSPBUF;
return(ad);
}
void setup_tlc2578(unsigned char cmd1,unsigned char cmd2)
{
unsigned char i;
tlc2578_cs=0;
commspi(cmd1,cmd2);
tlc2578_cs=1;
}
void read_tlc2578(unsigned char cmd1,unsigned char cmd2)
{
tlc2578_cs=0;
adh=commspi1(cmd1);
adl=commspi1(cmd2);
tlc2578_cs=1;
advalue=(adh<<8)+adl;
advalue=advalue>>4;
}
void delay(unsigned char time)
{
unsigned char i;
for(i=0;i<time*5;i++)
{
asm("clrwdt");
}
}
void delay10ms(unsigned char time)
{
unsigned char i;
unsigned int j;
for(i=0;i<time*5;i++)
for(j=0;j<0x390;j++)
asm("clrwdt");
}
// *******************************************************************************
// * 函 数 名: SCI_Init(void)
// * 函数功能: 异步串口通讯初始化
// * 入口参数: 无
// * 返 回: 无
// *******************************************************************************
void SCI_Init(void)
{
SPBRG = 129; //高速 波特率9600
TXSTA = 0x04;
RCSTA = 0xB0;
TRISC6 = 1; //设置为输入状态
TRISC7 = 1;
}
// ******************************************************************************
// *函 数 名:Serialsenddata(unsigned char data)
// *函数功能:串口数据发送
// *入口参数:需发送数据
// *返 回:无
// ******************************************************************************
void Serialsenddata(unsigned char data)
{
TXEN = 1; //发送使能
if(TXIF == 1) //发送
{
TXREG = data; //发送数据
while(TRMT == 0);
}
}
// ******************************************************************************
// *函 数 名:SerialSendString(unsigned char *string,unsigned char len)
// *函数功能:串口数据串发送
// *入口参数:需发送数据串
// *返 回:无
// ******************************************************************************
void SerialSendString(unsigned char *data,unsigned char len)
{
unsigned char i;
for(i=0;i<len;i++)
{
Serialsenddata(data[i]);
}
}
/******************************************************************************
* 函 数 名: interrupt USART_int(void)
* 函数功能: 中断函数
* 入口参数: 无
* 返 回: 无
*******************************************************************************/
void interrupt USART_int(void)
{
unsigned char temp;
GIE = 0; //关总中断
if(RCIF == 1) //有数据接收
{
temp = RCREG; //读取数据
SerialReceiveFlag=1;
Sdata[0] = temp; //存储接收的数据
//SdataLength++;
}
GIE = 1; //开总中断
}
/******************************************************************************
* 函 数 名: void SerialDeal(void)
* 函数功能: 串口处理函数
* 入口参数: 无
* 返 回: 无
*******************************************************************************/
void SerialDeal(void)
{
if (SerialReceiveFlag)
{
SerialReceiveFlag=0;
switch(Sdata[0])
{
case 0x01://AD采样通道选择
GIE = 0; //关外设中断
PEIE = 0; //开外设中断
delay(10);
ADdeal(0x20);
delay(100);
PEIE = 1; //开外设中断
GIE = 1; //开外设中断
break;
case 0x02:
TRISB |= 0x10;//
TRISC &= 0xfe;//
delay10ms(10);
break;
case 0x03:
TRISC |= 0x01;//
TRISB &= 0xef;//
delay10ms(10);
break;
case 0x04:
break;
case 0x05:
break;
default: break;
}
}
}
// ******************************************************************************
// *函数名:keydeal
// *函数功能:按键处理
// *入口参数:无
// *返回:无
// *****************************************************