Hi all, I am using Timer0 as interrupt for display time on LCD. But when the interrupt occurs, it won't come back to the last executed command of interrupted. It is repeating the ISR. I am using embedded C. I don't know what is happening. I did my best. Please help me.
here is my ISR
void timer0() interrupt 1 { TH0 = 0xFC; TL0 = 0x17; time_display(); }
Hi Saan, Where r u selecting the timer mode & why r u loading TH0 & TLO in ISR itself? And its better that avoid the function calling in ISR, instead of this better maintain flag in ISR and perform the operation in main function. Ensure that u r not using ur Timer0 any where else. Post the complete code for better answer...
Dear Abdul Hameed, Thanks for your response. My entire program contains lcd,12 bit adc, eeprom and 6 keys. It is for blood collection monitor. So when "START" key pressed, it have to display time on lcd as stop watch. At the same time i want to take readings from adc. I have used AT89c52. Here is my complete code. here i haven't included adc section.
#include <reg52.h> #include <string.h> #include <intrins.h>
# define uchar unsigned char # define uint unsigned int
//EEPROM Pins sbit CS = P3 ^ 4; //Chip Select sbit SCK = P3 ^ 5; //Clock sbit SDI = P3 ^ 6; //Serial Input sbit SDO = P3 ^ 7; //Seria Output sbit ON = P3 ^ 1; //EEPROM ON
//Keypad Pins sbit volup = P1^0; // Volume Up Key sbit voldn = P1^1; // Volume Down Key sbit pause = P1^2; // Pause Key sbit cmpup = P1^3; // Clamp Up Key sbit cmpdn = P1^4; // Clamp Dowm Key sbit start = P1^5; // Start Key
// LCD Pins sbit rs = P2^0; sbit en = P2^2; //pin enable is connected to lcd via inverter sbit rw = P2^1; sbit b = P0^7;
uint setvalue,ms_cnt=1,ms_sec,sec,min; uchar p=0,st=0;
void wait_key(); void set_display(uint disp, uchar loc, uchar dig)reentrant; void lcdcmd(uchar a); void lcddat(uchar b); void busy(); void lcdstr(uchar *s); void bcm_lcd_init(); void delay(); void time_display(); void WRAL (uint Data); void ERAL (); void ERASE (uchar Address); void Write (uchar Address, uint Data); uint Read (uchar Address); void EWEN (); void EWDS (); /*------------------------------------ISR-----------------------------------------
Timer 0 will start after pressing Start Key (when P1.5 = 0). */
void timer0() interrupt 1 { TH0 = 0xFC; TL0 = 0x17; time_display();
}
/*------------------------------------MAIN-----------------------------------------*/ main() { P0 = 0x00; P1 = 0xFF; P2 = 0xFF; TMOD = 0x01; IE = 0x82; ON = 1; setvalue = Read (0x40); bcm_lcd_init(); set_display(setvalue,0x85,3); while(1) { wait_key();
} }
/*------------------------------------void lcdcmd(unsigned char a)-----------------------------------------*/
void lcdcmd(uchar a) { busy(); rs = 0; rw = 0; P0 = a; en = 0; en = 1; } /*------------------------------------void lcddat(unsigned char b)-----------------------------------------*/ void lcddat(uchar b) { busy(); rs = 1; rw = 0; P0 = b; en = 0; en = 1; } /*------------------------------------void busy()-----------------------------------------*/ void busy() { en = 0; P0 = 0xFF; rs = 0; rw = 1; while(b==1) {
en=1; en = 0; } en=1; } /*------------------------------------void lcdstr(unsigned char *s)-----------------------------------------*/ void lcdstr(uchar *s) { uchar l,i; l = strlen(s); for(i=0;i<l;i++) { lcddat(*s); s++; } } /*------------------------------------void bcm_lcd_init() -----------------------------------------*/ void bcm_lcd_init() { uchar i; lcdcmd(0x3C); lcdcmd(0x0E); lcdcmd(0x14); lcdcmd(0x01); lcdcmd(0x80); lcdstr("haribharath Equipments Pvt.Ltd.Mumbai"); delay(); delay();
for (i=0;i<22;i++) { delay(); lcdcmd(0x18); }
lcdcmd(0x01); lcdcmd(0x80); lcdstr("Blood Collection"); lcdcmd(0xC4); lcdstr("Monitor"); delay(); delay();
lcdcmd(0x01); lcdcmd(0x80); lcdstr("Set:0350 Cnt:0000"); lcdcmd(0xC0); lcdstr("Bat:100% 00m:00s"); } /*------------------------------------void delay()-----------------------------------------*/ void delay() { uint hi_del; for(hi_del = 0 ; hi_del < 30800 ; hi_del++); } /*------------------------------------void wait_key()-----------------------------------------*/ void wait_key() { uchar key,mkey; key = P1; mkey = key&0x3f;; while(mkey!=63) { key = P1; mkey = key&0x3f; switch(mkey) { case 0x3E : if(setvalue==999) setvalue=0; else ++setvalue; set_display(setvalue,0x85,3); Write (0x40,setvalue); delay(); delay(); break;
case 0x3D : if(setvalue==0) setvalue=999; else --setvalue; set_display(setvalue,0x85,3); Write (0x40,setvalue); delay(); delay(); break;
case 0x3B : p = ~p ; delay(); delay(); if(p==255)TR0 = 0; else TR0 = 1; break; case 0x37 : break; case 0x2F : break; case 0x1F : if(st==1)break; st=1; start=0; TH0 = 0xFC; TL0 = 0x17; TR0 = 1; min=0; sec=0; set_display(min,0xC9,2); set_display(sec,0xCD,2); delay(); break; } } }
//--------------------------------------void time_display()--------------------------------------------
void time_display() { ms_cnt++; if(ms_cnt==1000) { sec++; if(sec==60) { min++; sec=0; if(min==60)min=0; set_display(sec,0xCD,2); set_display(min,0xC9,2); } else set_display(sec,0xCD,2); ms_cnt=0; } } /*------------------------------------void set_display()-----------------------------------------*/ void set_display(uint disp, uchar loc, uchar dig) reentrant { uchar j,asc,dis[5] ; uint rem=0,div=0; div = disp; for(j=3;j>0;j--) { rem=div%10; div/=10; asc=rem+0x30; dis[j]=asc; } lcdcmd(loc); for(j=4-dig;j<4;j++) { lcddat(dis[j]); } for(j=1;j<10;j++) lcdcmd(0x14); }
/*------------------------------------void EWEN ()-----------------------------------------*/ void EWEN ()
{ uchar i; uint Data; CS = 0; SCK = 0; CS = 1; //Data = 0x98; Data = 0x260; Data <<= 6; for (i = 0; i <10; i ++){ SDI = Data &0x8000; SCK = 1; SCK = 0; Data <<= 1; } CS = 0; } /*------------------------------------void EWDS ()-----------------------------------------*/ void EWDS () { uchar i; uint Data; CS = 0; SCK = 0; CS = 1; //Data = 0x80; Data = 0x200; Data <<= 6; for (i = 0; i <10; i ++){ SDI = Data &0x8000; SCK = 1; SCK = 0; Data <<= 1; } CS = 0; } /*------------------------------------uint Read (uchar Address)-----------------------------------------*/ uint Read (uchar Address) { uchar i,pack; uint TmpData,m_data;
for(pack=0;pack<2;pack++){ EWEN (); CS = 0; SCK = 0; CS = 1; TmpData = (Address & 0x7F) | 0x0300; TmpData <<= 6; for (i = 0; i < 10; i ++) { SDI = TmpData &0x8000; SCK = 1; SCK = 0; TmpData <<= 1; } SDO = 1; for (i = 0; i <8; i ++) { CS = 1; SCK = 1; m_data <<= 1; m_data |= SDO;
SCK = 0; } CS = 0; EWDS (); Address++; } return m_data; } /*------------------------------------void Write (uchar Address, uint Data)-----------------------------------------*/ void Write (uchar Address, uint Data)
{ uchar i,pack,lsb_dat,msb_dat; uint TmpData; TmpData = Data; lsb_dat = TmpData; TmpData >>= 8; msb_dat = TmpData; for(pack=0;pack<2;pack++) { EWEN (); CS = 0; SCK = 0; CS = 1; TmpData = (Address & 0x7F) | 0x0280; TmpData <<= 6; for (i = 0; i <10; i ++){ SDI = TmpData &0x8000; SCK = 1; SCK = 0; TmpData <<= 1; } if(pack==0)TmpData = msb_dat; else TmpData = lsb_dat; for (i = 0; i <8; i ++){ SDI = TmpData &0x80; SCK = 1; SCK = 0; TmpData <<= 1; } CS = 0; SDO = 1; CS = 1; SCK = 1; while (SDO == 0) { SCK = 0; SCK = 1; } SCK = 0; CS = 0; EWDS (); Address++; } } /*------------------------------------void ERASE (uchar Address)-----------------------------------------*/ void ERASE (uchar Address) { uchar i; uint TmpData; EWEN (); CS = 0; SCK = 0; CS = 1; TmpData = (Address & 0x3F) | 0x0380; TmpData <<= 6; for (i = 0; i <10; i ++){ SDI = TmpData &0x8000; SCK = 1; SCK = 0; TmpData <<= 1; } CS = 0; SDO = 1; CS = 1; SCK = 1; while (SDO == 0) { SCK = 0; SCK = 1; } SCK = 0; CS = 0; EWDS (); } /*------------------------------------void ERAL ()-----------------------------------------*/ void ERAL () { uchar i; uint TmpData; EWEN (); CS = 0; SCK = 0; CS = 1; TmpData = 0x240; TmpData <<= 6; for (i = 0; i <10; i ++){ SDI = TmpData &0x8000; SCK = 1; SCK = 0; TmpData <<= 1; } CS = 0; SDO = 1; CS = 1; SCK = 1; while (SDO == 0) { SCK = 0; SCK = 1; } SCK = 0; CS = 0; EWDS (); } /*------------------------------------void WRAL (uint Data)-----------------------------------------*/ void WRAL (uint Data)
{ uchar i; uint TmpData; EWEN (); ; CS = 0; SCK = 0; CS = 1; TmpData = 0x220; TmpData <<= 6; for (i = 0; i <10; i ++){ SDI = TmpData &0x8000; SCK = 1; SCK = 0 ; TmpData <<= 1; } TmpData = Data; for (i = 0; i <16; i ++){ SDI = TmpData &0x8000; SCK = 1; SCK = 0; TmpData <<= 1; } CS = 0; SDO = 1; CS = 1; SCK = 1; while (SDO == 0) { SCK = 0; SCK = 1; } SCK = 0; CS = 0; EWDS (); } /*------------------------------------END-----------------------------------------*/
Dear Abdul Hameed, The problem persist when i ignored the function inside the ISR.
if you want qualified advise, present your code in a legible way (formatted, indented, commented ) that you have checked for legibility in the preview
Erik
Look at the instructions: www.danlhenry.com/.../keil_code.png
Use Bold and Italic for emphasis - there is no point in making your entire post Bold!
Use the 'pre' tags for Source Code (and similar) only!
Also note the link to Tips for Posting Messages: http://www.keil.com/forum/tips.asp