Hello All,
I am completely new to programming PIC and C language. I am having trouble using the Timer0 on my pic16f690. I am using the MPLAB X IDE and compiler.
I am simply trying to get an LED to blink on and off at 2 second intervals. My code is below. If someone could please advise me as to what i am doing wrong, I would be most appreciative.
I am completely new to programming PIC and C language. I am having trouble using the Timer0 on my pic16f690. I am using the MPLAB X IDE and compiler.
I am simply trying to get an LED to blink on and off at 2 second intervals. My code is below. If someone could please advise me as to what i am doing wrong, I would be most appreciative.
Rich (BB code):
#include <xc.h>
#include <stdint.h>
#pragma config MCLRE = OFF, CP = OFF, CPD = OFF, BOREN = OFF, WDTE = OFF
#pragma config PWRTE = OFF, FOSC = INTRCIO
#define _XTAL_FREQ 4000000 //osc frequency
//#define __delay_ms(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000.0)))
int count = 0;
void TimerBlinkLED()
{
uint8_t cnt_8ms; //counter increments every 8ms
while(1)
{
cnt_8ms = 0;
while (cnt_8ms < 250) //250 * 8ms = 2 second count
{
TMR0 = 0; //clear Timer0
while(TMR0 < 250) //wait for 8ms to pass (.008ms / .000032us)
{ //do nothing until timer is > 8ms
}
++cnt_8ms; //now timer is > 8ms so add 1 to counter variable
}
if (PORTC == 0b00000000)
{
PORTC = 0b00001000;
}
else
{
PORTC = 0b00000000;
}
}
}
void main()
{
ADCON1 = 0x0F; //deactivate A-D conveter
//CMCON = 0x07; //deactivate comparator module
TRISC = 0b00000000; //set Input or 0utput bits
PORTC = 0x00; //clear port A
OPTION_REGbits.T0CS = 0; //select timer mode
OPTION_REGbits.PSA = 0; //assign presscaler to Timer0
OPTION_REGbits.PS = 0b100; //assign prescaler 32us
while(1)
{
TimerBlinkLED();
}
}
Last edited by a moderator: