Gopalkrishna.D
Newbie level 3
Hi,
I am developing some application in PICDEM SYSTEM MANAGEMENT board using PIC16F886.
I am trying to impliment thread (multitasking) concept in my program using Timer1.
The below shown program works with MPLAB SIM perfectly.But when i impliment the same program on the demo board using PICkit3
debugger it is hanging(Timer1 Interrupt not occuring) hence i am not getting the multitasking. What might be the problem ?
and please let me know how to use CONFIG1 Register in PIC16F886.
The Program is given below......
Main.c
#include <htc.h>
#include <pic.h>
#include <stdio.h>
#include <stdlib.h>
#include "877_i2c.h"
#include "delay.h"
#ifndef _XTAL_FREQ
// Unless already defined assume 4MHz system frequency
// This definition is required to calibrate __delay_us() and __delay_ms()
#define _XTAL_FREQ 4000000
#endif
//task counters used to tell when a task is ready to be executed
//all these counters are incremented every time a 100ms interrupt happens
//every task has its own counter that is updated every time a 100ms interrupt happens
unsigned int task_counter=0;
unsigned int Btn_flag=0;
#define TASK_COUNTER_MAX 2 // task every 100ms
#define TRUE 1
#define FALSE 0
//#BYTE CONFIG1 = 0X2007
//Button definitions
#define ON_OFF 1
#define AUD_INPUT_SRC_SELCT 2
#define VOL_UP 3
#define VOL_DOWN 4
#define MUTE 5
volatile unsigned char task_enable=TRUE; //enables the task
void column_4();
void column_5();
void initPORTS ()
{
TRISA = 1; // Input for RA0 to RA7
TRISB = 0; // presently this port is not used
TRISC = 0; //set all ports as output
ANSEL = 0; //make all ports as digital I/O
ANSELH = 0;
PORTA = 0; // set all ports to low
PORTB = 0;
PORTC = 0;
}
void setup_multitasking(void)
{
//set up tmr1 to interrupt every 100ms
TMR1CS=0; // 0 - internal clock(Fosc/4) 1- External clk
T1CKPS0=0; //1:1 prescale value
T1CKPS1=0; //1:1 prescale value
/*We want to wait 200 clock cycles, or 100ms @ 8MHz (instructions are 1/4 speed of clock).
Timer 1 interrupts when it gets to 0xFFFF or 65535. Therefore, we set timer 1 to 65535
minus 2000 = 63535, then wait 2000 ticks until rollover at 65535. To test, use simulator
to find that its exactly correct*/
#define TICKS_BETWEEN_INTERRUPTS 2000 //2000
#define INTERRUPT_OVERHEAD 19 //19
#define TMR1RESET (0xFFFF-(TICKS_BETWEEN_INTERRUPTS-INTERRUPT_OVERHEAD))
#define TMR1RESET_HIGH TMR1RESET >> 8
#define TMR1RESET_LOW TMR1RESET & 0xFF
TMR1ON=0;
TMR1H=TMR1RESET_HIGH;
TMR1L=TMR1RESET_LOW;
TMR1ON=1;
TMR1IF=0;
TMR1IE=1;
PEIE=1;
GIE=1;
}
void interrupt ISR(void)
{
//one tick every 100ms at 8Mhz
if (TMR1IF)
{
//set up timer 1 again to interrupt 100ms in future
TMR1IF=0;
TMR1ON=0;
TMR1H=TMR1RESET_HIGH;
TMR1L=TMR1RESET_LOW;
TMR1ON=1;
task_counter++;
if (task_counter>=TASK_COUNTER_MAX) //high frequency task every 1 tick
{
task_counter=0;
if (task_enable==TRUE)
{
column_4();
column_5();
}
}
} //if (TMR1IF)
} //interrupt service routine
void column_4()
{
RA5 = 1; //make FP_CLOCK pin high
RC0 = 1; //make FP_STROBE pin high
unsigned int i=0,temp=0;
unsigned int col4_check = 0x08;
for (i=0;i<8;i++)
{
temp = col4_check;
if ((temp & 0x80) == 0x80)
{
temp = (temp << 1);
RC1 = temp;
}
}
if (RA2 == 1)
{
Btn_flag = AUD_INPUT_SRC_SELCT; //indicates "PAUSE" button is pressed
}
if (RA3 == 1)
{
Btn_flag = MUTE; //indicates "STOP" button is pressed
}
if (RA4 == 1)
{
Btn_flag = VOL_UP ; //indicates "FORWARD" button is pressed
}
}
void column_5()
{
RA5 = 1; //make FP_CLOCK pin high
RC0 = 1; //make FP_STROBE pin high
unsigned int i=0,temp1=0;
unsigned int col5_check = 04;
for (i=0;i<8;i++)
{
temp1 = col5_check;
if ((col5_check & 0x80) == 0x80)
{
temp1 = (temp1 << 1);
RC1 = temp1;
}
}
if (RA2 == 1)
{
Btn_flag = ON_OFF; //indicates "RECORD" button is pressed
}
if (RA4 == 1)
{
Btn_flag = VOL_DOWN; //indicates "REWIND" button is pressed
}
}
/******************************************************************************************/
void write_ext_eeprom(unsigned char address, unsigned char data)
{
i2c_start();
i2c_write(0xa0);
i2c_write(0x00);
i2c_write(address);
i2c_write(data);
__delay_ms(1);
i2c_stop();
__delay_ms(1);
}
/******************************************************************************************/
unsigned char read_ext_eeprom(unsigned char address)
{
unsigned char data;
i2c_start();
i2c_write(0xa0);
i2c_write(0x00);
i2c_write(address);
i2c_repStart();
i2c_write(0xa1);
data=i2c_read(0);
i2c_stop();
return(data);
}
/******************************************************************************************/
main()
{
unsigned char i;
unsigned char j;
unsigned char k;
unsigned char m;
int flag=1;
//OSCCON = 0x6D; //select 8Mhz internal clock
TMR1IF=0;
initPORTS () ;
i2c_init(); // init i2c
//setup_multitasking();
while(flag)
{
flag=0;
i=0;
j=0x05;
while(i<=2)
{
//write_ext_eeprom(i,j); // the memory and show it on leds,it shoaled be the same like the address
// __delay_ms(1);
RA2=1;
/* __delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100); */
RA2=0;
i++;
j++;
}
RA2=1;
RA4=1;
RB5=1;
/* __delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100); */
RA2=0;
RA4=0;
RB5=0;
i=0;
j=0x05;
while(i<=2)
{
// k=read_ext_eeprom(i); // the memory and show it on leds,it shoaled be the same like the address
//__delay_ms(1);
if(k==j)
{
RA4=1;
/* __delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100); */
RA4=0;
}
i++;
j++;
}
RA2=1;
RA4=1;
RB5=1;
/* __delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100); */
RA2=0;
RA4=0;
RB5=0;
}
OSCCON = 0x70;
setup_multitasking();
m=0;
while(1)
{m++;
switch(Btn_flag)
{
case ON_OFF:
{
//printf("ON_OFF button is pressed\r\n");
Btn_flag = 0;
}
break;
case AUD_INPUT_SRC_SELCT:
{
//printf("AUD_I/P_SRC_SELCT button is pressed\r\n");
Btn_flag = 0;
}
break;
case VOL_UP:
{
//printf("VOL_UP button is pressed\r\n");
Btn_flag = 0;
}
break;
case VOL_DOWN:
{
//printf("VOL_DOWN button is pressed\r\n");
Btn_flag = 0;
}
break;
case MUTE:
{
//printf("MUTE button is pressed\r\n");
Btn_flag = 0;
}
break;
}
}
}
I am developing some application in PICDEM SYSTEM MANAGEMENT board using PIC16F886.
I am trying to impliment thread (multitasking) concept in my program using Timer1.
The below shown program works with MPLAB SIM perfectly.But when i impliment the same program on the demo board using PICkit3
debugger it is hanging(Timer1 Interrupt not occuring) hence i am not getting the multitasking. What might be the problem ?
and please let me know how to use CONFIG1 Register in PIC16F886.
The Program is given below......
Main.c
#include <htc.h>
#include <pic.h>
#include <stdio.h>
#include <stdlib.h>
#include "877_i2c.h"
#include "delay.h"
#ifndef _XTAL_FREQ
// Unless already defined assume 4MHz system frequency
// This definition is required to calibrate __delay_us() and __delay_ms()
#define _XTAL_FREQ 4000000
#endif
//task counters used to tell when a task is ready to be executed
//all these counters are incremented every time a 100ms interrupt happens
//every task has its own counter that is updated every time a 100ms interrupt happens
unsigned int task_counter=0;
unsigned int Btn_flag=0;
#define TASK_COUNTER_MAX 2 // task every 100ms
#define TRUE 1
#define FALSE 0
//#BYTE CONFIG1 = 0X2007
//Button definitions
#define ON_OFF 1
#define AUD_INPUT_SRC_SELCT 2
#define VOL_UP 3
#define VOL_DOWN 4
#define MUTE 5
volatile unsigned char task_enable=TRUE; //enables the task
void column_4();
void column_5();
void initPORTS ()
{
TRISA = 1; // Input for RA0 to RA7
TRISB = 0; // presently this port is not used
TRISC = 0; //set all ports as output
ANSEL = 0; //make all ports as digital I/O
ANSELH = 0;
PORTA = 0; // set all ports to low
PORTB = 0;
PORTC = 0;
}
void setup_multitasking(void)
{
//set up tmr1 to interrupt every 100ms
TMR1CS=0; // 0 - internal clock(Fosc/4) 1- External clk
T1CKPS0=0; //1:1 prescale value
T1CKPS1=0; //1:1 prescale value
/*We want to wait 200 clock cycles, or 100ms @ 8MHz (instructions are 1/4 speed of clock).
Timer 1 interrupts when it gets to 0xFFFF or 65535. Therefore, we set timer 1 to 65535
minus 2000 = 63535, then wait 2000 ticks until rollover at 65535. To test, use simulator
to find that its exactly correct*/
#define TICKS_BETWEEN_INTERRUPTS 2000 //2000
#define INTERRUPT_OVERHEAD 19 //19
#define TMR1RESET (0xFFFF-(TICKS_BETWEEN_INTERRUPTS-INTERRUPT_OVERHEAD))
#define TMR1RESET_HIGH TMR1RESET >> 8
#define TMR1RESET_LOW TMR1RESET & 0xFF
TMR1ON=0;
TMR1H=TMR1RESET_HIGH;
TMR1L=TMR1RESET_LOW;
TMR1ON=1;
TMR1IF=0;
TMR1IE=1;
PEIE=1;
GIE=1;
}
void interrupt ISR(void)
{
//one tick every 100ms at 8Mhz
if (TMR1IF)
{
//set up timer 1 again to interrupt 100ms in future
TMR1IF=0;
TMR1ON=0;
TMR1H=TMR1RESET_HIGH;
TMR1L=TMR1RESET_LOW;
TMR1ON=1;
task_counter++;
if (task_counter>=TASK_COUNTER_MAX) //high frequency task every 1 tick
{
task_counter=0;
if (task_enable==TRUE)
{
column_4();
column_5();
}
}
} //if (TMR1IF)
} //interrupt service routine
void column_4()
{
RA5 = 1; //make FP_CLOCK pin high
RC0 = 1; //make FP_STROBE pin high
unsigned int i=0,temp=0;
unsigned int col4_check = 0x08;
for (i=0;i<8;i++)
{
temp = col4_check;
if ((temp & 0x80) == 0x80)
{
temp = (temp << 1);
RC1 = temp;
}
}
if (RA2 == 1)
{
Btn_flag = AUD_INPUT_SRC_SELCT; //indicates "PAUSE" button is pressed
}
if (RA3 == 1)
{
Btn_flag = MUTE; //indicates "STOP" button is pressed
}
if (RA4 == 1)
{
Btn_flag = VOL_UP ; //indicates "FORWARD" button is pressed
}
}
void column_5()
{
RA5 = 1; //make FP_CLOCK pin high
RC0 = 1; //make FP_STROBE pin high
unsigned int i=0,temp1=0;
unsigned int col5_check = 04;
for (i=0;i<8;i++)
{
temp1 = col5_check;
if ((col5_check & 0x80) == 0x80)
{
temp1 = (temp1 << 1);
RC1 = temp1;
}
}
if (RA2 == 1)
{
Btn_flag = ON_OFF; //indicates "RECORD" button is pressed
}
if (RA4 == 1)
{
Btn_flag = VOL_DOWN; //indicates "REWIND" button is pressed
}
}
/******************************************************************************************/
void write_ext_eeprom(unsigned char address, unsigned char data)
{
i2c_start();
i2c_write(0xa0);
i2c_write(0x00);
i2c_write(address);
i2c_write(data);
__delay_ms(1);
i2c_stop();
__delay_ms(1);
}
/******************************************************************************************/
unsigned char read_ext_eeprom(unsigned char address)
{
unsigned char data;
i2c_start();
i2c_write(0xa0);
i2c_write(0x00);
i2c_write(address);
i2c_repStart();
i2c_write(0xa1);
data=i2c_read(0);
i2c_stop();
return(data);
}
/******************************************************************************************/
main()
{
unsigned char i;
unsigned char j;
unsigned char k;
unsigned char m;
int flag=1;
//OSCCON = 0x6D; //select 8Mhz internal clock
TMR1IF=0;
initPORTS () ;
i2c_init(); // init i2c
//setup_multitasking();
while(flag)
{
flag=0;
i=0;
j=0x05;
while(i<=2)
{
//write_ext_eeprom(i,j); // the memory and show it on leds,it shoaled be the same like the address
// __delay_ms(1);
RA2=1;
/* __delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100); */
RA2=0;
i++;
j++;
}
RA2=1;
RA4=1;
RB5=1;
/* __delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100); */
RA2=0;
RA4=0;
RB5=0;
i=0;
j=0x05;
while(i<=2)
{
// k=read_ext_eeprom(i); // the memory and show it on leds,it shoaled be the same like the address
//__delay_ms(1);
if(k==j)
{
RA4=1;
/* __delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100); */
RA4=0;
}
i++;
j++;
}
RA2=1;
RA4=1;
RB5=1;
/* __delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100);
__delay_ms(100); */
RA2=0;
RA4=0;
RB5=0;
}
OSCCON = 0x70;
setup_multitasking();
m=0;
while(1)
{m++;
switch(Btn_flag)
{
case ON_OFF:
{
//printf("ON_OFF button is pressed\r\n");
Btn_flag = 0;
}
break;
case AUD_INPUT_SRC_SELCT:
{
//printf("AUD_I/P_SRC_SELCT button is pressed\r\n");
Btn_flag = 0;
}
break;
case VOL_UP:
{
//printf("VOL_UP button is pressed\r\n");
Btn_flag = 0;
}
break;
case VOL_DOWN:
{
//printf("VOL_DOWN button is pressed\r\n");
Btn_flag = 0;
}
break;
case MUTE:
{
//printf("MUTE button is pressed\r\n");
Btn_flag = 0;
}
break;
}
}
}