#include <stdio.h>
#include <stdint.h>
#include "M051.h"
#include "Register_Bit.h"
#include "Common.h"
#include "UART.h"
#include "Retarget.h"
#include "Macro_SystemClock.h"
#define RXBUFSIZE 64
#define TXBUFSIZE 64
#define TRUE 1
#define FALSE 0
/* ----------------------------------------------------------------------------------
Global variables
----------------------------------------------------------------------------------*/
uint32_t HighCounter = 0;
uint32_t LowCounter = 0;
uint32_t LowFlag = FALSE;
uint32_t BrightNess[10] = {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10};
uint32_t BrightLevel = 0;
uint32_t PWMTime = 0;
uint32_t PWMTimeOut = 0;
uint32_t PWMCount = 0;
uint8_t comRbuf[RXBUFSIZE]; //recieve buffer
uint8_t comTbuf[TXBUFSIZE]; //send buffer
uint8_t comRbyte = 0;
uint8_t comRtail = 0;
void SendUARTChar( uint8_t c )
{
/* check Tx Empty */
UA1_THR = ( uint8_t )c;
while( (UA1_FSR & TX_EMPTY) != 0x00 );
}
void SendString( uint8_t *pbString , uint32_t length )
{
uint32_t j = 0;
/* send string to UART1 */
for( j = 0 ; j < length ; j++ )
{
SendUARTChar( *( pbString+j ));
}
}
/*-----------------------------------------------------------------------------
Function: UART_Init
Parameter:
None
Returns:
None
Description:
Initialize UART1, 9600bps, 8N1.
*-----------------------------------------------------------------------------*/
void UART1_Init( )
{
/* Step 1. GPIO initial */
P1_MFP &= ~( ((1<<11)|(1<<3)) | ((1<<10)|(1<<2)) );
P1_MFP |= (1<<11) | (1<<10); //P1.2 --> UART1 RX
//P1.3 --> UART1 TX
/* Step 2. Enable and Select UART clock source*/
UART1_Clock_EN; //UART Clock Enable, APBCLK[16]:1
UARTClkSource_ex12MHZ; //UART Clock is ext12MHz, CLKSEL1[25,24]: 00
CLKDIV &= ~(15<<8); //UART Clock DIV Number = 0;
/* Step 3. Select Operation mode */
IPRSTC2 |= UART1_RST; //Reset UART1
IPRSTC2 &= ~UART1_RST; //Reset end
UA1_FCR |= TX_RST; //Tx FIFO Reset
UA1_FCR |= RX_RST; //Rx FIFO Reset
UA1_LCR &= ~PBE; //Parity Bit Disable
UA1_LCR &= ~WLS;
UA1_LCR |= WL_8BIT; //8 bits Data Length
UA1_LCR &= NSB_ONE; //1 stop bit
/* Step 4. Set BaudRate */
UA1_BAUD |= DIV_X_EN; //Mode2:DIV_X_EN = 1
UA1_BAUD |= DIV_X_ONE; //Mode2:DIV_X_ONE =1
/* For XTAL = 12 MHz */
UA1_BAUD |= ((12000000 / 9600 ) -2); //Set BaudRate to 115200; UART_CLK/(A+2) = 115200, UART_CLK=12MHz
}
void TMR0_Init(void)
{
/* Enable Timer0 clock source */
APBCLK |= TMR0_CLKEN;
/* Select Timer0 clock source as external 12M */
CLKSEL1 = (CLKSEL1 & (~TM0_CLK)) | TM0_12M;
/* Reset IP TMR0 */
IPRSTC2 |= TMR0_RST;
IPRSTC2 &= ~TMR0_RST;
/* Select timer0 Operation mode as period mode */
TCSR0 &= ~TMR_MODE;
TCSR0 |= MODE_PERIOD;
/* Select Time out period = (Period of timer clock input) * (8-bit Prescale + 1) * (24-bit TCMP)*/
TCSR0 = TCSR0 & 0xFFFFFF00; // Set Prescale [0~255]
TCMPR0 = 12000; // Set TCMPR
/* Enable timer0 interrupt */
TCSR0 |= TMR_IE;
NVIC_ISER |= TMR0_INT;
/* Reset timer0 counter */
TCSR0 |= CRST;
/* Enable Timer0 */
TCSR0 |= CEN;
}
/*-----------------------------------------------------------------------------
Function: TMR0_IRQHandler
Parameter:
None
Returns:
None
Description:
Timer0 ISR routine
*-----------------------------------------------------------------------------*/
void TMR0_IRQHandler(void)
{
/* Clear timer0 interrupt flag */
TISR0 |= TMR_TIF;
if( HighCounter != BrightNess[BrightLevel] && LowFlag == FALSE )
{
P21_DOUT |= 0x01;
HighCounter++;
}
else
{
LowFlag = TRUE;
if( LowCounter != 10 - BrightNess[BrightLevel] )
{
P21_DOUT &= 0x00;
LowCounter++;
}
else
{
HighCounter = 0;
LowCounter = 0;
P21_DOUT |= 0x01;
LowFlag = FALSE;
}
}
}
void GPIOP2P3P4_IRQHandler( void )
{
P4_ISRC &= 0x02;
HighCounter = 0;
LowCounter = 0;
LowFlag = FALSE ;
BrightLevel = ( BrightLevel == 9 )? 0 : (BrightLevel+1);
}
void UART1_IRQHandler(void)
{
uint8_t bInChar[1]={0xFF};
if(UA1_ISR & RDA_INT)
{
/* Get all the input characters */
while(UA1_ISR & RDA_IF)
{
/* Check RX empty */
while (UA1_FSR & RX_EMPTY);
bInChar[0] = UA1_RBR;
/* Check if buffer full */
if(comRbyte < RXBUFSIZE)
{
/* Enqueue the character */
comRbuf[comRtail] = bInChar[0];
comRtail = (comRtail == (RXBUFSIZE-1)) ? 0 : (comRtail+1);
comRbyte++;
}
}
}
}
void main()
{
/* UART Clock is ext12MHz */
uint32_t i = 0;
// UARTClkSource_ex12MHZ;
Un_Lock_Reg();
// ClkRegUnlock;
/* Enable external 12M Crystal */
PWRCON |= XTL12M_EN;
/* Wait until 12M clock is stable. */
while((CLKSTATUS & XTL12M_STB) == 0);
/* Set external 12M crystal as the system clock */
CLKSEL0 = (CLKSEL0 & (~HCLK)) | HCLK_12M;
/* Set pin mode as output */
P3_PMD &= Px1_OUT;
P2_PMD &= Px1_OUT;
TMR0_Init();
/* Set P4.1 as interrpt pin */
P4_PMD |= 0xffff;
P4_PMD &= Px1_IN;
P4_IEN |= IF_EN1;
P4_IMD &= IMD1_EDG;
DBNCECON &= 0x00;
DBNCECON |= 0x33;
P4_DBEN &= 0x00;
P4_DBEN |= DBEN1; //Enable P4.1 De-bounce function.
NVIC_ISER |= GP234_INT; //Enable p2p3p4 interrupt
UART1_Init();
UA1_IER |= RDA_IEN ;
NVIC_ISER |= UART1_INT;
while(1)
{
// SendString( comRbuf , 64 );
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)