All About Circuits Forum  

Go Back   All About Circuits Forum > Software, Microcomputing, and Communications Forums > Embedded Systems and Microcontrollers

Notices

Embedded Systems and Microcontrollers Discussion forum for projects and working with embedded systems and microcontrollers (FPGAs, PICs, AVRs). Get help with hardware issues and embedded programming.

Reply   Post New Thread
 
Thread Tools Display Modes
  #1  
Old 07-31-2011, 06:48 PM
jj_alukkas's Avatar
jj_alukkas jj_alukkas is offline
Senior Member
 
Join Date: Jan 2009
Location: Ernakulam,Kerala,India (+5.5)
Posts: 751
Default PIC Programming in Hi-Tech C

Hello,

Im new to PIc programming and as a part of the interest to flash some projects onto pic's, I bought a PICkit2 programmer. So now Im trying to learn how to write programs on my own in Hi-tech C. Im using MPlab 8.53 and hi-tech PICC 9.7 modules and a PIC16F628A chip and the internal OSC. The problem I am facing is that I can flash my 16F628 well enough and blink my led's but it acts totally wierd.

Code:
#include <HTC.H>
#define _XTAL_FREQ 20000000 

__CONFIG(INTIO & WDTDIS & PWRTDIS & BORDIS & LVPDIS );

void main()

{
    TRISA=0;
    TRISB=0;
    unsigned char i=0;


while (1)
{
i=0;
while (i<2){
PORTA = 0xFF;
_delay(100000);
_delay(100000);
PORTA = 0x00;
_delay(100000);
_delay(100000);
i=i+1;
}
i=0;
while (i<2){
PORTB = 0xFF;
_delay(100000);
PORTB = 0x00;
_delay(100000);
i=i+1;
}
PORTB = 0x00;
}
}
Select All
The port A led's blink 2 times, then the port B blinks Twice and whole repeats..

However if I change the delay to _delay(1000), it doesnt blink at all.. and _delay_ms(100) gives unidetified errors.. how do I fix the delay problems??
__________________
Joseph Alukka

Last edited by jj_alukkas; 07-31-2011 at 06:56 PM.
Reply With Quote
  #2  
Old 07-31-2011, 07:24 PM
nerdegutta's Avatar
nerdegutta nerdegutta is offline
Senior Member
 
Join Date: Dec 2009
Location: (GMT +1)
Posts: 1,667
Blog Entries: 6
Default

Hi,

I'm also using the PIC16F628 MCU for my projects. The following program flashes all the LEDS on PORT A and B.

Code:
/*

Program:    Skeleton program        
Description:To get going
PIC:        PIC 16 F 628
IDE:        MPLAB 
Compiler:    High tech C
*/

#include <htc.h>

#define _XTAL_FREQ 4000000

/* Configuration */

__CONFIG    (WDTDIS &
            PWRTEN &
            MCLREN &
            BOREN &
            LVPDIS &
            DATUNPROT &
            UNPROTECT &
            INTIO);

/* Prototyping the functions */
void lightLED();
void turnoffLED();


/* Global variables */


/* Functions */
void lightLED()
{
    PORTA = 0b11111111;
    PORTB = 0b11111111;

__delay_ms(5000);

}

void turnoffLED()
{
    PORTA = 0b00000000;
    PORTB = 0b00000000;

__delay_ms(5000);

}


/* Main program */
void main()
{
TRISA = 0b00000000;    // Setting all bits on port a to output
TRISB = 0b00000000;    // Setting all bits on port b to output

PORTA = 0b00000000; // Setting all bits on port a to LOW
PORTB = 0b00000000;    // Setting all bits on port b to LOW

CMCON = 0x07;    // Disabling the analogue comparators



while (1)
{
lightLED();
turnoffLED();

} // end while

}  //end main
Select All
Depending on your circuit, that is...

Don't know if this is any good for you, but its usually how I start.
__________________
- nerdegutta

On the other hand - you have different fingers.
Reply With Quote
  #3  
Old 07-31-2011, 07:54 PM
jj_alukkas's Avatar
jj_alukkas jj_alukkas is offline
Senior Member
 
Join Date: Jan 2009
Location: Ernakulam,Kerala,India (+5.5)
Posts: 751
Default

