/*******************************************************************************
* Device      : AD18F06A
* Author      :
* Version     :
* Date        :
* Description :
* Note		  :TX -> PB0 RX -> PB1 BPS -> 9600  ;
                上位机发送数据，MCU反馈相同的数据
********************************************************************************/

/********************************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_ON_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      );


void putch(unsigned char tmp)
{
	while(!TX1IF)continue;
	TXREG1 = tmp;
	TX1IF = 0;
}

unsigned char getch(void)
{
	while(!RC1IF)continue;
	RC1IF = 0;
	return RCREG1;
}

#define BAUD1 19200UL
#define FSYS  16000000UL

unsigned char str1[32] = {0xB4,0xAE,0xBF,0xDA,0xBB,0xD8,0xBB,0xB7,0xB2,0xE2,0xCA,0xD4,0xA3,0xBA,0xB7,0xB5,0xBB,0xD8,0xCA,0xD5,0xB5,0xBD,0xB5,0xC4,0xCA,0xFD,0xBE,0xDD,0xA3,0xA1,0x0D,0x0A};

void main(void)
{
    unsigned char i= 0;

	//TX_PB0_RX_PB1@19200
	UART1CLKEN = 1;
    TX1_MAP    = MAP_PB0;
    RX1_MAP    = MAP_PB1;
	TRISB0     = 0;
	TRISB1     = 1;
	UARTCON1   = 0x00;
	SPBRG1     = FSYS/(BAUD1*64);
	RCSTA1     = 0x90;
	TXSTA1     = 0x20;

	printf("***********************************\r\n");
	for(i = 0;i < 32;i++)printf("%c",str1[i]);
	printf("***********************************\r\n");
	printf("                                   \r\n");

    RC1IP      = 1;	    //高优先级
	RC1IF      = 0;     //清除中断标志
	RC1IE      = 1;     //使能接收中断

	IPEN       = 1;     //允许高低优先级
	GIEL       = 0;     //关闭低优先级中断
	GIEH       = 1;     //开启高优先级中断

    while(1);
}

void interrupt ISR_h (void)
{
    if(RC1IE == 1 && RC1IF == 1 && RC1IP == 1)
	{
		RC1IF = 0;

		while(!TX1IF)continue;
		TXREG1 = RCREG1;
		TX1IF = 0;
	}
}

void interrupt low_priority ISR_l (void)
{

}
