Hi,
i am typing a code to make 4 pwm output using the Timer PIT1. I load a value on the PIT and I wait until the overflow to change the state of the pins. I am checking with the oscilloscope if the timer works and yes and I can see pins changing on/off.
I am trying to use pins 0,1,2 and 5 of the portC and according to the web are the pins 15,13,22 and 23 of the Teensy 3.1 but only pin 15 works... and pin 16 but this pins belongs to portB. I am surprised of this behavior and I need help to correct my code.
I know there are functions such as digitalWriteFast and pinMode to set pins as output and switch on and off but first I want to write the code using the registers and not these functions.
#include <stdint.h>
void setup() {
// put your setup code here, to run once:
SIM_SCGC5 |= (1<<11); //Enable clock for portC
//Enable the pins 0,1,2,5 of Port C to be GPIO
PORTC_PCR0 &= ~(1<<15);
PORTC_PCR0 &= ~(1<<10);
PORTC_PCR0 &= ~(1<<9);
PORTC_PCR0 |= (1<<8);
PORTC_PCR0 |= (1<<5);
PORTC_PCR1 &= ~(1<<15);
PORTC_PCR1 &= ~(1<<10);
PORTC_PCR1 &= ~(1<<9);
PORTC_PCR1 |= (1<<8);
PORTC_PCR1 |= (1<<5);
PORTC_PCR2 &= ~(1<<15);
PORTC_PCR2 &= ~(1<<10);
PORTC_PCR2 &= ~(1<<9);
PORTC_PCR2 |= (1<<8);
PORTC_PCR2 |= (1<<5);
PORTC_PCR5 &= ~(1<<15);
PORTC_PCR5 &= ~(1<<10);
PORTC_PCR5 &= ~(1<<9);
PORTC_PCR5 |= (1<<8);
PORTC_PCR5 |= (1<<5);
//Enable clock for the timers
SIM_SCGC6 |= (1<<24);
SIM_SCGC6 |= (1<<25);
SIM_SCGC6 |= (1<<23);
//Configure portC as output
GPIOC_PDDR=0xFF;
//Clear portC
GPIOC_PCOR=0XFF;
PIT_MCR=0x00; //Enable PITs
PIT_TFLG1=0x01;//Clear overflow flag
}
void loop() {
// put your main code here, to run repeatedly:
PORTC=0x00; //Clear portC
//Load the value on timerPIT1
PIT_LDVAL1=0x10;
PIT_TCTRL1=0x01; //Start the PIT
while(1)
{
/*---------------------------------------------------------------------------
----------------------------------PWM 1--------------------------------------
---------------------------------------------------------------------------*/
PIT_LDVAL1=0x50;//New value for the PIT
while((PIT_TFLG1 & (1<<0))==0)
{
//Wait until Timer1 overflows
}
PORTC=0xFF; //Set portC
PIT_TFLG1=0x01;//Clear the flag
PIT_LDVAL1=0x150;//New value for the PIT
while((PIT_TFLG1 & (1<<0))==0)
{
//Wait until Timer1 overflows
}
PORTC=0x00; //Clear portC
}
}
i am typing a code to make 4 pwm output using the Timer PIT1. I load a value on the PIT and I wait until the overflow to change the state of the pins. I am checking with the oscilloscope if the timer works and yes and I can see pins changing on/off.
I am trying to use pins 0,1,2 and 5 of the portC and according to the web are the pins 15,13,22 and 23 of the Teensy 3.1 but only pin 15 works... and pin 16 but this pins belongs to portB. I am surprised of this behavior and I need help to correct my code.
I know there are functions such as digitalWriteFast and pinMode to set pins as output and switch on and off but first I want to write the code using the registers and not these functions.
#include <stdint.h>
void setup() {
// put your setup code here, to run once:
SIM_SCGC5 |= (1<<11); //Enable clock for portC
//Enable the pins 0,1,2,5 of Port C to be GPIO
PORTC_PCR0 &= ~(1<<15);
PORTC_PCR0 &= ~(1<<10);
PORTC_PCR0 &= ~(1<<9);
PORTC_PCR0 |= (1<<8);
PORTC_PCR0 |= (1<<5);
PORTC_PCR1 &= ~(1<<15);
PORTC_PCR1 &= ~(1<<10);
PORTC_PCR1 &= ~(1<<9);
PORTC_PCR1 |= (1<<8);
PORTC_PCR1 |= (1<<5);
PORTC_PCR2 &= ~(1<<15);
PORTC_PCR2 &= ~(1<<10);
PORTC_PCR2 &= ~(1<<9);
PORTC_PCR2 |= (1<<8);
PORTC_PCR2 |= (1<<5);
PORTC_PCR5 &= ~(1<<15);
PORTC_PCR5 &= ~(1<<10);
PORTC_PCR5 &= ~(1<<9);
PORTC_PCR5 |= (1<<8);
PORTC_PCR5 |= (1<<5);
//Enable clock for the timers
SIM_SCGC6 |= (1<<24);
SIM_SCGC6 |= (1<<25);
SIM_SCGC6 |= (1<<23);
//Configure portC as output
GPIOC_PDDR=0xFF;
//Clear portC
GPIOC_PCOR=0XFF;
PIT_MCR=0x00; //Enable PITs
PIT_TFLG1=0x01;//Clear overflow flag
}
void loop() {
// put your main code here, to run repeatedly:
PORTC=0x00; //Clear portC
//Load the value on timerPIT1
PIT_LDVAL1=0x10;
PIT_TCTRL1=0x01; //Start the PIT
while(1)
{
/*---------------------------------------------------------------------------
----------------------------------PWM 1--------------------------------------
---------------------------------------------------------------------------*/
PIT_LDVAL1=0x50;//New value for the PIT
while((PIT_TFLG1 & (1<<0))==0)
{
//Wait until Timer1 overflows
}
PORTC=0xFF; //Set portC
PIT_TFLG1=0x01;//Clear the flag
PIT_LDVAL1=0x150;//New value for the PIT
while((PIT_TFLG1 & (1<<0))==0)
{
//Wait until Timer1 overflows
}
PORTC=0x00; //Clear portC
}
}