i need your answer(HIGH TECH C CODE on LED_BLINK)

Discussion in 'Programmer's Corner' started by Jaden5165, Sep 9, 2011.

  1. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0
    #include <htc.h>
    //device configuration bit
    __CONFIG(0x3F3A);
    //define
    #define LED1 RB7
    #define LED2 RB6
    //function prototype
    void blink_LED1(unsigned char n);
    void blink_LED2(unsigned char n);
    //main function
    //================================================================
    void main(void)
    {
    //device peripheral configuration
    TRISB7=0; //set RB7 as output
    TRISB6=0; //set RB6 as output

    //infinity loop
    while(1)
    {
    //blink LED1 for 3 times
    blink_LED1(3);

    //blink LED2 for 3 times
    blink_LED2(3);
    }
    }
    //function to blink LED1 for n times
    void blink_LED1(unsigned char n)
    {
    //loop for n times
    for(n+=1;n>0;n-=1)
    {
    //set LED1 to 1
    LED1=1;

    //short delay
    for(unsigned int i=0;i<20000;i+=1); //max value is 65535

    //set LED1 to 1
    LED1=0;

    //short delay
    for(unsigned int i=0;i<20000;i+=1); //max value is 65535
    }
    }
    //function to blink LED2 for n times
    void blink_LED2(unsigned char n)
    {
    //loop for n times
    for(n+=1;n>0;n-=1)
    {
    //set LED2 to 1
    LED2=1;

    //short delay
    for(unsigned int i=0;i<20000;i+=1); //max value is 65535

    //set LED2 to 1
    LED2=0;

    //short delay
    for(unsigned int i=0;i<20000;i+=1); //max value is 65535
    }
    }


    how to calculate the delay time?i mean if i want a 1 second delay how should i program in the for condition.
    for(unsigned int i=0;i<20000;i+=1); //max value is 65535
    how can i know the above command max value is 65535?
     
  2. cheezewizz

    Active Member

    Apr 16, 2009
    82
    10
    look in the samples folder, there's a delay.c and delay.h where all the work is done for you. then you can use the pre-written functions like DelayMs() or DelayUs().... you may need to check the exact names it's been a while since I used HTC.
     
  3. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Hi the functions you are looking for are __DELAY_MS or __DELAY_US You will find all you need in the manual. The manual is installed with HI-Tech C The folder will be something like this C:\Program Files\HI-TECH Software\PICC\9.81\docs The manual and the PIC data sheets will be your best friends in your MCU work.
    Since you are new I provide an example how to use the delay functions. One more thing do not use settings like __CONFIG(0x3F3A); it is very cryptic. And not very good practice. In the include folder. Locate your PIC header file and use the "Configuration mask definitions" And use logical and (&) to construct your configuration word(s). Like I have done in the example. Also use the latest HI-TECH C version. It is free to download from Microchip
    Code ( (Unknown Language)):
    1.  
    2. #include <htc.h>
    3. #define _XTAL_FREQ 4000000
    4. unsigned long slow_int=1000;
    5. unsigned long fast_int=50;
    6. unsigned char i=0xff;
    7. __CONFIG(INTIO & WDTDIS &PWRTDIS & BORDIS & MCLRDIS & FCMEN  & IESODIS & UNPROTECT);
    8. void fast_blink(void)
    9. // This config setting will only work up to HI-TECH C version 9.80. The naming style changed from version 9.81
    10. void fast_blink(void)
    11. {
    12.  PORTC = 0xFF;
    13.   __delay_ms(fast_int);
    14. PORTC = 0x0;
    15.   __delay_ms(fast_int);
    16. }
    17.  
     
  4. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0
    Thank to cheezewizz and t06afre!
     
  5. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0
    To:t06afre

    unsigned long slow_int=1000;
    unsigned long fast_int=50;
    unsigned char i=0xff;

    can u briefly explain to me what does the code purpose?
     
  6. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    I did not post the full program. As you can see it is no main in my example. slow_int,long_int, and i. Are just global declared variables
    I also think it would be better tus use
    Code ( (Unknown Language)):
    1.  
    2. #define slow_int 1000
    3. #define fast_int 50
    4.  
     
  7. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0
    Hi t06afre:


    my high tech c version cannot support the command
    #include "delay.h"
    #include "delay.c"
    and i try to download the latest and newest version in lite mode still the same without the directory file.
    <SNIP>
    or do you know how to solve it?
     
    Last edited by a moderator: Sep 9, 2011
  8. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    All you need is to include the htc.h You do not need anything else like any delay.h, or delay.c. And that is true for the lite version also.
    Also do as explained in the quickstart.pdf file. You will find it in the same folder as the manual.
    I do not know what the moderator Bertus(Brutus:D) removed. But actually. It is legal to redistribute the HI-Tech C compiler as long as you do not change anything. Just redistribute it as is. However in your case it would be of no help. Also next time you post. Include your current code
     
  9. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    @Jaden5156 This is not related to a crack or not. I am using HI-Tech C in lite mode as a hobbyist. And for my hobby projects. It has done the job. No problem with the __dealy_ms function at all
     
  10. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0
    #include<htc.h>
    __CONFIG(0x3f39);
    void delay()
    {
    int x;
    for(x=0;x<20000;x++)
    {
    NOP();
    }
    }
    void main()
    {
    TRISD=0;
    PORTD=0;
    while(1)
    {
    RD7=1;
    delay();
    RD7=0;
    }
    }



    this is my code.when i cancel the delay the led can bright but once i activate the delay there is no response of my led.it couldnt blink.what is the problem?
     
  11. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0
    //Xtal=8Mhz
    //Lcd 4-bit Mode
    //16x2 LCD
    #include <htc.h>
    #include "delay.h"
    #include "delay.c"
    static bit LCD_RS @ ((unsigned)&PORTE*8+1); // LCD Reg. Select
    static bit LCD_EN @ ((unsigned)&PORTE*8+2); // LCD Enable
    #define LCD_STROBE ((LCD_EN = 1),(LCD_EN=0))
    unsigned char SCROLL_LEFT,CURSOR_OFF;
    void lcd_init(void);
    void port_init(void);
    void lcd_write(unsigned char c);
    void lcd_clear(void);
    void lcd_puts(const char * s);
    void lcd_putch(char c);
    void lcd_goto(unsigned char c);
    void lcd_scroll(char Direction);
    void lcd_home(void);
    void lcd_cursor(char onoff);
    main()
    {
    port_init();
    lcd_init();
    lcd_goto(0x04);
    lcd_puts("LCD TEST");
    lcd_clear();
    while(1)
    {
    lcd_scroll(SCROLL_LEFT);
    lcd_goto(0x40);
    lcd_puts("WELCOME TO LCD TEST");
    DelayMs(150);
    }
    }
    void port_init(void)
    {
    ADCON1= 0b10000010;
    TRISA = 0x3F;
    TRISE = 0b00000000; //PORTE as output port
    TRISD = 0b00000000; //PORTD as output port
    }
    void lcd_write(unsigned char c)
    {
    PORTD = (PORTD & 0xF0) | (c >> 4);
    LCD_STROBE;
    PORTD = (PORTD & 0xF0) | (c & 0x0F);
    LCD_STROBE;
    DelayUs(40);
    }
    void lcd_clear(void)
    {
    LCD_RS = 0;
    lcd_write(0x1); //-- Clear the Display
    DelayMs(2);
    lcd_write(0x02); //-- Home the display
    DelayMs(2);
    }
    void lcd_puts(const char * s)
    {
    LCD_RS = 1; // write characters
    while(*s)
    lcd_write(*s++);
    }
    void lcd_goto(unsigned char pos)
    {
    LCD_RS = 0;
    lcd_write(0x80+pos); //-- sets the DDRAM Address
    }
    void lcd_init(void)
    {
    LCD_RS = 0; // write control bytes
    DelayMs(15); // power on delay
    PORTD = 0x3; // attention!
    LCD_STROBE;
    DelayMs(5);
    LCD_STROBE;
    DelayUs(100);
    LCD_STROBE;
    DelayMs(5);
    PORTD = 0x2; // set 4 bit mode
    LCD_STROBE;
    DelayUs(40);
    lcd_write(0x28); // 4 bit mode, 1/16 duty, 5x8 font
    lcd_write(0x08); // display off
    lcd_write(0x0F); // display on, blink curson on
    lcd_write(0x06); // entry mode
    }
    void putch(char c)
    {
    LCD_RS=1; //- write characters
    lcd_write(c);
    }
    void puts(char *s)
    {
    LCD_RS = 1; // write characters
    while(*s)
    lcd_write(*s++);
    }
    void lcdprint(unsigned char x,unsigned char *str)
    {
    lcd_goto(x);
    lcd_puts(str);
    }
    void lcd_scroll(char Direction)
    {
    LCD_RS=0; //-- write command
    if(Direction==SCROLL_LEFT)
    {
    lcd_write(0x18); //- S/C=1 R/L=0 => Scroll LEFT
    }
    else
    {
    lcd_write(0x1b); //- S/C=1 R/L=1 => Scroll RIGHT
    }
    DelayMs(1);
    }
    void lcd_home(void)
    {
    LCD_RS=0; //-- write command
    lcd_write(0x02); //-- Home Everything
    DelayMs(2);
    }
    void lcd_cursor(char onoff)
    {
    LCD_RS=0;
    if(onoff==CURSOR_OFF) //- 0000 1DCB
    { //- D= Display C= Cursor B=Blink
    lcd_write(0x0C); //- D on/off= 1 C on/off=0 B on/off=0
    }
    else
    {
    lcd_write(0x0F); //- D=1 C=1 B=1 => Cursor ON + Blink
    }
    DelayMs(1);
    }

    this is my previous problem:there is an error as shown here:
    Build C:\Users\Jaden_Ng Chun Hau\Desktop\FYP\LCD_CODE\lcd for device 16F877A
    Using driver D:\Program Files\HI-TECH Software\PICC\9.81a\bin\picc.exe
    Make: The target "C:\Users\Jaden_Ng Chun Hau\Desktop\FYP\LCD_CODE\Untitled.p1" is out of date.
    Executing: "D:\Program Files\HI-TECH Software\PICC\9.81a\bin\picc.exe" --pass1 "C:\Users\Jaden_Ng Chun Hau\Desktop\FYP\LCD_CODE\Untitled.c" -q --chip=16F877A -P --runtime=default --opt=default -D__DEBUG=1 --rom=default --ram=default -g --asmlist "--errformat=Error [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s"
    Error [141] C:\Users\Jaden_Ng Chun Hau\Desktop\FYP\LCD_CODE\Untitled.c; 7.18 can't open include file "delay.h": No such file or directory
    ********** Build failed! **********
     
  12. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    What is the source of your code? Your code looks like something made for a very old HI-Tech C version. AND HOW MANY TIMES do I have to say this. YOU do not need any "delay.h", or "delay.c" You just replace any DelayMs(x) with the
    Code ( (Unknown Language)):
    1.  __delay_ms(x)
    and any delayus(x) with
    Code ( (Unknown Language)):
    1. __delay_us(x)
    You add
    Code ( (Unknown Language)):
    1. #define _XTAL_FREQ xxxxxxx
    with the correct value for your system.
    Last replace
    Code ( (Unknown Language)):
    1.  
    2. static bit LCD_RS @ ((unsigned)&PORTE*8+1); // LCD Reg. Select
    3. static bit LCD_EN @ ((unsigned)&PORTE*8+2); // LCD Enable
    with
    Code ( (Unknown Language)):
    1.  
    2. #define  LCD_RS  RE1
    3. #define  LCD_EN  RE2
    4.  
    Please do not make it so hard to help you
     
  13. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0
    ya.i understood what you said!thank u!and sorry I know my brain not so smart as u.lol!


    one question here?
    #include<htc.h>
    #define _XTAL_FREQ 4000000
    __CONFIG(0x3F39);
    void main()
    {
    unsigned char dcnt;
    TRISD=0;
    PORTD=0;
    for(; ;)
    {
    RD7=1;
    //stay on for 200ms
    for(dcnt=0;dcnt<10;dcnt++)
    {
    __delay_ms(50);
    }
    RD7=0;
    //stay off for 800ms
    for(dcnt=0;dcnt<101;dcnt++)
    {
    __delay_ms(50);
    }
    }
    }



    when i programmed into my PIC
    the LED no response
    I try to make the LED light up it can work but when i want to make them blink they failed.
     
    Last edited: Sep 10, 2011
  14. cheezewizz

    Active Member

    Apr 16, 2009
    82
    10
    I don't understand where you got your figures from... surley doing __delay_ms(50) 10 times as per first for loop gives a delay of 500ms not 200 and the maths on the other is way off too, 101 * 50ms gives 5050ms so 1.05 seconds. and do us all a favour like t06afre mentioned don't use magic numbers in your config() setting.

    what chip are you actually using? if it's the 16f877a then portd shared functionality with the Parallel Slave Port but to be fair that should default to off on reset. I'd try it on port b instead there shouldn't be any analog settings or PSP settings interfering with that.
     
  15. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    This error is very often due to wrong __config word. What kind of programmer do you use. Are you able to verify that your code in the chip. Do you use your own PIC circuit or do you use some premade demo board. A schematic of your test setup would help me.
    Why not also keep things simple. Like using the code like this as a "hello world" type of code
    Code ( (Unknown Language)):
    1.  
    2. #include<htc.h>
    3. #define _XTAL_FREQ 4000000
    4. __CONFIG(0x3F39);
    5. void main()
    6. {
    7. unsigned char dcnt;
    8. TRISD=0;
    9. PORTD=0;
    10. while(1)
    11. {
    12. PORTD=0xff;
    13. __delay_ms(1000);
    14. PORTD=0;
    15. __delay_ms(1000);
    16. }
    17. }
    18.  
     
  16. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0




    sorry,because when i test the previous delay time the circuit cant work so i try to modify the number of delay.ya,my chip is 16f877a!
     
  17. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0



    I'm using pic 16f877a.i use my own application board to try.the led can work without delay but it cannot work if i insert delay.i use the cytron programmer UIC00A. i'll try to test the code u wrote here.if still cant work then mayb is my circuit problem rite?
     
    Last edited: Sep 10, 2011
  18. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0
    Last edited: Sep 10, 2011
  19. stahta01

    Member

    Jun 9, 2011
    133
    21
    Is there a common ground between your breadboard and the MCU board?

    How are you powering your MCU board?

    What frequency is your external Oscillator?

    Tim S.
     
  20. stahta01

    Member

    Jun 9, 2011
    133
    21
Loading...