Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Support needed on updown counter for pic16f882

Status
Not open for further replies.

tong143

New Member
Hi
To start with I have written a program using mikroC, for pic16f882 using 4 MHz external crystal oscillator, to increase and decrease a number based on input from RA1/RA2 and display on multiplexed 7 segment display. 1s position is controlled by RA3 and 10s position is controlled by RA4. It is a common cathode. I am usin real pic simulator for simulation. The sequence is not followed while increase or decrease operations and both the digits are showing the same value. Following is the program. Shall someone help me on this.
Code:
unsigned char seg[10]={0x80,0xf2,0x48,0x60,0x32,0x24,0x04,0xF0,0x00,0x30};   // this is a common anode display and pins 1-7 of PORTC are connected to A-G of 7 segment display
unsigned short i, DD0=0x80,DD1=0x80;
unsigned int tc,k=0,t1c;
void display_temp()                //displaying number in tc for 100 ms
{
    for (i=0;i<1;i++)
      {
       TMR1IE_bit=1;               //enabling timer 1 interrupt
       while(!TMR1IF_bit )
        {
            PORTC = seg[DD0];
            PORTA.f3= 1;          // Select Ones Digit. RA3 is connected to seven segement display on the right
            PORTA.f4= 0;          // RA4 is connected to seven segement display on the left
            delay_ms(1);          // wait for 1 ms
            PORTC = seg[DD1];
            PORTA.f3= 0;
            PORTA.f4= 1;          // Select Tens Digit
            delay_ms(1);          // wait for 1 ms
            if(PORTA.f1 == 1 || PORTA.f2 == 1)        //if there is any input from RA1 or RA2 break from loop
             {
               TMR1IE_bit=0;   // timer 1 interrupt is disabled
               TMR1IF_bit=0;   // timer 1 interrupt flag is cleared
               break;
             }
        }
       TMR1H=0x9E;             // initialization of timer1 for 100 ms count
       TMR1L=0x58;
       TMR1IE_bit=0;           // timer 1 interrupt is disabled
       TMR1IF_bit=0;           // timer 1 interrupt flag is cleared
       if(PORTA.f1 == 1 || PORTA.f2 == 1)   //if there is any input from RA1 or RA2 break from loop
       break;
      }
    return;
}


void main()
{
  int tc=0,count=0,set=0, t1,t1c=0;
  GIE_bit=1;
  TRISA=0xE7;             // Set PORTA pins 3 and 4 as output and others as input
  TRISC=0x01;             // set PORTC as OUTPUT except pin0
  PORTA=0x00;             // initialize port A
  PORTC=0x80;             // initialize port C
  ANSEL=0x00;             // set all ports digital
  ANSELH=0x00;
  T1CON=0x01;             // tmr1on bit is set
  TMR1H=0x9E;             // initialization of timer1 for 100 ms count
  TMR1L=0x58;
  display_temp();  // calling display_temp function to display DD0 and DD1 on segment on right and left respectively
  while(1)                // man program starts here
    {
     if(PORTA.f2==1)      // checking if decrease push to on button connected to portA pin 2 is pressed
       {
         if(tc==0) tc=67;
         else    tc--;
       }
     if(PORTA.f1==1)      // checking if increase push to on button connected to portA pin 1 is pressed
       {
         if(tc==67) tc=0;
         else       tc++;
       }
     DD0 = tc%10;         // digit in 1's position of tc is read to DD0
     DD1 = tc/10;         // digit in 10's position of tc is read to DD1
     display_temp();
     TMR1IE_bit=0;        // timer 1 interrupt is disabled
     TMR1IF_bit=0;        // timer 1 interrupt flag is cleared
    }
}
 
Last edited:
You should use the code tags to post code..

Code:
unsigned char seg[10]={0x80,0xf2,0x48,0x60,0x32,0x24,0x04,0xF0,0x00,0x3 0};
unsigned short i, DD0=0x80,DD1=0x80;
unsigned int tc,k=0,t1c;

void display_temp(short DD0, short DD1) 
	{
	for (i=0;i<1;i++)
		{
		TMR1IE_bit=1;
		while(!TMR1IF_bit )
			{
			PORTC = seg[DD0];
			PORTA.f3= 1; // Select Ones Digit
			PORTA.f4= 0;
			delay_ms(1);
			PORTC = seg[DD1];
			PORTA.f3= 0;
			PORTA.f4= 1; // Select Tens Digit
			delay_ms(1);
			if(PORTA.f0==1 || PORTA.f1 == 1 || PORTA.f2 == 1)
				{
				TMR1IE_bit=0;
				TMR1IF_bit=0;
				break;
				}
			}
		TMR1H=0x9E; //begin with 0
		TMR1L=0x58;
		TMR1IE_bit=0;
		TMR1IF_bit=0;
		if(PORTA.f0==1 || PORTA.f1 == 1 || PORTA.f2 == 1)
		break;
		}
	return;
	}