Of course it would be usefull because I can 'know' all the ways to do something and then use whats best for the situation. I have a doubt though, why is this used?
CMCON = 0x07; // Disabling the analogue comparators its not enabled in the beginning??
__________________
Joseph Alukka
Reply With Quote
  #4  
Old 07-31-2011, 08:46 PM
ErnieM's Avatar
ErnieM ErnieM is online now
Senior Member
 
Join Date: Apr 2011
Location: Lon Guyland, Noo Yawk
Posts: 5,173
Default

Quote:
Originally Posted by jj_alukkas View Post
Of course it would be usefull because I can 'know' all the ways to do something and then use whats best for the situation. I have a doubt though, why is this used?
CMCON = 0x07; // Disabling the analogue comparators its not enabled in the beginning??
Actually, they are already enabled and on. PIC pins that have both analog and digital functions default to analog on reset.
Reply With Quote
  #5  
Old 08-01-2011, 04:40 AM
AlexR's Avatar
AlexR AlexR is offline
Senior Member
 
Join Date: Jan 2008
Location: Australia (UTC+10)
Posts: 735
Default

Using the internal 4MHz oscillator the instruction _delay(1000) will only give you a 1mSec delay. This is much too short for the eye to see hence you will not see any LED blinks.

The syntax for the mSec delay command is __delay_ms(100); with 2 leading underscores rather than just one as used in the simple instruction cycle delay routine.
__________________
Alex

"A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools."-Douglas Adams
Reply With Quote
  #6  
Old 08-01-2011, 06:49 AM
AlexR's Avatar
AlexR AlexR is offline
Senior Member
 
Join Date: Jan 2008
Location: Australia (UTC+10)
Posts: 735
Default

One further point.
If you use the __delay_ms(); command you should correct the line
Code:
#define _XTAL_FREQ 20000000
Select All
The __delay_ms(); routine uses the _XTAL_FREQ information to calculate the number of machine cycles required to produce a given delay so as you are using the chip's internal oscillator your clock frequency will be 4 MHz and that line should read
Code:
#define _XTAL_FREQ 4000000
Select All
__________________
Alex

"A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools."-Douglas Adams
Reply With Quote
  #7  
Old 08-01-2011, 06:58 AM
jj_alukkas's Avatar
jj_alukkas jj_alukkas is offline
Senior Member
 
Join Date: Jan 2009
Location: Ernakulam,Kerala,India (+5.5)
Posts: 751
Default

Quote:
Originally Posted by ErnieM View Post
Actually, they are already enabled and on. PIC pins that have both analog and digital functions default to analog on reset.
Thank you for that, so can I vary the brightness of an led by varying the output level on just one pin in analogue mode? or do I need to use 4 pins tied up with resistors and connected to led and run a binary counter to do that?
Sorry If that was a dumb question :P
__________________
Joseph Alukka
Reply With Quote
  #8  
Old 08-01-2011, 07:05 AM
jj_alukkas's Avatar
jj_alukkas jj_alukkas is offline
Senior Member
 
Join Date: Jan 2009
Location: Ernakulam,Kerala,India (+5.5)
Posts: 751
Default

Quote:
Originally Posted by AlexR View Post
One further point.
If you use the __delay_ms(); command you should correct the line
Code:
#define _XTAL_FREQ 20000000
Select All
The __delay_ms(); routine uses the _XTAL_FREQ information to calculate the number of machine cycles required to produce a given delay so as you are using the chip's internal oscillator your clock frequency will be 4 MHz and that line should read
Code:
#define _XTAL_FREQ 4000000
Select All
Wow wow.. finally Got __delay_ms() working with this! Thank You a lot. Actually I was trying with 20Mhz clock and it keeps throwing errors on compile, so I was about to post a screenshot question for your first post, then saw this reply!! Now its working, thanks a lot. I was trying to do this since a day and all websites just give info that the xtal frq must be defined to use it not that it shud be set to 4mhz..!! Thank you!!

Could you also tell me if I can test every change i make to the program by flashing it onto the pic, I mean, is it safe, or will I end the read write cycles allowed within a week with a 100 flashes a day? Or should I use some simulators instead? Chip is a 16F628a
__________________
Joseph Alukka
Reply With Quote
  #9  
