📄 zuhe.c
字号:
#include<iom8v.h> //??¨1o?ATMEGA8|ì?¨a?¤???t
#include<macros.h> //??¨1o?¨°?D?3?ê¨??o¨o|ì???§¨°?
#define uchar unsigned char
#define uint unsigned int
#define osccal_addr 0x1ff
#define fosc 8000000 //??ì??8MHZ
#define baud 9600 //2?§¨???¨o
#define LcdCmdPort PORTB
#define LcdDataPort PORTB
uint datax;
char d[9]={0x57,0x3d,0x11,0x2e,0x11,0x11,0x11,0x77,0x23};
uchar x,datah,K,datal,a[3],i,K,w1,w2,w3,w4;
float v,W;
//?á?D??¨?¨o?à|죤???ê???16??é??
void delay(uint x)
{ uchar j;
while(x-->0)
{
for(j=0;j<5;j++)
{;}
}
}
unsigned char EEPROM_read(unsigned int uiAddress)
{while(EECR&(1<<EEWE));
EEAR=uiAddress;
EECR|=(1<<EERE);
return EEDR;
}
void init_adc(void)
{ADMUX=(3<<REFS0); //ADC2???|ì??1?a?¨22?2.56V,??ê?a¨a?§|쨤????ADC0
ADCSRA=(1<<ADEN)|(1<<ADSC); //ADC¨o1?¨1?ê?2?é????¥¨°?????áa???ê?2?à??¤??|ì,
}
void readadc(void)
{datal=ADCL;
datah=ADCH;
//ADCSRA=(1<<ADSC); //|죤????áa??¥
//datax=(datah<<8)+datal;
}
void LcdBusy(void); /* LCD???ì?????? */
void LcdBusy()
{
PORTC &= ~(1<<PORTC4);
PORTC |= (1<<PORTC5);
LcdCmdPort = 0xff;
DDRB=0x00;
PORTC |= (1<<PORTC3);
PORTC &= ~(1<<PORTC3);
while ((PINB&0x80)!=0)
{
PORTC &= ~(1<<PORTC3);
PORTC |= (1<<PORTC3);
}
PORTC |= (1<<PORTC3);
DDRB=0xff;
//LcdDelay(500);
}
void LcdXY(char,char); /* LCD?????¨?????? */
void LcdXY(char x,char y)
{
if ((y&0x01)!=0)LcdCmd(0x80|(x-1));
else LcdCmd(0xC0|(x-1));
}
void LcdCmd(char); /* LCD?????ü?????? */
void LcdCmd(char c)
{
LcdBusy();
PORTC |= (1<<PORTC3);
PORTC &= ~(1<<PORTC4);
PORTC &= ~(1<<PORTC5);
//PORTC &= ~(1<<PORTC3);
LcdCmdPort = c;
delay(500);
PORTC &= ~(1<<PORTC3);
}
void LcdData(char); /* LCD???????????? */
void LcdData(char c)
{
LcdBusy();
PORTC |= (1<<PORTC3);
PORTC |= (1<<PORTC4);
PORTC &= ~(1<<PORTC5);
//PORTC &= ~(1<<PORTC3);
LcdDataPort = c;
delay(500);
PORTC &= ~(1<<PORTC3);
}
void init_wdt(void)
{WDTCR=(1<<WDCE)|(1<<WDE);
WDTCR=(0<<WDCE)|(1<<WDE)|(1<<WDP2)|(1<<WDP1)|(1<<WDP0);
/*看门狗允许、溢出时间2.1s,p44*/
}
void LcdInit(void); /* LCD?????????? */
void LcdInit()
{
PORTC |= (1<<PORTC4);
PORTC |= (1<<PORTC5);
PORTC |= (1<<PORTC3);
LcdCmd(0x38);
LcdCmd(0x38);
LcdCmd(0x06);
LcdCmd(0x0c);
LcdCmd(0x01);
}
void LcdClr(void); /* LCD???????? */
void LcdClr()
{
LcdCmd(0x01);
}
void LcdStr(char *); /* ????×?·??????? */
void LcdStr(char *c)
{
int d=0;
while (c[d]!=0)
LcdData(c[d++]); /* char of LCM */
}
void main(void)
{
delay(2000);
OSCCAL=EEPROM_read(osccal_addr);
DDRC=0xfe;
DDRB=0Xff;
//init_wdt();
SEI();
init_adc();
LcdInit();
LcdClr();
while(1)
{ delay(10000);
WDR();
init_adc();
//LcdClr();
ADCSRA|=(1<<ADSC);
while(ADCSRA&(1<<ADIF));
readadc();
ADCSRA|=(1<<ADIF);
datax=(datah<<8)+datal;
ADCSRA&=~(1<<ADEN);
v=(float)datax/0x3ff*2.56;
W=(float)v*v;
w1=(uint)W;
w2=(uint)((W-w1)*10);
w3=(uint)((W-w1-w2/10)*100);
w4=(uint)((W-w1-w2/10-w3/100)*1000);
LcdXY(5,1);
delay(500);
d[2]=0x30+w1;
d[4]=0x30+w2;
d[5]=0x30+w3;
d[6]=0x30+w4;
delay(500);
LcdStr(d);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -