#include <iom48v.h>
#include <macros.h>
#include <eeprom.h>
//#include <eeprom.c>
#define bit248 8
#define lcd_data_port PORTD
#define lcd_data_ddr DDRD
#define lcd_busy_pin PIND
#define lcd_busy_ddr DDRD
#define PB PORTB
#define PC PORTC
#define PD PORTD
#define lcd_control_port PORTB
#define lcd_control_pin PINB
#define lcd_control_ddr DDRB
#define busy 0x80
#define RW BIT(1);
#pragma data:sram
unsigned char dsfrgr;
#pragma data:eeprom
#define EEM 00
unsigned char vref1,vref2,vref3,vref4;
#pragma data:data
//---------------------- 显示的两行字符
unsigned char strA0_1[]=" K1 K2 ";
unsigned char strA0_2[]=" ON ";
unsigned char strA0_3[]=" OFF ";
unsigned char strA1_1[]=" K1 1#"; //1
unsigned char strA1_2[]=" K2 ";
unsigned char strA2_1[]=" SET K1 2#"; //2
unsigned char strA2_2[]=" OFF 0000 (S) ";
unsigned char strA3_1[]=" SET K1 3#"; //3
unsigned char strA3_2[]=" ON 0000 (S) ";
unsigned char strA4_1[]=" SET K2 4#"; //4
unsigned char strA4_2[]=" OFF 0000 (S) ";
unsigned char strA5_1[]=" SET K2 5#"; //5
unsigned char strA5_2[]=" ON 0000 (S) ";
unsigned char strA6_1[]=" K1_ON_OFF 6#";
unsigned char strA6_2[]="set 00000 k00000";
unsigned char strA7_1[]=" K2_ON_OFF 7#";
unsigned char strA7_2[]="set 00000 k00000";
unsigned char km=8;
unsigned char km_1=0;
unsigned int vref=0;
unsigned char ac_loss=0;
unsigned int BATT=0;
unsigned char uart0_rx=0;
unsigned char ups_state=0;
unsigned char ups_buzz=0;
unsigned char ups_bypass=0;
unsigned char out_flag=0;
unsigned char rx_char=0;
unsigned char rx_ok=0;
unsigned int m=0;
unsigned long LCD_LED=0;
unsigned char LCD_LED_1=0;
unsigned int LCD_LED_2=0;
unsigned int din=0000;
unsigned int dout=0000;
unsigned int dfreq_in=000;
unsigned int dfreq_out=0;
unsigned int dbat=000;
unsigned int dvload=000;
unsigned int dtemper=555;
unsigned int LCD_LEDx1=0;
unsigned char uart0_rxok=0;
unsigned char rx_end_ok=0;
unsigned char ups_fault=0;
unsigned char bat_low=0;
unsigned char ups_shutdown=0;
unsigned char ups_testing=0;
unsigned char back_online=0;
unsigned char PORTBBAK=0;
unsigned char PORTCBAK=0;
unsigned int ms_100=0;
unsigned int ms_1S_1=0;
unsigned int ms_1S_2=0;
unsigned int kja_ms=0;
unsigned int kjin_ms=0;
unsigned char kon_ms=0;
unsigned char koff_ms=0;
unsigned int k1on_ms=1;
unsigned int k2on_ms=1;
unsigned int k1on_ms_mm=0;
unsigned int k2on_ms_mm=0;
unsigned int k1off_ms=1;
unsigned int k2off_ms=1;
unsigned int K1_ON_OFF=0;
unsigned int K2_ON_OFF=0;
unsigned char K_K2_ON=0;
unsigned int K1_ON_OFF_MM=0;
unsigned int K2_ON_OFF_MM=0;
unsigned int K1_ON_OFF_MS=0;
unsigned int K2_ON_OFF_MS=0;
/*----------------------------------函数声明------------------------------------------*/
int main(void);
void timer1_init(void);
//void uart0_init(void);
void lcd_init(unsigned char bit48);
void lcd_write_data(unsigned char data,unsigned char mode,unsigned char bit48);
void delay_1ms(void);
void delay_nms(unsigned int n);
void LCD_KM(void) ;
void port_init(void)
{
DDRB = 0b11001111;//xff;
PINB = 0XFF;
PORTB = 0b00110000;
DDRC = 0xff;
PINC = 0XFF;
PORTC = 0X00;
DDRD = 0b11101111;//xff;
PIND = 0XFF;
PORTD = 0b00010000;
}
void init_devices(void)
{
CLI();
port_init();
timer1_init();
MCUCR = 0x00;
EICRA = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK0 = 0x01; //timer 0 interrupt sources
TIMSK1 = 0x01; //timer 1 interrupt sources
TIMSK2 = 0x00; //timer 2 interrupt sources
PCMSK0 = 0x00; //pin change mask 0
PCMSK1 = 0x00; //pin change mask 1
PCMSK2 = 0x00; //pin change mask 2
PCICR = 0x00; //pin change enable
//PRR = 0x00; //power controller
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//--------------------------液晶初始化 bit48=4/8
void lcd_init(unsigned char bit48)
{
PORTD&=~BIT(6); //始终写有效
delay_nms(10);
lcd_write_data(0X01,0,bit248); //清屏
delay_nms(50);
lcd_write_data(0B00111000,0,bit248); // 模式设置:8位操作,2行显示,5X7点阵
delay_nms(50);
lcd_write_data(0B00001100,0,bit248); //光标设置:显示开,光标开,闪耀关
delay_nms(50);
lcd_write_data(0B00000110,0,bit248); //输入方式设置:设置AC为增量方式显示不移位
delay_nms(50);
}
//------------------------------data 数据,mode方式:0=命令,1=数据,2=单次命令
void lcd_write_data(unsigned char data,unsigned char mode,unsigned char bit48)
{
unsigned char data_DEMP;
if(mode==1) PORTD|=BIT(5);else PORTD&=~BIT(5); //RS=0/1
data_DEMP=data&0X0F; // ----- 低4位
PORTBBAK=PORTB&0XF0;
PORTB=PORTBBAK|data_DEMP;
PORTD|=BIT(7);
data_DEMP=(data>>4)&0X0F;//--- 高4位
PORTCBAK=PORTC&0XF0;
PORTC=PORTCBAK|data_DEMP;
PORTD&=~BIT(7);
delay_nms(1);
}
//指定位置显示一个字符:第一行位置0~15,第二行16~31
void display_a_char(unsigned char position,unsigned char char_data)//参数position指定位置0~31,char_data为要显示的字符
{
unsigned char position_tem;
if(position>=0x10)
position_tem=0XC0;//position/2+0xC0;
else
position_tem=0X80;//position/2+0x80;
//if((position==0x1A)&&(km==7)) lcd_write_data(0b11011111,1,bit248);
//else
{
if((position ==0x00) || (position ==0x10)) lcd_write_data(position_tem,0,bit248);
lcd_write_data(char_data,1,bit248);}
}
//指定一行显示连续字符串:0显示在第一行,1显示在第二行,注字符串不能长于16个字符
void display_a_string(unsigned char col,unsigned char str_beg,unsigned char str_len,unsigned char *ptr)//参数col指定行,*ptr指字符串数组的首指针
{
unsigned char col_tem,i,kk;
kk=str_len;
if(kk>16) kk=16;
col_tem=(col<<4)+str_beg; //若col为1(即在LCD第二行显示字符串),先把col左移4位,使显示字符的首位置改到第二行首位,即位置16
for(i=0;i<kk;i++)
display_a_char(col_tem++,*(ptr+i));
}
/*====================================延时函数================================*/
//1ms延时函数
void delay_1ms(void)
{unsigned int i;
for(i=0;i<600;i++);
}
//n ms延时函数
void delay_nms(unsigned int n)
{
unsigned int i;
for(i=0;i<n;i++) delay_1ms();
}
int main(void)
{
delay_nms(500);
init_devices();
//vref=EEPROM_read(vref1);
EEPROM_write1(vref1,50);
//vref=EEPROM_read(EEM);
lcd_init(bit248);
while(1){
//LCD_LEDx1++;
k_on_off();
if(LCD_LEDx1==1)
{ LCD_LEDx1=0;
PORTD&=~BIT(6); //始终写有效
PORTD&=~BIT(5);
lcd_write_data(0b00110000,0,bit248); //清屏
delay_nms(20);
lcd_write_data(0b00110000,0,bit248); //清屏
delay_nms(10);
lcd_write_data(0b00110000,0,bit248); //清屏
delay_nms(10);
lcd_write_data(0B00111000,0,bit248); // 模式设置:8位操作,2行显示,5X7点阵
delay_nms(10);
lcd_write_data(0B00001000,0,bit248); //光标设置:显示开,光标开,闪耀关
delay_nms(10);
lcd_write_data(0B00000001,0,bit248); //输入方式设置:设置AC为增量方式显示不移位
delay_nms(10);
lcd_write_data(0B00000110,0,bit248); //输入方式设置:设置AC为增量方式显示不移位
delay_nms(10);
lcd_write_data(0X01,0,bit248); //清屏
delay_nms(30);
lcd_write_data(0B00111000,0,bit248); // 模式设置:8位操作,2行显示,5X7点阵
delay_nms(10);
lcd_write_data(0B00001100,0,bit248); //光标设置:显示开,光标开,闪耀关
delay_nms(10);
lcd_write_data(0B00000110,0,bit248); //输入方式设置:设置AC为增量方式显示不移位
delay_nms(10);
}
if (km==8) //商标
{display_a_string(0,0,16,strA0_1);
if(K_K2_ON==0) display_a_string(1,0,16,strA0_2);
else display_a_string(1,0,16,strA0_3);
}
else if (km==1) //主屏
{
strA1_1[6]=(K1_ON_OFF_MM/10000)%10+48;
strA1_1[7]=(K1_ON_OFF_MM/1000)%10+48;
strA1_1[8]=(K1_ON_OFF_MM/100)%10+48;
strA1_1[9]=(K1_ON_OFF_MM/10)%10+48;
strA1_1[10]=K1_ON_OFF_MM%10+48;
display_a_string(0,0,16,strA1_1);
str