alexan_e
Administrator
- Joined
- Mar 16, 2008
- Messages
- 11,888
- Helped
- 2,021
- Reputation
- 4,158
- Reaction score
- 2,031
- Trophy points
- 1,393
- Location
- Greece
- Activity points
- 64,371
I saw that here :As I wrote in a previous reply the -1 not needed, I don't know where you saw that.
there
if SysTick_Config(SystemCoreClock/1000) makes 1ms, I should see the LED blinks every 300 ms, but it doesn't blinkSystemCoreClock represents the frequency that the core runs , for example 100MHz (100000000)
That means 100000000 ticks /sec
For 1ms you want 1/1000 of that so you use 100M/1000 , this is the same as SystemCoreClock/1000
SysTick_Config(SystemCoreClock/1000) will set the SysTick counter to give an interrupt every 1ms
#include"lpc17xx.h"
#include "lpc17xx_gpio.h"
#include "lpc17xx_libcfg.h"
volatile unsigned long SysTickCnt;
void SysTick_Handler (void);
void Delay (unsigned long tick);
void SysTick_Handler (void) {
SysTickCnt++;
}
void Delay (unsigned long tick) {
unsigned long systickcnt;
systickcnt = SysTickCnt;
while ((SysTickCnt - systickcnt) < tick);
}
int main(void){
GPIO_SetDir(0, 0x00000001, 1); /* LED on P1.0 defined as Output */
SysTick_Config(SystemCoreClock/1000 - 1); /* Generate interrupt each 1 ms */
while(1){
GPIO_SetValue(0,0x00000001); /* LED on P1.0 on */
Delay(300);
GPIO_ClearValue(0, 0x00000001); /* LED on P1.0 off */
Delay(300);
}
}
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 / void SYSTICK_InternalInit(uint32_t time) { uint32_t cclk; float maxtime; cclk = SystemCoreClock; /* With internal CPU clock frequency for LPC17xx is 'SystemCoreClock' * And limit 24 bit for RELOAD value * So the maximum time can be set: * 1/SystemCoreClock * (2^24) * 1000 (ms) */ //check time value is available or not maxtime = (1<<24)/(SystemCoreClock / 1000) ; if(time > maxtime) //Error loop while(1); else { //Select CPU clock is System Tick clock source SysTick->CTRL |= ST_CTRL_CLKSOURCE; /* Set RELOAD value * RELOAD = (SystemCoreClock/1000) * time - 1 * with time base is millisecond */ SysTick->LOAD = (cclk/1000)*time - 1; } }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include "LPC17xx.h" uint32_t msTicks = 0; /* Variable to store millisecond ticks */ void SysTick_Handler(void) { /* SysTick interrupt Handler. msTicks++; See startup file startup_LPC17xx.s for SysTick vector */ } int main (void) { uint32_t returnCode; returnCode = SysTick_Config(SystemCoreClock / 1000); /* Configure SysTick to generate an interrupt every millisecond */ if (returnCode != 0) { /* Check return code for errors */ // Error Handling } while(1); }
Code C - [expand] 1 2 3 4 5 6 /****************************************************************************** Vectored Interrupt initialization ******************************************************************************/ NVIC_SetPriority(SysTick_IRQn,0); /* Default priority group 0, can be 0(highest) - 31(lowest) */ NVIC_EnableIRQ(SysTick_IRQn); /* Enable System Tick Interrupt */
#include "lpc17xx.h"
#include "type.h"
int main (void)
{
uint32_t j;
/* SystemClockUpdate() updates the SystemFrequency variable */
SystemClockUpdate();
LPC_GPIO2->FIODIR = 0x00000001; /* P2.xx defined as Outputs */
LPC_GPIO2->FIOCLR = 0x00000001; /* turn off all the LEDs */
while(1)
{
LPC_GPIO2->FIOSET =0x00000001;
for(j = 1000000; j > 0; j--);
LPC_GPIO2->FIOCLR = 0x00000001;
for(j = 1000000; j > 0; j--);
}
}
thanks my friendSo the SysTick method didn't work?
what do you suggest for calculating delays accurately ? I should use timer for that?For some reason the SystemFrequency is reported as 99MHz instead of 100MHz so the calculations are not accurate, if you want you can use SysTick_Config(100000000/1000)
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 void delayMs(BYTE timer_num, DWORD delayInMs) { if ( timer_num == 0 ) { /* * setup timer #0 for delay */ T0TCR = 0x02; /* reset timer */ T0PR = 0x00; /* set prescaler to zero */ T0MR0 = delayInMs * (Fpclk / 1000-1); T0IR = 0xff; /* reset all interrrupts */ T0MCR = 0x04; /* stop timer on match */ T0TCR = 0x01; /* start timer */ /* wait until delay time has elapsed */ while (T0TCR & 0x01); } else if ( timer_num == 1 ) { /* * setup timer #1 for delay */ T1TCR = 0x02; /* reset timer */ T1PR = 0x00; /* set prescaler to zero */ T1MR0 = delayInMs * (Fpclk / 1000-1); T1IR = 0xff; /* reset all interrrupts */ T1MCR = 0x04; /* stop timer on match */ T1TCR = 0x01; /* start timer */ /* wait until delay time has elapsed */ while (T1TCR & 0x01); } return; }
AREA DELAY, CODE, READONLY
delay4 PROC
EXPORT delay4
subs R0,R0,#1
bne delay4
BX LR
ENDP
END
extern void delay4(int loops);
#define delay_us(x) delay4(((x)*(SystemCoreClock/4000))/1000)
#define delay_ms(x) delay4(((x)*(SystemCoreClock/400))/10)
delay_ms(100);
delay_us(162);
#include "lpc17xx.h"
//crystal=12Mhz
//cpu clk=72Mhz
main(void)
{
unsigned long int i;
LPC_GPIO0->FIODIR=0X00000001;//P0.0 OUTPUT
LPC_SC->PCONP=1<<22;//POWER ON
LPC_TIM2->TCR=0X02;//STOP TIMER
LPC_TIM2->PR=0X04;
LPC_TIM2->TC=0X0;
LPC_TIM2->TCR=0X01;//START TIMER
while(1)
{
if(LPC_TIM2->TC>=0X0044AA20)//wait1s
{
LPC_GPIO0->FIOSET=0X00000001;
for(i=0;i<14400000;i++);//wait 1s
LPC_GPIO0->FIOCLR=0X00000001;
LPC_TIM2->TCR=0X02;
LPC_TIM2->TC=0X0;
LPC_TIM2->TCR=0X01;
}
}
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?