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.

pic18f4520 timer0 interrupts c programming!

Status
Not open for further replies.

RozzaMozza

New Member
Hello ! i need to develop a C program that displays a
counting sequence on 7 segment display Um (where Um represents U1 or U2).
The counting sequence will display the members of the set of the first 16
hexadecimal numbers H:
H = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F}
in ascending numerical order. When F is reached, the display should ‘roll-over’
back to 0 and the process should be repeated.
Each character in the sequence will be displayed for T seconds. This will be
accomplished by using TIMER0 to issue an interrupt every T seconds. The
occurrence of an interrupt will cause the display to be updated with the next
character in the sequence.
The display of the counting sequence will be controlled by the push-buttons PB1
and PB2. PB2 will be configured to issue an interrupt when pressed. Pressing PBi
will start the counting sequence and its display, and pressing PBj stops the
counting and display. If PBi = PB1, then PBj = PB2 and vice versa.

This is my code so far but i am unsure as to how to use the timer to make the delays instead of normal delays
Code:
#include <p18f2550.h>
#include <timers.h>
#include <delays.h>

#pragma config FOSC = INTOSCIO_EC	
#pragma config WDT = OFF 			//watchdog Timer off
#pragma config MCLRE = OFF 		//Master clear off
#pragma config LVP = OFF //LVP off
#pragma interruptlow a_func1                   	 // says that a C function a_func1 is a low priority ISR (instruction to compiler)

void timer_isr (void);
unsigned char digit[] = {0x01,0x4F,0x12,0x06,0x4C,0x24,0x20,0x0F,0x00,0x04};

#pragma code vector=0x08                       	// locates branch instruction at interrupt vector
					//when an int occurs we go to address 8
void vector (void)				// dummy function containing an unconditional jump to the ISR
{ 
	_asm 
	GOTO timer_isr
	_endasm
}

#pragma code
#pragma interrupt timer_isr		 	// identifies and specifies that function timer_isr as the interrupt handler
		save=PROD		// saves contents of PROD register on entry to ISR, restores it on exit (contents preserved)
void timer_isr (void)				// the actual interrupt handler, acknowledges the interrupt, reloads the timer and causes the digits to be displayed
{
	INTCONbits.TMR0IF = 0;
	PORTA = digit[3];
	Delay10KTCYx(10);
}

void main(void)
{
	TRISA = 0x00;
	PORTA = digit[0];
	OpenTimer0 (TIMER_INT_ON & T0_SOURCE_INT & T0_8BIT);
	PIR1bits.ADIF=0;			//clears interrupt flag in ISR to stop program responding to same interrupt multiple times

	INTCONbits.GIE = 1; 		//enable global interrupts (always last in configuration section)
	while (1);
}
unsigned char patterns [16]=  // array of bit patterns
{ 0b11011110,                     //0
0b00000110,	           //1
0b11001101,	           //2		
0b11011001,                       //3
0b01010011,	           //4.....	
0b10011011,                       
0b10011111,
0b11010010,
0b11011111,
0b11010011,
0b11010111,
0b00011111,
0b10001110,
0b01011101,
0b10001111,
0b10000111 };


void configure_LEDS(void)
{

TRISC= 0b11101111;
TRISD= 0x00000000; // set up the tris functions

}

void display_on_Um(unsigned char display_val)   // delay and patterns
{
LATD=patterns[0];
Delay10KTCYx(Delay);
LATD=patterns[1];
Delay10KTCYx(Delay);
LATD=patterns[2];
Delay10KTCYx(Delay);
LATD=patterns[3];
Delay10KTCYx(Delay);
LATD=patterns[4];
Delay10KTCYx(Delay);
LATD=patterns[5];
Delay10KTCYx(Delay);
LATD=patterns[6];
Delay10KTCYx(Delay);
LATD=patterns[7];
Delay10KTCYx(Delay);
LATD=patterns[8];
Delay10KTCYx(Delay);
LATD=patterns[9];
Delay10KTCYx(Delay);
LATD=patterns[10];
Delay10KTCYx(Delay);
LATD=patterns[11];
Delay10KTCYx(Delay);
LATD=patterns[12];
Delay10KTCYx(Delay);
LATD=patterns[13];
Delay10KTCYx(Delay);
LATD=patterns[14];
Delay10KTCYx(Delay);
LATD=patterns[15];
Delay10KTCYx(Delay);
LATD=patterns[16];
Delay10KTCYx(Delay);

}
}
} 	
void switch_on_Um(void)

{		LATC=0b11011111;}	
void switch_off_Um(void)

{		LATC=0b11111111;
}		

void display_on_Um(unsigned char display_val)
{
    LATD=display_val;
}
 
Last edited by a moderator:
I added code tags to your post to preserve the indentation. After doing so I looked it over and am thinking it may have messed up you code. The posted code should not compile.

If I did mess up your code please repost it using the # button and put your code between the tags it generates.
 
It isn't messed up it is probably just a mistake i have made. I want to know how to change the normal delays i have entered and use timer0 interrupts to create time intervals inbetween my hex characters which are being displayed on seven seg. can you help me do this?
 
If the code is as you wrote it you have other real problems.

all variables in a c function must be at the start of a code block. Most often at the start of the function prior to any code.

Inside main you have

Code:
	INTCONbits.GIE = 1; 		//enable global interrupts (always last in configuration section)
	while (1);
}
unsigned char patterns [16]=  // array of bit patterns

This should not compile. I would make this array global and constant.
To make it global move it outside main and above all the functions.
To make it const use "const unsigned char patterns[16]=

Please post code that compiles prior to asking how to make it work.
 
Ok thank you for your advice, can you help me with the timer interrupt coding? I need to set it up so i can make time intervals inbetween each character display with the timer0 interrupt.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top