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.

[SOLVED] 18f452 timer0 - interrupts

Status
Not open for further replies.

buddhikaneel

Member level 1
Joined
Jul 27, 2011
Messages
41
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,542
Hi my dear friends.

I have 16f877a and it's working with internal interrupts and external interrupts also. But I've confuse with 18f452. i see the data sheet. but still i can't do that. I want to make interrupt every 60 seconds variable cnt could update ones.
(ex: cnt++);
I'm using mikroc pro. Please any body can help me.
 

Hi my dear friends.

I have 16f877a and it's working with internal interrupts and external interrupts also. But I've confuse with 18f452. i see the data sheet. but still i can't do that. I want to make interrupt every 60 seconds variable cnt could update ones.
(ex: cnt++);
I'm using mikroc pro. Please any body can help me.

You need to implement a timer to overflow at 60 seconds and enable that timer interrupt, then increment "cnt" within the Interrutpt Service Routine (ISR).

Here is an example of a similar application:

**broken link removed**

Can you post your code, so that we may help you with the problem?

BigDog
 
Hi,
You can take the idea from this code snippet:
Code:
//Clock is set at 4MHz

unsigned int cnt;

void interrupt(){
      if (TMR0IF == 1){ //Not required if it is the only source of interrupt
         TMR0H = 0xF0;  //61630
         TMR0L = 0xBE;
         cnt++; //cnt is incremented approximately every sec
         TMR0IF_bit = 0;
      }
}

void main() {
     T0CON = 0x07; //16-bit, prescale 1:256
     GIE_bit = 1;
     PEIE_bit = 1;
     TMR0IF_bit = 0;
     TMR0IE_bit = 1;
     TMR0H = 0xF0; //61630
     TMR0L = 0xBE;
     TMR0ON_bit = 1;
     while(1){
     //......... Do whatever else is required
     //.........
     }
}

Then, you should upload your code so we can see where your specific problem lies.

Hope this helps.
Tahmid.
 
Code was written with mikroc pro

/*
* Project name:
TMR0 (Simple 'Hello World' demonstration of interrupt handling)
* Copyright:
(c) Mikroelektronika, 2005.
* Description:
This code demonstrates using interrupts in mikroC. Program turns on/off
LEDs on PORTB approximately each second.
* Test configuration:
MCU: P18F452
Dev.Board: EasyPIC4
Oscillator: HS, 08.0000 MHz
Ext. Modules: -
SW: mikroC v7.0
* NOTES:
None.
*/
#include "timelib.h"
TimeStruct ts1, ts2 ;
long epoch ;
long diff ;
unsigned cnt;

// LCD module connections
sbit LCD_RS at RC5_bit;
sbit LCD_EN at RC4_bit;
sbit LCD_D4 at RC0_bit;
sbit LCD_D5 at RC1_bit;
sbit LCD_D6 at RC2_bit;
sbit LCD_D7 at RC3_bit;

sbit LCD_RS_Direction at TRISC5_bit;
sbit LCD_EN_Direction at TRISC4_bit;
sbit LCD_D4_Direction at TRISC0_bit;
sbit LCD_D5_Direction at TRISC1_bit;
sbit LCD_D6_Direction at TRISC2_bit;
sbit LCD_D7_Direction at TRISC3_bit;
// End LCD module connections
char txt[4];

void interrupt() {
cnt++; // Increment value of cnt on every interrupt
TMR0L = 96;
INTCON = 0x20; // Set T0IE, clear T0IF
}//~

void main() {
Lcd_init();
Lcd_Cmd(_LCD_CURSOR_OFF);
ts1.ss = 1 ;
ts1.mn = 2 ;
ts1.hh = 3 ;
ts1.md = 4 ;
ts1.mo = 5 ;
ts1.yy = 2006 ;
epoch=Time_dateToEpoch(&ts1);
LongIntToStrWithZeros(epoch,txt);
Lcd_Out(1,1,txt);

T0CON = 0xC4; // Assign prescaler to TMR0
TRISB = 0; // PORTB is output
PORTB = 0xFF; // Initialize PORTB
TMR0L = 96;
INTCON = 0xA0; // Enable TMRO interrupt
cnt = 0; // Initialize cnt

do {
if (cnt == 400) {
PORTB = ~PORTB; // Toggle PORTB LEDs
cnt = 0; // Reset cnt
epoch++;
Time_epochToDate(epoch, &ts1) ;
LongIntToStrWithZeros(epoch,txt);
Lcd_Out(1,1,txt); //Hear display epoch's value
}
} while(1); // endless loop
}//~!

