CH552/LCD/usr/common.c

244 lines
9.7 KiB
C
Raw Normal View History

2018-10-17 05:37:36 +02:00
/********************************** (C) COPYRIGHT *******************************
* File Name : Debug.C
* Author : WCH
* Version : V1.0
* Date : 2017/01/20
* Description : CH554 DEBUG Interface
CH554<EFBFBD><EFBFBD>Ƶ<EFBFBD>޸ġ<EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD><EFBFBD>շ<EFBFBD><EFBFBD>Ӻ<EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD>Ź<EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
*******************************************************************************/
#include <intrins.h>
#include "CH554.H"
#include "common.h"
/*******************************************************************************
* Function Name : CfgFsys( )
* Description : CH554ʱ<EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ú<EFBFBD><EFBFBD><EFBFBD>,Ĭ<EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>Fsys 6MHz<EFBFBD><EFBFBD>FREQ_SYS<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD>
CLOCK_CFG<EFBFBD><EFBFBD><EFBFBD>õõ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD>£<EFBFBD>
Fsys = (Fosc * 4/(CLOCK_CFG & MASK_SYS_CK_SEL);<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>Լ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void CfgFsys( )
{
// SAFE_MOD = 0x55;
// SAFE_MOD = 0xAA; //<2F><><EFBFBD>밲ȫģʽ SFR<46>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD>޸ı<DEB8><C4B1><EFBFBD><EFBFBD>Ƚ<EFBFBD><C8BD>밲ȫģʽ<C4A3><CABD>
// CLOCK_CFG |= bOSC_EN_XT; //ʹ<><CAB9><EFBFBD>ⲿ<EFBFBD><E2B2BF><EFBFBD><EFBFBD>
// CLOCK_CFG &= ~bOSC_EN_INT; //<2F>ر<EFBFBD><D8B1>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD> <20>л<EFBFBD><D0BB><EFBFBD><EFBFBD>ⲿ<EFBFBD><E2B2BF><EFBFBD><EFBFBD>
SAFE_MOD = 0x55;
SAFE_MOD = 0xAA;
// CLOCK_CFG = CLOCK_CFG & ~ MASK_SYS_CK_SEL | 0x07; // 32MHz
CLOCK_CFG = CLOCK_CFG & ~ MASK_SYS_CK_SEL | 0x06; // 24MHz
// CLOCK_CFG = CLOCK_CFG & ~ MASK_SYS_CK_SEL | 0x05; // 16MHz
// CLOCK_CFG = CLOCK_CFG & ~ MASK_SYS_CK_SEL | 0x04; // 12MHz
// CLOCK_CFG = CLOCK_CFG & ~ MASK_SYS_CK_SEL | 0x03; // 6MHz
// CLOCK_CFG = CLOCK_CFG & ~ MASK_SYS_CK_SEL | 0x02; // 3MHz
// CLOCK_CFG = CLOCK_CFG & ~ MASK_SYS_CK_SEL | 0x01; // 750KHz
// CLOCK_CFG = CLOCK_CFG & ~ MASK_SYS_CK_SEL | 0x00; // 187.5MHz
SAFE_MOD = 0x00; //<2F>˳<EFBFBD><CBB3><EFBFBD>ȫģʽ
}
/*******************************************************************************
* Function Name : L0_nop_1us(void)
* Description : 1us<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Input : UNIT16 n
* Output : None
* Return : None
*******************************************************************************/
void L0_nop_1us(void) // <20><>uSΪ<53><CEAA>λ<EFBFBD><CEBB>ʱ
{
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();
}
/*******************************************************************************
* Function Name : mDelayus(UNIT16 n)
* Description : us<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Input : UNIT16 n
* Output : None
* Return : None
*******************************************************************************/
void delayus( UINT16 n ) // <20><>uSΪ<53><CEAA>λ<EFBFBD><CEBB>ʱ
{
while( n )
{
L0_nop_1us();
n--;
}
}
/*******************************************************************************
* Function Name : mDelayms(UNIT16 n)
* Description : ms<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Input : UNIT16 n
* Output : None
* Return : None
*******************************************************************************/
void delayms( UINT16 n ) // <20><>mSΪ<53><CEAA>λ<EFBFBD><CEBB>ʱ
{
while ( n )
{
delayus(1000);
n--;
}
}
/*******************************************************************************
* Function Name : CH554UART0Alter()
* Description : CH554<EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>P0.2<EFBFBD><EFBFBD>P0.3
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void CH554UART0Alter()
{
PIN_FUNC |= bUART0_PIN_X; //<2F><><EFBFBD><EFBFBD>ӳ<EFBFBD>䵽P1.2<EFBFBD><EFBFBD>P1.3
}
/*******************************************************************************
* Function Name : mInitSTDIO()
* Description : CH554<EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>,Ĭ<EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>T1<EFBFBD><EFBFBD>UART0<EFBFBD>IJ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,Ҳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>T2
<EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void mInitSTDIO( )
{
UINT32 x;
UINT8 x2;
SM0 = 0; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
SM1 = 1; //<2F><><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1> 0-<2D><><EFBFBD>ù̶<C3B9><CCB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1-<2D>ɱ䲨<C9B1><E4B2A8><EFBFBD><EFBFBD> <20><>t1t2<74><32><EFBFBD><EFBFBD>
SM2 = 0; //<2F><><EFBFBD><EFBFBD>0ʹ<30><CAB9>ģʽ1
//ʹ<><CAB9>Timer1<72><31>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD>ʷ<EFBFBD><CAB7><EFBFBD><EFBFBD><EFBFBD>
RCLK = 0; //UART0<54><30><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
TCLK = 0; //UART0<54><30><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
PCON |= SMOD;
x = 10 * FREQ_SYS / UART0_BUAD / 16; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5>ע<EFBFBD><D7A2>x<EFBFBD><78>ֵ<EFBFBD><D6B5>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>
x2 = x % 10;
x /= 10;
if ( x2 >= 5 ) x ++; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//PIN_FUNC
TMOD = TMOD & ~ bT1_GATE & ~ bT1_CT & ~ MASK_T1_MOD | bT1_M1; //0X20<32><30>Timer1<72><31>Ϊ8λ<38>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>ض<EFBFBD>ʱ<EFBFBD><CAB1>
T2MOD = T2MOD | bTMR_CLK | bT1_CLK; //Timer1ʱ<31><CAB1>ѡ<EFBFBD><D1A1>
TH1 = 0-x; //12MHz<48><7A><EFBFBD><EFBFBD>,buad/12Ϊʵ<CEAA><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD>
TR1 = 1; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>1
TI = 1;
REN = 1; //<2F><><EFBFBD><EFBFBD>0<EFBFBD><30><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>
}
/*******************************************************************************
* Function Name : CH554UART0RcvByte()
* Description : CH554UART0<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>
* Input : None
* Output : None
* Return : SBUF
*******************************************************************************/
UINT8 CH554UART0RcvByte( )
{
while(RI == 0); //<2F><>ѯ<EFBFBD><D1AF><EFBFBD>գ<EFBFBD><D5A3>жϷ<D0B6>ʽ<EFBFBD>ɲ<EFBFBD><C9B2><EFBFBD>
RI = 0;
return SBUF;
}
/*******************************************************************************
* Function Name : CH554UART0SendByte(UINT8 SendDat)
* Description : CH554UART0<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>
* Input : UINT8 SendDat<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Output : None
* Return : None
*******************************************************************************/
void CH554UART0SendByte(UINT8 SendDat)
{
SBUF = SendDat; //<2F><>ѯ<EFBFBD><D1AF><EFBFBD>ͣ<EFBFBD><CDA3>жϷ<D0B6>ʽ<EFBFBD>ɲ<EFBFBD><C9B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2<EFBFBD><32><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0>TI=0
while(TI ==0);
TI = 0;
}
/*******************************************************************************
* Function Name : UART1Setup()
* Description : CH554<EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void UART1Setup( )
{
U1SM0 = 0; //UART1ѡ<31><D1A1><38><CEBB><EFBFBD><EFBFBD>λ
U1SMOD = 1; //<2F><><EFBFBD><EFBFBD>ģʽ
U1REN = 1; //ʹ<>ܽ<EFBFBD><DCBD><EFBFBD>
SBAUD1 = 0 - FREQ_SYS/16/UART1_BUAD;
}
/*******************************************************************************
* Function Name : CH554UART1RcvByte()
* Description : CH554UART1<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>
* Input : None
* Output : None
* Return : SBUF
*******************************************************************************/
UINT8 CH554UART1RcvByte( )
{
while(U1RI == 0); //<2F><>ѯ<EFBFBD><D1AF><EFBFBD>գ<EFBFBD><D5A3>жϷ<D0B6>ʽ<EFBFBD>ɲ<EFBFBD><C9B2><EFBFBD>
U1RI = 0;
return SBUF1;
}
/*******************************************************************************
* Function Name : CH554UART1SendByte(UINT8 SendDat)
* Description : CH554UART1<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>
* Input : UINT8 SendDat<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Output : None
* Return : None
*******************************************************************************/
void CH554UART1SendByte(UINT8 SendDat)
{
SBUF1 = SendDat; //<2F><>ѯ<EFBFBD><D1AF><EFBFBD>ͣ<EFBFBD><CDA3>жϷ<D0B6>ʽ<EFBFBD>ɲ<EFBFBD><C9B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2<EFBFBD><32><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0>TI=0
while(U1TI ==0);
U1TI = 1;
}
/*******************************************************************************
* Function Name : CH554WDTModeSelect(UINT8 mode)
* Description : CH554<EFBFBD><EFBFBD><EFBFBD>Ź<EFBFBD>ģʽѡ<EFBFBD><EFBFBD>
* Input : UINT8 mode
0 timer
1 watchDog
* Output : None
* Return : None
*******************************************************************************/
void WDTModeSelect(UINT8 mode)
{
SAFE_MOD = 0x55;
SAFE_MOD = 0xaa; //<2F><><EFBFBD>밲ȫģʽ
if(mode){
GLOBAL_CFG |= bWDOG_EN; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ź<EFBFBD><C5B9><EFBFBD>λ
}
else GLOBAL_CFG &= ~bWDOG_EN; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ź<EFBFBD><C5B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ʱ<EFBFBD><CAB1>
SAFE_MOD = 0x00; //<2F>˳<EFBFBD><CBB3><EFBFBD>ȫģʽ
WDOG_COUNT = 0; //<2F><><EFBFBD>Ź<EFBFBD><C5B9><EFBFBD><EFBFBD><EFBFBD>ֵ
}
/*******************************************************************************
* Function Name : CH554WDTFeed(UINT8 tim)
* Description : CH554<EFBFBD><EFBFBD><EFBFBD>Ź<EFBFBD><EFBFBD><EFBFBD>ʱʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Input : UINT8 tim <EFBFBD><EFBFBD><EFBFBD>Ź<EFBFBD><EFBFBD><EFBFBD>λʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
00H(6MHz)=2.8s
80H(6MHz)=1.4s
* Output : None
* Return : None
*******************************************************************************/
void WDTFeed(UINT8 tim)
{
WDOG_COUNT = tim; //<2F><><EFBFBD>Ź<EFBFBD><C5B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
}