#include <iom16v.h>
#include <macros.h>
#include <stdlib.h>
#include <eeprom.h>
#include <math.h>
#include "head.h"
#include "headRBHZ.h"
#define MENU 6
#define fosc 18432000 //晶振8MHZ
#define baud 9600 //波特率
#define verify1 0x0a
#define verify2 0x46
#define T 490 //检测周期
#define addr 01 //address
#define IN_DBUS() {DDRB=0x00;PORTB=0xff;}
#define OUT_DBUS() DDRB=0xff
#define LCDDAT PORTB
#define LCDDATIN PINB
#define DI1 PORTC|=0x01
#define DI0 PORTC&=~0x01
#define RW1 PORTC|=0x02
#define RW0 PORTC&=~0x02
#define E1 PORTC|=0x04
#define E0 PORTC&=~0x04
#define CS1_1 PORTC|=0x08
#define CS1_0 PORTC&=~0x08
#define CS2_1 PORTC|=0x10
#define CS2_0 PORTC&=~0x10
#define REST1 PORTC|=0x20
#define REST0 PORTC&=~0x20
#define K1 !(PIND&0x40)
#define K3 !(PIND&0x20)
#define K2 !(PIND&0x10)
#define K4 !(PIND&0x08)
#define out1 {PORTD&=~0x08; PORTD|=0x04;}
#define out0 {PORTD&=~0x04; PORTD|=0x08;}
uchar LCD_X,LCD_Y;
uchar menu,C,CH,LINK,OUT,on,menu,v;
unit menu1,limup,limdown;
uchar h=0;
uchar dat[5]={0};
unit dat1;
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0xFF;
PORTC = 0x00; //m103 output only
DDRC = 0xFf;
PORTD = 0xf0;
DDRD = 0x0c;
}
//Watchdog initialize
// prescale: 1024K
void watchdog_init(void)
{
WDR(); //this prevents a timout on enabling
WDTCR = 0x0E; //WATCHDOG ENABLED - dont forget to issue WDRs
}
//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9615 (0.2%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = BIT(URSEL) | 0x06;
UBRRL=(fosc/16/(baud+1))%256;
UBRRH=(fosc/16/(baud+1))/256;
UCSRB = 0x98;
}
unsigned long adc_rel[8];//AD转换结果
unsigned char adc_mux;//AD通道
//ADC initialize
// Conversion time: 3uS
void adc_init(void)
{
ADCSR = 0x00; //disable adc
ADMUX = 0x40; //select adc input 0 00-ref 01-avcc 11-2.56
ACSR = 0x80;
ADCSRA=(1<<ADEN)|(1<<ADSC)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0) ;//64分频
}
unit adc(uchar mux) ///*****检测信号*****///
{
ADMUX=(1<<REFS0)|(mux&0x0f);
ADCSRA|=0x40;
del(500);
return(ADC);
}
unit cw(uchar CH,uchar n)
{
ulong dat1=0;
uchar i;
for(i=0;i<n;i++)
{
dat1+=adc(CH);
}
return dat1=dat1/n;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
uart0_init();
adc_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
/* 字符输出函数 */
void putchar(unsigned char c)
{
while (!(UCSRA&(1<<UDRE))) WDR();
UDR=c;
}
/* 字符串输出函数 */
void puts(void)
{
uchar a=0,i=0;
UCSRB=0x88;
putchar(123);
putchar(verify2);
while (i<5)
{
putchar(*(dat+i));
a^=(*(dat+i));
i++;
}
putchar(a);
UCSRB=0x00;
}
void output(unit dd)
{
uchar i,j,tn[5];
unit div=10000;
for(j=5;j>0;j--)
{
tn[j-1]=dd/div;
dd%=div;
div/=10;
}
for(j=4;j>0;j--)
{
if(tn[j]<1||tn[j]>9) tn[j]=15;
else { break;}
}
for( i=0;i<5;i++)
dat[i]=tn[4-i]+0x30;
}
/*************LCD***************/
void writelcd(uchar dat)
{
E0; del(1);
RW0; del(1);
E1; del(1);
OUT_DBUS(); del(3);
LCDDAT=dat; del(3);//PORTD&=~0xc0; PORTD|=(0xc0&dat);
E0; del(3);
WDR();
}
void writelcdcon(uchar dat)
{
DI0;
CS1_1;CS2_1;
writelcd(dat);
}
void writelcddat(uchar dat)
{
DI1;
writelcd(dat);
}
void restlcd(void)
{
REST1;
delay(1);
REST0;
delay(1);
REST1;
}
void lcdon(void)
{
DI0;
CS1_1;CS2_1;
writelcd(0x3f);
}
void lcdoff(void)
{
DI0;
CS1_1;CS2_1;
writelcd(0x3E);
}
void fromx(uchar dat)
{
DI0;
CS1_1;CS2_1;
writelcd(dat|0xC0);
}
void sety(uchar dat)
{
DI0;
writelcd(0xB8|dat);
}
void setx(uchar dat)
{
if(dat<64)
{CS1_1;CS2_0;}
else
{CS1_0;CS2_1;dat-=64;}
DI0;
writelcd(0x40|dat);
}
void w8(uchar x,uchar y,uchar dat)
{
setx(x);
sety(y);
writelcddat(dat);
WDR();
}
void clrlcd(void)
{
uchar i,j;
for(j=0;j<8;j++)
for(i=0;i<128;i++)
{
w8(i,j,0x0);
}
}
void clr_row(uchar row,uchar a,uchar b)
{
uchar i;
CS1_1,CS2_1;
for(i=a;i<b;i++)
w8(i,row,0);
}
uchar dis_en(uchar x,uchar y,uchar c)
{
uchar i;
const uchar *p;
//*取指针
if(c>='A'&&c<='Z')
{
p=&A_Z[(c-'A')*5];
}
else if (c>='a' && c<='z')
{
p=&a_z[(c-'a')*5];
}
else if (c>='0' && c<='9')
{
p=&num0_9[(c-'0')*5];
}
else
{
switch (c)
{
case '.':p=x_p;break;
case ':':p=x_mh;break;
case '_':p=x_ds;break;
case '^':p=x_shang;break;
case '~':p=x_xia;break;
case '/':p=x_xg;break;
default:p=space;break;
}
}
for(i=0;i<5;i++)
w8(x+i,y,p[i]);
return x+5;
}
//返回操作后的x(列)坐标值
uchar dis_en_row(uchar x,uchar y,uchar *p)
{
uchar i,cnt=x;
for(i=0;p[i]!=0;i++)
{
cnt=dis_en(cnt,y,p[i]);
}
return cnt;
}
uchar dis_num(uchar x,uchar y,uchar c)
{
uchar i;
for(i=0;i<5;i++)
{
w8(x+i,y,num0_9[(5*c)+i]);//num0_9[c*5+i]);
}
return x+5;
}
uchar dis_5(uchar x,uchar y,unit dat)
{
uchar num[5]={10};
uchar i,n=0,lock=0,ent=x;
for(i=0;i<5;i++)
{
num[i]=dat%10;
dat/=10;
}
for(i=5;i>1;i--)
{
if((num[i-1]>0&&num[i-1]<10)||(lock==1))
{
lock=1;
dis_num(x+n*5,y,num[i-1]);
n++;
ent+=5;
}
}
dis_num(x+n*5,y,num[0]);
return ent+5;
}
uchar disen(uchar x,uchar y,uchar ASD)
{
uchar i,j;
ASD=ASD-0x20;
for(i=0;i<2;i++)
for(j=0;j<8;j++)
{
w8(x+j,y+i,ASCII[(unit)ASD*2+i][j]);
}
return x+8;
}
uchar disen_row(uchar x,uchar y,uchar *ASD) //显示ASCLL字符串
{
uchar k=0,n;
while(*(ASD+k)!=0x00)
{
n=disen(x+8*k,y,ASD[k]);
k++;
}
return n;
}
uchar disnum(uchar x,uchar y,uchar ASD)
{
uchar i,j;
ASD=ASD+0x10;
for(i=0;i<2;i++)
for(j=0;j<8;j++)
{
w8(x+j,y+i,ASCII[ASD*2+i][j]);
}
return x+8;
}
uchar dis5(uchar x,uchar y,unit dat)
{
uchar num[5]={10};
uchar i,n=0,lock=0,ent;
for(i=0;i<5;i++)
{
num[i]=dat%10;
dat/=10;
}
for(i=5;i>1;i--)
{
if((num[i-1]>0&&num[i-1]<10)||(lock==1))
{
lock=1;
ent=disnum(x+n*8,y,num[i-1]);
n++;
}
}
ent=disnum(x+n*8,y,num[0]);
return ent;
}
uchar dis_hz(uchar x,uchar y,const uchar *hz)
{
uchar i;
uchar t[32];
for(i=0;i<32;i++)
t[i]=hz[i];
for(i=0;i<16;i++)
w8(i+x,y,t[i]);
for(i=0;i<16;i++)
w8(i+x,y+1,t[i+16]);
return x+16;
}
//*显示一行汉字,从y开始显示
uchar dis_hz_row(uchar x,uchar row,const uchar **hz)
{
uchar i,j,n=x;
for(i=0;hz[i]!=NULL;i++)
{
dis_hz(i*16+x,row,hz[i]);
n+=16;
}
return n;
}
//*画点函数
void DRAW_DOT(uchar x,uchar y) //*128*64*
{
uchar y_date;
LCD_Y=y/8;y_date=y%8;y_date=0x01<<y_date;
LCD_X=x;
w8(LCD_X,LCD_Y,y_date);
}
uchar read_x(uchar row, uchar line)//*读制定行,列的数据
{
uchar c;
sety(line);
setx(row);
//*读取数据
// IN_DBUS(); /*设置为输入
DI1; //*数据模式*
RW1; //*读操作*
//*第一次读
E0;
del(1); //*延迟1us
E1;
del(1);
E0;
del(1);
//*第二次读*
E0;
del(1); //*延迟1us
E1;
del(5);
c=PIND;
E0;
del(1);
//***********
RW0;
OUT_DBUS();
return c;
}
void write_p(uchar x,uchar y,uchar i)
{
uchar c;
//*read_x里面用的参数就是第二屏幕的,置操作第二屏幕*
c=read_x((uchar)(x>>3),y);
if(i=='\0')
{
c&=~(1<<(x&7));
}
else if(i=='\1')
{
c|=(1<<(x&7));
}
else
{
c^=(1<<(x&7));
}
w8(y,(uchar)(x>