need help understanding this the delay function.

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

need help understanding this the delay function.

754 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by black_ghost on Fri Oct 07 23:25:38 MST 2011
Hi i am trying to understand this delay function as i want to use to this function for a delay to drive my st7066 16x2 lcd. this would be of great help to me below is code i got from the example file from lpcxpresso site.
void Delay (uint32_t dlyTicks); volatile uint32_t msTicks; // counts 1ms timeTicks //******************************************* // system stick interrupt handler void SysTick_Handler(void) { msTicks++; // increment counter necessary in Delay() } //******************************************* // delays number of tick Systicks (happens every 1 ms) void Delay (uint32_t dlyTicks) { uint32_t curTicks; curTicks = msTicks; while ((msTicks - curTicks) < dlyTicks); }
0 项奖励
回复
6 回复数

683 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by black_ghost on Wed Oct 12 16:02:27 MST 2011

Quote: gbm
There are so many errors in your code that it's hard to start correcting it.
Why don't you start by rewriting the code known to be correct on some other platform?

My advices:
- start by writing a routine for writing 4 bits to LCD; be sure to put some delay required for proper strobe widths
- decide if you want to check the busy bit or wait a known delay before sending each command/character to the LCD
- implement the selected sync method - write the "wait until ready" routine
- use 4-bit writes to initialize the LCD to 4-bit mode
- write a routine for writing 8-bit data/comands, using the previously written "wait untli ready" and 4-bit write routine


Ok thanks for the tip but could pls assist or give me ideas of how to implement the delay function. i know the for loop function i wrote is wrong. And i am a newbie by the way pls assistance would really be appreciated. Thanks
0 项奖励
回复

683 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by gbm on Wed Oct 12 12:43:10 MST 2011
There are so many errors in your code that it's hard to start correcting it.
Why don't you start by rewriting the code known to be correct on some other platform?

My advices:
- start by writing a routine for writing 4 bits to LCD; be sure to put some delay required for proper strobe widths
- decide if you want to check the busy bit or wait a known delay before sending each command/character to the LCD
- implement the selected sync method - write the "wait until ready" routine
- use 4-bit writes to initialize the LCD to 4-bit mode
- write a routine for writing 8-bit data/comands, using the previously written "wait untli ready" and 4-bit write routine
0 项奖励
回复

683 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by black_ghost on Wed Oct 12 11:42:48 MST 2011
below is my code sending commands to the lcd im using an lpc1769 and  have no success. i have been working on this for couple of days and its  getting frustrating pls i need help on this anyone i would appreciate  it. the init sends the initailisation commands. i am trying to dsiplay  the message hello just having no success.

#define EN       LPC_GPIO4->FIOSET |= (1<<28)
#define DIS_EN   LPC_GPIO4->FIOCLR |= (1<<28)
#define RS       LPC_GPIO3->FIOSET |= (1<<25)
#define DIS_RS   LPC_GPIO3->FIOCLR |= (1<<25)
#define RW       LPC_GPIO3->FIOSET |= (1<<26)
#define DIS_RW   LPC_GPIO3->FIOCLR |= (1<<26)
#define lcd_delay 1000000

void lcd_init();
void lcd_initwrite(unsigned char value);
void command(uint8_t value);
void delay(unsigned int i);

void delay(unsigned int i)
{
    unsigned int j =0;
    for (j=0;j<i;j++);
}


void lcd_init()
{
    delay(1800000);
    lcd_initwrite(0x30);
    delay(600000);
    lcd_initwrite(0x30);
    delay(24000);
    lcd_initwrite(0x30);
    delay(24000);
    lcd_initwrite(0x20);
    delay(600000);
    command(0x01); //clear display
    command(0x28); // 4bit
    command(0x06); // entry mode
    command(0x0E); // display on
}
void lcd_initwrite(unsigned char value)
{
    DIS_RW; // disable rw signal
    DIS_RS; // disable rs 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_GPIO1->FIOSET = (1<<20)) : (LPC_GPIO1->FIOCLR = (1 << 20));
    bit[1] ? (LPC_GPIO1->FIOSET = (1<<19)) : (LPC_GPIO1->FIOCLR = (1 << 19));
    bit[2] ? (LPC_GPIO1->FIOSET = (1<<18)) : (LPC_GPIO1->FIOCLR = (1 << 18));
    bit[3] ? (LPC_GPIO1->FIOSET = (1<<21)) : (LPC_GPIO1->FIOCLR = (1 << 21));
    EN;
    DIS_EN;
}