void main() 
	{
	int tc=0,count=0,set=0, t1,t1c=0;
	GIE_bit=1;
	TRISA=0xE7; // Set PORTA
	TRISC=0x01; // set PORTC as OUTPUT except pin0
	PORTA=0x00;
	PORTC=0x80;
	ANSEL=0x00;
	ANSELH=0x00;
	T1CON=0x01; //tmr1on bit is set
	TMR1H=0x9E; //begin with 0
	TMR1L=0x58;
	while(1)
		{
		if(PORTA.f2==1)
			{
			if(tc==0) tc=67;
			else tc--;
			}
		if(PORTA.f1==1)
			{
			if(tc==67) tc=0;
			else tc++;
			}
		DD0 = tc%10;
		DD1 = tc/10;
		display_temp(DD0,DD1);
		TMR1IE_bit=0;
		TMR1IF_bit=0;
		}
	}

It makes it easier for us old frumps to read it.

I will look at it and see if something sticks out.

Ian
 
I have changed the code and written a new one. Now the numbers displayed are changing but i need to keep the key(up or down) pressed for some time before the number changes and the jump is high e.g., changes from 16 to 18 or higher when up key is pressed. Can some one help me.
Code:
unsigned char seg[10]={0x80,0xf2,0x48,0x60,0x32,0x24,0x04,0xF0,0x00,0x30};   // this is a common anode display and pins 1-7 of PORTC are connected to A-G of 7 segment display
unsigned short i, DD0=0x80,DD1=0x80;
unsigned int tc,k=0,t1c;
sbit DD0_Set at RA2_bit;
sbit DD1_Set at RA1_bit;
void display_temp()                //displaying number in tc for 100 ms
{
    for (i=0;i<1;i++)
      {
       TMR1H=0x00;             // initialization of timer1 for 100 ms count
       TMR1L=0x00;
       TMR1IE_bit=1;               //enabling timer 1 interrupt
       while(!TMR1IF_bit )
        {
            PORTC = 0xFF;
            PORTA.f3= 1;          // Select Ones Digit. RA3 is connected to seven segement display on the right
            PORTA.f4= 0;          // RA4 is connected to seven segement display on the left
            PORTC = seg[DD0];
            delay_ms(1);          // wait for 1 ms
            PORTC = 0xFF;
            PORTA.f3= 0;
            PORTA.f4= 1;          // Select Tens Digit
            PORTC = seg[DD1];
            delay_ms(1);          // wait for 1 ms
            if(PORTA.f1 == 1 || PORTA.f2 == 1)        //if there is any input from RA1 or RA2 break from loop
             {
               TMR1IE_bit=0;   // timer 1 interrupt is disabled
               TMR1IF_bit=0;   // timer 1 interrupt flag is cleared
               break;
             }
        }
       TMR1IE_bit=0;           // timer 1 interrupt is disabled
       TMR1IF_bit=0;           // timer 1 interrupt flag is cleared
       if(PORTA.f1 == 1 || PORTA.f2 == 1)   //if there is any input from RA1 or RA2 break from loop
       break;
      }
    return;
}


void main()
{
  int tc=0,count=0,set=0, t1,t1c=0;
  GIE_bit=1;
  TRISA=0xE7;             // Set PORTA pins 3 and 4 as output and others as input
  TRISC=0x01;             // set PORTC as OUTPUT except pin0
  PORTA=0x00;             // initialize port A
  PORTC=0xFF;             // initialize port C
  ANSEL=0x00;             // set all ports digital
  ANSELH=0x00;
  T1CON=0x31;             // tmr1on bit is set and prescaler is assigned
  TMR1H=0x00;             // initialization of timer1 for 100 ms count
  TMR1L=0x00;
  display_temp();         // calling display_temp function to display DD0 and DD1 on segment on right and left respectively
  while(1)                // man program starts here
    {
     if(PORTA.f2==1)      // checking if decrease push to on button connected to portA pin 2a is pressed
       {
         if(tc==0) tc=20;
         else    tc--;
       }
     if(PORTA.f1==1)      // checking if increase push to on button connected to portA pin 1 is pressed
       {
         if(tc==20) tc=0;
         else       tc++;
       }
     DD0 = tc%10;         // digit in 1's position of tc is read to DD0
     DD1 = tc/10;         // digit in 10's position of tc is read to DD1
     display_temp();
     TMR1IE_bit=0;        // timer 1 interrupt is disabled
     TMR1IF_bit=0;        // timer 1 interrupt flag is cleared
    }
}
 
Last edited:
Can you edit your post and change [quote] and [/quote] to [code] and [/code]. You may also have to recopy your code if the formatting has been removed.

Mike.
 
Last edited by a moderator:
At first guess, you probably need to insert a delay after the button press so that you don't cycle througj the numbers too fast. How are you debouncing the buttons?
 
Last edited:
The real pic simulator is having push button, we can assign rules to it, for which we can assign either pressed high or pressed low.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top