Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

I want to start ARM with lpc1768 cortex M3

Status
Not open for further replies.
As I wrote in a previous reply the -1 not needed, I don't know where you saw that.

SystemCoreClock 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

In your keil installation folder browse to ARM\CMSIS (e.g. C:\Keil\ARM\CMSIS) , open the index.htm and select CORE -> Reference -> SysTick , you can read about the function there
 
As I wrote in a previous reply the -1 not needed, I don't know where you saw that.
there
I saw that here :
http://ics.nxp.com/support/documents/microcontrollers/zip/lpc17xx.cmsis.driver.library.zip
SystemCoreClock 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
if SysTick_Config(SystemCoreClock/1000) makes 1ms, I should see the LED blinks every 300 ms, but it doesn't blink
what is the problem of my program ?
Code:
#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);
 
 }
 }
 

So what you refer to is code like

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;
    }
}



I'm not sure why they are using this.
In the CMSIS help file in the kail installation folder they use


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);
}



You have used the interrupt handler for SysTick but you haven't enabled it.
Try this:

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 */



- - - Updated - - -

Actually I'm not sure if SysTick_Config enables the interrupt too and I can't check it right now so try the above code to see if it helps.
I'm also not sure is there may be a problem with the GPIO functions, I have never used them.GPIO
 
I wrote this program from code bundle,this program used "FOR" for make delay,IS it exactly right?
Code:
#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--);
    }
}
 

you would want to declare j as a volatile to avoid optimization of the loop, when the compiler finds a loop that serves no purpose and makes no difference in the code execution it can remove it.

So the SysTick method didn't work?
 
So your code didn't work?

In a couple of hours when I get home but I don't have a LPC1768 device so I can only check it in the simulator.
 

When I tried your code from post #42 in Uvisionn 4.60 I got an error that SystemCoreClock is undefined.
The correct variable that holds the frequency in 17xx is SystemFrequency but to use it you have to use SystemClockUpdate(); first.
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)

I have attached the working project, just change the delays back to 500
 

Attachments

  • Copy of GPIO.zip
    193.4 KB · Views: 119
thanks my friend alexan for every thing,the only problem was about the following sentence that I didn't write
SystemClockUpdate();

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)
what do you suggest for calculating delays accurately ? I should use timer for that?
 

In the LPc23xx code bundle you will find the following function


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;
}



You just need to define the Fpclk which represents your clock frequency and change te register names to the ones for your mcu.
T0TCR to LPC_TIM0->TCR , T0PR to LPC_TIM0->PR etc , the same for timer1
 
thanks for your reply

I read that each "FOR" instruction execute in the 4 clk,Is it true?and can we use this for get delay accurately?

Thanks in advance
 
Last edited:

I have discussed about a loop delay some time ago in the LPC2000 yahoo group.
After a lot of discussion I was provided with the following function that takes four clocks per loop and made a couple of macro based on it

Code:
	AREA DELAY, CODE, READONLY
delay4 PROC
	EXPORT delay4
	subs R0,R0,#1
	bne delay4
	BX LR
	ENDP
	END

Save this to an s file , for example my_delay.s as it is with the spaces in front of all lines except delay4 PROC

Add the my_delay.s to the project and in the main file add
Code:
extern void delay4(int loops);
#define delay_us(x) delay4(((x)*(SystemCoreClock/4000))/1000)
#define delay_ms(x) delay4(((x)*(SystemCoreClock/400))/10)

Then you can use delays like
Code:
delay_ms(100);
delay_us(162);

Note that this is not 100% accurate delay but gives an accurate enough result (if interrupted the accuracy will worsen of course)
It is a simple delay which is very convenient for cases like LCD delays where an absolute accuracy is not needed.
If you need high accuracy then a timer is the only way.

This code works in any cortex or ARM7TDMI
 
I wrote program with timer for gets (1s) delay , but it doesn't work :-(
I can't find what is the mistake!!
Code:
#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;
     } 
  }  
}
 

Hello my friends
I want to use lcd TFT 2.4 with touch in my project
Would you please help me!!
Is there any library for them ?
 

Hello
I want to debug with jtag in uvision4 but I face with this error
how can I found AGDIRDI.Dll
Thanks in advance
keil-error.PNG
 

Hi every body
I want to make flash magic programmer but I can't find flash magic programmer schematic
would you please help me to make it
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top