CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

PIC16F526 - Hello World

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
dossdev



Joined: 08 Jul 2009
Posts: 24

View user's profile Send private message

PIC16F526 - Hello World
PostPosted: Wed Jul 08, 2009 8:39 am     Reply with quote

Hello all,

I'm having trouble getting started with the 16F526. The two output pins don't seem to be acting as anticipated. As you can see I've tried several different ways to enable the internal 4 MHz RC osc as well as a few different fuse options. My compiler is PCB V4.083.

TIA
Code:

#include <16F526.h>
#fuses INTRC_IO, NOWDT, NOPROTECT, MCLR, IOSCFS_4
//#fuses INTRC_IO, NOWDT, NOPROTECT, NOMCLR
//#use delay(clock = 4000000, RESTART_WDT)
#use delay(clock = 4000000)
//#use delay(clock=4M, oscillator=4M)

//#use fast_io(B)
//#use fast_io(C)

//#use standard_io(C)

//**** I/O DEFINITIONS ****/

#define B0            PIN_B0
#define B1            PIN_B1
#define B2            PIN_B2
#define B3            PIN_B3   
#define B4            PIN_B4
#define B5            PIN_B5
                         
#define C0            PIN_C0
#define C1            PIN_C1
#define C2            PIN_C2
#define C3            PIN_C3
#define C4            PIN_C4
#define C5            PIN_C5

void main() {

set_tris_b(0x3f);                // SET PORT B TO (xxii iiii) < o= out, i=in
set_tris_c(0x23);                // SET PORT C TO (xxio ooii) < o= out, i=in

SETUP_ADC(ADC_OFF);
SETUP_ADC_PORTS(NO_ANALOGS);

setup_comparator(NC_NC_NC_NC);

   OUTPUT_LOW(C3);
   OUTPUT_LOW(C4);

   while(TRUE) {
   OUTPUT_LOW(C3);
   OUTPUT_LOW(C4);
   delay_ms(500);
   OUTPUT_HIGH(C3);
   OUTPUT_HIGH(C4);
     
   }
}
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

PostPosted: Wed Jul 08, 2009 8:48 am     Reply with quote

Try this:

Code:

#include <16F526.h>
#fuses INTRC_IO, NOWDT, NOPROTECT, NOMCLR
#use delay(clock = 4000000)

//**** I/O DEFINITIONS ****/

#define B0            PIN_B0
#define B1            PIN_B1
#define B2            PIN_B2
#define B3            PIN_B3   
#define B4            PIN_B4
#define B5            PIN_B5
                         
#define C0            PIN_C0
#define C1            PIN_C1
#define C2            PIN_C2
#define C3            PIN_C3
#define C4            PIN_C4
#define C5            PIN_C5


void main() {

   
   while(TRUE) {
   OUTPUT_LOW(C3);
   OUTPUT_LOW(C4);
   delay_ms(500);
   OUTPUT_HIGH(C3);
   OUTPUT_HIGH(C4);
   delay_ms(500);   
   }
}

Notice the delay_ms(500) after the OUTPUT_HIGH(C4). Otherwise C3 and C4 will only be high for a few instruction cycles at the most.
dossdev



Joined: 08 Jul 2009
Posts: 24

View user's profile Send private message

PostPosted: Wed Jul 08, 2009 9:09 am     Reply with quote

