(M052)利用PWM波实现LED灯亮度调节

#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 );    
    }
}    


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值