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

Programming a PIC

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



Joined: 08 Aug 2005
Posts: 22

View user's profile Send private message

Programming a PIC
PostPosted: Wed Nov 09, 2005 5:33 am     Reply with quote

Hi there

I am trying to program a PIC with another PIC. I managed to read the target ID of the device and i manged to erase the device. Now i am trying to program the device. I tried sending one byte(hard coded - defined as a) to write to the memory and then used a normal programmer (ICD2) to read back the target PIC and see if the byte was written in memory but it wasnt.when i checked with the oscilloscope, i could see the bits coming out(sent) on the data pin when the clock pin cycled 16 times but couldnt read it. I have pasted my code below. Can anyone tell me what I am doing wrong?

Code:

int8 a = 0xCB;
int  PIC_HEX_ProgMem_LastLoadedAddress;
#define P_MCLR PIN_B0 //Pin MCLR
#define P_DATA PIN_B2 //pin RB7
#define P_CLOCK PIN_B1 //pin RB6
#include <16F873.h>
#use delay(clock=20000000)
#fuses XT,NOWDT,BROWNOUT, PUT, NOLVP
#use rs232(baud=115200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)


typedef int16 WORD;
typedef short BOOL;
//typedef int16 FILE;

#define PIC_CMD_LOADCONFIG 0x00
#define PIC_CMD_LOADPROGRAM 0x02
#define PIC_CMD_READPROGRAM 0x04
#define PIC_CMD_BEGINERASEPROGRAM 0x08

#define PIC_CMD_INCADDRESS 0x06
#define PIC_CMD_LOADDATA 0x03
#define PIC_CMD_BULKERASE1 0x01
#define PIC_CMD_BULKERASE2 0x07
#define PIC_CMD_ENDPROGRAM 0x0E
#define PIC_CMD_BEGINPROGRAM 0x18

#define PIC_HEX_ID_BUFFER_SIZE   4 /* max size of ID memory      */
#define PIC_HEX_NR_CONFIG_WORDS  2
#define PIC_HEX_PROG_BUF_SIZE 8192 /* max size of program memory */
#define buffersize 64

#define lConfMemBase 0x2000
#define lConfWordAdr 0x2007
#define wCfgmask_bandgap 0x0000
#define wCfgmask2_used 0x0000
#define code_protection_bits 0x3030
#define data_protection_bits 0x0100
char buffer[buffersize];
WORD PIC_HEX_prog_buf[PIC_HEX_PROG_BUF_SIZE];
WORD PIC_HEX_id_buffer[PIC_HEX_ID_BUFFER_SIZE];
WORD PIC_HEX_config_word[PIC_HEX_NR_CONFIG_WORDS];

void program_mode() //puts to program/verify mode
{
   output_low(P_DATA);
   output_low(P_CLOCK);
   output_low(P_MCLR);
   delay_us(1);
   output_high(P_MCLR);
}

void send_command(unsigned char cmd) //sends a command
{
   int i;
   output_low(P_CLOCK);
   output_low(P_DATA);
   output_low(P_MCLR);
   delay_us(1);
   output_high(P_MCLR);
    for(i = 0; i< 6; i++)
   {
      output_low(P_CLOCK);
      if(bit_test(cmd, i))
      {
         output_high(P_DATA);
      }
      else
      {
         output_low(P_DATA);
      }
      output_high(P_CLOCK);
    }
   output_low(P_CLOCK);
 }

void sendData(unsigned int tempData) //loads 14 bit data when clock cycles 16 times
{
   char i;
   unsigned char b;
   for (i=0; i<16; i++)
   {
      output_low(P_CLOCK);
      if( bit_test(tempData, i))
      {
         output_high(P_DATA);
      }
      else
      {
         output_low(P_DATA);
      }
      output_high(P_CLOCK);
   }
      output_low(P_CLOCK);
}

unsigned int readData() //reads a 14bit data word when the clock cycles 16 times
{
   char i;
   int16 tempData = 0;

   for (i = 0; i<16; i++)
   {
      output_high(P_CLOCK);
      if(input(P_DATA))
      {
         tempData |= 0x8000;
      }
      else
      {
         tempData |= 0x0000;
      }

      output_low(P_CLOCK);
      tempData >>= 1;
    }

    tempData >>= 1;
    tempData &= 0x3FFF;
    return tempData;
}
void Program_Flash(WORD w) //programs a word
{
   sendData(w);
   send_command(PIC_CMD_BEGINPROGRAM);
   delay_ms(10);
}
void LoadConfig (int base) //loads config
{
   int i,n;
   send_command(PIC_CMD_LOADCONFIG);
   sendData(PIC_HEX_config_word[0]);
   n = base - lConfMemBase;
   for(i=0;i<n;++i)
   {
      send_command(PIC_CMD_INCADDRESS);
   }
}
BOOL Program(WORD *buf, int n, WORD mask, int loadcmd, int readcmd, int base) //program function
  {
      int i;
      WORD r, w;
      WORD wFlags;
      int PIC_HEX_ProgMem_LastLoadedAddress;
      BOOL still_ok = TRUE;

      program_mode();

      if ((base >= lConfMemBase) && (base <= lConfWordAdr))
      {
         LoadConfig(base);
      }

      for(i=0; i<n; ++i)
      {
         w=buf[i] & mask;
         send_command(loadcmd);
         Program_Flash(a);
         send_command(PIC_CMD_INCADDRESS);
      }
      output_low(P_MCLR);
      output_low(P_CLOCK);
}

void main()
{
   char value;
   int i;
   WORD w;
   int16 storage;
  // printf(" Welcome to the PIC programmer\n");
  // printf("software was compiled on");
 //  printf(__DATE__);
  // printf("\tat about\t");
  //printf(__TIME__);
   setup_counters(RTCC_INTERNAL,RTCC_DIV_2);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   enable_interrupts(INT_RDA);
   enable_interrupts(global);
   program(PIC_HEX_prog_buf, PIC_HEX_ProgMem_LastLoadedAddress+1,0x3FFF,PIC_CMD_LOADPROGRAM,PIC_CMD_READPROGRAM,1);
while(1);
  }                                                                                                                       


thanks in advance
Ttelmah
Guest







PostPosted: Wed Nov 09, 2005 5:57 am     Reply with quote

Well, without even looking at the program itself, youcannot have these declarations (in fact I'm suprised the compiler does not moan...)
Code:

typedef int16 WORD;

#define PIC_HEX_PROG_BUF_SIZE 8192 /* max size of program memory */

WORD PIC_HEX_prog_buf[PIC_HEX_PROG_BUF_SIZE];

You need to rethink your storage in the local PIC. You should declare a buffer, that is _only_ the size of a 'page' on the target PIC, put a block of data into this, and transfer just this small block 'offset' by a variable that represents the starts of the block in the destination memory.
As it stands, you are trying to define a buffer that is 16384 bytes long, on a PIC that only has 192 bytes of RAM...

Best Wishes
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Nov 09, 2005 2:02 pm     Reply with quote

Quote:

#include <16F873.h>
#use delay(clock=20000000)
#fuses XT,NOWDT,BROWNOUT, PUT, NOLVP

You're using the XT oscillator setting with a 20 MHz crystal.
It's probably not going to start-up in a reliable way.
You should change it to HS.

Look in the 16F873 data sheet, in the following section, for
a chart which shows the recommended oscillator settings,
based on the crystal frequency:
TABLE 12-2: CAPACITOR SELECTION FOR CRYSTAL OSCILLATOR
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