using SysTick as a delay function

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

using SysTick as a delay function

3,232 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by black_ghost on Thu Oct 13 00:29:53 MST 2011
Hello everyone i am using the lpc1769 to drive a 4 bit lcd and i need to use a delay function. I seen an example using SysTick_Handler used the example but i am getting errors below is my code any suggestion would be grateful.


void lcd_init();
void lcd_initwrite(unsigned char value);
void command(uint8_t value);
void delay(unsigned int i);
void lcd_line1();
void lcd_string(char *a);
static void ms_delay(uint32_t msTicks);
volatile uint32_t msTicks;

void lcd_init()
{

    ms_delay(20); // sets delay off 20ms
    lcd_initwrite(0x30);  // initialize commands for LCD
    ms_delay(5);
    lcd_initwrite(0x30);
    ms_delay(5);
    lcd_initwrite(0x30);
    ms_delay(5);
    lcd_initwrite(0x20); //initialization command for a 4 bit
    ms_delay(5);
    command(0x28);   //set to 4 bit and 2 lines
    command(0x01);   //clears the display
    command(0x06);  //Entry mode for the LCD
    command(0x0E);  //Turn on the display

}
//sends the initialization commands
void lcd_initwrite(unsigned char value)
{
    LPC_GPIO0->FIOCLR |= (1<<9); // clears the RS signal
    LPC_GPIO0->FIOCLR |= (1<<8); // clears the RW signal

    uint8_t bit[4];
    bit[0]=(value & 0x10)>>4;
    bit[1]=(value & 0x20)>>5;
    bit[2]=(value & 0x40)>>6;
    bit[3]=(value & 0x80)>>7;
    bit[0] ? (LPC_GPIO0->FIOSET = (1<<6)) : (LPC_GPIO0->FIOCLR = (1<<6));
    bit[1] ? (LPC_GPIO0->FIOSET = (1<<0)) : (LPC_GPIO0->FIOCLR = (1<<0));
    bit[2] ? (LPC_GPIO0->FIOSET = (1<<1)) : (LPC_GPIO0->FIOCLR = (1<<1));
    bit[3] ? (LPC_GPIO0->FIOSET = (1<<18)) :(LPC_GPIO0->FIOCLR = (1<<18));
    LPC_GPIO0->FIOSET |= (1<<7); //sets the enable pin
    ms_delay(1);                 //lms second delay
    LPC_GPIO0->FIOCLR |= (1<<7); //clears the enable pin
}

void command(uint8_t value)
{
    uint8_t bit[4];
      //RS pin is low
    LPC_GPIO0->FIOCLR |= (1<<9); //RW is low
    LPC_GPIO0->FIOCLR |= (1<<8); //RS is set low
    //upper nibble and sending the data to the ports
    bit[0] = (value & 0x10) >> 4;
    bit[1] = (value & 0x20) >> 5;
    bit[2] = (value & 0x40) >> 6;
    bit[3] = (value & 0x80) >> 7;
    bit[0] ? (LPC_GPIO0->FIOSET = (1<<6))  : (LPC_GPIO0->FIOCLR = (1<<6));
    bit[1] ? (LPC_GPIO0->FIOSET = (1<<0))  : (LPC_GPIO0->FIOCLR = (1<<0));
    bit[2] ? (LPC_GPIO0->FIOSET = (1<<1))  : (LPC_GPIO0->FIOCLR = (1<<1));
    bit[3] ? (LPC_GPIO0->FIOSET = (1<<18)) : (LPC_GPIO0->FIOCLR = (1<<18));
    LPC_GPIO0->FIOSET |= (1<<7);
    ms_delay(1);                 
    LPC_GPIO0->FIOCLR |= (1<<7);

  //lower nibble and
    bit[0]=(value & 0x01)>>0;
    bit[1]=(value & 0x02)>>1;
    bit[2]=(value & 0x04)>>2;
    bit[3]=(value & 0x08)>>3;
    bit[0] ? (LPC_GPIO0->FIOSET = (1<<6)) : (LPC_GPIO0->FIOCLR = (1<<6));
    bit[1] ? (LPC_GPIO0->FIOSET = (1<<0)) : (LPC_GPIO0->FIOCLR = (1<<0));
    bit[2] ? (LPC_GPIO0->FIOSET = (1<<1)) : (LPC_GPIO0->FIOCLR = (1<<1));
    bit[3] ? (LPC_GPIO0->FIOSET = (1<<18)): (LPC_GPIO0->FIOCLR = (1<<18));
    LPC_GPIO0->FIOSET |= (1<<7);
    ms_delay(1);
    LPC_GPIO0->FIOCLR |= (1<<7);

}

