/*******************************************************************************
* Device      : AD18F06A
* Author      : AD Application Team
* Version     : V0.0.1
* Date        : 202012001
* Description :
* Note		  :
********************************************************************************/

/********************************INCLUDES*************************************/
#include <stdio.h>
#include "AD18F06A.h"

__CONFIG(1,_FINTOSC_DIV1_1L   & _LVREN_OFF_1L      & _LVR24V_1L                                                                                          );
__CONFIG(2,_FLASH_CP7_OFF_2L  & _FLASH_CP6_OFF_2L  & _FLASH_CP5_OFF_2L   & _FLASH_CP4_OFF_2L   & _FLASH_CP3_OFF_2L
         & _FLASH_CP2_OFF_2L  & _FLASH_CP1_OFF_2L  & _FLASH_CP0_OFF_2L   & _CRYLEN_OFF_2L      & _CRYHEN_OFF_2L
  	     & _IDIS_OFF_2L        & _CKSUM_OFF_2L      & _CBP_OFF_2L         & _RESETE_OFF_2L      & _RSEL_LOW_2L     & _STV_ON_2L              	         );
__CONFIG(3,_DBG_OFF_3L        & _PSUT_8_3L         & _FCPU_2T_3L         & _WDTEN_OFF_3L       & _TWDT_111_3L                                            );
__CONFIG(4,_OSCM_HIRC_16M_4L  & _ENVDDL_ON_4L      & _EEDIS_OFF_4L       & _FLDIS_OFF_4L       & _FCMEN_OFF_4L    & _IESO_OFF_4L&  _DBGS_IOSEL_1_4L      );

static volatile unsigned char LED_S1@0X400 = 0;
static volatile unsigned char LED_S2@0X401 = 0;
static volatile unsigned char LED_S3@0X402 = 0;
static volatile unsigned char LED_S4@0X403 = 0;
static volatile unsigned char LED_S5@0X404 = 0;
static volatile unsigned char LED_S6@0X405 = 0;
//SEG6@0X406 复用为COM8
static volatile unsigned char LED_S7@0X407 = 0;
static volatile unsigned char LED_S8@0X408 = 0;

//                                           0    1    2    3    4    5    6    7    8    9    A    B    C    D    E    F    H    L    n    u    P    o    -    _   OFF
volatile const unsigned char buf_F[25] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0x89,0xC7,0xC8,0xC1,0x8C,0xA3,0xBF,0xF7,0xFF};
volatile const unsigned char buf_Z[25] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,0x76,0x38,0x37,0x3E,0x73,0x5C,0x40,0x08,0x00};

volatile const unsigned char Str1[12]  = {0x00,0x76,0x79,0x38,0x38,0x3F,0x08,0x77,0x5E,0x06,0x39,0x00};

