Timer0 One Second Delay PORTC.RCO (Beginner)

General discussion on mikroElektronika website & forums.
Post Reply
Author
Message
rasterman101
Posts: 22
Joined: 09 Nov 2013 17:26

Timer0 One Second Delay PORTC.RCO (Beginner)

#1 Post by rasterman101 » 28 Dec 2013 03:25

Using the below code. Is it possible to turn on RC0 for one second and turn it off for one second?

// Timer0
// Prescaler 1:32; TMR0 Preload = 3036; Actual Interrupt Time : 1
// Turn on PORTC.RC0 on for one second and off for one second.

void InitTimer0(){
T0CON = 0x84;
TMR0H = 0x0B;
TMR0L = 0xDC;
GIE_bit = 1;
TMR0IE_bit = 1;
}

void Interrupt(){
if (TMR0IF_bit) {
TMR0IF_bit = 0;
TMR0H = 0x0B;
TMR0L = 0xDC;
}
}

main {
ANSELC = 0;
}

tpetar
Posts: 593
Joined: 27 Apr 2012 18:44
Location: Pancevo, Serbia

Re: Timer0 One Second Delay PORTC.RCO (Beginner)

#2 Post by tpetar » 28 Dec 2013 08:13

Hi,

Download and use Timer Calculator software
http://www.mikroe.com/timer-calculator/

Code: Select all

ANSELC   = 0;  // Set PortC as digital
TRISC    = 0;  // Set PortC as Output
.
.
.
LATC.F0 = ~LATC.F0; // Alternately change state of RC0

Best regards,
Peter

rasterman101
Posts: 22
Joined: 09 Nov 2013 17:26

Re: Timer0 One Second Delay PORTC.RCO (Beginner)

#3 Post by rasterman101 » 28 Dec 2013 16:55

Thank you. I did use the Timer Calculator software.
I am apparently incorrectly understanding how the Interrupt works. Why would the below not work?

// Timer0
// Prescaler 1:32; TMR0 Preload = 3036; Actual Interrupt Time : 1
// Turn on PORTC.RC0 on for one second and off for one second.

void InitTimer0(){
T0CON = 0x84;
TMR0H = 0x0B;
TMR0L = 0xDC;
GIE_bit = 1;
TMR0IE_bit = 1;
}

void Interrupt(){
if (TMR0IF_bit) {
TMR0IF_bit = 0;
TMR0H = 0x0B;
TMR0L = 0xDC;

LATC.F0 = ~LATC.F0;
}
}

main {
ANSELC = 0;
TRISC = 0;
while(1) {

}
}

lejeff
Posts: 218
Joined: 04 Dec 2011 16:10
Location: France

Re: Timer0 One Second Delay PORTC.RCO (Beginner)

#4 Post by lejeff » 28 Dec 2013 16:59

Your main code doesn't call the sub procedure "InitTimer0"
So it cannot works

Read the examples in the compiler folder :wink:

rasterman101
Posts: 22
Joined: 09 Nov 2013 17:26

Re: Timer0 One Second Delay PORTC.RCO (Beginner)

#5 Post by rasterman101 » 29 Dec 2013 01:23

Thank you for the assistance. I had to type it in different from the Timer Calculator copy/paste but it does work. I guess I will do more reading to figure out the timer. Thank you again.


//Timer0
//Prescaler 1:32; TMR0 Preload = 3036; Actual Interrupt Time : 1
// PIC18F45K22 1 Second Timer



int num = 0;
int second = 0;
int minute = 26;
int hour = 14;
void Interrupt(){
if (TMR0IF_bit){
TMR0IF_bit = 0;
TMR0H = 0x0B;
TMR0L = 0xDC;
++num;
++second;

}
}

main() {
ANSELB = 0;
ANSELC = 0;
ANSELD = 0;

TRISB = 0;
TRISC = 0;
TRISD = 0;
LATB =0;
LATC = 0;
LATD = 0;

T0CON = 0x84;
TMR0H = 0x0B;
TMR0L = 0xDC;
GIE_bit = 1;
TMR0IE_bit = 1;

while(1) {

if(num>=1) {
LATB = hour;
LATC = minute;
LATD = second;

num = 0;
}
if(second == 61) {
second = 0;
++minute;
if(minute == 61) {
minute = 0;
++hour;
}
if(hour == 25) {
hour = 0;
}
}
}
}

tpetar
Posts: 593
Joined: 27 Apr 2012 18:44
Location: Pancevo, Serbia

Re: Timer0 One Second Delay PORTC.RCO (Beginner)

#6 Post by tpetar » 29 Dec 2013 07:40

Hi,

For posting code in forum please use code tags, this make better view of your code, also is good to post topic in right thread group.

Read uC datasheet chapters about timers:

PIC18F45K22 datasheet
http://www.microchip.com/wwwproducts/De ... e=en546239


Additionaly read this (based on PIC16F887 but can give you basic):

Chapter 4: Timers
http://www.mikroe.com/chapters/view/5/chapter-4-timers/


Best regards,
Peter

Post Reply

Return to “Website & Forums General Discussion”