Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Multiple DS18S20 problem

Status
Not open for further replies.
That's a difficult question to answer without knowing all the software beforehand. In general you can write two programs and debug each one alone. When you know they both work you can combine the code by either editing one source after the other in a single file, taking care to put the declarations together at the top of the program or, you can use both sources and link their object files together. The other alternative is to write a completely new program and copy parts of the other programs in to it as needed.

I'm working on a project at the moment that uses a DS1302 RTC with 6 DS18B20 sensors that controls amongst other things several 2HP water pumps but my code would would not fit into a 16F873 (it's already around 11K long and not finished yet) and the sensors are radio linked so my software wouldn't be a good example for you.

Brian.
 

Hi, Brian, I found out that I need interrupts, but I cant understand, how to use them. have any ideas?

Povilas.
 

Interrupts are easy. All you have to do is create a routine to handle the interrupt when it occurs and in it, clear the interrupt flag bit. The interrupt is like a subroutine that gets called by something happening in hardware rather than being called as part of the program. When the 'trigger' for the interrupt occurs, the present place in the program is saved then the interrupt routine is called automatically. At the end of the interrupt routine the program continues from where it was before it was interrupted.

Brian.
 

Interrupts are easy. All you have to do is create a routine to handle the interrupt when it occurs and in it, clear the interrupt flag bit. The interrupt is like a subroutine that gets called by something happening in hardware rather than being called as part of the program. When the 'trigger' for the interrupt occurs, the present place in the program is saved then the interrupt routine is called automatically. At the end of the interrupt routine the program continues from where it was before it was interrupted.

Brian.

Hi Brian, how I can post you a code that you could help me with interrupts? :)

Povilas
 

It's difficult for me to post code at the moment so the formating below might be wrong:
Code:
myInterruptCode interrupt()  // this is the interrupt routine (check the syntax for your compiler)
{
TMR0 = 0x08;  // reload the timer for the next interrupt
INTCON &= (1 << TMR0IF);  // clear the timer 0 interrupt flag
myTimerFlag = 1;  // this is so the main program knows the timer interrupt occurred
}

void main()
{
if(myTimerFlag == 1)
{
myTimerFlag = 0;
<do whatever the interrupt needs to do>
.
.
}
}

What happens is when TMR overflows (in this case in 0x100 - 0x08 counts), the TMR1IF flag is set and the interrupt routine is called. All the routne does is reload the timer for the next overflow, clear the interrupt flag and set a variable to 1 before returning. The main program checks the variable and when it sets, resets it and does whatever is needed. The variable will be set by the timer hardware so the main program can continure doing something else as long as it checks it periodically. By varying the value loaded into TMR0 you can change the delay before the interrupt happens.

Brian.
 

You are using interrupts already to check for timer 0 overflow but the code isn't quite right. You have an extra '}' at the end of the interrupt routine and the interrupt enable flag isn't set anyway. If you remove the '//' from the line "//TMR0IE=1;" it will enable the interrupts and the variable "cnt" should count up. At the moment, it will re-interrupt as soon as the interrupt routine ends because you haven't cleared the interupt flag, you should add a line TMR0IF = 0; inside the interrupt routine. Also note that the interrupt will happen once every 256 timer clocks as it rolls over from 0xFF to zero again, if you want it to interrupt faster you have to load a new value into TMR0 before the interrupt routine ends so it needs fewer ticks to reach overflow value.

Brian.
 

You are using interrupts already to check for timer 0 overflow but the code isn't quite right. You have an extra '}' at the end of the interrupt routine and the interrupt enable flag isn't set anyway. If you remove the '//' from the line "//TMR0IE=1;" it will enable the interrupts and the variable "cnt" should count up. At the moment, it will re-interrupt as soon as the interrupt routine ends because you haven't cleared the interupt flag, you should add a line TMR0IF = 0; inside the interrupt routine. Also note that the interrupt will happen once every 256 timer clocks as it rolls over from 0xFF to zero again, if you want it to interrupt faster you have to load a new value into TMR0 before the interrupt routine ends so it needs fewer ticks to reach overflow value.


Brian.

If I am writing TMR0IF = 0; it gives me an error.
 

I'm not sure why that is but try writing it a different way like this:

INTCON &= ~(1 << TMR0IF);

That should logic AND the value in INTCON with the inverse of the interrupt flag which forces it to zero.

Brian.
 

I write this code
Code:
int cnt = 0;               // Global variable cnt

void interrupt()
{
//Check if it is TMR0 Overflow ISR

if(TMR0IE && TMR0IF)
{
//TMR0 Overflow ISR
cnt++;  //Increment Over Flow Counter
}
INTCON.TMR0IF = 0;
}
in main function
Code:
INTCON.GIE = 1;
 INTCON.TMR0IE=1;
 INTCON.TMR0IF=1;

is this good?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top