Thanks for the quick reply mkung. Tried your code (after adding the required set_tris's) - still not working. Both C3 & C4 outputs (driving LEDs) stay HI.
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

PostPosted: Wed Jul 08, 2009 9:17 am     Reply with quote

dossdev wrote:
Thanks for the quick reply mkung. Tried your code (after adding the required set_tris's) - still not working. Both C3 & C4 outputs (driving LEDs) stay HI.

I purposely got rid of the fast_io statements so that you do not need to use the set_tris statements. The compiler takes care of the direction of the pins and when you use output_high() it automatically knows to set the pins as outputs. Did you try my program as writen (without the set_tris() statements)? If it doesn't work try just setting the 2 output pins low and see whether something is stuck somewhere.
dossdev



Joined: 08 Jul 2009
Posts: 24

View user's profile Send private message

PostPosted: Wed Jul 08, 2009 9:48 am     Reply with quote

OK, I wasn't aware that the tris would be automatically taken care of. Good to know. Yes, I did try your code exactly as written. Both outputs stayed LO and did not toggle as written. I then changed the code to force one of the outputs LO and another output HI. Both outputs went LO/HI as expected. It seems as if either the while(true) or the delay_ms() is not working.


Here's the listing for your code:

Code:
CCS PCB C Compiler, Version 4.083, 46448               08-Jul-09 10:27

               Filename: main.lst

               ROM used: 53 words (5%)
                         Largest free fragment is 512
               RAM used: 4 (16%) at main() level
                         5 (20%) worst case
               Stack:    1 locations

*
0000:  MOVWF  05
0001:  GOTO   015
0002:  GOTO   003
.................... #include <16F526.h>
.................... //////// Standard Header file for the PIC16F526 device ////////////////
.................... #device PIC16F526
.................... #list
.................... 
.................... #fuses INTRC_IO, NOWDT, NOPROTECT, NOMCLR
.................... #use delay(clock = 4000000)
*
0003:  MOVF   0B,W
0004:  BTFSC  03.2
0005:  GOTO   014
0006:  MOVLW  01
0007:  MOVWF  08
0008:  CLRF   07
0009:  DECFSZ 07,F
000A:  GOTO   009
000B:  DECFSZ 08,F
000C:  GOTO   008
000D:  MOVLW  4A
000E:  MOVWF  07
000F:  DECFSZ 07,F
0010:  GOTO   00F
0011:  GOTO   012
0012:  DECFSZ 0B,F
0013:  GOTO   006
0014:  RETLW  00
.................... 
.................... //**** I/O DEFINITIONS ****/
.................... 
.................... #define B0            PIN_B0
.................... #define B1            PIN_B1
.................... #define B2            PIN_B2
.................... #define B3            PIN_B3   
.................... #define B4            PIN_B4
.................... #define B5            PIN_B5
....................                           
.................... #define C0            PIN_C0
.................... #define C1            PIN_C1
.................... #define C2            PIN_C2
.................... #define C3            PIN_C3
.................... #define C4            PIN_C4
.................... #define C5            PIN_C5
.................... 
.................... 
.................... void main() {
0015:  CLRF   04
.................... 
.................... 
....................    while(TRUE) {
....................    OUTPUT_LOW(C3);
*
0018:  BCF    09.3
0019:  MOVF   09,W
001A:  TRIS   7
001B:  BCF    07.3
....................    OUTPUT_LOW(C4);
001C:  BCF    09.4
001D:  MOVF   09,W
001E:  TRIS   7
001F:  BCF    07.4
....................    delay_ms(500);
0020:  MOVLW  02
0021:  MOVWF  0A
0022:  MOVLW  FA
0023:  MOVWF  0B
0024:  CALL   003
0025:  DECFSZ 0A,F
0026:  GOTO   022
....................    OUTPUT_HIGH(C3);
0027:  BCF    09.3
0028:  MOVF   09,W
0029:  TRIS   7
002A:  BSF    07.3
....................    OUTPUT_HIGH(C4);
002B:  BCF    09.4
002C:  MOVF   09,W
002D:  TRIS   7
002E:  BSF    07.4
....................    delay_ms(500);   
002F:  MOVLW  02
0030:  MOVWF  0A
0031:  MOVLW  FA
0032:  MOVWF  0B
0033:  CALL   003
0034:  DECFSZ 0A,F
0035:  GOTO   031
....................    }
0036:  GOTO   018
.................... }
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

PostPosted: Wed Jul 08, 2009 10:16 am     Reply with quote

Are you running this part with a 5V supply?
Are you measuring the output with a voltmeter or just trying to see whether your led is flashing?

Try replacing main() with this and see what happens.

Code:

void main() {

OUTPUT_LOW(C3);
OUTPUT_LOW(C4);

   while(TRUE)
   {
   delay_ms(500);
   OUTPUT_TOGGLE(C3);
   OUTPUT_TOGGLE(C4); 
   }
}
dossdev



Joined: 08 Jul 2009
Posts: 24

View user's profile Send private message

PostPosted: Wed Jul 08, 2009 10:43 am     Reply with quote

Tried your new main(), both LEDs stayed off. Vdd=5V and using LEDs as indicators. Also observed with a scope. No toggling.

My circuit is minimal and consists of a '526, 2 LEDs/resistors, and a 10K pullup on MCLR.

Is there a quick way to see if the internal osc is running?
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

PostPosted: Wed Jul 08, 2009 11:13 am     Reply with quote

If you change your fuses to:

Code:

#fuses INTRC, NOWDT, NOPROTECT, NOMCLR


Then stick a scope probe on RB4/OSC2/CLKOUT pin (either pin 3 if you have the DIP or pin 2 if you have the QFN you should see the clock signal.

DId you remember to include the 0.1uF capacitor on the Vdd pin?
dossdev



Joined: 08 Jul 2009
Posts: 24

View user's profile Send private message

PostPosted: Wed Jul 08, 2009 11:24 am     Reply with quote

RB3/CLKOUT is oscillating @ 2 MHz which is according to the datasheet should be Fosc/4. That would mean the IntRC is running at 8 MHz instead of 4 MHz. The #use delay(clock = 4000000) should set the osc to 4 MHz. What am I missing??
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

PostPosted: Wed Jul 08, 2009 12:34 pm     Reply with quote

I don't know. I am stumped. I don't have your part or your compiler. I tried the same program on an 18F2630 using PCH 4.064 and I see the output toggling and I also see the internal oscillator at 1Mhz at the OSC2/CLKout pin. Try contacting CCS technical support if you can't figure it out.
Guest








PostPosted: Wed Jul 08, 2009 3:20 pm     Reply with quote

Just upgraded my PCB compiler from 4.083 to 4.093 and everything works fine now. Thanks again for all your help mkuang.
saksun



Joined: 01 Jun 2017
Posts: 15

View user's profile Send private message

PostPosted: Fri Jun 09, 2017 1:19 am     Reply with quote

hi,
I tried same code with only changed port c to port b and code is not working.

It compiled with no errors but on Proteus there is no toggling of leds which are connected to port b, rb1 and rb2.

Thanks in advance.
temtronic



Joined: 01 Jul 2010
Posts: 9097
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Jun 09, 2017 6:02 pm     Reply with quote

Please read sticky PIC101 and you'll see that Proteus is busted, broken, doesn't work, worthless as a true simulator.....
The ONLY proper way to test a PIC is in the Real World.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group