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

Thread Starter

Jaden5165

Joined Sep 9, 2011
69
#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?
 

cheezewizz

Joined Apr 16, 2009
82
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.
 

t06afre

Joined May 11, 2009
5,934
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
Rich (BB code):
#include <htc.h>
#define _XTAL_FREQ 4000000
unsigned long slow_int=1000;
unsigned long fast_int=50;
unsigned char i=0xff;
__CONFIG(INTIO & WDTDIS &PWRTDIS & BORDIS & MCLRDIS & FCMEN  & IESODIS & UNPROTECT);
void fast_blink(void)
// This config setting will only work up to HI-TECH C version 9.80. The naming style changed from version 9.81
void fast_blink(void)
{
 PORTC = 0xFF;
  __delay_ms(fast_int);
PORTC = 0x0;
  __delay_ms(fast_int);
}
 

Thread Starter

Jaden5165

Joined Sep 9, 2011
69
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?
 

t06afre

Joined May 11, 2009
5,934
unsigned long slow_int=1000;
unsigned long fast_int=50;
unsigned char i=0xff;
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
Rich (BB code):
#define slow_int 1000
#define fast_int 50
 

Thread Starter

Jaden5165

Joined Sep 9, 2011
69
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:

t06afre

Joined May 11, 2009
5,934
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
 

t06afre

Joined May 11, 2009
5,934
@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
 

Thread Starter

Jaden5165

Joined Sep 9, 2011
69
#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?
 

Thread Starter

Jaden5165

Joined Sep 9, 2011
69
//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! **********
 

t06afre

Joined May 11, 2009
5,934
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
Rich (BB code):
 __delay_ms(x)
and any delayus(x) with
Rich (BB code):
__delay_us(x)
You add
Rich (BB code):
#define _XTAL_FREQ xxxxxxx
with the correct value for your system.
Last replace
Rich (BB code):
static bit LCD_RS @ ((unsigned)&PORTE*8+1); // LCD Reg. Select 
static bit LCD_EN @ ((unsigned)&PORTE*8+2); // LCD Enable
with
Rich (BB code):
#define  LCD_RS  RE1
#define  LCD_EN  RE2
Please do not make it so hard to help you
 

Thread Starter

Jaden5165

Joined Sep 9, 2011
69
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:

cheezewizz

Joined Apr 16, 2009
82
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.
 

t06afre

Joined May 11, 2009
5,934
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
Rich (BB code):
#include<htc.h>
#define _XTAL_FREQ 4000000
__CONFIG(0x3F39);
void main()
{
unsigned char dcnt;
TRISD=0;
PORTD=0;
while(1)
{
PORTD=0xff;
__delay_ms(1000);
PORTD=0;
__delay_ms(1000);
}
}
 

Thread Starter

Jaden5165

Joined Sep 9, 2011
69
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.




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!
 

Thread Starter

Jaden5165

Joined Sep 9, 2011
69
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
Rich (BB code):
#include<htc.h>
#define _XTAL_FREQ 4000000
__CONFIG(0x3F39);
void main()
{
unsigned char dcnt;
TRISD=0;
PORTD=0;
while(1)
{
PORTD=0xff;
__delay_ms(1000);
PORTD=0;
__delay_ms(1000);
}
}



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:

stahta01

Joined Jun 9, 2011
133
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.
 
Top