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

How to devise a PC interface to configure and program a PIC?

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



Joined: 17 Jul 2009
Posts: 21
Location: Kolkata, India

View user's profile Send private message

How to devise a PC interface to configure and program a PIC?
PostPosted: Wed Jun 06, 2012 12:41 am     Reply with quote

Hi All

I have prepared a PIC board with different types of sensors and a GSM Modem is attached with it. I have used PIC18F452. Now for different installations it require to change the sensors cut of value and users mobile numbers. So I want to build a Windows/Linux based interface software in which I will configure these things and using that, I want to program the PIC, so that things will be much more flexible. The user can change its settings by his own. Otherwise I need to program each PIC board individually by gathering the user requirements, which is not possible in a mass production.

I am sure there is something(API), using which I can devise a simple software. But I don't know how to do that. Please help me out.

I tried to search the forum but could not manage proper keywords. So, sorry for that.

Any suggestion is valuable for me.

Thanks in Advance
Amit
bkamen



Joined: 07 Jan 2004
Posts: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Wed Jun 06, 2012 1:09 am     Reply with quote

Easiest thing to do (and I've done this) is to write a whole serial based "user interface" complete with the menus you need to allow users to enter in the data you describe.

The only "program" they would need on their computer is a terminal program like PuTTY (which handles com-port communications) or RealTerm, or... (there's a lot of free terminal programs out there)

-ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
amit78



Joined: 17 Jul 2009
Posts: 21
Location: Kolkata, India

View user's profile Send private message

PostPosted: Wed Jun 06, 2012 2:16 am     Reply with quote

Ben

Thanks for the reply.

As I know little about the microcontroller programming I am bit confused with the stuffs.

1. First of all I use to program a PIC using MPLAB ICD3 and my codes are written in CCS-C.
2. Is there any other way to program the PIC without the hardware programmer like MPLAB ICD3? If so then I need to know the things first. Say using simple COM Port? Please help.
3. If I write a serial interface program (Say using C# or wxWidget(C++)) which is running in the PC and the PIC board is connected with a COM port, how can I program the PIC using this? Because, I need to compile the program once to get the new parameter in the code.
4. If I put the the configurations into a file in the MCU then it can be possible I hope. But can we use file in the PIC? So that using the COM port I will just transfer the updated file to the PIC.


Amit
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Wed Jun 06, 2012 3:15 am     Reply with quote

You are going about this in a difficult way. There are a number of things to think about:

1) It is possible to program a PIC with a "bootloader" that then allows application code to be loaded through a serial port, or in more complex bootloaders, CAN, LAN or USB. The bootloader has to be programmed into the PIC using something like the ICD3 or other PIC programmer.

2) Its more normal to have fixed code that's then configured as required. This way, once again you program your PIC once then upload a "configuration" to it using serial, USB, LAN, whatever. The configuration can be stored in the on-chip EEPROM that most (but not all) PICs have. The problem with this is that your code must be able to do all the functions you require, even those you don't configure, and must be able to interpret and manage the configuration.

RF Developer
amit78



Joined: 17 Jul 2009
Posts: 21
Location: Kolkata, India

View user's profile Send private message

PostPosted: Wed Jun 06, 2012 4:48 am     Reply with quote

Hi RF_Developer

What I understood is:
There are two ways to achieve my requirements:
1. Using a Bootloader
2. Using a Configurable On-chip EEPROM


Now For BootLoader can you please guide me with a manual or tutorial so that I can make one? I got few by googling like AN247 (Some says it is buggy) or DS30. Is these work for me?


If I choose the 2nd option then
Quote:
This way, once again you program your PIC once then upload a "configuration" to it using serial, USB, LAN, whatever

i. What is the format of "Configuration"? Is this a Hex code or a file?
ii.
Quote:
The configuration can be stored in the on-chip EEPROM that most (but not all) PICs have.

How to write it in the EEPROM?
iii. How can my Generic code collects the value of a field from the configuration stored in the EEPROM?
iv. As I am using PIC18F452, In its datasheet they are saying it has 256bytes Data EEPROM can this work for me? or I need to use external EEPROM?
iv. Can I have a tutorial for that?

Please Help.
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Wed Jun 06, 2012 6:47 am     Reply with quote

amit78 wrote:

Now For BootLoader can you please guide me with a manual or tutorial so that I can make one? I got few by googling like AN247 (Some says it is buggy) or DS30. Is these work for me?


Those may work for you. Serial bootloaders are fairly simple. USB, LAN and CAN bootloaders are more difficult. Many people have a lot of trouble trying to get their own bootloader code to work. Take a look at some of the threads on here. Its simpler to get a pre-made bootloader like DS30 or the example ones from Microchip. I've had success with the AN1310 serial bootloader. There are many, and they work in different ways. Which is best for you is something you'll have to decide.


Quote:

i. What is the format of "Configuration"? Is this a Hex code or a file?
ii. How to write it in the EEPROM?
iii. How can my Generic code collects the value of a field from the configuration stored in the EEPROM?
iv. As I am using PIC18F452, In its datasheet they are saying it has 256bytes Data EEPROM can this work for me? or I need to use external EEPROM?
iv. Can I have a tutorial for that?


i. The format of the configuration is whatever you want it to be. You have to decide what needs to be configured and how to transfer it to the PIC. The PIC code needs to be simple, so the configuration needs to be in a format the PIC code can easily "understand". I don't know what needs to be configured so I can't say how to format it. This is a systems issue, and you'll have to work out what's best for your needs.

ii. CCS C provides code for writing and reading from data eeprom. Again, people have some problems with this, though its actually simple. Take a look at your CCS manual.

iii. Using the configuration is up to you. You have to read the data from EEPROM into some variables, maybe into a set of arrays. You probably want to do that whenever the PIC starts up, and maybe when you command it to reset. This is where formatting the data in a way that's suitable for your application comes in: you want to make it so that the PIC doesn;t have to do any re-formatting or parsing to "understand" the data. It will also be as small as it can be, which makes storing it simpler.

iv. 256 bytes of EEPROM is not much :-(. It may not be enough for your requirements. If not, then either use a bigger PIC with more EEPROM, or go to bootloading. Be aware that its good practice to include a checksum or CRC with your EEPROM data to help detect any corruption.

v. I'm not writing anything for you. I doubt anyone here will. It appears that you don't have anh clear idea about any of this yet. The way to approach it is to do it a stage at a time.

Rf Developer
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Wed Jun 06, 2012 7:09 am     Reply with quote

.... use flags..

each EEPROM byte has 8 bits... or 8 flags... you have plenty of flags as you can see...

write your code to execute diferent parts of the code depending on the flags...

... so if you have 8 sensors, you assign each one a flag(bit) of a byte stored in EEPROM...

if a "model" of your board reads only sensors 8, 4 and 1 you set your byte to b10001001

and then using bit tests when your code executes you can include or exclude parts of your code..



if your sensor flag byte is : b10001001
then:
Pseudocode:
Code:

If(bit1==True) //this sensor will be read
Read_sensor1();

If(bit2==True) // sensor is excluded
Read_sensor2();

If(bit3==True) // sensor is excluded
Read_sensor3();

If(bit4==True) //this sensor will be read
Read_sensor4();

If(bit5==True) // sensor is excluded
Read_sensor5();

.
.
.
If(bit8==True) //this sensor will be read
Read_sensor8();




the same can be aplied to any other functions you wish to enable or disable depending on the desired configuration of your product...

with 2 bytes you have control over 16 diferent functions to be enabled or disabled... that should cover all your needs...



Edit:
writing a small serial terminal menu to set in eeprom 2 bytes as suggested above should not be dificult at all...... so you can program all your boards with the same code... and then configure the products individually using a serial terminal... should take all of 5 keystrokes to do so.
g.
_________________
CCS PCM 5.078 & CCS PCH 5.093
temtronic



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

View user's profile Send private message

PostPosted: Wed Jun 06, 2012 7:53 am     Reply with quote

Ben's approach( 1st reply) is really the best way(well easy and simple).
1) you need to develop a 'menu + options' system
2) the 'options data' needs to be stored on the PICs EEPROM
3) integrate the 'options' into Main..

