View previous topic :: View next topic |
Author |
Message |
dossdev
Joined: 08 Jul 2009 Posts: 24
|
PIC16F526 - Hello World |
Posted: Wed Jul 08, 2009 8:39 am |
|
|
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
|
|
Posted: Wed Jul 08, 2009 8:48 am |
|
|
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
|
|
Posted: Wed Jul 08, 2009 9:09 am |
|
|
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
|
|
Posted: Wed Jul 08, 2009 9:17 am |
|
|
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
|
|
Posted: Wed Jul 08, 2009 9:48 am |
|
|
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
|
|
Posted: Wed Jul 08, 2009 10:16 am |
|
|
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
|
|
Posted: Wed Jul 08, 2009 10:43 am |
|
|
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
|
|
Posted: Wed Jul 08, 2009 11:13 am |
|
|
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
|
|
Posted: Wed Jul 08, 2009 11:24 am |
|
|
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
|
|
Posted: Wed Jul 08, 2009 12:34 pm |
|
|
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
|
|
Posted: Wed Jul 08, 2009 3:20 pm |
|
|
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
|
|
Posted: Fri Jun 09, 2017 1:19 am |
|
|
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: 9245 Location: Greensville,Ontario
|
|
Posted: Fri Jun 09, 2017 6:02 pm |
|
|
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. |
|
|
|