void LED_Device(unsigned char Bit1_Temp,unsigned char Bit2_Temp,unsigned char Bit3_Temp,unsigned char Bit4_Temp,unsigned char Bit5_Temp,unsigned char Bit6_Temp,unsigned char Bit7_Temp,unsigned char Bit8_Temp)
{
    unsigned char LED_Display_Buf[8];
	unsigned char i = 0;

	for(i = 0;i < 8;i++)LED_Display_Buf[i] = 0;

	if(Bit1_Temp&0x01)LED_Display_Buf[0] |= 0x01;if(Bit1_Temp&0x02)LED_Display_Buf[1] |= 0x01;if(Bit1_Temp&0x04)LED_Display_Buf[2] |= 0x01;if(Bit1_Temp&0x08)LED_Display_Buf[3] |= 0x01;if(Bit1_Temp&0x10)LED_Display_Buf[4] |= 0x01;if(Bit1_Temp&0x20)LED_Display_Buf[5] |= 0x01;if(Bit1_Temp&0x40)LED_Display_Buf[6] |= 0x01;if(Bit1_Temp&0x80)LED_Display_Buf[7] |= 0x01;
    if(Bit2_Temp&0x01)LED_Display_Buf[0] |= 0x02;if(Bit2_Temp&0x02)LED_Display_Buf[1] |= 0x02;if(Bit2_Temp&0x04)LED_Display_Buf[2] |= 0x02;if(Bit2_Temp&0x08)LED_Display_Buf[3] |= 0x02;if(Bit2_Temp&0x10)LED_Display_Buf[4] |= 0x02;if(Bit2_Temp&0x20)LED_Display_Buf[5] |= 0x02;if(Bit2_Temp&0x40)LED_Display_Buf[6] |= 0x02;if(Bit2_Temp&0x80)LED_Display_Buf[7] |= 0x02;
    if(Bit3_Temp&0x01)LED_Display_Buf[0] |= 0x04;if(Bit3_Temp&0x02)LED_Display_Buf[1] |= 0x04;if(Bit3_Temp&0x04)LED_Display_Buf[2] |= 0x04;if(Bit3_Temp&0x08)LED_Display_Buf[3] |= 0x04;if(Bit3_Temp&0x10)LED_Display_Buf[4] |= 0x04;if(Bit3_Temp&0x20)LED_Display_Buf[5] |= 0x04;if(Bit3_Temp&0x40)LED_Display_Buf[6] |= 0x04; if(Bit3_Temp&0x80)LED_Display_Buf[7] |= 0x04;
	if(Bit4_Temp&0x01)LED_Display_Buf[0] |= 0x08;if(Bit4_Temp&0x02)LED_Display_Buf[1] |= 0x08;if(Bit4_Temp&0x04)LED_Display_Buf[2] |= 0x08;if(Bit4_Temp&0x08)LED_Display_Buf[3] |= 0x08;if(Bit4_Temp&0x10)LED_Display_Buf[4] |= 0x08;if(Bit4_Temp&0x20)LED_Display_Buf[5] |= 0x08;if(Bit4_Temp&0x40)LED_Display_Buf[6] |= 0x08;if(Bit4_Temp&0x80)LED_Display_Buf[7] |= 0x08;
	if(Bit5_Temp&0x01)LED_Display_Buf[0] |= 0x10;if(Bit5_Temp&0x02)LED_Display_Buf[1] |= 0x10;if(Bit5_Temp&0x04)LED_Display_Buf[2] |= 0x10;if(Bit5_Temp&0x08)LED_Display_Buf[3] |= 0x10;if(Bit5_Temp&0x10)LED_Display_Buf[4] |= 0x10;if(Bit5_Temp&0x20)LED_Display_Buf[5] |= 0x10;if(Bit5_Temp&0x40)LED_Display_Buf[6] |= 0x10;if(Bit5_Temp&0x80)LED_Display_Buf[7] |= 0x10;
	if(Bit6_Temp&0x01)LED_Display_Buf[0] |= 0x20;if(Bit6_Temp&0x02)LED_Display_Buf[1] |= 0x20;if(Bit6_Temp&0x04)LED_Display_Buf[2] |= 0x20;if(Bit6_Temp&0x08)LED_Display_Buf[3] |= 0x20;if(Bit6_Temp&0x10)LED_Display_Buf[4] |= 0x20;if(Bit6_Temp&0x20)LED_Display_Buf[5] |= 0x20;if(Bit6_Temp&0x40)LED_Display_Buf[6] |= 0x20;if(Bit6_Temp&0x80)LED_Display_Buf[7] |= 0x20;
	if(Bit7_Temp&0x01)LED_Display_Buf[0] |= 0x40;if(Bit7_Temp&0x02)LED_Display_Buf[1] |= 0x40;if(Bit7_Temp&0x04)LED_Display_Buf[2] |= 0x40;if(Bit7_Temp&0x08)LED_Display_Buf[3] |= 0x40;if(Bit7_Temp&0x10)LED_Display_Buf[4] |= 0x40;if(Bit7_Temp&0x20)LED_Display_Buf[5] |= 0x40;if(Bit7_Temp&0x40)LED_Display_Buf[6] |= 0x40;if(Bit7_Temp&0x80)LED_Display_Buf[7] |= 0x40;
	if(Bit8_Temp&0x01)LED_Display_Buf[0] |= 0x80;if(Bit8_Temp&0x02)LED_Display_Buf[1] |= 0x80;if(Bit8_Temp&0x04)LED_Display_Buf[2] |= 0x80;if(Bit8_Temp&0x08)LED_Display_Buf[3] |= 0x80;if(Bit8_Temp&0x10)LED_Display_Buf[4] |= 0x80;if(Bit8_Temp&0x20)LED_Display_Buf[5] |= 0x80;if(Bit8_Temp&0x40)LED_Display_Buf[6] |= 0x80;if(Bit8_Temp&0x80)LED_Display_Buf[7] |= 0x80;

	LED_S1  = LED_Display_Buf[0];
	LED_S2  = LED_Display_Buf[1];
	LED_S3  = LED_Display_Buf[2];
	LED_S4  = LED_Display_Buf[3];
	LED_S5  = LED_Display_Buf[4];
	LED_S6  = LED_Display_Buf[5];
	LED_S7  = LED_Display_Buf[6];
	LED_S8  = LED_Display_Buf[7];
}

