View previous topic :: View next topic |
Author |
Message |
dan king
Joined: 22 Sep 2003 Posts: 119
|
problem programming 16f688 - SOLVED |
Posted: Wed Apr 18, 2007 12:10 pm |
|
|
Hi All,
I'm having a bit of trouble with my first exposure to the 16f688. I have always used a 16f877 in the past with a serial bootloader but now I have a rather simple project where the 688 should do fine. Famous last words.
I have code that I tested on my dev platform (16f877) so I know that the code is ok but I have two issues that are turning me gray far too early.
1. I'm not sure if I'm configuring the config bits properly for this part
2. I'm trying to flash the code with a standard eprom programmer and I'm not sure how it handles the config bits
I'm using compiler CCS PCM C Compiler, Version 4.018 with the following code:
Code: | #include <16f688.h>
//#include <16f877.h>
#fuses INTRC //internal rc osc with clockout(clkout) and i/o on osc1/clkin
#fuses NOWDT //no watchdog
#fuses NOMCLR //internal MCLR
#fuses NOPROTECT //no code protect
#fuses NOPUT //disabled power up timer
#fuses NOBROWNOUT //disabled brownout detect/reset
#fuses NOCPD //no EE read protect
#fuses NOIESO //no internal/external switchover mode
#fuses NOFCMEN //no monitor clock failsafe mode (auto switch if no clock)
#use delay(clock=8000000,INTRC) //after fuse to set intrc clock speed or use
//setup_oscillator( );
////////////////////////////////////////////////////
// Constants and variables
#byte PORTA = 5 // port a = 0x05, b = 0x06, c = 0x07, d = 0x08, e = 0x09 address
#byte PORTC = 7
#bit Di = PORTA.0 //MISO bitstream to flash (INPUT)
#bit clk = PORTA.1 //clock (OUTPUT)
//portA.2 is lcd enable (OUTPUT)
#bit cs = PORTA.5 //chip select signal (active low) (OUTPUT)-can't use a.3 in only
struct {
BYTE data:4; // bits 0 - 3
} LCD_PORT;
#BYTE LCD_PORT = 7 //PORTC
#BIT LCD_E = PORTA.2 // bit 6
#BIT LCD_RD = PORTC.4
#BIT LCD_RS = PORTC.5
#define LCD_DATA LCD_PORT.data
#define DATA_TO_LCD set_tris_c(0)
#define DATA_FROM_LCD set_tris_c(0x0f)
#define WORD unsigned long // Data type definitions
//#include <lcd_custom_pins.h>
int1 tc_status;
int16 result;
float meas;
///////////////////////////////////////////////////////
// General prototypes //
// local prototypes
//int16 read_16SPI(); //customized to get 16 bits of info
/////////////////////////////////////////////////////
void main(){
SETUP_ADC_PORTS(NO_ANALOGS); // no adc's
//setup_oscillator(OSC_8MHZ); //sets the sw selectable internal rc clock speed
set_tris_a(0b11000001); //a.3 is input only so can't use, ignores 0 for tris
set_tris_c(0b00000000);
LCD_E = 0;
// init_lcd(); // Init LCD
cs = 1; //
clk = 0;
// loop until reset
while(1){
}
}
|
I know I have a lot of extra commented out stuff shown but I wanted to show my configuration up to the problems.
First, I can't seem to get the clkout to follow my settings(to use the internal rc clock), it either doesn't exist or is always 1MHZ (4 MHZ clk/4) even though I'm setting it for 8MHZ.
If someone has a simple test program using the internal clock for the 16f688 that they know works, I'd appreciate if they can post it - maybe toggling an I/O so I can see something happening.
Also, if anyone uses the needhams EMP-21 for programming flash pics, if they can tell me how they go about programming the config bits. I'm not sure if the SW is smart enough to poll the config bits out of the hex file or not. I've tried manually programming the config bits using the needhams UI but it doesn't seem to work (no 8MHZ clk, this method usually gives me no clock at all. If I simply load the hex file and go right into programming the part, I get a 4Mhz master clock.
I'm really confused, and have too many unknowns to determine where I'm going wrong. Any help is appreciated.
Thanks,
Dan
Last edited by dan king on Fri Apr 20, 2007 10:23 am; edited 1 time in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Apr 18, 2007 12:55 pm |
|
|
I don't have a 16F688 to test, but I compiled the following program
with vs. 4.018 and looked at the .LST file, and I think it should work.
Try it.
Code: |
#include <16F688.h>
#fuses INTRC,NOWDT,PUT,NOPROTECT
#use delay(clock=8000000)
//=============================
void main()
{
while(1)
{
output_high(PIN_C1);
delay_ms(500);
output_low(PIN_C1);
delay_ms(500);
}
} |
|
|
|
dan king
Joined: 22 Sep 2003 Posts: 119
|
|
Posted: Wed Apr 18, 2007 1:01 pm |
|
|
I'll give it a shot, but I'm starting to think that the fuses aren't being set properly using the needhams. It "feels" as though the defaults are being set when I load my hex file, and not my fuse settings.
It looks like this enables the clock failsafe so I always get the 4Mhz master clock. If I try and manually change the fuses in the programmer UI, I get nothing out of the clock out pin.
Thanks for your help, I'll reply with the results. |
|
|
dan king
Joined: 22 Sep 2003 Posts: 119
|
|
Posted: Wed Apr 18, 2007 1:33 pm |
|
|
I compiled your example, and then flashed the code using my EMP-21 and still no good. I looked at the CLKOUT and still only get 1Mhz (4MHZ master clk).
I didn't try to manually change the config bits, and just loaded the hex file and programmed. When it still didn't work, I reloaded the hex file and used the buffer editor to read the config bits and they were correct = 3FE5 but when I read the chip contents back, the config bits were 3F 0x05 5 so I'm not sure what is happening. It looks like the programmer is failing to write the config bits properly.
I have ordered a picstart plus upgrade to allow it to program 16f688 parts so I guess I'll have to wait to see how that pans out.
If anyone has any ideas, I'd love to hear them.
Thanks again.
Dan |
|
|
Ttelmah Guest
|
|
Posted: Wed Apr 18, 2007 2:50 pm |
|
|
If it is working, and giving 1MHz out, then there are no fuse changes involved. Switching to 8MHz, is just a matter of changing the value in the OSCCON register.
Code: |
#byte OSCCON=0x8F
//Then add at the start of your main:
OSCCON=(OSCCON & 0xF) | 0x70;
|
If this works, then there is a coding fault in the setup_oscillator instruction on your compiler version, which should do exactly the same.
Best Wishes |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Apr 18, 2007 2:58 pm |
|
|
I compiled the test program with vs. 4.018 and it initializes
the OSCCON register correctly in the startup code:
Code: |
.... void main()
.... {
0018: CLRF 04
0019: BCF 03.7
001A: MOVLW 1F
001B: ANDWF 03,F
001C: MOVLW 71
001D: BSF 03.5
001E: MOVWF 0F // OSCCON = 0x8F
001F: BCF 03.5
0020: BCF 1F.6
0021: MOVLW 00
0022: BSF 03.5
0023: MOVWF 11
0024: MOVLW 07
0025: BCF 03.5
0026: MOVWF 19
|
--------
With regard to the programmer, my advice is to use Microchip tools.
They have to make it work. They're the manufacturer.
Last edited by PCM programmer on Wed Apr 18, 2007 3:00 pm; edited 1 time in total |
|
|
dan king
Joined: 22 Sep 2003 Posts: 119
|
|
Posted: Wed Apr 18, 2007 2:59 pm |
|
|
I added the changes you suggested and I still get 1Mhz out of the Clkout pin. Additionally, the test program still doesn't run as I don't see any changes on Pin C.1
The verify from the programmer claims the codes match, but no go.
Thanks again |
|
|
Jim Hearne
Joined: 22 Dec 2003 Posts: 109 Location: West Sussex, UK
|
|
Posted: Thu Apr 19, 2007 2:39 am |
|
|
I found i have to have Code: | setup_oscillator(OSC_8MHZ); | in the first line of main() on the 16F688 otherwise it doesn't run at the correct speed even with
Code: | #use delay(clock=8000000,RESTART_WDT) |
I think the manual implys the #use delay should setup the clock to the correct speed but it doesn't seem to.
Fuses in my code are
Code: | #fuses INTRC, PUT, BROWNOUT, WDT, NOMCLR, PROTECT,CPD |
Jim |
|
|
dan king
Joined: 22 Sep 2003 Posts: 119
|
|
Posted: Thu Apr 19, 2007 6:29 am |
|
|
Hi Jim,
I added the setup osc line and still nothing but 1Mhz clkout. I'm just about convinced the eprom programmer is just not handling the fuses properly. I have an upgrade to my picstart plus on order so when I receive that, I'll be able to confirm this. When I program the part, not only does the osc not operate at the right speed, but the code isn't executing either.
Thanks for the suggestion.
BTW - I assume your not using a standard eprom programmer to flash the part, correct? |
|
|
Jim Hearne
Joined: 22 Dec 2003 Posts: 109 Location: West Sussex, UK
|
|
Posted: Thu Apr 19, 2007 6:53 am |
|
|
Hi Dan,
For the 16F688 i still use a K149 programmer that came from Quasar Electronics and uses Micropro software.
For new projects i now use CCS's own ICD_U40.
The only other thing apart from the fuses i can think of is what are you doing externally with the Mclr pin ?
Even though i see it's set as I/O use in your code i've had a design where it would float above the pic supply and set the pic into programming mode.
Jim |
|
|
dan king
Joined: 22 Sep 2003 Posts: 119
|
Problem resolved - emp21 = NG |
Posted: Fri Apr 20, 2007 9:38 am |
|
|
Hi Folks,
Hope this might help someone else in the future. It appears the Needhams EMP-21 does NOT program fuses correctly. I received an upgrade for the Picstart Plus in order to support the 16f688 and my original code operates perfectly when programmed with that programmer.
I was never able to get ANY code to function when programmed with the needhams emp-21. I even tried to manually program the fuses with the EMP-21 user interface set up for this function but nothing worked.
Rgds,
Dan |
|
|
|