//instruction command for setting up line 1.
void lcd_line1()
{
    command(0x80);
}
//displays characters on LCD
void print(unsigned char cmd)
{
        ms_delay(4800);
        uint8_t bit[4]; //array of 4bits
        LPC_GPIO0->FIOCLR |= (1<<8); //RW is 0
        LPC_GPIO0->FIOSET |= (1<<9); //sets RS = 1;


        bit[0] = (cmd & 0x10) >> 4; //
        bit[1] = (cmd & 0x20) >> 5;
        bit[2] = (cmd & 0x40) >> 6;
        bit[3] = (cmd & 0x80) >> 7;
        bit[0] ? (LPC_GPIO0->FIOSET = (1<<6)) : (LPC_GPIO0->FIOCLR =  (1<<6));
        bit[1] ? (LPC_GPIO0->FIOSET = (1<<0)) : (LPC_GPIO0->FIOCLR =  (1<<0));
        bit[2] ? (LPC_GPIO0->FIOSET = (1<<1)) : (LPC_GPIO0->FIOCLR =  (1<<1));
        bit[3] ? (LPC_GPIO0->FIOSET = (1<<18)): (LPC_GPIO0->FIOCLR =  (1<<18));
        LPC_GPIO0->FIOSET |= (1<<7); //sets Enable pin
        ms_delay(1);  
        LPC_GPIO0->FIOCLR |= (1<<7); //clear enable pin



        bit[0]=(cmd & 0x01)>>0;
        bit[1]=(cmd & 0x02)>>1;
        bit[2]=(cmd & 0x04)>>2;
        bit[3]=(cmd & 0x08)>>3;
        bit[0] ? (LPC_GPIO0->FIOSET = (1<<6))  : (LPC_GPIO0->FIOCLR = (1<<6));
        bit[1] ? (LPC_GPIO0->FIOSET = (1<<0))  : (LPC_GPIO0->FIOCLR = (1<<0));
        bit[2] ? (LPC_GPIO0->FIOSET = (1<<1))  : (LPC_GPIO0->FIOCLR = (1<<1));
        bit[3] ? (LPC_GPIO0->FIOSET = (1<<18)) : (LPC_GPIO0->FIOCLR = (1<<18));
        LPC_GPIO0->FIOSET |= (1<<7); //sets enable to high
        ms_delay(1);
        LPC_GPIO0->FIOCLR |= (1<<7); //sets enable pin to low

}
void lcd_string(char *a)
{
while (*a != '/0')
  {
    print(*a);
    *a++;
  }
}

void SysTick_Handler(void)
{
    msTicks++;
}
static void ms_delay(uint32_t delayTicks)
{
    uint32_t currentTicks;
    currentTicks = msTicks;
    while ((msTicks-currentTicks)<delayTicks);
}


int main(void)
{
    //selecting my data line pins on port 1
    LPC_PINCON->PINSEL0 = 0x00000000;
    LPC_GPIO0->FIODIR = 0x000403C3;      //selecting to outputs on port 0.
    LPC_GPIO0->FIOCLR = 0x000403C3;


    lcd_init();
    while(1)
    {
        lcd_line1();
        lcd_string("Hello.");
        ms_delay(1);

    }

    if (SysTick_Config(SystemCoreClock / 1000))
    {
    while (1);
    }
    // Enter an infinite loop, just incrementing a counter

    return 0 ;
}
i am getting errors trying to use the SysTick_Handler.
0 Kudos
Reply
4 Replies

1,873 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by black_ghost on Thu Oct 13 10:59:14 MST 2011

Quote: ToBeFrank
You might want to call SysTick_Config before you attempt to use the SysTicks.


Thanks for the reply yes i was reading the user manual and i have to set up an initilaization for SysTick. I have ideas of what to do but dont know how to start pls would you give an example or how to start. this will be highly appreciated thank you.
0 Kudos
Reply

1,873 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by black_ghost on Thu Oct 13 10:43:24 MST 2011

Quote: whitecoe
And what might the errors actually be? Its hard to see your screen from here :p

PS - if you want to post code on the forum, then please use the formatting (the # button in the forum editor) or even attach the code as a file to your post.


My error comes from
if (SysTick_Config(SystemCoreClock / 1000))
    {
    while (1);
    }
the compiler tells me undeclared SysTick_config and SystemCoreClock.
I have been reading on the SysTick Operation from the user manual and seem to understand it but dont know where to begin if u could explain, break down the Systick Time operation or give an example that would really help thanks.
0 Kudos
Reply

1,873 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ToBeFrank on Thu Oct 13 10:39:18 MST 2011
You might want to call SysTick_Config before you attempt to use the SysTicks.
0 Kudos
Reply

1,873 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by whitecoe on Thu Oct 13 06:40:03 MST 2011

Quote: black_ghost

i am getting errors trying to use the SysTick_Handler.



And what might the errors actually be? Its hard to see your screen from here

PS - if you want to post code on the forum, then please use the
 
formatting (the # button in the forum editor) or even attach the code as a file to your post.
0 Kudos
Reply