void command(uint8_t value)
{
    uint8_t bit[4];
    DIS_RW; //RW is low
    DIS_RS;
bit[0] = (value & 0x10) >> 4;
    bit[1] = (value & 0x20) >> 5;
    bit[2] = (value & 0x40) >> 6;
    bit[3] = (value & 0x80) >> 7;
    bit[0] ? (LPC_GPIO1->FIOSET = (1<<20)) : (LPC_GPIO1->FIOCLR = (1 << 20));
    bit[1] ? (LPC_GPIO1->FIOSET = (1<<19)) : (LPC_GPIO1->FIOCLR = (1 << 19));
    bit[2] ? (LPC_GPIO1->FIOSET = (1<<18)) : (LPC_GPIO1->FIOCLR = (1 << 18));
    bit[3] ? (LPC_GPIO1->FIOSET = (1<<21)) : (LPC_GPIO1->FIOCLR = (1 << 21));
    EN;
    DIS_EN;


    bit[0]=(value & 0x01)>>0;
    bit[1]=(value & 0x02)>>1;
    bit[2]=(value & 0x04)>>2;
    bit[3]=(value & 0x08)>>3;
    bit[0] ? (LPC_GPIO1->FIOSET = (1<<20)) : (LPC_GPIO1->FIOCLR = (1 << 20));
    bit[1] ? (LPC_GPIO1->FIOSET = (1<<19)) : (LPC_GPIO1->FIOCLR = (1 << 19));
    bit[2] ? (LPC_GPIO1->FIOSET = (1<<18)) : (LPC_GPIO1->FIOCLR = (1 << 18));
    bit[3] ? (LPC_GPIO1->FIOSET = (1<<21)) : (LPC_GPIO1->FIOCLR = (1 << 21));
    EN;
    DIS_EN;
}

void print(uint8_t cmd)
{
        delay(4800);
        uint8_t bit[4];
        RS;
        DIS_RW;
bit[0] = (cmd & 0x10) >> 4;
        bit[1] = (cmd & 0x20) >> 5;
        bit[2] = (cmd & 0x40) >> 6;
        bit[3] = (cmd & 0x80) >> 7;
        bit[0] ? (LPC_GPIO1->FIOSET = (1<<20)) : (LPC_GPIO1->FIOCLR = (1 << 20));
        bit[1] ? (LPC_GPIO1->FIOSET = (1<<19)) : (LPC_GPIO1->FIOCLR = (1 << 19));
        bit[2] ? (LPC_GPIO1->FIOSET = (1<<18)) : (LPC_GPIO1->FIOCLR = (1 << 18));
        bit[3] ? (LPC_GPIO1->FIOSET = (1<<21)) : (LPC_GPIO1->FIOCLR = (1 << 21));
        EN;
        DIS_EN;



        bit[0]=(cmd & 0x01)>>0;
        bit[1]=(cmd & 0x02)>>1;
        bit[2]=(cmd & 0x04)>>2;
        bit[3]=(cmd & 0x08)>>3;
        bit[0] ? (LPC_GPIO1->FIOSET = (1<<20)) : (LPC_GPIO1->FIOCLR = (1 << 20));
        bit[1] ? (LPC_GPIO1->FIOSET = (1<<19)) : (LPC_GPIO1->FIOCLR = (1 << 19));
        bit[2] ? (LPC_GPIO1->FIOSET = (1<<18)) : (LPC_GPIO1->FIOCLR = (1 << 18));
        bit[3] ? (LPC_GPIO1->FIOSET = (1<<21)) : (LPC_GPIO1->FIOCLR = (1 << 21));
        EN;
        DIS_EN;
        DIS_RS;
}
int main()
{

    LPC_PINCON->PINSEL3 |= (1<<20) | (1<<19) | (1<<18) |(1<<21); //Data lines D4-D7
    LPC_GPIO1->FIODIR |= (1<<20) |(1<<19) |(1<<18) | (1<<21); //sets the data lines as output.
    LPC_GPIO1->FIOCLR |= (1<<20) |(1<<19) | (1<<18) |(1<<21);// clears the output pins
    LPC_GPIO4->FIODIR |= (1<<28);
    LPC_GPIO3->FIODIR |= (1<<25) |(1<<26);
    lcd_init();


   int count = 0;
   char output[] = "Hello.";

   while (output[count] != '\0')
   {
       print(output[count]);
       count++;
   }

   while(1);
return 0 ;
}
0 项奖励
回复

683 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Sat Oct 08 19:52:47 MST 2011
#1 If you don't provide further Information, nobody can help you :mad:

#2 As mentioned above, SysTick timer has to be configured with something like:

 SysTick_Config(...);
#3 Use the debugger to check msTicks
0 项奖励
回复

683 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by black_ghost on Sat Oct 08 19:33:41 MST 2011
i am tryint to interface a 4 bit lcd to the lpc1769 and having problems plus i am trying to use a delay function and came across systicks from importing one of the example files but still dont get it. i am a newbie to this and sometimes it really gets frustrating.
0 项奖励
回复

683 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Sat Oct 08 08:03:00 MST 2011
So what is the problem with this functions?

SysTick_Handler is your interrupt handler, which should increment msTicks every ms (if it's configured somewhere :rolleyes:),

Delay is just waiting until the difference between current and actual ms counter is reached :eek:
0 项奖励
回复