//==== M05xx ¹¤³ÌÄ£°å˵Ã÷ ===============================================================
// 22MHzÓÉPLL±¶Æµµ½ 49.7664MHz. ÁíÍ⻹ÅäÖÃÁËTimer3, UART0, ADC, PWM, GPIO
// Copyright reserved by Nuvoton (SH) Ltd. All right reserved.
// Author : Angus Wang. Date : 2013/12/27
//=======================================================================================
#include "M05xxDx.h"
#define unFosc 12000000
#define Baud 115200
#define unFosc1 12000000
#define Baud1 115200
#define beep P00
uint32_t TMR_Tick = 0;
uint32_t Tem_count = 0,flag_1ms = 0,buff1;
uint8_t TMR_Select_Flag = 0;
uint8_t TMR_Mode_Select_Flag = 0;
/* ÑÓʱ ms ºÁÃë */
void delay_ms(uint32_t ms)
{
while(ms > 0) {
flag_1ms = 0;
while(!flag_1ms);
ms--;
}
}
void BEEP(uint32_t ms)
{
beep = 0;
delay_ms(ms);
beep = 1;
}
void UartSend(uint8_t pBuf[],uint8_t unNumOfBytes)
{
uint8_t i;;
for(i=0; i<unNumOfBytes; i++)
{
UART0->THR = pBuf[i];
while ((UART0->FSR & TX_FIFO_EMPTY) != 0x00); //¼ì²é·¢Ë͵ÄFIFOÊÇ·ñΪ¿Õ
}
}
void UartSend1(uint8_t pBuf1[],uint8_t unNumOfBytes1)
{
uint8_t i;;
for(i=0; i<unNumOfBytes1; i++)
{
UART1->THR = pBuf1[i];
while ((UART1->FSR & TX_FIFO_EMPTY) != 0x00); //¼ì²é·¢Ë͵ÄFIFOÊÇ·ñΪ¿Õ
}
}
void UnlockReg(void)
{
while((GCR->REGWRPROT & 1) == 0){ // ·ÀÖ¹±»´ò¶Ï½âËø²»³É¹¦
GCR->REGWRPROT = 0x59 ;
GCR->REGWRPROT = 0x16 ;
GCR->REGWRPROT = 0x88 ;
}
}
void SysClk_Init(void) // PLL: (22.1184M/2)*9 = 2 * 49.7664MHz
{
UnlockReg() ;
CLK->PWRCON |= CLKSTA_EXT_STABLE ;
while((CLK->CLKSTATUS & CLKSTA_EXT_STABLE) == 0) ; // µÈÆäÎȶ¨
CLK->CLKSEL0 = CLKS0_HCLK_EXT_0_ ;
LOCKREG() ; // ÉÏËø
}
//// Timer1, 1MHz Á¬Ðø¼ÆÊý, ÓÃÓÚͨÓö¨Ê±, ÖÜÆÚ16.77Ãë ///////////////////////////////////
// дTimer ¼Ä´æÆ÷Ðè2¸öTimerʱÖÓºó²ÅÓÐЧ, TimerʱÖÓÈôºÜµÍ,дÍêºóÒªÑÓʱ,²ÅÄܶÁ
/*
void Timer3Init(void)
{
TIMER3->TCSR = T_DEBUG_WORK | T_IE | T_MODE_CONTINUOUS // 2¸ö 25MHz ʱÖÓºó²ÅÄÜÔÙ¶Á
| T_CRST | T_TDR_EN + 49 ; // ¸´Î», 50·ÖƵ, 1us ¼ÆÊý
TIMER3->TCMPR = 0 ;
NVIC_SetPriority(TMR3_IRQn, 2) ; // ÖжÏÓÅÏȼ¶Îª 2
NVIC->ISER[0] = 1<<TMR3_IRQn ; // ´ò¿ªÖжÏÏòÁ¿
TIMER3->TCSR |= T_CEN ; // Æô¶¯Timer, Õâ¸öÊǶÁ¸ÄдָÁî
}
*/
void Timer3Init(void)
{
GCR->IPRSTC2 |= RST_T3 ;
GCR->IPRSTC2 &= ~ RST_T3 ;
CLK->APBCLK |= CLK_TMR3_EN ;
CLK->CLKSEL1 = CLKS1_TMR3_EXT_0 ;
TIMER3->TCSR |= T_MODE_PERIODIC ;
//TIMER3->TCSR |= T_MODE_ONCE_0 ;
TIMER3->TCSR &= 0xFFFFFF00; ;
TIMER3->TCMPR = 1200 ;
TIMER3->TCSR |= T_IE ;
NVIC->ISER[0] = 1<<TMR3_IRQn ;
TIMER3->TCSR |= T_CEN ;
}
void Delayus(uint32_t Tim) // ²ÎÊý×î´ó0xFFFFFF, Ô¼16.77Ãë
{
uint32_t Tick = TIMER3->TDR ;
while(((TIMER3->TDR - Tick)&0xFFFFFF) <= Tim) ;
}
void UART0_Init(void)
{
CLK->APBCLK |= CLK_UART0_EN ; //´®¿Ú0ʱÖÓʹÄÜ
CLK->CLKSEL1 = CLKS1_UART_EXT_0 ; //´®¿ÚʱÖÓµ÷ΪÍⲿ¾§Õñ
CLK->CLKDIV &= ~(15<<8) ; //´®¿ÚʱÖÓ·ÖƵΪ0
GCR->IPRSTC2 |= RST_UART0 ; //¸´Î»´®¿Ú0
GCR->IPRSTC2 &= ~ RST_UART0 ; //¸´Î»½áÊø
UART0->FCR |= TX_RESET ; //·¢ËÍFIFO¼Ä´æÆ÷¸´Î»
UART0->FCR |= RX_RESET ; //½ÓÊÕFIFO¼Ä´æÆ÷¸´Î»
UART0->LCR = WORD_LEN_8BIT ; //УÑ鹦ÄÜÈ¡Ïû£¬8¸öÊý¾Ý룬һ¸öֹͣλ
UART0->BAUD |= DIV_X_EN | DIV_X_ONE ; //ÉèÖò¨ÌØÂʵķÖƵ
UART0->BAUD |= ((unFosc / Baud) -2); //²¨ÌØÂÊÊÇ115200£¬Íⲿ¾§ÕñÊÇ12M
UART0->IER = 1 ; //½ÓÊÕÊý¾ÝÖжÏʹÄÜ
NVIC_EnableIRQ(UART0_IRQn) ;
}
void UART1_Init(void)
{
CLK->APBCLK |= CLK_UART1_EN ; //´®¿Ú1ʱÖÓʹÄÜ
CLK->CLKSEL1 = CLKS1_UART_EXT_0 ; //´®¿ÚʱÖÓµ÷ΪÍⲿ¾§Õñ
CLK->CLKDIV &= ~(15<<8) ; //´®¿ÚʱÖÓ·ÖƵΪ0
GCR->IPRSTC2 |= RST_UART1 ; //¸´Î»´®¿Ú0
GCR->IPRSTC2 &= ~ RST_UART1 ; //¸´Î»½áÊø
//UART1->FCR |= TX_RESET ; //·¢ËÍFIFO¼Ä´æÆ÷¸´Î»
//UART1->FCR |= RX_RESET ; //½ÓÊÕFIFO¼Ä´æÆ÷¸´Î»
UART1->LCR = WORD_LEN_8BIT ; //УÑ鹦ÄÜÈ¡Ïû£¬8¸öÊý¾Ý룬һ¸öֹͣλ
UART1->BAUD |= DIV_X_EN | DIV_X_ONE ; //ÉèÖò¨ÌØÂʵķÖƵ
UART1->BAUD |= ((unFosc1 / Baud1) -2); //²¨ÌØÂÊÊÇ115200£¬Íⲿ¾§ÕñÊÇ12M
UART1->IER = 1 ; //½ÓÊÕÊý¾ÝÖжÏʹÄÜ
NVIC_EnableIRQ(UART1_IRQn) ;
}
void PWM_Init(void)
{
PWMA->PPR = PPR_PWM01_DIV(250) ; // PWM ʱÖÓ250·ÖƵ, µÃ200KHz
PWMA->CSR = CSR_DIV4_CH(0) ; // ͨµÀ0ÔÙ4·ÖƵ, µÃ 50KHz
PWMA->PCR = PCR_EN_CH(0) | PCR_PERIOD_MOD_CH(0) ; // ʹÄÜPWM0, ÖÜÆÚģʽ
PWMA->POE = POE_CH(0) ; // PWM Êä³öʹÄÜ
PWMA->PIER = 0 ; // ²»¿ªÖжÏ
PWMA->CNR0 = 50000 ; // ÖÜÆÚÖµ, Ô¼ 1 Ãë
PWMA->CMR0 = 40000 ; // ¸ßµçƽ 0.8 Ãë
}
void ADC_Init(void)
{
ADC->CR = AD_EN | AD_SINGLE_MODE_0 ; // ADC ʹÄܵ¥´Îģʽ
}
//// ¼¯ÖÐÅäÖÃËùÓеÄGPIO¿Ú, Ðü¿ÕµÄGPIOÅä³É׼˫Ïò, ²¢Êä³ö1 ////////////////////////////////
void GPIO_Init(void)
{
P0->DOUT = 0xFF ; // ÏÈдÊä³öÖµ
P0->PMD = Px7_QB | Px6_QB | Px5_QB | Px4_QB // GPIO¹¦ÄÜÒ»Çл»¾ÍÊä³öÕýÈ·µÄÖµ
| Px3_QB | Px2_QB | Px1_QB | Px0_OUT ;
GCR->P0_MFP = 0 ; // ÔÙÇл» GPIO ¿Ú¹¦ÄÜ
P1->DOUT = 0xFF ;
P1->PMD = Px7_QB | Px6_QB | Px5_QB | Px4_QB | Px3_QB | Px2_QB | Px1_QB | Px0_QB ;
GCR->P1_MFP = P13_TXD1 ; // UART1 ·¢ËͽÅ,
GCR->P1_MFP = P12_RXD1 ;
P2->DOUT = 0xF5 ;
P2->PMD = Px7_QB | Px6_QB | Px5_QB | Px4_QB // µÍËÄλ½ÓLED, Åä³ÉÊä³ö
| Px3_OUT | Px2_OUT | Px1_OUT | Px0_OUT ; // P20 ±»PWM½Ó¹Ü,Åä³Éʲô¶¼¿ÉÒÔ
GCR->P2_MFP = P20_PWM0 ; // P20 Åä³É PWM0 Êä³ö
// ÆäËü½ÅÅäÖÃ0, GPIO¿Ú
P3->DOUT = 0xFF ;
P3->PMD = Px7_QB | Px6_OUT | Px5_QB | Px4_QB // Nutiny °å P36 ½ÓÁË LED
| Px3_QB | Px2_QB | Px1_QB | Px0_QB ;
GCR->P3_MFP = P31_TXD0 ; // UART0 ·¢ËͽÅ,
GCR->P3_MFP = P30_RXD0 ;
P4->DOUT = 0xFF ;
P4->PMD = Px7_QB | Px6_OUT | Px5_QB | Px4_QB | Px3_QB | Px2_QB | Px1_QB | Px0_QB ;
GCR->P4_MFP = 0 ;
}
//=======================================================================================
// ---------------------------main()----------------------------------------------
//=======================================================================================
int main(void)
{
uint16_t temp16 ;
//uint32_t LED_Flash = 1 ;
// µÚÒ»²½, ¸÷ÍâÉèʱÖÓÑ¡Ôñ,ÍâÉèʱÖÓʹÄܺó,дÍâÉè¼Ä´æÆ÷²ÅÓÐЧ ///////////////////////////
SysClk_Init() ;
// µÚÈý²½, GPIO¿Ú¹¦ÄÜÅäÖÃ, GPIO¿Ú¿ªÊ¼Êä³öÕýÈ·µÄÖµ /////////////////////////////////////
GPIO_Init() ;
// µÚ¶þ²½, ƬÄÚ¸÷ÍâÉ蹦ÄÜÅäÖà /////////////////////////////////////////////////////////
Timer3Init() ; //ÅäºÃTimer1,²ÅÄÜʹÓÃDelayus()
PWM_Init() ;
ADC_Init() ;
UART0_Init() ;
UART1_Init() ;
// PWM0 ¿ÚÉ쵀 LED ¿ªÊ¼ÉÁ˸
P46 = 1;
delay_ms(500);
//BEEP(50);
P46 = 0;
//UART0_Init(115200) ;
//Tx0FillFiFo("\nUART0 is OK ! ", 16) ; // ´®¿ÚÅäÖúÃ, ָʾһÏÂ
// Ö÷Ñ»· /////////////////////////////////////////////////////////////////////////////
while(1)
{
//Delayus(50000) ;
if(TMR_Select_Flag == 1)
{
TMR_Select_Flag = 0;
// P46 = ~P46 ;
//P46 = 1;
}
if(temp16 > 3000) P23 = 0 ;
}
}
void TMR3_IRQHandler(void)
{
TIMER3->TISR = ~0 ; // ÇåÖжϱêÖ¾
if(++Tem_count >= 10)
{
Tem_count = 0;
flag_1ms = 1;
}
if(++buff1 >= 10000)
{
buff1 = 0;
TMR_Select_Flag = 1;
}
//TIMER3->TEXISR = ~0 ; // Çå±êÖ¾Ðè2¸öTimerʱÖÓ(25M)ÖÜÆÚ
}
void UART0_IRQHandler(void)
{
uint8_t Data, j;;
if(UART0->ISR & RDA_INT)
{
//P46 = 1;
while(UART0->ISR & RX_DATA_IF) //»ñÈ¡ËùÓеÄÊý¾Ý
{
//P46 = 1;
while (UART0->FSR & RX_FIFO_EMPTY); //¼ì²éFIFOÊÇ·ñΪ¿Õ
//P46 = 1;
Data = UART0->RBR; //¶ÁÈ¡Êý¾Ý
UART0->THR = Data;
UartSend(&Data,1); //·¢ËÍÊý¾Ý
//if(Data == 0x11) P46 = 1;
}
}
}
void UART1_IRQHandler(void)
{
uint8_t ucData_1 ;
P46 = 1;
if(UART1->ISR & RDA_INT) //¼ì²éÊÇ·ñ½ÓÊÕÊý¾ÝÖжÏ
{
while(UART1->ISR & RX_DATA_IF) //»ñÈ¡ËùÓеÄÊý¾Ý
{
while (UART1->FSR & RX_FIFO_EMPTY); //¼ì²éFIFOÊÇ·ñΪ¿Õ
ucData_1 = UART1->RBR; //¶ÁÈ¡Êý¾Ý
UART1->THR = ucData_1;
//UartSend1(&ucData_1,1); //·¢ËÍÊý¾Ý
//if(ucData_1 == 0x11) P46 = 1;
}
}
}
麻烦各位大哥帮小弟看看,到底是什么原因,先说声谢谢了。
|
|