Haremos uso del módulo MTU6 con el que cuenta el MCU R5F563NB de la tarjeta de evaluación YRDKRX63N. Generaremos una frecuencia de 10 Khz que será reflejado en un LED por medio de la variación del potenciómetro (duty cycle).
- Inicializaremos la unidad de timer MTU4
 - Inicializaremos el ADC de 10 bits
 - Variaremos el potenciómetro para modificar el duty cycle de la frecuencia de 10Khz
 
DESARROLLO:
- Del RX63N Group User's Manual: Hardware página 753 está especificado la salida MTIOC4A que utilizaremos del módulo MTU4.
 
- Del YRDKRX63N schematic observamos que la señal de salida del PWM esta referenciada al LED11:
 
PASOS:
- Creación de un proyecto:
 
2.- New/ C Project / Renesas RXC ToolChain
3.- Seleccionar el target R5F563NB, debug hardware Segger jLink, después next
4.- Seleccionar C/C++ Source file y por ultimo Finish.
5.- La inicialización del MTU4 se encuentra en el archivo pwm.c y se muestra a continuación:
void MTU4_Init(void)
{
            MSTP(MTU4)
= 0;      /* Cancel MTU peripheral stop state. */
            MTU.TSTR.BIT.CST4 = 0; /* Stop MTU 4 */
            MTU.TRWER.BIT.RWE = 1; /* Enable read/write access to the
write-protected MTU3 and MTU4 registers. */
            MTU.TOER.BIT.OE4A = 1; /* Enable MTIOC4A output. In MTU3 and
MTU4, set TOER prior to setting TIOR. */
            /* Port E2 Pin Function Select Register
(PE2PFS)
             b7     
Reserved: This bit is always read as 0. The write value should always be
0.
             b6     
ISEL:     Interrupt Input Function
Select, 0 = not used as IRQn input pin
             b5     
Reserved: This bit is always read as 0. The write value should always be
0.
             b4:b0  
PSEL:     These bits select the
peripheral function.
             */
            MPC.PE2PFS.BYTE = 0x01; /* 1 defines PE2 to be MTIOC4A, with no
IRQ. */
            PORTE.PDR.BIT.B2 = 1; /* Set PE2 as output. */
            PORTE.PMR.BIT.B2 = 1; /* Set PE2 as peripheral function bit */
            /* Timer Control Register (TCR)
             b7:b5  
CCLR: Counter Clear Source 1 = TCNT cleared by TGRA compare match/input
capture
             2 = TCNT cleared by TGRB compare match/input
capture
             b4:b3  
CKEG: Clock Edge           0 =
count at rising edge
             b2:b0  
TPSC: Time Prescaler      
1 = count on PCLK / 4
             */
            MTU4.TCR.BYTE = 0x21; /* Clear on TGRA match. Prescaler
= PCLK / 4 setting. */
            /* Timer Mode Register (TMDR)
             b7     
---   Reserved. Always read/write
0.
             b6     
BFE - TPUm.TGRE operates normally
             b5     
BFB - TPUm.TGRB operates normally
             b4     
BFA - TPUm.TGRA operates normally
             b3:b0  
MD  - 0 = Normal operation
             2 = Set PWM mode 1
             */
            MTU4.TMDR.BYTE = 0x02;
            /* Timer I/O Control Register (TIORH)
             b7:b4  
IOB - 5 = Ouptut: initial 1, 0 at TGRB compare match
             b3:b0  
IOA - MTIOC4A pin function:
             6 = output: initial 1, then 1 after TGRA match
             */
            MTU4.TIORH.BYTE = 0x56; /* High on TGRA, Low on TGRB. */
            /* Timer I/O Control Register (TIORL)
             b7:b4  
IOD - 0 = output: none
             b3:b0  
IOC - 0 = output: none
             */
            MTU4.TIORL.BYTE = 0x00;
            /* Timer General Registers (TGRx)
             * 
b15:b0 TGRx either output compare or input capture register.
             *        
x may be A, B, C, or D, depending on which registers this MTU has.
             * TGRA is being used as the PWM base frequency
counter. Count is cleared on match to this value.
             * TGRB is being used as the PWM pulse width
counter. The output is switched when this count is reached.
             */
            MTU4.TGRA = 1200-1;               // 10KHz    --> 12 Mhz / 1200 = 10 khz
            MTU4.TGRB = 600;                  // Duty 50%
            /* Timer Interrupt Enable Register
(TIER)
             b7     
TTEG - A/D conversion start request generation
             b6     
reserved
             b5     
TCIEU - Underflow interrupts
             b4     
TCIEV - Overflow interrupts
             b3     
TGIED - TGRD interrupt
             b2      TGIEC - TGRC interrupt
             b1     
TGIEB - TGRB interrupt
             b0     
TGIEA - TGRA interrupt
             */
            MTU4.TIER.BYTE = 0x0; /* 0 = no interrupts */
            MTU4.TCNT  =
0;                               // Clear Count
            MTU.TSTR.BIT.CST4 = 1;           // Count Start    // Enable MTU4.
}
6.- El programa main queda de la siguiente manera:
void main(void)
{
            set_ipl(0);              // enable interrupts
            SR_Oscilador();          //  F = 96 Mhz
            SR_TIMER_0();            // Inicializa
el Timer 0 en cascada para 16 bits
            SR_ADC();                // ADC 10 bits ch 2
            SR_INIT_PWM();           // inicia pwm MTU6 pin A2
            while(1)
            {
                        AdcValue =
adc_get_vdd();          // adquiere el valor 10 bits del ch 2  4096 posiciones
                        // MTU4.TGRA  = 1200;  MTU4.TGRB 
= 600; al 50%
                        // 4096     --- 1200
                        // AdcValue ---  X
                period = 0xFFFF & (MTU4.TGRA);
                        u16DutyCycle
= (period * AdcValue) / 4096;
                        MTU4.TGRB = u16DutyCycle;                            // duty cycle 0 -- 100%
                        delay_ms(100);
            }
}
- Agregar código, compilar y debug:
 
--> Practica #13
2.- Compilar con el icono del martillo y debug con el icono del insecto:
VÍDEO:






Hi, Nice blog
ResponderBorrari am not able to open this downloaded file