This code is working properly. In hear epoch is update time. how can display this epoch's values in lcd as date time format like 2011-06-21 13:25:41

Actually i want to run time.(this will do with epoch and it will count seconds upto ts1 data.) when i want to read time/date into project. then i can use epoch for it. But i want to display time when i want. but i can't do that. how can i do it. I try to do that using char txt[4] but it show like this

1234567890123456 Column numbers
_2011__06___25
-6___ 12____25
It's look like this. but i want to do it like this
1234567890123456 Column numbers
2011-06-25
13:42:32

Thanks for reply..
 

You could manually convert each part of the "ts1" from integer to string and concatenate each small string into the complete format you desire.

Or

You could use one of two library functions MikroE offers:

sprintf(char *wh, const char *f,...);

sprintf() combined with the parts of "ts1" structure and format specifications would produce the desired string format.

Or

void PrintOut(void (*prntoutfunc)(char ch), const char *f,...);

Output the produced string directly to a PrintHandler like Lcd_Out().

Reference the MikroC User Manual for examples of both library functions:

MikroC Pro for PIC User Manual

If you are still have problems using any of the above suggestions, post your problem here and I will provide some additional examples.

BigDog
 
  • Like
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
If you can send me code sample about sprintf() it will help to me.
Thank you for your post.
 

why do you want to use itt... it increases your memory usage too much.. this is not recommended in professional level of code development... since controllers have specific memory limits, these libraries like stdio.h, and other libraries are discouraged...

what will you achieve using sprintf???????? i dont see any reason here... people can give you many links regarding using this, but you should look at the practical point of view, if you have to design a project with memory constraints..........

---------- Post added at 16:39 ---------- Previous post was at 16:37 ----------

for your happiness see the code snippet

#include <p18f452.h>
#include <stdio.h>
const rom char * format[] = "%f";
void main (void)
{
float i =(float)78920533.352;
auto char buf[20];
sprintf (buf,format,i);

}

---------- Post added at 16:40 ---------- Previous post was at 16:39 ----------

example 2.

sprintf (buffer," %02d:%02d:%02d:%03d ", hr, min, s, ms);

The first '0' (zero) after the '%' indicates that the value should be zero-padded. The number following that '0' says what the full width of the printed number should be. Above I've chosen widths of 2 (i.e. '01, '02', '22', etc.) except for milliseconds, where you need a width of 3.

There are many other options and rules for using sprintf formats. I strongly suggest that you Google 'sprintf' and read the first link. Also, the output format specifiers are fully documented in the C18 library manual, section 4.7.2, under 'fprintf()' (which is another function which uses the same formatting).

---------- Post added at 16:45 ---------- Previous post was at 16:40 ----------

example code snippet.....l

Code:
#include "p18f452.h"
#include "lcd.h"
#include "delay.h"
#include "stdio.h"


#pragma config OSC = HS
#pragma config PWRT = OFF
#pragma config BOR = OFF
#pragma config LVP = OFF
#pragma config WDT = OFF,DEBUG=OFF


void main (void)
{
char buffer [50];
float x=20.25539; //lets assume you want to display 4 decimal digits
unsigned int x_int, x_dec;
char i=0;
x_int = (unsigned int)x;
x_dec = (unsigned int)(((float)x - (float)x_int) * 100000);
sprintf(buffer,(const far rom char*)"%d.%04d", x_int, x_dec);
lcd_init();
lcd_strrom((const rom char*)"Float to String");
while(1)
{
lcd_cmd(0XC0);
lcd_strram((const ram char*) buffer);

}

}
 
  • Like
Reactions: ramina

    ramina

    Points: 2
    Helpful Answer Positive Rating
if sprint is not good tell me how can i do timedate display on 16x2 lcd and store it every 1 mint in eeprom of flash memory

---------- Post added at 17:16 ---------- Previous post was at 17:15 ----------

i'm using mikroc pro. is this code working with mikroc pro
 

sprint syntax will not change from compiler to compiler... its a standard in which ever compiler you use....

first make the project up and running then we will discuss getting output without using sprintf.....

check the code size with this and without this and see the code size in the memory or the hex file generate....

the code snippet is to display time.. i think you can use it by modifying it as per your need....
 
  • Like
Reactions: ramina

    ramina

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top