Continue to Site

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.

Blinking LED Delay Issue - PIC 16f1847

Status
Not open for further replies.

MezmerizedMonkey

Newbie level 5
Newbie level 5
Joined
Feb 9, 2013
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,342
Hi,

It's my first post here and I'm just starting out with MCUs. I managed to get an LED to blink (yay!), however, at an uneven rate when it's programmed for an even one. I've tried everything that came to mind without success. It's a bit frustrating...

I'm running:
MPLAB X v1.51
XC 8 v 1.12 Compiler
PicKit 2 v2.61 IPE with support for:
PicCircuit ICP02v2 Programmer
PIC 16f1847

My code:

Code:
#include <pic16f1847.h>
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>

//Internal clock setting (OSC of 32Mhz/4)
#define _XTAL_FREQ 800000

void main ()
{
    TRISA = 0x00;          //PORTA as an output
while (1)                 //infinite loop
 {
    PORTA = 0x0f;         //PORTA high
 __delay_ms(1000);
    PORTA = 0x00;        //PORTA low
 __delay_ms(1000);
  }
}

As I mentioned, this does work but poorly:
The LED seems to be on for 3/4 of the loop, off for 1/4 and I have no idea why when both delays are set for 1000ms.
Can anyone help me out?
 

Hi,

Well done, its always great to see things start for the first time.

Cannot help with the C code detail, but most of the Ports default to Analogue Inputs at power on, suggest you turn then to Digital before making them Outputs using ANSEL
 

Look at the datasheet!!!
EXAMPLE 12-1: INITIALIZING PORTA
Code:
BANKSEL PORTA ;
CLRF PORTA ;Init PORTA
BANKSEL LATA ;Data Latch
CLRF LATA ;
BANKSEL ANSELA ;
CLRF ANSELA ;digital I/O
BANKSEL TRISA ;
MOVLW 0Ch ;Set RA<3:2> as inputs
MOVWF TRISA ;and set RA<7:4,1:0>
;as outputs

Clear LATA and ANSEL
 

I added this bit without luck:
Code:
 LATA = 0x00;
 ANSELA = 0x00;     //Turning analog ?input? function off; now digital I/O
 TRISA = 0x00;     //Setting PORTA as output

I don't really understand assembly very well but what I could tell from the that example and the datasheet, setting ANSELA low would set my port for digital input. The datasheet also says that ANSELA only affects inputs but can prevent abnormal behavior when setting pin states. Why would I worry about inputs? Thanks for your help.
 
Last edited:

My code in Hitech C is working...

Code:
// HI-TECH C Compiler for PIC10/12/16 MCUs V9.83
#include <htc.h>
//#include <xc.h>
//#include <pic16f1847.h>
//#include <stdio.h>
//#include <stdlib.h>

__CONFIG ( 1 & FOSC_INTOSC & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & CP_OFF & CPD_OFF & BOREN_OFF & CLKOUTEN_OFF & IESO_OFF & FCMEN_OFF );
__CONFIG ( 2 & WRT_OFF & PLLEN_OFF & STVREN_OFF & BORV_LO & LVP_ON );

//Internal clock setting (OSC of 32Mhz/4)
#define _XTAL_FREQ 800000

void main ()
{
    TRISA = 0x00;				//PORTA as an output
	LATA = 0x00;
 	ANSELA = 0x00;  
	while (1)					//infinite loop
 	{
    	PORTA = 0x0f;			//PORTA high
 		__delay_ms(1000);
    	PORTA = 0x00;			//PORTA low
 		__delay_ms(1000);
 	}
}
 

In the XC8 compiler, try to replace the _delay_ms(1000) with _delay(1000)

the first gives a delay in milisec. and the second gives the delay in cycles.

Of course it will not give you the same delay but just see if it symmetric.
 

Success! It was the configuration that I was missing. By trial and error, disabling the watchdog timer fixed it (I'm not sure why). I had a config earlier but removing it made no difference so I left it out.
Thank you for your help everyone!
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top