GPIO pins on Teensy 3.1

Status
Not open for further replies.

Borja

Active member
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

}

}
 
In German, I'd say that this is from behind through the chest into the eye.

Using the FTM0 flex timer of the T3.x, you might have 6 to 8 PWM channels just by writing a few registers without any processor load.
 
Hi,

thanks for your answer.

My problem is not with the timer because the timer1 counts with no problem. My problem is changing the state of these 4 pins of portC (0,1,2,5).

I use PORTC="state of the pins" but it doesn't really works in my code and I don't know why....
 
Your pins are probably mis-configured. "PORTC_PCR0 |= (1<<5);", really?

Kinetis.h has #defines for everything, use them:
Code:
#define PORT_PCR_ISF			((uint32_t)0x01000000)		// Interrupt Status Flag
#define PORT_PCR_IRQC(n)		((uint32_t)(((n) & 15) << 16))	// Interrupt Configuration
#define PORT_PCR_IRQC_MASK		((uint32_t)0x000F0000)
#define PORT_PCR_LK			((uint32_t)0x00008000)		// Lock Register
#define PORT_PCR_MUX(n)			((uint32_t)(((n) & 7) << 8))	// Pin Mux Control
#define PORT_PCR_MUX_MASK		((uint32_t)0x00000700)
#define PORT_PCR_DSE			((uint32_t)0x00000040)		// Drive Strength Enable
#define PORT_PCR_ODE			((uint32_t)0x00000020)		// Open Drain Enable
#define PORT_PCR_PFE			((uint32_t)0x00000010)		// Passive Filter Enable
#define PORT_PCR_SRE			((uint32_t)0x00000004)		// Slew Rate Enable
#define PORT_PCR_PE			((uint32_t)0x00000002)		// Pull Enable
#define PORT_PCR_PS			((uint32_t)0x00000001)		// Pull Select

PORTC is the AVR port emulation. Look at the manual, "49.2 Memory map and register definition" for the real port registers.
 
Status
Not open for further replies.
Back
Top