Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Toggling LED's high when Timer0 overflows

Status
Not open for further replies.

M3GAPL3X

Junior Member level 3
Joined
Jul 29, 2009
Messages
30
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,521
timer0 overflow

Hi guys,

I am trying to make a simple Timer0 program where LED's light off and than on after 1 second. I am using a 8 bit timer, 31 kHz internal LPINSRC clock with a 1:30 pre-scaler. The LED's are not toggling off and than on and than off again. They are just going from off to on almost instanteously.

Here is my code:

Code:
//LED will blink on and off every 1 second without interrupts

#include <p18f14k50.h>

//pragma codes
#pragma config FOSC = IRC //enable Internal Oscillator

//function prototypes
void init(void);

void main()
{
	TRISC = 0x00; //set PORTC's as outputs
	LATC = 0x00;

	init();
	
	while(1)
	{

	//if overflow flag is set for timer0, light up LED, reset the overflow flag bit and reset TMR0L low byte to 0x05
	if (INTCONbits.TMR0IF == 1)
		{
			LATC = 0xff;
		//	INTCONbits.TMR0IF = 0;
		    TMR0L = 0x05;
		}
		
	}
}


//Function to initalize oscillator registers
void init(void)
{
	OSCCONbits.SCS1 = 1;
	//enable 31 kHz kHz clock
	OSCCONbits.IRCF2 = 0;
	OSCCONbits.IRCF1 = 0;
	OSCCONbits.IRCF0 = 0;

	OSCCONbits.OSTS = 0; //device is running from internal oscillator
	OSCTUNEbits.INTSRC = 0; //31 kHz device is derived directly from LFINTOSC
	
	//configure Timer0
	T0CONbits.T08BIT = 1; //Timer0 configured as an 8 bit register

	//Internal Instruction Cycle Clocka
	T0CONbits.T0CS = 0;
	
	T0CONbits.PSA = 0;
	//configure prescale value of 1:32
	T0CONbits.T0PS2 = 0;
	T0CONbits.T0PS1 = 1;
	T0CONbits.T0PS0 = 0;
	
	//load TMR0L with a value of 05
    TMR0L = 0x05;
	INTCONbits.TMR0IF = 0;

	//enable TMR0 after enabling all related registers
	T0CONbits.TMR0ON = 1; 
}
	
	
	
/****************NOTES*************
With a 31 kHz clock, frequency is 31 kHz / 4 = about 8 kHz. 8 kHz corresponds
to .125 ms per instruction so 8000 instructions per second. With a 32 bit pre-scalar,
instructions per ms is 4. Therefore, about 250 counts per second. An 8-bit timer can hold 
a max value fo 256, so load TMR0L with 0x05.
*/

Any help would be appreciated
 

millwood

Advanced Member level 3
Joined
Jul 2, 2009
Messages
734
Helped
80
Reputation
164
Reaction score
35
Trophy points
1,308
Activity points
5,088
timer0 pragma

here is my code for flashing an LED based on timer0 on 12F675. It uses interrupts but the same thought process applies.

hope it helps.

Code:
//preload the timer with an offset to help adjust timing

#include <htc.h>

__CONFIG (MCLRDIS & UNPROTECT & BORDIS & WDTDIS & INTIO & PWRTEN);

//hardware configuration
#define nLED		2			//LED on gpio2
#define MaxCnt		1000000 / 250			//maximum count to flip the LED
#define T0Offset	(255-250+3)	//timer0 offset so that the timer overflows in exactly 250 cycles (=250us)

unsigned char sGPIO;	//shadow gpio

void interrupt LED(void) {
	static unsigned int T0Counter=MaxCnt;	//initialize t0 counter. "static" to retain value
	T0IF=0;					//clear the interrupt flag
	TMR0+=T0Offset;			//load the timer with the offset
	T0Counter-=1;
	if (T0Counter==0) {
		T0Counter=MaxCnt;		//reset T0Counter
		sGPIO ^=(1<<nLED);		//flash nLED pin
	}
}

void
main(void)
{
	TRISIO=~(1<<nLED);		//all gpio pins to input other than nLED and nButtonLED
	OPTION=0b11011000;		//setting up the options register;
	//       1         		//GPPU: pull-up disabled
	//        1 			//INTEDG: interrupt on rising edge
	//         0			//T0CS: timer0 source select - internal clock cycle
	//          1   		//T0SE: timer0 edge select - interrupt on high-to-low transisiton
	//           1			//PSA: prescaler selected to watchdog
	//            000		//PS0..2: prescaler rate select - 000=1
	
	//GPPU=1; INTEDG=1;T0CS=0;T0SE=1;PSA=0;

	sGPIO=0;				//clear gpio pins
	
	T0IE=1;					//enable timer 0 interrupt
	ei();					//enable global interrupt. = GIE=1;
	
	while (1){
		//TODO Auto-generated main function
		GPIO=sGPIO;
	}
}
 

    M3GAPL3X

    Points: 2
    Helpful Answer Positive Rating

M3GAPL3X

Junior Member level 3
Joined
Jul 29, 2009
Messages
30
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,521
sgpio programm

Thanks! I'll take a look at it. I actually figured it out.

Here is my final code if anyone else wants it:

Code:
//LED will blink on and off every 1 second without interrupts

#include <p18f14k50.h>

//pragma codes
#pragma config FOSC = IRC //enable Internal Oscillator

//function prototypes
void init(void);

void main()
{
	TRISC = 0x00; //set PORTC's as outputs
	init();
	while (1)
	{
	//if overflow flag is set for timer0, light up LED, reset the overflow flag bit and reset TMR0L low byte to 0x05
	if (INTCONbits.TMR0IF == 1)
		{
			LATC ^= 0xff;
			INTCONbits.TMR0IF = 0;
		    TMR0L = 0x00;
		}
		
	}
}


//Function to initalize oscillator registers
void init(void)
{
	OSCCONbits.SCS1 = 1;
	//enable 31 kHz kHz clock
	OSCCONbits.IRCF2 = 0;
	OSCCONbits.IRCF1 = 0;
	OSCCONbits.IRCF0 = 0;

	OSCCONbits.OSTS = 0; //device is running from internal oscillator
	OSCTUNEbits.INTSRC = 0; //31 kHz device is derived directly from LFINTOSC
	
	//configure Timer0
	T0CONbits.T08BIT = 1; //Timer0 configured as an 8 bit register

	//Internal Instruction Cycle Clocka
	T0CONbits.T0CS = 0;
	
	T0CONbits.PSA = 0;
	//configure prescale value of 1:32
	T0CONbits.T0PS2 = 1;
	T0CONbits.T0PS1 = 0;
	T0CONbits.T0PS0 = 0;
	
	//load TMR0L with a value of 05
    TMR0L = 0x00;
	INTCONbits.TMR0IF = 0;

	//enable TMR0 after enabling all related registers
	T0CONbits.TMR0ON = 1; 
}
	
	
	
/****************NOTES*************
With a 31 kHz clock, frequency is 31 kHz / 4 = about 8 kHz. 8 kHz corresponds
to .125 ms per instruction so 8000 instructions per second. With a 32 bit pre-scalar,
instructions per ms is 4. Therefore, about 250 counts per second. An 8-bit timer can hold 
a max value fo 256, so load TMR0L with 0x05.
*/
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top