//HEll0_Ad1C
void LED_Display_Hello(void)
{
	LED_Device(Str1[0],Str1[0],Str1[0],Str1[0],Str1[0],Str1[0],Str1[0],Str1[0]);TMR0IF = 0;while(TMR0IF == 0);
	LED_Device(Str1[0],Str1[0],Str1[0],Str1[0],Str1[0],Str1[0],Str1[0],Str1[1]);TMR0IF = 0;while(TMR0IF == 0);
	LED_Device(Str1[0],Str1[0],Str1[0],Str1[0],Str1[0],Str1[0],Str1[1],Str1[2]);TMR0IF = 0;while(TMR0IF == 0);
	LED_Device(Str1[0],Str1[0],Str1[0],Str1[0],Str1[0],Str1[1],Str1[2],Str1[3]);TMR0IF = 0;while(TMR0IF == 0);
	LED_Device(Str1[0],Str1[0],Str1[0],Str1[0],Str1[1],Str1[2],Str1[3],Str1[4]);TMR0IF = 0;while(TMR0IF == 0);
	LED_Device(Str1[0],Str1[0],Str1[0],Str1[1],Str1[2],Str1[3],Str1[4],Str1[5]);TMR0IF = 0;while(TMR0IF == 0);
	LED_Device(Str1[0],Str1[0],Str1[1],Str1[2],Str1[3],Str1[4],Str1[5],Str1[6]);TMR0IF = 0;while(TMR0IF == 0);
	LED_Device(Str1[0],Str1[1],Str1[2],Str1[3],Str1[4],Str1[5],Str1[6],Str1[7]);TMR0IF = 0;while(TMR0IF == 0);
	LED_Device(Str1[1],Str1[2],Str1[3],Str1[4],Str1[5],Str1[6],Str1[7],Str1[8]);TMR0IF = 0;while(TMR0IF == 0);
	LED_Device(Str1[2],Str1[3],Str1[4],Str1[5],Str1[6],Str1[7],Str1[8],Str1[9]);TMR0IF = 0;while(TMR0IF == 0);
	LED_Device(Str1[3],Str1[4],Str1[5],Str1[6],Str1[7],Str1[8],Str1[9],Str1[10]);TMR0IF = 0;while(TMR0IF == 0);
	LED_Device(Str1[4],Str1[5],Str1[6],Str1[7],Str1[8],Str1[9],Str1[10],Str1[11]);TMR0IF = 0;while(TMR0IF == 0);
	LED_Device(Str1[5],Str1[6],Str1[7],Str1[8],Str1[9],Str1[10],Str1[11],Str1[11]);TMR0IF = 0;while(TMR0IF == 0);TMR0IF = 0;while(TMR0IF == 0);
}

void LED_Display(unsigned long Temp0,unsigned long Temp1)
{
    unsigned char Com1_Dat = buf_Z[Temp0 / 1000 % 10];
	unsigned char Com2_Dat = buf_Z[Temp0 / 100 % 10];
	unsigned char Com3_Dat = buf_Z[Temp0 / 10 % 10];
	unsigned char Com4_Dat = buf_Z[Temp0 % 10];
	unsigned char Com5_Dat = buf_Z[Temp1 / 1000 % 10];
	unsigned char Com6_Dat = buf_Z[Temp1 / 100 % 10];
	unsigned char Com7_Dat = buf_Z[Temp1 / 10  % 10];
	unsigned char Com8_Dat = buf_Z[Temp1 % 10];

    LED_Device(Com1_Dat,Com2_Dat,Com3_Dat,Com4_Dat,Com5_Dat,Com6_Dat,Com7_Dat,Com8_Dat);
}

void LED_Clear()
{
	LED_S1  = 0x00;LED_S2  = 0x00;
	LED_S3  = 0x00;LED_S4  = 0x00;
	LED_S5  = 0x00;LED_S6  = 0x00;
	LED_S7  = 0x00;LED_S8  = 0x00;
}

void main(void)
{
	unsigned char  LCD_Div_SEL  = 0;
	unsigned char  LCDCON0_Temp = 0;
	unsigned short b            = 9999;

	//__delay_ms(20);

    TRISA = 0x00;
	TRISB = 0x00;
	TRISC = 0x00;
	TRISD = 0x00;

	//LCD初始化
	LCDCON0 = 0x06;
	LCDCON1 = 0xB8;
	COMEN   = 0xFF;
	SEGEN0  = 0xBF;
    SEGEN1  = 0x01;
    SEGEN2  = 0x20;
    LCDCON0_Temp = LCDCON0;

    T0CLKEN = 1;
	TMR0    = 0x00;
	PR0     = 200;
	T0CON0  = 0x26;
	T0CON1  = 0x00;

	LED_Clear();
	LED_Display_Hello();

	LED_Clear();
	LED_Display(0,0);

	while(1)
	{
	    //显示数据
	    if(TMR0IF == 1)
		{
		    TMR0IF = 0;
			b--;
			LED_Display(b,LCD_Div_SEL);
			if(b <= 0)b = 9999;

			if(LCD_Div_SEL <= 32)
			{
				LCDCON0 = (LCD_Div_SEL << 3) | LCDCON0_Temp;
				LCD_Div_SEL ++;
			}

			if(LCD_Div_SEL == 33)LCD_Div_SEL = 0;
		}
	}
}

void interrupt ISR_h (void)
{

}

void interrupt low_priority ISR_l (void)
{

}
