📄 avr i2c.c
字号:
#include "twi.h"
#include "mega8.h"
#include "stdlib.h"
#include "math.h"
#include "I2CFEEP.h"
#include "eeprom.h"
#include "adc.h"
#include "systeminit.h"
unsigned char *cpEEP,sig,sigOTC,sigSTC;
unsigned int adcustore[20];
unsigned int adcmstore[14];
unsigned int adcuadd,adcmadd,lastadcmadd,adc6add;
unsigned int adcustore2[20];
unsigned int adcmstore2[14];
unsigned int adcuadd2,adcmadd2,lastadcmadd2,adc7add;
unsigned int R,R2,Rf,Rs,Rd;
unsigned int adc6min;
unsigned int adc6max;
unsigned int adc7min;
unsigned int adc7max;
unsigned int adc6mid;
unsigned int adc7mid;
float cangle6,cangle7;
unsigned int icangle6,icangle7;
float A6;
float A7;
unsigned int offset,poffset;
unsigned char delaytime;
/*
unsigned int time_cnt=0;
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
TCNT0=5;
if(time_cnt==4000)
{
time_cnt=0;
PORTD=~PIND;
}
else
{
time_cnt++;
}
}
*/
/***********求反正弦*************/
unsigned int arcsin(unsigned int x)
{ unsigned int px;
unsigned int r;
px=x;
if((px>=0)&&(px<=2500)) r=(px+86)/86; //0-14
else if((px>2500)&&(px<=4300)) r=29+(px-2500+83)/83; // 15-25
else if((px>4300)&&(px<=5800)) r=51+(px-4300+76)/76; //26-35
else if((px>5800)&&(px<=7100)) r=71+(px-5800+67)/67; //36-45
else if((px>7100)&&(px<=7700)) r=91+(px-7100+59)/59; //46-50
else if((px>7700)&&(px<=8200)) r=101+(px-7700+53)/53; //51-55
else if((px>8200)&&(px<=8700)) r=111+(px-8200+47)/47; //56-60
else if((px>8700)&&(px<=9100)) r=121+(px-8700+40)/40; //61-65
else if((px>9100)&&(px<=9400)) r=131+(px-9100+33)/33; //66-70
else if(px>9400) r=180; //71-90
r=r>>1;
// if((y==1)&&(x>=0)) r=90-r;
// else if((y==1)&&(x<0)) r=90+r;
return r;
}
/************************/
/******点亮某个LED*********/
void lightled(unsigned char num)
{
if((num%2)==1)
{PORTB&=0xfc;PORTB|=0x02;PORTD=~(0x01<<((num+1)/2-1));}
else
{PORTB&=0xfc;PORTB|=0x01;PORTD=~(0x01<<((num+2)/2-1));}
}
/*************/
/******延时******/
void delayms(unsigned char ii)
{
unsigned char i=100;
while(ii--)
{
while(i--);
}
}
/****************/
/*******开关延时***********/
unsigned char delayswitch(void)
{ unsigned int ii;
ii=0;
PORTB|=0x01;
PORTB&=0xfd;
delayms(10);
while(!(PINC&0x08))
{ delayms(1);
ii++;
if(ii>4500)
{
return 0;
}
else if((ii>3000)&&(ii<=4500)) {PORTD=0xf8; }
else if((ii>1500)&&(ii<=3000)) {PORTD=0xfc; }
else if((ii>50)&&(ii<=1500)) {PORTD=0xfe; }
}
PORTB&=0xfe;
if((ii>3000)&&(ii<=4500)) {return 3; }
else if((ii>1500)&&(ii<=3000)) {return 2; }
else if((ii>50)&&(ii<=1500)) {return 1; }
return 0;
}
/****************/
/****端口初始化*****/
static void io_init(void)
{
PORTD = 0xff;
DDRD = 0xff;
PORTC =0xff;
DDRC=0x00;
PORTB =0x00;
DDRB=0xff;
}
/************/
/******主函数******/
void main(void)
{
unsigned int countnum0,countnum1;
unsigned int i,j,k,uu6,uu7;
// int f=0;
// unsigned char ii,jj,nn;
#asm("sei")
SREG&=0x7f; /* 关中断 */
delayms(100); /*延时*/
SYS_INIT(); /*系统初始化*/
io_init(); /*IO初始化*/
adcinit(ADC_VREF_TYPE,ADC_ADCSRA); /*ADC初始化*/
I2CINIT(cpEEP); /*将指针指向数组*/
twi_slave_init(0x00); //初始化为从机
/************变量初始化***************/
countnum0=0;
countnum1=0;
adcuadd=0;
adcmadd=0;
adcuadd2=0;
adcmadd2=0;
lastadcmadd=1000;
lastadcmadd2=1000;
adc6min=900;
adc6max=300;
adc7min=900;
adc7max=300;
adc6mid=600;
adc7mid=600;
sig=1;
sigOTC=0;
sigSTC=0;
/*******************************/
/************读EEPROM初始化极值****************/
adc6min=EEPROM_Read(0)*256+EEPROM_Read(1);
adc6max=EEPROM_Read(2)*256+EEPROM_Read(3);
adc7min=EEPROM_Read(4)*256+EEPROM_Read(5);
adc7max=EEPROM_Read(6)*256+EEPROM_Read(7);
/****************************************/
/***************读EEPROM初始化零点*****************/
offset=EEPROM_Read(10)*256+EEPROM_Read(11);
/*****************************************/
/*************初始化幅度值和中值***************/
A6=(float)(adc6max-adc6min)/2.0;
A7=(float)(adc7max-adc7min)/2.0;
adc6mid=(adc6min+adc6max)/2;
adc7mid=(adc7min+adc7max)/2;
/****************************/
if(offset>15) /*****零点约束*****/
offset=0;
read_adc(6);read_adc(7); /*******ADC第一次读取********/
delayms(20); /**********延时*******/
SREG|=0x80;
while(1)
{
if(countnum0==16) /**是否到第十六次读ADC***/
{ countnum0=0;sigOTC=1; /***计数值清零 置标志****/
for(i=0;i<15;i++) /***对十六个值排序*****/
for(j=i+1;j<16;j++)
{
if(adcustore[i]>adcustore[j])
{
k=adcustore[j];
adcustore[j]=adcustore[i];
adcustore[i]=k;
}
if(adcustore2[i]>adcustore2[j])
{
k=adcustore2[j];
adcustore2[j]=adcustore2[i];
adcustore2[i]=k;
}
}
for(i=7;i<9;i++) /***取中间值****/
{
adcuadd=adcuadd+adcustore[i];
adcuadd2=adcuadd2+adcustore2[i];
}
adcmstore[countnum1]=adcuadd/2; /*****存中间值*******/
adcmstore2[countnum1]=adcuadd2/2;
adcuadd=0; adcuadd2=0;
countnum1++;
}
else
{
SREG&=0x7f; /*关中断*/
adcustore[countnum0]= read_adc(6); /**读转换结果***/
adcustore2[countnum0++]=read_adc(7);
SREG|=0x80; /*开中断*/
}
//////////////////////////////////////////////////////////////////////
if((countnum1==10)&&sigOTC) /*是否到第十个中间值*/
{ sigSTC=1; sigOTC=0; /*置标志**/
/*
for(i=0;i<9;i++)
for(j=i+1;j<10;j++)
{
if(adcmstore[i]>adcmstore[j])
{
k=adcmstore[j];
adcmstore[j]=adcmstore[i];
adcmstore[i]=k;
adcmstore[i]=k;
}
if(adcmstore2[i]>adcmstore2[j])
{
k=adcmstore2[j];
adcmstore2[j]=adcmstore2[i];
adcmstore2[i]=k;
}
}
*/
/////////////////////////////
for(i=0;i<10;i++) /*求均值*/
{
adcmadd=adcmadd+adcmstore[i];
adcmadd2=adcmadd2+adcmstore2[i];
}
adcmadd=adcmadd/10;
adcmadd2=adcmadd2/10;
countnum1=0; /*计数值清零*/
/////////////////////////
/*
if(lastadcmadd!=1000)
{
if(lastadcmadd>=adcmadd)
{
adcmadd=(adcmadd*8+lastadcmadd*2-abs(lastadcmadd-adcmadd))/10;
}
else
{
adcmadd=(adcmadd*8+lastadcmadd*2+abs(lastadcmadd-adcmadd))/10;
}
}
//else
//{
// adcmadd=(adcmadd*8+lastadcmadd*2)/10;
//}
lastadcmadd=adcmadd;
if(lastadcmadd2!=1000)
{
if(lastadcmadd2>=adcmadd2)
{
adcmadd2=(adcmadd2*8+lastadcmadd2*2-abs(lastadcmadd2-adcmadd2))/10;
}
else
{
adcmadd2=(adcmadd2*8+lastadcmadd2*2+abs(lastadcmadd2-adcmadd2))/10;
}
}
//else
// {
//adcmadd2=(adcmadd2*8+lastadcmadd2*2)/10;
//}
lastadcmadd2=adcmadd2;
*/
/* //for(ii=0;ii<8;ii++)
//if(((adcmadd-adc6min)>(170-ii*10))||((adcmadd-adc6min)<(10+ii*10))) uratc=urat[ii];
for(ii=16;ii>=1;ii--)
{
if((adcmadd-adc6min)>(ii*200/rat6)) break;
}
pdegree=0;
for(jj=1;jj<=ii;jj++)
{ if(ii==jj)
{
if(jj<=8) pdegree+=urat[jj-1]*((adcmadd-adc6min)-(ii*200/rat6))/10 ;
else pdegree+=urat[16-jj]*((adcmadd-adc6min)-(ii*200/rat6))/10 ;
}
else
{
if(jj<=8) pdegree+=urat[jj-1];
else pdegree+=urat[16-jj];
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -