I developed an application to generate the pulses(pwm) using timer of lpc1769. The period of pulse is 10milli seconds and pulse width can be varied as required. The pulse width is generated depending on the reference signal which is a square wave of 10milli seconds(with on period of 7.2ms and off period of 2.8ms). whenever there is rising edge of this signal PWM pulse should start. Now this is working fine. To detect the rising edge i used GPIO interrupt with external interrupt3 the ISR is as :
1) if rising edge on GPIO pin(P2.11)
2) Clear the rising edge status on the pin.
3) then set the timer for few milli seconds
and in timer ISR
1) Clear any pending Timer IRQs
2)Make the GPIO pin high on which generating PWM pulse(P2.6)
3) set the timer for few milli seconds and then clear the pin(P2.6)(same timer is used in both the ISRs)
4) Disabling the timer and re enabling the rising edge interrupt on GPIO pin(P2.11), so again serves the ISR of external interrupt3 on rising edge of reference signal and it continues as above.
Now i am getting the problem in the code in developing the application as,
1) if rising edge disable the interrupt and set timer to 8 milli seconds delay.
2) In timer ISR generate 10 pwm cycles after this re enable the external interrupt. (the same timer is used in both ISRs).
3) so my output should be with respect to rising edge of reference signal 8 milli second delay then 10 pwm cycles(all have time period of 10ms) again 8ms delay with respect to rising edge of reference signal and 10 pwm cycles.
But once after 10 cycles when i re enable the external interrupt irrespective of rising edge of reference signal a delay of 8ms is added.
whenever last 10th cycle is complete I am enabling the interrupt again, so from this point only it adding a 8ms delay. My question is if interrupt is enabled for rising edge of reference signal then it should serve ISR for rising edge only. But this is not happening in this case. I am not understanding this behaviour.
/////////////////////////////////////////////////////////////////////////////////////////////////////
This is my code, this is ISR for external interrupt3
void GPIO_IRQ_HANDLER(void)
{
uit32_t i;
if(((Chip_GPIOINT_GetStatusRising(LPC_GPIOINT, GPIO_INTERRUPT1_PORT) >> GPIO_INTERRUPT1_PIN) & 0x01) != 0) /*Group 1*/
{
count1 = 0;
start_timer1((HALF_WAVE_PERIOD - DELAY) * timerTick);
NVIC_ClearPendingIRQ(GPIO_INTERRUPT_NVIC_NAME);
NVIC_DisableIRQ(GPIO_INTERRUPT_NVIC_NAME);
}
}
Timer1 ISR is
void TIMER1_IRQHandler(void)
{
if (Chip_TIMER_MatchPending(LPC_TIMER1, 1) == 0)
{
return;
}
Chip_TIMER_ClearMatch(LPC_TIMER1, 1);
if(count1 > 10)
{
LPC_GPIO1->PIN = (LPC_GPIO1->PIN & ~(1 << 19));
stop_timer1();
}
else
{
switch(state1)
{
case 1:
state1 = 2;
LPC_GPIO1->PIN = (LPC_GPIO1->PIN | (1 << 19));
set_timer1(2000 * timerTick);
break;
case 2:
state1 = 1;
LPC_GPIO1->PIN = (LPC_GPIO1->PIN & ~(1 << 19));
count1++;
set_timer1((HALF_WAVE_PERIOD - DELAY) * timerTick);
break;
default:
break;
}
}
}
where,
HALF_WAVE_PERIOD = 10000 microseconds
DELAY = 2800 microseconds
GPIO_INTERRUPT1_PORT= PORT2
GPIO_INTERRUPT1_PIN = P2.12
GPIO_INTERRUPT_NVIC_NAME = EINT3_IRQn
GPIO_IRQ_HANDLER = EINT3_IRQHandler
and functions start_timer1 is used to initialize the timer1 and set the timer1 for given number of ticks and interrupt on match.
set_timer1 : set timer1 for given ticks and interrupt.
stop_timer1 : disable the timer1 and enables the external interrupt3.
Related
I am trying to implement my own SPI communication from FPGA to STM in which my FPGA serve as MASTER and generate Chip enable and clock for communication. FPGA transmit data at its rising edge and receive data at its falling edge my FPGA code works properly.
In STM side i capture this master clock on interrupts and receive data at its rising edge and transmit at its falling edge but communication not work properly if i increase clock speed from 250khz
According to my understand STM work at 168 Mega hz i set clock setting according to 168Mhz and handling of 1mhz interrupt is not a big problem so can you any guide how i handle this high speed clock in STM
My code is written below
/*
* Project name:
EXTI_interrupt (EXTI interrupt test)
* Copyright:
(c) Mikroelektronika, 2011.
* Revision History:
20111226:
- Initial release;
* Description:
This code demonstrates how to use External Interrupt on PD10.
PD10 is external interrupt pin for click1 socket.
receive data from mosi line in each rising edge.
* Test configuration:
MCU: STM32F407VG
http://www.st.com/st-web-
ui/static/active/en/resource/technical/document/datasheet/DM00037051.pdf
dev.board: EasyMX PRO for STM32
http://www.mikroe.com/easymx-pro/stm32/
Oscillator: HSI-PLL, 140.000MHz
Ext. Modules: -
SW: mikroC PRO for ARM
http://www.mikroe.com/mikroc/arm/
* NOTES:
receive 32 bit data from mosi line in each rising edge
*/
//D10 clk
//D2 ss
//C0 MOSI
//C1 FLAG
int read=0;
int flag_int=0;
int val=0;
int rec_data[32];
int index_rec=0;
int display_index=0;
int flag_dint=0;
void ExtInt() iv IVT_INT_EXTI15_10 ics ICS_AUTO {
EXTI_PR.B10 = 1; // clear flag
flag_int=1; //Flag on interrupt
}
TFT_Init_ILI9340();
void main() {
GPIO_Digital_Input(&GPIOD_BASE, _GPIO_PINMASK_10);
GPIO_Digital_Output(&GPIOD_BASE, _GPIO_PINMASK_13); // Set PORTD as
digital output
GPIO_Digital_Output(&GPIOD_BASE, _GPIO_PINMASK_12); // Set PORTD as
digital output
GPIO_Digital_Output(&GPIOD_BASE, _GPIO_PINMASK_14); // Set PORTD as
digital output
GPIO_Digital_Output(&GPIOD_BASE, _GPIO_PINMASK_15); // Set PORTD as
digital output
GPIO_Digital_Input(&GPIOA_IDR, _GPIO_PINMASK_0); // Set PA0 as
digital input
GPIO_Digital_Input(&GPIOC_IDR, _GPIO_PINMASK_0); // Set PA0 as
digital input
GPIO_Digital_Input(&GPIOC_IDR, _GPIO_PINMASK_2); // Set PA0 as
digital input
GPIO_Digital_Output(&GPIOC_IDR, _GPIO_PINMASK_1); // Set PA0 as
digital input
//interupt register
SYSCFGEN_bit = 1; // Enable clock for alternate pin
functions
SYSCFG_EXTICR3 = 0x00000300; // Map external interrupt on PD10
EXTI_RTSR = 0x00000000; // Set interrupt on Rising edge
(none)
EXTI_FTSR = 0x00000400; // Set Interrupt on Falling edge
(PD10)
EXTI_IMR |= 0x00000400; // Set mask
//NVIC_IntEnable(IVT_INT_EXTI15_10); // Enable External interrupt
while(1)
{
//interrupt is not enable until i push the button
if((GPIOD_ODR.B2==0)&&(flag_dint==0))
{ if (Button(&GPIOA_IDR, 0, 1, 1))
{
Delay_ms(100);
GPIOC_ODR.B1=1; //Status for FPGA
NVIC_IntEnable(IVT_INT_EXTI15_10); // Enable External interrupt
}
}
if(flag_int==1)
{
//functionality on rising edge
flag_int=0;
if(index_rec<31)
{
//display data on led
GPIOD_ODR.B13= GPIOC_IDR.B0;
//save data in an array
rec_data[index_rec]= GPIOC_IDR.B0;
//read data
index_rec=index_rec+1;
}
else
{
flag_dint=1;
NVIC_IntDisable(IVT_INT_EXTI15_10);
}
} // Infinite loop
}
}
Without getting into your code specific, see PeterJ_01's comment, the clock rate problem can be explained by a misunderstanding of throughput in your assumtions.
You assume that given that your STM device has a clock of 168Mhz it can sustain the same throughput of interrupts, which you seem to have conservatively relaxed to 1Mhz.
However the throughput of interrupts it will be able to support is given by the inverse of the time it takes the device to process each interrupt. This time includes both the time the processor takes to enter the service routing (ie detect the interrupt, interrupt the current code and resolve from the vector table where to jump to) plus the time taken to execute the service routine.
Lets be super optimistic and say that entering the routine takes 1 cycle and the routing itself takes 3 (2 for the flags you set and 1 for the jump out of the routine). This gives 4 cycles at 168Mhz is 23.81ns, taking the inverse 42Mhz. This can also be computed by dividing the maximum frequency you would achieve (168Mhz) by the number of cycles spent processing.
Hence our really optimistic bound is 42Mhz, but realistically will be lower. For a more accurate estimate you should test your implementation timings and dig into your device's documentation to see interrupt response times.
The problem is that the timing behavior is totally wrong (factor 30).
I have an ATmega 644PA which is running with 8 Mhz (CKDIV8 fuse is not set).
I wanted an interrupt every 1 ms. I use Timer1 (16 bit timer) in the compare mode.
The prescaler was choiced by the timing range which i need.
Here 1 ms to ~2s. (limited through the 16 bit timer reg.)
Calculation
CPU: 8.000.000 Hz
Prescaler: 256
8.000.000 Hz / 256 = 31250 Cycles / s (Hz)
1000 ms = 31250 Cycles
1 ms = 30.25 Cycles (31.25 - 1, timer reg. start by 0)
From here i can calculate it up, for example:
100 ms = 3025 Cycles
2s = 60500 Cycles
Code
Timer Init
cli(); // disable global interrupts
TCCR1A = (1 << WGM01); // CTC ON
TCCR1B = 0x04; // Prescaler 256
OCR1A = 30; // set compare reg.
TIMSK1 = (1 << OCIE1A); // set interrupt mask
TCNT1 = 0x00; // set counter reg. to zero
sei(); // enable global interrupts
ISR:
ISR(TIMER1_COMPA_vect)
{
// start own code
...
// end own code
TCNT1 = 0x00; // reset counter reg. to zero after match (same which should CTC do)
}
Now i have for example a volatile variable which will be incremented in the ISR function.
I'm polling the variable and wait so long till it reach 60500, which is equal to 2s.
After it i just set an led on.
I measured it by phone and it takes 1 minute till the led goes on.
It takes to long with the factor 30 => 60s divided by 2s are 30.
I also tried to use an external program to calculate it, it came to the same results. (0x1E = 30)
Someone an idea where my mistake is ?
2 seconds is 60500 cycles. But your ISR is incrementing the volatile variable every 1 millisecond. Naturally, it will take 60.5 seconds for the variable to reach 60500.
As a side note, it is better to change the compare value, rather than reset TCNT, to avoid timing drift. That is, let the counter run freely, and use OCR1A += 31; to hit the next interrupt. Resetting TCNT as you do will ignore the time inside the ISR.
Using CCS, embededded programming, MSP430F63736A
In my app I´m usingTimer A to switching LED(ON/OFF) for various time.
For example:
Led is ON - 0.5 sec
Led is OFF- 3 sec
I need to reset timer when it reach the value in register TA1CCR0. These times(0.5 s and 3 s) are in the register TA1CCR0. The problem is, that if I change the value of register TA1CCR0 from 0.5 sec to 3 sec, it will count from 0.5 to 3 sec. And I need a full 3 sec so I need a reset timer.
Thats the interrupt routine where I´m switching LED
if (P4OUT == 0x00)
{
P4OUT ^= BIT6; // LED ON
TA1CCR0 = (sekunda*t1); //t1- 3 s.... pc 2s
}
else
{
P4OUT = 0x00;
TA1CCR0 = (sekunda*t2);
}
}
Which counter mode are you using? You need to set the mode by writing to the TA1CTL register. The modes are:
#define MC_0 (0x0000) /* Timer A mode control: 0 - Stop */
#define MC_1 (0x0010) /* Timer A mode control: 1 - Up to CCR0 */
#define MC_2 (0x0020) /* Timer A mode control: 2 - Continuous up */
#define MC_3 (0x0030) /* Timer A mode control: 3 - Up/Down */
"Continuous" mode is typically the most used one. It counts until the overflow of the timer register, then restarts from zero. If you have it enabled, add to CCR continuously rather than setting it to the interval value:
TA1CCR0 += (sekunda*t1);
You can also use the "up" mode. In that case the timer counter should automatically reset to zero after reaching CCR. If you're modifying CCR within the ISR and then apparently this reset does not happen, you can modify the timer counter register (TAR or TA1R) by subtracting the value of the shortest period. Do not just set the timer register to zero, as it's usually not a good coding practice: it opens doors for timing inaccuracy accumulation in case the ISR calls are occasionally delayed (e.g. because some other interrupt is execution for multiple ticks).
In your ISR add an offset to TA1CCR0 and not reset it:
Say you want a 1 sec delay after the first interrupt, and let's say to generate 1 sec delay the timer must count up to 50000. So what you need to do in you ISR is TA1CCR0 +=50000.
In your case:
if (P4OUT == 0x00)
{
P4OUT ^= BIT6; // LED ON
TA1CCR0 += (sekunda*t1); //t1- 3 s.... pc 2s
}
else
{
P4OUT = 0x00;
TA1CCR0 += (sekunda*t2);
}
}
For an a complete example please see
programming on MSP430 in CCS
Using Timer_A, ACLK and his interrupt to blink LED(just blinking now- same long time torned off- same time turned on).
This code blink led with 2 sec delay. There is problem that register TA1CCR0 can be max 0xFFFF= 65535 (2 sec for ACLK). And for my application(blinking LED is only exapmle) I will need scale from 1 sec to 999 sec. (row 6-7 in code). How can I do that? Is it possible?
#include <msp430.h>
#include <msp430f6736.h>
void CfgTA(unsigned long delayCycles)
{
int t2=2; // must be variable from 1 to 999
t2=delayCycles*t2;
TA1CCTL0 |= CCIE; //Enable Interrupts on Timer
TA1CCR0 = t2-1; //Number of cycles in the timer
TA1CTL |= TASSEL_1 | MC_1; //ACLK , UP mode
}
void ledblink()
{
//LED config
P4DIR |= BIT6;
P4OUT &= ~BIT6;
CfgTA(32768); //Timer configuration to blink every 1 sec
while (1)
{
_bis_SR_register(LPM3_bits + GIE); //Enter Low Power Mode 3 with interrupts
}
}
#pragma vector=TIMER1_A0_VECTOR
__interrupt void Timer_A0(void)
{
P4OUT ^= BIT6; // Swapping on/off LED
}
int main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
ledblink();
return 0;
}
how to count seconds on a 1 second interrupt.
1) initialize interrupt to occur once each second and reload its' timer/counter register
2) set global variable to number of seconds to delay:
int delaySeconds = 10;
3) inside the interrupt function
static int count =0;
count++;
if( count >= delaySeconds )
{
count = 0;
P4OUT ^= BIT6; // Swapping on/off LED
}
I think the interrupt function, before exiting, also needs to clear the time1 interrupt pending flag
On your MSP430, you can slow down ACLK with the DIVA field in the UCSCTL5 register, and you can further divide down the timer's clock input with the ID and IDEX fields in the TAxCTL and TAxEX0 registers.
With the timer input divided down to 16 Hz, you would be able to count for up to 4096 seconds.
I´m quite new in this kind of programming.
I need to create a real time count on MSP430F6736A (with millisecond accuracy). I'm creating an app that neeeds to do something in short intervals
(for example: every 2 seconds turn on LED for 50 milliseconds).
Using Code composer 6.1.1
I was thinking about use of Timer and interrupts but I don´t know if it is possible to count millisec like this. I just read that can be applied on seconds, hours .... If it is possible which clock should I choose?
The other way I thought about was some delay or sleep. Can i sleep uC for a 50 millisec (real time) preciselly?
EDIT
Here´s code. / [Code ][1]/ Using Timer_A, ACLK and his interrupt to blink LED(just blinking now- same long time torned off- same time turned on).
This code blink led with 2 sec delay.
There is problem that register TA1CCR0 can be max 0xFFFF= 65535 (2 sec for ACLK)
And for my application i will need scale from 1 sec to 999 sec. (row 6-7 in code). How can I do that? Is it possible?
#include <msp430.h>
#include <msp430f6736.h>
void CfgTA(unsigned long delayCycles)
{
int t2=2; // must be variable from 1 to 999
t2=delayCycles*t2;
TA1CCTL0 |= CCIE; //Enable Interrupts on Timer
TA1CCR0 = t2-1; //Number of cycles in the timer
TA1CTL |= TASSEL_1 | MC_1; //ACLK , UP mode
}
void ledblink()
{
//LED config
P4DIR |= BIT6;
P4OUT &= ~BIT6;
CfgTA(32768); //Timer configuration to blink every 1 sec
while (1)
{
_bis_SR_register(LPM3_bits + GIE); //Enter Low Power Mode 3 with interrupts
}
}
#pragma vector=TIMER1_A0_VECTOR
__interrupt void Timer_A0(void)
{
P4OUT ^= BIT6; // Swapping on/off LED
}
int main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
ledblink();
return 0;
}