void init_timer1(void)
{
T1CKPS1 = 1;
T1CKPS0 = 1;
T1OSCEN = 1; //Prescaler of 1:8
T1SYNC = 1;
TMR1CS = 0;
TMR1H = 11;
TMR1L = 219; //Period of 0.1 secs
TMR1IE = 1;
PEIE = 1;
GIE = 1;
TMR1ON = 1;
while (1);
}
void interrupt ISR(void)
{
if(TMR1IE && TMR1IF)
{
//TMR0 Overflow ISR
varinc++; //Increment Over Flow Counter
if(varinc==10)
{
counter++;
varinc = 0;
}
//Clear Flag
TMR0IF=0;
}
}
void sixseconds(void)
{
if(counter<6)
{
lcd_clr();
lcd_goto(0) ;
send_string("Drying In ");
lcd_goto(20);
send_string("Process. ");
}
else if (counter==6)
{
counter = 0;
lcd_clr() ;
lcd_goto(0) ;
send_string("Drying Is ");
lcd_goto(20);
send_string("Complete! ");
}
}
void init_timer1(void)
{
counter = 0;
T1CKPS1 = 1; // bits 5-4 Prescaler Rate Select bits
T1CKPS0 = 1; // bit 4
T1OSCEN = 0; // bit 3 Timer1 Oscillator Enable Control bit 1 = on
T1SYNC = 1; // bit 2 Timer1 External Clock Input Synchronization Control bit...1 = Do not synchronize external clock input
TMR1CS = 0; // bit 1 Timer1 Clock Source Select bit...0 = Internal clock (FOSC/4)
TMR1H = 11; // preset for timer1 MSB register
TMR1L = 219; // preset for timer1 LSB register
TMR1IE = 1; // enable Timer1 interrupts
PEIE = 1; // bit6 Peripheral Interrupt Enable bit...1 = Enables all unmasked peripheral interrupts
GIE = 1; // bit7 global interrupt enable
TMR1ON = 1; // bit 0 enables timer
while(1);
}
void interrupt ISR(void)
{
if(TMR1IF)
{
//TMR0 Overflow ISR
varinc++; //Increment Over Flow Counter
if(varinc==10)
{
counter++;
varinc = 0;
}
//Clear Flag
TMR0IF=0;
}
}
void sixtyminutes(void)
{
if(counter<6)
{
lcd_clr(); // clear LCD screen
lcd_goto(0) ; // set the position of message to be displayed
send_string("Drying In "); // send message to be displayed
lcd_goto(20); // set the position of message to be displayed
send_string("Process. ");
}
else if (counter==6)
{
counter = 0;
lcd_clr() ; // clear LCD screen
lcd_goto(0) ; // set the position of message to be displayed
send_string("Drying Is "); // send message to be displayed
lcd_goto(20); // set the position of message to be displayed
send_string("Complete! ");
}
}
//configure lcd
send_config(0b00000001); //clear display at lcd
send_config(0b00000010); //lcd return to home
send_config(0b00000110); //entry mode-cursor increase 1
send_config(0b00001100); //display on, cursor off and cursor blink off
send_config(0b00111000); //function set
void init_timer1(void)
{
counter = 0;
T1CKPS1 = 1;
T1CKPS0 = 1;
T1OSCEN = 0;
T1SYNC = 1;
TMR1CS = 0;
TMR1H = 11;
TMR1L = 220;
TMR1IE = 1;
PEIE = 1;
GIE = 1;
TMR1ON = 0;
}
void interrupt ISR(void)
{
if(TMR1IF==1)
{
//TMR1 Overflow ISR
varinc++; //Increment Over Flow Counter
if(varinc==10)
{
counter++;
varinc = 0;
}
//Clear Flag
TMR1IF=0;
TMR1H=11;
TMR1L=220;
}
}
void tenseconds(void)
{
TMR1ON=1;
if(counter<1)
{
lcd_clr();
lcd_goto(0) ;
send_string("Drying In ");
lcd_goto(20);
send_string("Process. ");
}
else if(counter==1)
{
counter = 0;
lcd_clr() ;
lcd_goto(0) ;
send_string("Drying Is ");
lcd_goto(20);
send_string("Complete! ");
}
}
while(1) // loop
{
if(!sw1) // if button SW1 is pressed
{
while(!sw1); // wait until button is released
j++;
if (j > 5) j = 0; // if mode is added more than three, set to zero
lcd_goto(20); // start display at 20
send_string(time[j]); // display string depend on mode
}
if (!sw2) // if button SW2is pressed
{
while(!sw2); // wait until button is released
switch(j)
{
case 0 : tenseconds();
break;
case 1 : twentyseconds();
break;
case 2 : thirtyseconds();
break;
case 3 : fortyseconds();
break;
case 4 : fiftyseconds();
break;
case 5 : sixtyseconds();
break;
default : ;
}
}
}
}
//configure lcd
send_config(0b00000001); //clear display at lcd
send_config(0b00000010); //lcd return to home
send_config(0b00000110); //entry mode-cursor increase 1
send_config(0b00001100); //display on, cursor off and cursor blink off
send_config(0b00111000); //function set
But whenever i insert the while(1) loop line in my init_timer1, it will never startup (line of black boxes). When i removed it, everything's normal, but my interrupt never works...
void init_timer1(void)
{
counter = 0;
T1CKPS1 = 1;
T1CKPS0 = 1;
T1OSCEN = 0;
T1SYNC = 1;
TMR1CS = 0;
TMR1H = 11;
TMR1L = 220;
TMR1IE = 1;
PEIE = 1;
GIE = 1;
TMR1ON = 0;
while(1); <<<<<<<<<<<<<<<the infinite loop
}
You should only have one, in fact, only the main execution loop as an while(1) infinite loop.
void init_timer1(void)
{
TMR1H = 11;
TMR1L = 220;
counter = 0;
T1CKPS1 = 1; // bits 5-4 Prescaler Rate Select bits
T1CKPS0 = 1; // bit 4
T1OSCEN = 0; // bit 3 Timer1 Oscillator Enable Control bit 1 = on
T1SYNC = 1;
TMR1CS = 0;
TMR1IE = 1; // enable Timer1 interrupts
PEIE = 1;
GIE = 1; // bit7 global interrupt enable
TMR1ON = 0; // bit 0 enables timer
while(1);
}
void interrupt ISR(void)
{
if(TMR1IF)
{
//TMR1 Overflow ISR
varinc++; //Increment Over Flow Counter
if(varinc==10) // If 1 second is achieved,
{
counter++;
varinc = 0;
}
//Clear Flag
TMR1IF=0;
TMR1H=11;
TMR1L=220;
}
}
void tenseconds(void)
{ TMR1ON=1;
lcd_clr(); // clear LCD screen
lcd_goto(0) ; // set the position of message to be displayed
send_string("Drying In "); // send message to be displayed
lcd_goto(20); // set the position of message to be displayed
send_string("Process. ");
if(counter==10)
{
counter = 0;
lcd_clr() ; // clear LCD screen
lcd_goto(0) ; // set the position of message to be displayed
send_string("Drying Is "); // send message to be displayed
lcd_goto(20); // set the position of message to be displayed
send_string("Complete! ");
}
}
I saw the program...
1- if you look at the program carefully, the while(1) is used only once in his program.
2- while(1); //Sit Idle Timer will do every thing! ; this means that he don't want to do anything in the main loop, if you look at the program flow, he initializes and then puts the main execution branch in an infinite loop. thus, not affecting the timer counter and and it's interruption handling routines.
In effect when you write while(1) you put a black hole in your program so nothing else will execute, not before neither after it; you could execute other code only and if only the processor is interrupted (from his endless loop) and brought to the handling routine, and then back to the loop.
#include <pic.h>
__CONFIG (0x3F32);
#define rs RB3
#define rw RB4
#define e RB5
#define lcd_data PORTD
#define sw1 RB0
#define sw2 RB1
#define sw3 RB2
#define motor_1 RC1 // 1st terminal of the motor.
#define motor_speed CCPR2L // The PWM value to control motor's speed.
void init_timer1(void);
void init(void);
void delay(unsigned long data);
void send_config(unsigned char data);
void send_char(unsigned char data);
void e_pulse(void);
void lcd_goto(unsigned char data);
void lcd_clr(void);
void send_string(const char *s);
void manualmode(void);
void automode(void);
void tenminutes(void);
void twentyminutes(void);
void thirtyminutes(void);
void fortyminutes(void);
void fiftyminutes(void);
void sixtyminutes(void);
unsigned long varinc;
unsigned long counter;
unsigned char data[6] = {0};
const unsigned char manual_mode[] = {"1.Manual Mode "};
const unsigned char auto_mode[] = {"2.Auto Mode "};
const unsigned char *mode[2] = {&manual_mode[0],&auto_mode[0]};
const unsigned char ten[] = {"10 Minutes "};
const unsigned char twenty[] = {"20 Minutes "};
const unsigned char thirty[] = {"30 Minutes "};
const unsigned char forty[] = {"40 Minutes "};
const unsigned char fifty[] = {"50 Minutes "};
const unsigned char sixty[] = {"60 Minutes "};
const unsigned char *time[6] = {&ten[0], &twenty[0], &thirty[0], &forty[0], &fifty[0], &sixty[0]};
void main(void)
{
unsigned char m = 0;
init_timer1();
init(); //initialize microcontroller
lcd_clr() ; // clear LCD screen
lcd_goto(0) ; // set the position of message to be displayed
send_string("Select Mode"); // send message to be displayed
lcd_goto(20); // set the position of message to be displayed
send_string(mode[m]);
while(1) // loop
{
if(!sw1) // if button SW1 is pressed
{
while(!sw1); // wait until button is released
m++;
if ( m > 1) m = 0; // if mode is added more than three, set to zero
lcd_goto(20); // start display at 20
send_string(mode[m]); // display string depend on mode
}
if (!sw2) // if button SW2is pressed
{
while(!sw2); // wait until button is released
switch(m) // check what is the current mode, execute the mode
{
case 0 : manualmode(); // mode 1 : manual mode
break;
case 1 : automode(); // mode 2 : auto mode
break;
default : ;
}
}
}
}
//============================================================================================================//
void manualmode()
{
unsigned char j=0;
lcd_clr() ; // clear LCD screen
lcd_goto(0) ; // set the position of message to be displayed
send_string("Select Time "); // send message to be displayed
lcd_goto(20); // set the position of message to be displayed
send_string(time[j]);
while(1) // loop
{
if(!sw1) // if button SW1 is pressed
{
while(!sw1); // wait until button is released
j++;
if (j > 5) j = 0; // if mode is added more than three, set to zero
lcd_goto(20); // start display at 20
send_string(time[j]); // display string depend on mode
}
if (!sw2) // if button SW2is pressed
{
while(!sw2); // wait until button is released
switch(j)
{
case 0 : tenminutes();
break;
case 1 : twentyminutes();
break;
case 2 : thirtyminutes();
break;
case 3 : fortyminutes();
break;
case 4 : fiftyminutes();
break;
case 5 : sixtyminutes();
break;
default : ;
}
}
}
}
void tenminutes(void)
{ TMR1ON=1;
lcd_clr(); // clear LCD screen
lcd_goto(0) ; // set the position of message to be displayed
send_string("Drying In "); // send message to be displayed
lcd_goto(20); // set the position of message to be displayed
send_string("Process. ");
if(counter==10)
{
lcd_clr() ; // clear LCD screen
lcd_goto(0) ; // set the position of message to be displayed
send_string("Drying Is "); // send message to be displayed
lcd_goto(20); // set the position of message to be displayed
send_string("Complete! ");
}
}
void twentyminutes(void)
{
TMR1ON=1;
lcd_clr(); // clear LCD screen
lcd_goto(0) ; // set the position of message to be displayed
send_string("Drying In "); // send message to be displayed
lcd_goto(20); // set the position of message to be displayed
send_string("Process. ");
if (counter==20)
{
lcd_clr() ; // clear LCD screen
lcd_goto(0) ; // set the position of message to be displayed
send_string("Drying Is "); // send message to be displayed
lcd_goto(20); // set the position of message to be displayed
send_string("Complete! ");
}
}
void thirtyminutes(void)
{
TMR1ON=1;
lcd_clr(); // clear LCD screen
lcd_goto(0) ; // set the position of message to be displayed
send_string("Drying In "); // send message to be displayed
lcd_goto(20); // set the position of message to be displayed
send_string("Process. ");
if (counter==30)
{
lcd_clr() ; // clear LCD screen
lcd_goto(0) ; // set the position of message to be displayed
send_string("Drying Is "); // send message to be displayed
lcd_goto(20); // set the position of message to be displayed
send_string("Complete! ");
}
}
void fortyminutes(void)
{
TMR1ON=1;
lcd_clr(); // clear LCD screen
lcd_goto(0) ; // set the position of message to be displayed
send_string("Drying In "); // send message to be displayed
lcd_goto(20); // set the position of message to be displayed
send_string("Process. ");
if (counter==40)
{
lcd_clr() ; // clear LCD screen
lcd_goto(0) ; // set the position of message to be displayed
send_string("Drying Is "); // send message to be displayed
lcd_goto(20); // set the position of message to be displayed
send_string("Complete! ");
}
}
void fiftyminutes(void)
{
TMR1ON=1;
lcd_clr(); // clear LCD screen
lcd_goto(0) ; // set the position of message to be displayed
send_string("Drying In "); // send message to be displayed
lcd_goto(20); // set the position of message to be displayed
send_string("Process. ");
if (counter==50)
{
lcd_clr() ; // clear LCD screen
lcd_goto(0) ; // set the position of message to be displayed
send_string("Drying Is "); // send message to be displayed
lcd_goto(20); // set the position of message to be displayed
send_string("Complete! ");
}
}
void sixtyminutes(void)
{
TMR1ON=1;
lcd_clr(); // clear LCD screen
lcd_goto(0) ; // set the position of message to be displayed
send_string("Drying In "); // send message to be displayed
lcd_goto(20); // set the position of message to be displayed
send_string("Process. ");
if (counter==60)
{
lcd_clr() ; // clear LCD screen
lcd_goto(0) ; // set the position of message to be displayed
send_string("Drying Is "); // send message to be displayed
lcd_goto(20); // set the position of message to be displayed
send_string("Complete! ");
}
}
//===================================================================================================//
void automode()
{
lcd_clr(); // clear LCD screen
lcd_goto(0) ; // set the position of message to be displayed
send_string("2.Auto Mode "); // send message to be displayed
lcd_goto(20); // set the position of message to be displayed
send_string("OHYEAHOHYEAH");
}
void init_timer1(void)
{
TMR1H = 11; // preset for timer1 MSB register
TMR1L = 220; // preset for timer1 LSB register
counter = 0;
T1CKPS1 = 1; // bits 5-4 Prescaler Rate Select bits
T1CKPS0 = 1; // bit 4
T1OSCEN = 0; // bit 3 Timer1 Oscillator Enable Control bit 1 = on
T1SYNC = 1; // bit 2 Timer1 External Clock Input Synchronization Control bit...1 = Do not synchronize external clock input
TMR1CS = 0; // bit 1 Timer1 Clock Source Select bit...0 = Internal clock (FOSC/4)
TMR1IE = 1; // enable Timer1 interrupts
PEIE = 1; // bit6 Peripheral Interrupt Enable bit...1 = Enables all unmasked peripheral interrupts
GIE = 1; // bit7 global interrupt enable
TMR1ON = 0; // bit 0 enables timer
while(1);
}
void interrupt ISR(void)
{
if(TMR1IF)
{
varinc++; //Increment Over Flow Counter
if(varinc==10)
{
counter++;
varinc = 0;
}
TMR1IF=0;
TMR1H=11;
TMR1L=220;
}
}
void init()
{
// Setup PWM mode.
CCP2CON = 0b00001100; // Set as PWM Mode.
T2CON = 0b00000101; // Turn on Timer 2 with prescaler = 4.
PR2 = 0xFF; // Set the PWM period.
motor_1 = 0;
motor_speed = 0; // No PWM Duty Cycle during startup.
//set I/O input output
TRISB = 0b00000111; //configure PORTB I/O direction
TRISD = 0b00000000; //configure PORTD I/O direction
TRISC = 0b00000000;
//setup ADC
ADCON1 = 0b00000110; //set ADx pin digital I/O
//configure lcd
send_config(0b00000001); //clear display at lcd
send_config(0b00000010); //lcd return to home
send_config(0b00000110); //entry mode-cursor increase 1
send_config(0b00001100); //display on, cursor off and cursor blink off
send_config(0b00111000); //function set
}
void delay(unsigned long data)
{
for( ;data>0;data-=1);
}
void send_config(unsigned char data)
{
rs=0; //clear rs into config mode
lcd_data=data;
delay(500);
e_pulse();
}
void send_char(unsigned char data)
{
rs=1; //set rs into write mode
lcd_data=data;
delay(500);
e_pulse();
}
void e_pulse(void)
{
e=1;
delay(500);
e=0;
delay(500);
}
void lcd_goto(unsigned char data)
{
if(data<16)
{
send_config(0x80+data);
}
else
{
data=data-20;
send_config(0xc0+data);
}
}
void lcd_clr(void)
{
send_config(0x01);
delay(500);
}
void send_string(const char *s)
{
unsigned char i=0 ;
while (s && *s)send_char (*s++) ;
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?