Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

can anyone tell me whats wrong with this program

Status
Not open for further replies.

sanoopgr8

New Member
hi everyone i wrote this program expecting a increment in the port c for every second but something is wrong that i get rb6 and rb7 la 2.39 to 2.71
other rb0-rb5 2.55 v somebody please help

code is

#include <htc.h>

/*
* code for using timer0 on a 16F877A
* Just sets up a 1 micro-second interrupt and increments a variable
*/

/*
* Calculate preload value for one second timer
*/

#define PERIOD 1000000 // period in uS - one second here
#define XTAL 10000000 // crystal frequency - 10MHz

#define IPERIOD (10*10000000/XTAL) // Period of instruction clock in uSeconds

#define SCALE 256 // Timer 0 prescaler
#define T0_TICKS 256 // Number of counts for interrupt

#define TICK_PERIOD (SCALE * IPERIOD) // Period (uSec) of one increment of timer 0

#define RELOADS ((PERIOD/T0_TICKS)/TICK_PERIOD)
__CONFIG(LVPDIS & BORDIS & PWRTEN & WDTDIS & HS);
unsigned long seconds; // second count
near char reload = 0;

/* service routine for timer 0 interrupt */
void interrupt
timer0_isr(void)
{
if(reload == 0){
// effect a change on PORTC whenever our desired period is reached.
reload = RELOADS + 1;
seconds++;
PORTB++; // effect a change on PORTB
}
reload--;
T0IF = 0;
}

main()
{
// initialize timer 0;

OPTION = 0b0000; // prescale by 2
T0CS = 0; // select internal clock
T0IE = 1; // enable timer interrupt
GIE = 1; // enable global interrupts
TRISB = 0; // output changes on LED

for(;;)
continue; // let interrupt do its job
}
 
hi everyone i wrote this program expecting a increment in the port c for every second but something is wrong that i get rb6 and rb7 la 2.39 to 2.71
other rb0-rb5 2.55 v somebody please help

code is

#include <htc.h>

/*
* code for using timer0 on a 16F877A
* Just sets up a 1 micro-second interrupt and increments a variable
*/

/*
* Calculate preload value for one second timer
*/

#define PERIOD 1000000 // period in uS - one second here
#define XTAL 10000000 // crystal frequency - 10MHz

#define IPERIOD (10*10000000/XTAL) // Period of instruction clock in uSeconds

#define SCALE 256 // Timer 0 prescaler
#define T0_TICKS 256 // Number of counts for interrupt

#define TICK_PERIOD (SCALE * IPERIOD) // Period (uSec) of one increment of timer 0

#define RELOADS ((PERIOD/T0_TICKS)/TICK_PERIOD)
__CONFIG(LVPDIS & BORDIS & PWRTEN & WDTDIS & HS);
unsigned long seconds; // second count
near char reload = 0;

/* service routine for timer 0 interrupt */
void interrupt
timer0_isr(void)
{
if(reload == 0){
// effect a change on PORTC whenever our desired period is reached.
reload = RELOADS + 1;
seconds++;
PORTB++; // effect a change on PORTB
}
reload--;
T0IF = 0;
}

main()
{
// initialize timer 0;

OPTION = 0b0000; // prescale by 2
T0CS = 0; // select internal clock
T0IE = 1; // enable timer interrupt
GIE = 1; // enable global interrupts
TRISB = 0; // output changes on LED

for(;;)
continue; // let interrupt do its job
}

Perhaps you could use the code tags next time? It'll help keep the spacing better, and will keep the smilies out of the code :D Just click the "#" symbol in the tools menu.

I assume the ;) is supposed to be ; ), so I am curious--why do you have a
Code:
for(;;)
? Did that even compile? I'm pretty sure you need values in there in order for it to work. Also, where is "continue" defined? Perhaps I'm blind, but I just can't see it anywhere....?

Regards,
Der Strom
 
Last edited:
well its an infinite for loop it did complie and hex file was created and i burnned the pic with it and got the output as mentioned above

and continue is also a built in keyword in C i believe
 
Last edited:
In C, infinite loops are usually done with a "while(1);". I haven't seen a for loop used in that way before. Perhaps it's just something I haven't come across--I'm still quite new to programming, myself :D
Anyway, I think you can replace
Code:
for(;;)
{
     continue;
}
with
Code:
while(1);

Regards
 
Last edited:
In C, infinite loops are usually done with a "while(1);". I haven't seen a for loop used in that way before. Perhaps it's just something I haven't come across--I'm still quite new to programming, myself :D
Anyway, I think you can replace
Code:
for(;;)
{
     continue;
}
with
Code:
while(1);

Regards


