Print_Cte_String(" Init PWM1 10bits at 1000Hz on RC2 \r\n");
TRISC2_bit = 1; //set TRIS bit
// CCPTMRS0: PWM TIMER SELECTION CONTROL REGISTER
CCPTMRS0 = 0x00; //select timer resources, Timer2 for CCP1 and all others, page 208
PR2 = 0xff; // load with PWM period value (fixed at 1 kHz at 16Mhz)
//CCPxCON
// bit 7-6 PxM<1:0>: Enhanced PWM Output Configuration bits
//00 = Single output; PxA modulated; PxB, PxC, PxD assigned as port pins
// bit 5-4 DCxB<1:0>: PWM Duty Cycle Least Significant bits
//Half-Bridge ECCP Modules(1):
//1100 = PWM mode: PxA active-high; PxB active-high
CCP1CON = 0b00001100; // setup for PWM mode 5:4 are PWM Duty Cycle LSB
CCPR1L = 0x00; // eight high bits of duty cycle
// T2CON = 0b00000101; // Timer2 On, 1:1 Post, 4x prescale at 8MHz
// PWM at 1KHz
T2CON = 0b00000110; // Timer2 On, 1:1 Post, 16x prescale at 16MHz
// T2CKPS1_bit=1;// Timer2-type Clock Prescale Select bits =1/16
// T2CKPS0_bit=1
TRISC2_bit = 0;
IRatio=0;
CCP1CON.DC1B0 = IRatio & 1; //set low bit
CCP1CON.DC1B1 = (IRatio >> 1) & 1; //set second lowest
CCPR1L = (IRatio >> 2); //set highest eight
CRLF1();
// test cde PWM via clavier terminal
// mode pooling SANS interrupt
RCIE_bit=0;
txt=&TEXTE[0];
Print_Cte_String(" Entrer la consigne PWM via le clavier : valeur 0 à 1023 + terminaison <CR> \r\n");
while(1) // do forever
{
if (UART1_Data_Ready())
{
UART1_Read_Text(txt,"\r",5);
UART1_Write_Text(txt);
CRLF();
IRatio=atoi(txt);
PWM1_Set_10b_Duty(IRatio);
}
txt=&TEXTE[0];
}