For all my dataloggers, I use this approach,as the PIC has total control of the data.If you use ANY PC based program, you have to be 100% sure the data you download to the PIC is 100% correct.Misread a configuration and someone will be getting someone else's data !
Having a 100% PIC based system is great, only program on the PC is a simple terminal program and every PC has those.If you try to code a custom PC based system, every user MUST have the same version(Windows as well as your program),so really you're creating two programs 1 PIC based, 1 PC based.Twice the work and 4 times the problems!
Also cutting code in CCS C is easy and you need 99% of it on the PIC anyway( to receive the data, store it, validate true options,conditional tests in the Main program.It's a lot easier to keep things straight when the program is inside the PIC.
Unless you're super good at PC programming(standalone Delphi is a great choice) you WILL have 'issues' with the PC side of the overall project.You could waste days tracking down some bug due to say a Windows driver that was updated or a 'new' DLL....
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Wed Jun 06, 2012 8:45 am     Reply with quote

Hi,

I have a couple of comments on this project. First, you seem to be "putting the cart before the horse" here by worrying about "custom configurations" before your project even exists in its most basic form. A multi-sensor GSM telemetry station is a formidable project in its own right, and even more so for a self-professed "beginner" PIC programmer. Personally, I'd concentrate on getting your hardware working correctly, and your basic firmware working before I gave any consideration to "custom configurations", etc. Sure, it's always good to plan ahead, but get the house built first before you go pick out all the paint colors Very Happy

Now, if (and that is a BIG 'if') a menu driven configuration system is really needed, others have already given you some good advise. I'd challenge that premise by asking you to tell us how many options are required, and what exactly needs to be configured? I have a GSM based product that is used to gather remote sensor data entirely by SMS messaging. There is ONE firmware version for this product that everyone receives, but it is remotely configurable. Completely by SMS messaging, users can configure sensor alarm limits, time delay values, digital I/O states, telephone numbers, passwords, etc. all remotely. This is a simple and flexible system that completely does away with the need for special versions of the firmware, and PC-based configuration utilities. Maybe your requirements are much more complex, but this technique is worth a look. The bottom line is fully understand your needs BEFORE you go blasting off on a new project which may not really be needed, and may add unnecessary complexity to your product!

In my product, all configuration data is stored on the PIC's internal EEPROM. I use the 18F2550 to control my product because I also use a USB bootloader to allow on-site firmware updates.


John
amit78



Joined: 17 Jul 2009
Posts: 21
Location: Kolkata, India

View user's profile Send private message

PostPosted: Wed Jun 06, 2012 10:30 am     Reply with quote

Hi John

Thanks for your reply. I think Let me tell you all, about my project. I want to monitor a remote location with some sensor and camera on users mobile. I already developed the major part of it. I am using a 3G enabled DVR with a motion sensor. From my Android mobile I can view the camera feed. Along with the camera feed I need some more info few are like:
1. Motion Detection
2. Temperature of that room
3. Mains fail (If the electric supply fails)
3. Backup battery low voltage alert.
4. DG fuel level
5. I built an touch sensor to protect an costly item
etc etc.. lot other...

Now one customer may not need every thing. So I need to customize these. And the most important thing is the mobile number. My idea was something like that, if I have an PC application like updating the firmware of the IP cameras, so that user can set the alarm level by themselves. Say one user may require an alarm if the DG fuel level decrease to 50liter, some other user may set it to 30 liter. Of course every one will set his mobile number. Some may want these alarms to 3 other mobiles simultaneously. So, I think I need a interface where user can customize these.


One more thing... I have developed the sensors parts and they are working fine... all I have done by searching this forum. The camera part is also working fine.



Amit
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Wed Jun 06, 2012 11:01 am     Reply with quote

Hi,

Just out of curiosity, how is the motion sensor data, and the video data integrated into your PIC-based project? Also, you (presumably) have a wired, or wireless, internet connection available at the remote site, right? If so, and you're already using that connection to transport data, why not use it for everything? IOW, instead of a hybrid internet-GSM scheme, just use the internet connection for everything. I've got a PIC-based home automation controller that does essentially what you are doing. My system lets me check the hot tub temperature (for example) from anywhere I can access the internet. I also have a DVR based camera system sharing the same connection. Perhaps you could do something similar?

John
amit78



Joined: 17 Jul 2009
Posts: 21
Location: Kolkata, India

View user's profile Send private message

PostPosted: Wed Jun 06, 2012 12:32 pm     Reply with quote

Hi Jhon

Look I have not prepare the DVR. The DVR can work by using 2G/3G/CDMA dongle. Now thing is that I can not use their communication as because I dont have access to their code. Its simply a third party box with my solution. So to communicate the sensor outputs and the control inputs I need to think for an separate communication mode. Thats why I choose SIM based communication. Which is reliable. In the first phase I am trying the SMS part only by AT command sets using SIM900. In the next phase I will try GPRS based communication using SIM900. I think GPRS is not much reliable means of communication in rural India. So SMS always will be an backup option.

The DVR is providing an NC/NO output while find motion. I used this as an input to PIC, and triggering an SMS.

One more thing, My system is not a home automation kind of. Its a security system. So I need to ensure the Alarm first. So data communication may fail but SMS alert hard to fail.

Amit
bkamen



Joined: 07 Jan 2004
Posts: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Wed Jun 06, 2012 1:13 pm     Reply with quote

amit78 wrote:
Ben

Thanks for the reply.

As I know little about the microcontroller programming I am bit confused with the stuffs.

1. First of all I use to program a PIC using MPLAB ICD3 and my codes are written in CCS-C.


This is no problem. I use CCS-C in MPLAB with my full set of Microchip programmers all the time. (PICstart-Plus, ICD2, ICD3, PICkit2)

The CCS compiler will work with MPLAB as a "language suite" -- you just need to make sure to run the CCS program that configures MPLAB for you (which is normally done with every update. I think it's part of the default installation)

Quote:

2. Is there any other way to program the PIC without the hardware programmer like MPLAB ICD3? If so then I need to know the things first. Say using simple COM Port? Please help.


Yes and No.

Typically, you MUST program the PIC with a programmer to get some form of code on the PIC.

If the base code is a bootloader, than (as RF_DEV has said), you can load anything after that.

Additionally, if your base code is only needing customer parameters, you could make this part of your general code (no bootloader needed) and then write into that something that accepts user settings.

On the PC, this could be written in a scripting language at its simplest. I like TCL... (good for text) but Python, Perl, etc are all usable.

In general, I try to avoid writing PC code that "must run on Windows" or some specific system. I like to write scripts that run on anything that has a commandshell like TCL (which runs on Windows/Linux/MacOS).


Quote:

3. If I write a serial interface program (Say using C# or wxWidget(C++)) which is running in the PC and the PIC board is connected with a COM port, how can I program the PIC using this? Because, I need to compile the program once to get the new parameter in the code.


No. you don't. You can write into your code the ability to accept commands from the PC to take raw/bulk data and then save them in memory somewhere.

(some PIC's have built in EEPROM while others do not - but have bank writable portions of program FLASH. In cases of needs for large amount of memory, an external SPI FLASH can be used (I've done this to whole large web pages).

Quote:

4. If I put the the configurations into a file in the MCU then it can be possible I hope. But can we use file in the PIC? So that using the COM port I will just transfer the updated file to the PIC.


Huh? This is a poorly worded question...

If you put the configs into a file on the computer so they can be referenced and transfered to the PIC -- yes, you can do that via COM port.. (as I've been saying all along)

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Wed Jun 06, 2012 1:46 pm     Reply with quote

Hi Amit,

I kind of surmised that you weren't actually integrating the DVR data into the PIC, so thanks for the clarification! Your notion of having the SMS messaging form a more robust, and reliable communications medium is also a good idea! In fact, many security systems use cellular radio networks as the primary or backup means to signal a problem!

I think you are missing a key point regarding the configuration of your project. You would develop and program your project using CCS 'C' and a PIC programmer, and then once programmed, your project would communicate with the PC to 'upload' configuration info. You could design a communications protocol to do this, and then send commands from the PC to your project. Your project would interpret these commands, and then store new configuration values in EEPROM. Here is an example of what I'm talking about:

Code:

#SM1:UL1,4.8<CR>


The 'SM' is just an arbitrary module name ('Security Module') I came up with, and the '1' is a module address, which may or may not be needed. The 'UL1' stands for 'Upper Limit' on sensor 1, while the '4.8' is the actual limit value. Note the command is prefixed with a '#' and terminated with a '<CR>', both of which make the receive part of your PIC program much simpler!

You could use something like this to set the phone number:

Code:

#SM1:NUM1,18005551212<CR>


Hopefully, you get the idea?

John
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