- 阅读权限
- 90
- UID
- 12
- 帖子
- 30
- 精华
- 2
- 注册时间
- 2013-2-26
- 在线时间
- 12 小时
- UID
- 12
- 帖子
- 30
- 精华
- 2
- 注册时间
- 2013-2-26
- 在线时间
- 12 小时
|
// ICC-AVR application builder :
// Target : M8
// Crystal: 8.0000Mhz
// Author: Eagle Studio
#include <iom8v.h>
#include <macros.h>
#include <math.h>
#include "init.h"
#include "LCD1602_H4.h"
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
uint ADC_data[2]={0}; //四通道AD转换值
uint ADC0_temp[10]={0};
uint ADC1_temp[10]={0};
uchar data[4]={48,48,48,48};
unsigned char flag_timer0_1s=0;
unsigned char coder_num=1;
/******************************* ADC Convert function ******************************/
uint ADC_Convert(void)
{
uint temp1,temp2;
temp1=(uint)ADCL;
temp2=(uint)ADCH;
temp2=(temp2<<8)+temp1;
return(temp2);
}
void AD_conversion(void)
{
unsigned int i;
unsigned char j;
ADCSRA = 0xe3; //star ADC mode,
ACSR=(1<<ACD); //关闭模拟比较器
ADMUX=0x00; //选择外部AREF为基准,REF=2.75V
for(j=0;j<10;j++)
{
ADMUX &=0xFE;
delay_us(25);
ADC0_temp[j]=ADC_Convert();
delay_us(25);
ADMUX |=0x01;
delay_us(25);
ADC1_temp[j]=ADC_Convert();
delay_us(10);
}
ADCSRA = 0x00; //disable adc
ADC_data[0]=Avg_ADC(ADC0_temp);
ADC_data[1]=Avg_ADC(ADC1_temp);
}
/* ****************************************************************
** 函 数 名: Avg_ADC(uint *data )
** 功能描述: 中值滤波
*************************************************************** */
unsigned int Avg_ADC(unsigned int *data )
{
unsigned char count,i,j;
unsigned int value_buf[10];
unsigned long sum=0,temp;
for (count=0;count<10;count++)
{
value_buf[count] =data[count];
}
for (j=0;j<9;j++)
{
for (i=0;i<9-j;i++)
{
if ( value_buf[i]>value_buf[i+1] )
{
temp = value_buf[i];
value_buf[i] = value_buf[i+1];
value_buf[i+1] = temp;
}
}
}
for(count=1;count<9;count++)
{
sum += value_buf[count];
}
sum =sum>>3;
return (unsigned int)sum;
}
/******************Write LCD function******************/
void LCD_Display(void)
{
LCD1602_sendstr(0,0,"U:",2);
LCD1602_sendstr(0,6,"v",1);
LCD1602_sendstr(0,8,"I:",2);
LCD1602_sendstr(0,13,"mA",2);
LCD1602_sendstr(1,0,"u:",2);
LCD1602_sendstr(1,6,"v",1);
LCD1602_sendstr(1,8,"Co:",3);
}
void Data_to_ASCII(unsigned int i,unsigned char *data)
{
data[0]=i/1000+48;
i=i%1000;
data[1]=i/100+48;
i=i%100;
data[2]=i/10+48;
i=i%10;
data[3]=i+48;
}
unsigned int Process_U0(void)
{
unsigned int I,U;
//I=(ADC_data[1]-ADC_data[0])*2750/102400 //REF为2.75V,采样电阻为100mΩ
//I=(ADC_data[1]-ADC_data[0])*55/2048; //理论值
//U=ADC_data[0]*13.4; //理论值 ,电压放大4.99倍
I=(ADC_data[1]-ADC_data[0])*7/256; //修正值
U=ADC_data[0]*16.64; /修正值
if(U>9999)
{
U=U/10;
Data_to_ASCII(U,data);
CD1602_sendstr(0,2,&data[0],1);
LCD1602_sendstr(0,3,&data[1],1);
LCD1602_sendstr(0,4,".",1);
LCD1602_sendstr(0,5,&data[2],1);
}
else
{
Data_to_ASCII(U,data);
LCD1602_sendstr(0,2,&data[0],1);
LCD1602_sendstr(0,3,".",1);
LCD1602_sendstr(0,4,&data[1],1);
LCD1602_sendstr(0,5,&data[2],1);
}
Data_to_ASCII(I,data);
LCD1602_sendstr(0,10,data,3);
}
unsigned int Process_U1(void)
{
unsigned int U;
U=ADC_data[1]*16.64; //修正值
if(U>9999)
{
U=U/10;
Data_to_ASCII(U,data);
LCD1602_sendstr(1,2,&data[0],1);
LCD1602_sendstr(1,3,&data[1],1);
LCD1602_sendstr(1,4,".",1);
LCD1602_sendstr(1,5,&data[2],1);
}
else
{
Data_to_ASCII(U,data);
LCD1602_sendstr(1,2,&data[0],1);
LCD1602_sendstr(1,3,".",1);
LCD1602_sendstr(1,4,&data[1],1);
LCD1602_sendstr(1,5,&data[2],1);
}
}
/******************Timer0 interrupt function******************/
#pragma interrupt_handler timer0_ovf_isr:iv_TIMER0_OVF //8ms溢出中断
void timer0_ovf_isr(void)
{
static uint CNT0=0;
TCNT0 = 0x06; //reload counter value
CNT0++;
if(CNT0==125) //askdistance flag,every 2s once
{
CNT0=0;
flag_timer0_1s=1;
}
}
#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void)
{
//static uint CNT0=0;
//TIMER1 has overflowed
TCNT1H = 0xFF; //reload counter high value
TCNT1L = 0xFE; //reload counter low value
//coder_num++;
if(CoderA_Read==0)//==0x00) //Forward
{
coder_num=coder_num+1;
if(coder_num>30)
{
coder_num=30;
}
}
if(CoderA_Read==1)//==0x10) //Forward
{
coder_num=coder_num-1;
if((coder_num<1)||(coder_num>30))
{
coder_num=0;
}
}
}
void SPI_MasterTransmit(unsigned char data)
{
SPDR = data; //启动命令传输
while(!(SPSR & (1<<SPIF))) //等待传输结束
;
}
void SPI_Max5484_Data(unsigned int Max5481_data)
{
unsigned char tempH,tempL;
Max5481_data=Max5481_data<<6; //将D9移到最高位,所有往高位移6位
tempH=Max5481_data>>8; //取高八位
tempL=Max5481_data; //取低八位,强制转换
SPI_CS_L; //启动从机芯SPI模式
SPI_MasterTransmit(0x00); //发送写数据命令
SPI_MasterTransmit(tempH); //发送高八位
SPI_MasterTransmit(tempL); //发送低八位
SPI_CS_H; //关闭从机芯SPI模式
}
void SPI_Max5484_Cmd(unsigned char Max5481_cmd)
{
SPI_CS_L; //启动从机芯SPI模式
SPI_MasterTransmit(Max5481_cmd);
SPI_CS_H; //关闭从机芯SPI模式
}
/****************************main function*******************************/
void main(void)
{
init_devices(); //system init
LCD1602_init();
delay_ms(500);
LCD1602_clear();
LCD1602_sendstr(0,2,"Hello world!",12);
LCD1602_sendstr(1,2,"Eagle Studio",12);
delay_ms(2000);
LCD1602_clear();
while(1)
{
WDR ();
if(flag_timer0_1s==1)
{
flag_timer0_1s=0;
AD_conversion();
Process_U0();
Process_U1();
}
if(Reset_Read==0) //按键复位为0V
{
coder_num=0;
}
LCD_Display();
Data_to_ASCII(coder_num,data);
LCD1602_sendstr(1,11,data,4);
SPI_Max5484_Data(coder_num);
}
} |
|