ok sir i would do that but dose it have anything to do with the output ?
 
Also, I'm a little confused about what you're trying to do, and what's actually happening. What do you mean your'e trying to increment port c?
 
actuallu i need a program which dose this

a signal for 5uS and then 200 uS delay after that sense the positive edge of the pulse and start a counter until the negative edge of the pulse and get the counted value in integer form in a variable

here i have just wrote a program to use timer0 and see how it increments the portC for every 1 sec
 
actuallu i need a program which dose this

a signal for 5uS and then 200 uS delay after that sense the positive edge of the pulse and start a counter until the negative edge of the pulse and get the counted value in integer form in a variable

here i have just wrote a program to use timer0 and see how it increments the portC for every 1 sec

Perhaps it's just terminology here that's confusing me. When you say "increment", do you actually mean switch on/off? Increment really means "to count up", not to switch on or off. Is that what you actually mean?

Anyway, I think I understand what you mean. But first, you are not writing to port C. You're writing to port B. Also, you seem to be freezing the program at the end with the infinite loop. You'll want to set it up differently from what I told you earlier. I misunderstood your intentions. I actually think you can do this without actually using the isr. It would probably be easier to do it by polling the Timer0 interrupt flag (T0IF), and executing the code only if T0IF==1. Does that make sense?
Basically, it would look something like this:

Code:
main()
{
     while(1)
     {
          if(T0IF==1)
          {
               //toggle port;
               TOIF=0;
          }
     }
}

You could use the isr, but I think it'll make it more complicated than it needs to be.

regards
 
Last edited:
Something else you might try is the readTimer() function (found in the libraries). You can read the value on the timer directly, and translate it to actual time (using the processor clock frequency). That way, you could say something like:

Code:
time=readTimer();
if(time==xxx) //if timer reads less than corresponding time...
{
     //toggle port;
     //reset timer;
}
 
sir will this do the task i want as i told earlier

Code:
main()
while(1)
{
RC2 = 1;  //generate for 5us square wave

__delay_us(5);  //5uS delay 

RC2 = 0;

__delay_us(200); //200uS delay

}


now i need to set the timer and start measuring the time of start and end of the pulse 
can you say what to do sir ?
 
sir will this do the task i want as i told earlier

Code:
main()
while(1)
{
RC2 = 1;  //generate for 5us square wave

__delay_us(5);  //5uS delay 

RC2 = 0;

__delay_us(200); //200uS delay

}


now i need to set the timer and start measuring the time of start and end of the pulse 
can you say what to do sir ?[/QUOTE]

Hahaha, you know what? I feel like an idiot now :D I forgot Hi-Tech had ms and us delays :p
Yes, that should work, and you would not even need a timer if you're only trying to toggle the port on and off.

Wow, sorry about all that--Here I was thinking you were making it harder than it needed to be, and it turns out I was doing the same :D
 
sir i need to check only the pulse time duration whether it has reached say somewhere btw 0-19.5mS as i am using this time to measure the distance

now could you plz give me a code which sets the timer0 accordingly to measure that time

i guess i can easily use readTimer() for my operation after i receive my data
 
sir i need to check only the pulse time duration whether it has reached say somewhere btw 0-19.5mS as i am using this time to measure the distance

now could you plz give me a code which sets the timer0 accordingly to measure that time

i guess i can easily use readTimer() for my operation after i receive my data

Oh, are you the member who is working on the ultrasonic rangefinder? I remember talking to someone about it before, but the name completely slipped my mind :D

So the first part of the code sends out the signal, but now you're working on the part of the code for the receiver, am I correct?

Basically, you'll want to start the timer as soon as the chip "hears" a "click" (from the transmitter), and stop it when you hear the next one. After you stop it, you'll need to read the value on the timer and do the calculations to determine distance. Then you can repeat the process to update the data.

Regards
 
Oh, are you the member who is working on the ultrasonic rangefinder? I remember talking to someone about it before, but the name completely slipped my mind :D

So the first part of the code sends out the signal, but now you're working on the part of the code for the receiver, am I correct?

Basically, you'll want to start the timer as soon as the chip "hears" a "click" (from the transmitter), and stop it when you hear the next one. After you stop it, you'll need to read the value on the timer and do the calculations to determine distance. Then you can repeat the process to update the data.

Regards

:D yeah thats me :)

sir if you dont mind can you just send me a code to initalise the timer0 plz :)
 
:D yeah thats me :)

sir if you dont mind can you just send me a code to initalise the timer0 plz :)

Timer initialization registers/bits are shown in your chip's datasheet. Look under the section that says "Timer0 module", or something like that. It will give you all the information you need to initialize your timer.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top