Old 08-01-2011, 08:02 AM
t06afre's Avatar
t06afre t06afre is offline
Senior Member
 
Join Date: May 2009
Posts: 5,939
Default

Quote:
Originally Posted by jj_alukkas View Post
Could you also tell me if I can test every change i make to the program by flashing it onto the pic, I mean, is it safe, or will I end the read write cycles allowed within a week with a 100 flashes a day? Or should I use some simulators instead? Chip is a 16F628a
You can debug in MPLAB. But I do not almost sure you can not debug the 16f628 using the PICKIT hardware debugger. Without any extra hardware named a ICD header. An extra piece of hardware. Some PICs in the 16F series can be debugged directly by PICKIT 2. These are
Quote:
PIC16F87, 88
PIC16F818, 819
PIC16F870, 871, 872, 873, 874, 876, 877
PIC16F873A, 874A, 876A, 877A
PIC16F882, 883, 884, 886, 887
PIC16F913, 914, 916, 917
PIC16F946
But I think all 18F series PICs that are compatible with PICKIT 2 can be debugged without any extra hardware.
You can find info about it here
http://www.microchip.com/stellent/id...cName=en027813
Many errors can also be tracked by the software simulator in MPLAB. You will find my sticky her useful http://forum.allaboutcircuits.com/sh...ad.php?t=44852
I would not worry about about your flash. Your chip can take about
100000 write Flash cycles.
I would also have changed to a newer version of HI-TECH C. In version 9.71Aa they changed the delay function so it could take MUCH larger numbers in the _delay() functions. The __delay_ms() functions use the latter functions. Now seconds delays is not a problem.
Reply With Quote
  #10  
Old 08-01-2011, 09:11 AM
jj_alukkas's Avatar
jj_alukkas jj_alukkas is offline
Senior Member
 
Join Date: Jan 2009
Location: Ernakulam,Kerala,India (+5.5)
Posts: 751
Default

Quote:
Originally Posted by t06afre View Post
You can debug in MPLAB. But I do not almost sure you can not debug the 16f628 using the PICKIT hardware debugger. Without any extra hardware named a ICD header. An extra piece of hardware. Some PICs in the 16F series can be debugged directly by PICKIT
Ok, Now I understand why mplab throws an error when i select debugger>select tool>pickit2 !! but there is an extra 6th pin on the ICSP header on my pickit2 which is not used but goes directly to pin 6 of the 18F on the PICKIT, any Idea what it is for?


Quote:
But I think all 18F series PICs that are compatible with PICKIT 2 can be debugged without any extra hardware.
It think for now I will be sticking to the 16F628 as its the only one I have in hand and is very cheap and small, but have to try on 18F's once I get a hold of all this.

Quote:
Many errors can also be tracked by the software simulator in MPLAB. You will find my sticky her useful http://forum.allaboutcircuits.com/sh...ad.php?t=44852
I checked that thread yesterday and downloaded that very important file you had linked, thanks for that, but Im not quite getting a hold of how to show the port led's while in the SIM mode..

Quote:
I would also have changed to a newer version of HI-TECH C. In version 9.71Aa they changed the delay function so it could take MUCH larger numbers in the _delay() functions. The __delay_ms() functions use the latter functions. Now seconds delays is not a problem.
Yes, I tried that and something got messed up totally. It stopped recognizing my fuses and showed each of my fuse bits as an individual error, but I should try again because I could save a couple of lines of codes with that update.
__________________
Joseph Alukka
Reply With Quote
Reply   Post New Thread

Tags
, ,


Similar Threads
Thread Thread Starter Forum Replies Last Post
programming PIC 16F677 and PIC 18F67J60 with transceiver pierokun Embedded Systems and Microcontrollers 0 03-09-2011 01:40 PM
Whats the cheapest way to start pic programming? reaman4ever Embedded Systems and Microcontrollers 26 11-05-2009 04:25 PM
PIC programming for a newbie mrtom Embedded Systems and Microcontrollers 10 10-27-2008 03:41 AM
PIC programming algorithm Vicktor Embedded Systems and Microcontrollers 7 12-26-2007 05:47 PM

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 02:08 AM.


User-posted content, unless source quoted, is licensed under a Creative Commons Public Domain License.
Powered by vBulletin
Copyright ©2000 - 2014, vBulletin Solutions, Inc.