I use Timer 1 for creating Complimentary PWM Output. I Set all registers according to the STM32F3 Reference Manual. When i debug code, The Timer Counter counts very well but the TIM1_CH1 Output doesnt output anything.(Other Channel Outputs have the same issue)
Here is my code :
void Timer1_PWMInit()
{
//----------------Setup Timer 1 for 25khz Center Aligned PWM-----------------
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; // Enable Timer 1 Clock
GPIOE->AFR[1] |= 0x20; // Set Alternate Function 2 For Timer 1 CH1 Output
TIM1->CR1 = 0; // Clear CR1 Register(Turn off Timer 1)
TIM1->CNT = 0; // Clear Timer Counter
TIM1->PSC = 0; // Set Timer 1 Prescaler to 1:1
TIM1->ARR = 320; // 40 us(25 khz) PWM Period
TIM1->CCR1 = 320; // Timer 1 Channel 1 Duty Cycle(%50)
TIM1->CCR2 = 320; // Timer 1 Channel 2 Duty Cycle(%50)
TIM1->CCR3 = 320; // Timer 1 Channel 3 Duty Cycle(%50)
TIM1->CCR5 = 0; // Timer 1 Channel 5 Duty Cycle
TIM1->CCMR1 = 0x6868; // Set Channel 1 and Channel 2 Output Compare mode to PWM Mode
TIM1->CCMR2 = 0x68; // Set Channel 3 Output Compare mode to PWM Mode
TIM1->CCMR3 = 0x10048; // Set Channel 5 Output Compare Mode to Combined PWM Mode
TIM1->CCER = 0x10555; // Enable Output Compare Channels
TIM1->CR1 = 0xE0; // Set Counting Mode to Center Aligned Mode
TIM1->EGR |= 0x1; // Set UG bit for Updating Registers
TIM1->BDTR = 0xC800; // Set MOE and OOSR bits for enabling Output
TIM1->CR1 |= 0x1; // Enable Timer 1
//-------------------------------------------------------------------------
}
Try to enable GPIOE clock using 'RCC_AHBENR_GPIOEEN' and also set alternate function for the other timer channel output pins.
Related
The While(busy); loop is instantly skipped. But the only place where busy can be set to 0 is in the Timer1 ISR. But Timer 1 is stopped and only ever starts when in the Pin Change ISR.
From the UART output I can tell that Timer 1 ISR happens, while Pin Change ISR never does. which should not be possible, right?
What am I missing?
In my main function:
...
uint32_t temp = 0;
busy = 1;
mode = 1;
// Timer Interrupt Init
TCCR1B &= ~((1<<2) | (1<<1) | (1<<0)); // Makeing sure timer is not running
TIMSK1 |= (1 << TOIE1); // Timer 1 overflow interrupt enable
TCNT1 = 0; // Makeing sure Timer is on 0
// Pin Change Interrupt Init
PCICR |= (1<<2); // Activating PCMSK2
PCMSK2 |= (1<<6); // PCMSK2 -> PCINT23.. 16 seem to correspond to physical pins D 0-7
UartSendstring("1");
// Scanning (see ISR)
sei();
TCCR1B &= ~((1<<2) | (1<<1) | (1<<0));
while(busy);
cli();
...
Timer 1 ISR:
ISR(TIMER1_OVF_vect)
{
UartSendstring("3");
busy = 0;
}
Pin Change ISR:
ISR(PCINT2_vect)
{
UartSendstring("2");
//todo make first values not empty
TCCR1B &= ~((1<<2) | (1<<1) | (1<<0));// CS12 - CS10 are set to 0 to stop the timer
data[addr] |= TCNT1L;
data[addr] |= (TCNT1H << 8); // High and low byte are saved to data
TCNT1 = 0; // Timer is reset
TCCR1B |= ((1<<1) | (1<<0)); // CS12 is set to 1 to restart the timer with prescaler 64 -> tick time = 4us
// Signal period duration is 1s / 38 000 = 26us
// -> at least on timer tick in one signal period
addr++; // Prepares to write to the next address with next edge
}
Uart output is:
13
edit
I tried moving the TIMSK1 |= (1 << TOIE1); to the Pin Change ISR. Now it goes in there at least once like I want it but as soon as I enable the Interrupt it triggers the ISR again and ends.
As the Arduino core starts all timers by default (because of PWM), there is possibility that interrupt flags are already set and they fires as soon as you enable the corresponding interrupts. So you have to clear them before reenabling interrupts. But there is a tiny little obstacle: interrupt flags are cleared by writing logic one into corresponding bit. Therefore you have to use something like this TIFR1 = _BV(ICF1) | _BV(OCF1B) | _BV(OCF1A) | _BV(TOV1); (however as you don't use any other Timer1 interrupt, you can clear TOV1 flag only).
I'm trying to make a PWM output with this code, but apparently something is missing. Because when I check the Logic Analyzer in MPLab, nothing happens on CCP2 output. I'm working with the pic18f25k80.
void main() {
// Set up PWM
CCP2CON = 0b00001100; // Enable PWM on CCP2, bits 5:4 are LSB part of duty cycle
CCPTMRS = 0b00000000; // Use timer2 for all CCP modules
CCPR2L = 31; // MSB part of duty cycle
TRISC = 0b00000000; // Set port C as output
PORTC = 0; // Clear port C
// Set Up timer2
PR2 = 249; // PWM period = (PR+1)*4*Tcy = 1ms
T2CON = 0b00000100; // Enable TMR2 with prescaler = 1
while(1)
{
}
}
I expect when
TMR2 = PR2, CCP2 output toggles and timer is reset
further, TMR2 = CCPR2L (duty cycle), CCP2 output toggles
TMR2 keeps counting until step 1 is reached.
I suppose this is what should happen automatically. I mean I don't have to write the code for that, because that's THE function of the PWM module, right?
What am I missing?
Additional info:
TMR 2 is counting.
When I add PORTC = 0xFF; in the while loop and debug the code again. All signals on Port C are set, except RC2 (RC2 = corresponding output of CCP2).
When I try the same code for CCP3, all signals on port C are set, except RC2 and RC6 (RC6 = corresponding output of CCP3).
When I replace PORTC = 0xFF; with PORTCbits.CCP2 = 1;, only RC1 is set high.
Does this last bullet mean that the CCP2 is muxed with RC1 instead of RC2?
I'm using PIC32MX575F256L microcontroller. This is a simple program to turn on and turn of LED's connected to lower 8 bits of port A using a simple switch as external interrupt:
#include<p32xxxx.h>
#include<plib.h>
void __ISR(3,ipl4) EXTInterrupthandller(void) // interrupt handler
{
PORTAINV = 0x0ff; //toggle lower 8 bits of port A (LEDs)
IFS0CLR = 0x00000800; //clear the interrupt flag
}
main()
{
DDPCONbits.JTAGEN = 0;
TRISA= 0; // set port A as output port
TRISESET = 0x0200; // set external interrupt pin as input pin
PORTA = 0; // initially LEDs are off
IEC0CLR = 0x00000800; // disable INT3
IPC0SET = 0x10000000; // set priority and sub priority of interrupt
INTCONCLR = 0x00000004; // clear the bit for falling edge trigger
IFS0CLR = 0x00000800; // clear the interrupt flag
INTEnableSystemSingleVectoredInt(); // configure system to handle single vector interrupts
IEC0SET = 0x00000800; // enable INT3
while(1); // main loop
}
I am having trouble setting up high speed PWM on my ATtiny85. I need to use the PCK, at a speed of 400 kHz. I believe that I have followed the data sheet correctly, but for some reason, the timer interrupt flags are not working.
If I program the device, the output of the corresponding pin is a constant 5 V.
If I comment out the PCK setup and use the system clock instead, the flags are correctly set and PWM works fine. The code is posted. Why aren't the flags setting and the PWM isn't working?
#include <avr/io.h>
#include <avr/interrupt.h>
int main(void)
{
PORTB = 0; //Reset values on port B
// After setting up the timer counter,
// set the direction of the ports to output
DDRB |= (1<<PB1) | (1<<PB0); // Set the direction of PB1 to an output
// PLLCSR - PLL control and status register:
// PLL is a clock multiplier - multiplies system 8 MHz by 8 to 64 MHz
// PLL is enabled when:PLLE bit is enabled,
// CKSEL fuse is programmed to 0001. This clock is
// switched off in sleep modes!
PLLCSR |= (1<<PLLE); // PLL enable
// Wait until the PLOCK bit is enabled
// before allowing the PCK to be enabled
//WaitForPLOCK();
//unsigned int i = 0;
while ((PLLCSR & (1<<PLOCK)) == 0x00)
{
// Do nothing until plock bit is set
}
PLLCSR |= (1<<PCKE); // Enable asynchronous mode, sets PWM clock source
TCCR1 =
(1<<CTC1) | // Enable PWM
(1<<PWM1A) | // Set source to pck
(1<<(CS10)) | // Clear the pin when match with ocr1x
(1<<COM1A1);
GTCCR = (1<<PWM1B) | (1<<COM1B1);
// Set PWM TOP value - counter will count and reset
// after reaching this value
// OCR1C
// 400 kHz 159
// 450 kHz 141
// 500 kHz 127
OCR1C = 159;
// Enable Timer1 OVF interrupt
TIMSK = (1<<OCIE1A) | (1<<TOIE1);
sei();
// This should set the duty cycle to about 75%
OCR1A = 120;
The solution involved the CKDIV8 fuse. To program this fuse correctly however, HVSP "High Voltage Serial Programming" was required. After removing this fuse so that the device operated at 8 MHz, the PWM gave a 400 kHz output. I hope other people find this useful!
The errata (section 27 of the datasheet) states 'PLL will not lock under 6 MHz'. The only workaround listed is to set the clock to 6 MHz or higher.
I have an atmega168a chip. I use Counter 0 to toggle PORTC by using ISR(TIMER0_COMPA_vect) and ISR(TIMERB_COMPA_vect) interrupt sub-routines. I would like to activate the 16-bit timer when if condition is true. So, I use TIMSK1 = (1<<OCIE1A), but this line calls ISR(TIMER1_COMPA_vect) interrupt instantly where I want 16 bit timer to be interrupted only when the counter reaches to OCR1A value. How can I activate the 16-bit timer on the run-time without causing an instant interrupt?
here is my code:
#define F_CPU 8000000
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdint.h>
volatile uint8_t counter;
int main (void){
DDRC = 0xFF; //sets PORTC as output
PORTC = 0xFF; //initial output value
/*COUNTER0 settings*/
TIMSK0 = ((1<<OCIE0A) | (1<<OCIE0B)); // Enable Interrupt TimerCounter0 Compare Match A & B
TCCR0A = (1<<WGM01); // Mode = CTC
TCCR0B = (1<<CS01) | (1<<CS00); // Clock/64, 1/(8000000/64)= 0.000008 seconds per tick
OCR0A = 200; // 0.000008 *230 = 1.6 ms
OCR0B = 100; // 0.8 ms
/*16bit timer - counter1 settings*/
TIMSK1 &= ~(1<<OCIE1A); // Disable Interrupt Counter 1, output compare A (TIMER1_CMPA_vect)
TCCR1B = ((1<<CS12) | (1<<CS10) | (1<<WGM12)); // Clock/1024, 1/(8000000/1024) = 0.000128 seconds per tick, Mode=CTC
OCR1A = 40; // 0.000128*40 ~= 5.12 milliseconds
sei(); //interrupts are globally switched on
counter =0;
while(1){
if(counter >= 4){
TCNT1 = 0; // clear the counter 1
TIMSK1 = (1<<OCIE1A);// Enables the interrupt for Counter 1,(TIMER1_CMPA_vect)
TIMSK0 &= ~((1<<OCIE0A) | (1<<OCIE0B)); //disables the Counter 0's interrupts
counter = 0;
}
}
return 0;
}
ISR(TIMER0_COMPA_vect){ //1.6ms
PORTC = 0xFF;
counter++;
}
ISR(TIMER0_COMPB_vect){ //0.8 ms
PORTC = ~PORTC;
}
ISR(TIMER1_COMPA_vect){ // 5.2 milisecond interrupt
PORTC = 0x00;
TCNT0 = 0; //clear the counter of counter0
// TIMSK0 = ((1<<OCIE0A) | (1<<OCIE0B)); //Enable the Counter 0 interrupts
// TIMSK1 &= ~(1<<OCIE1A);// Disable Interrupt Counter 1, output compare A (TIMER1_CMPA_vect)
}
here is an oscilloscope output that shows why I don't want the interrupt to be set instantly, because it set the signal 0 instantly.
I think the problem might be that in CTC mode, interrupt is generated when OCF1A flag is set (in TIFR). Since your timer is always running, just not generating interrupts, it sets OCF1A flag, which never gets cleared. On page 142 in the datasheet it says:
OCF1B is automatically cleared when the Output Compare Match B
Interrupt Vector is executed. Alternatively, OCF1B can be cleared by
writing a logic one to its bit location.
This means that when you set up timer 1, you also need to clear OCF1A:
TIFR1 &= ~(1<<OCF1A)
However, I think you can do better. You could just stop the timer when not needed, and start it when you do, instead of twiddling the TIMSK and having timer 1 run always. If you set TCCR1B to zero, that clears CS12, CS11, and CS10, which, according to the datasheet means "Timer stopped." Then, when your counter reaches 4 you can turn on timer1 as you have it above:
TCCR1B = ((1<<CS12) | (1<<CS10) | (1<<WGM12));
If you do this, you shouldn't need to turn timer 1 interrupts on and off: just leave them on, and only turn the counting on when you need it.
Also I am wondering if it is actually necessary to fire off two interrupts to toggle pins on PORTC? Are you not using PWM for that because it doesn't give you the pulse lengths precisely enough?