Help with Code Error for PIC16F1826

Discussion in 'Programmer's Corner' started by JLeith, Oct 30, 2012.

  1. JLeith

    Thread Starter New Member

    Oct 30, 2012
    7
    0
    Hello members,
    I'm working on restoring our Baseball Scoreboard and I have knowledge with Electronics but this is the new beginning with PIC Code. I have managed to read everything I can find on programming in C and Assembly.

    I have a couple of odd errors that I have looked at and can not answer why it won't work.

    Using two programs (CodeBlock) (MPLAB X IDE).

    Here is the code that I need help with
    Code ( (Unknown Language)):
    1.  
    2. main
    3. {
    4. ORG 0x00     /*This is where the PC points to on power up and reset*/
    5. goto main    /*Goto our main program */
    6. ORG 0x04     /*This is where our interrupt routine will start*/
    7. retfie          /*This tells the PIC that the interrupt routine has*/
    8.                  /*finished and the PC will point back to the main program*/
    9.  
    10. }
    11.  
    John
     
    Last edited by a moderator: Oct 30, 2012
  2. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    I am not really sure what youre asking, but the code should look more like:

    Code ( (Unknown Language)):
    1.  
    2. void main(void){
    3. //do something
    4. }
    5.  
    6. ISR(uart){
    7. //interrupt code
    8. }
     
  3. JLeith

    Thread Starter New Member

    Oct 30, 2012
    7
    0
    Hello Kubeek

    The code I used was from a similar project back in 2002 by a programmer from London.

    The code was related to a 'Reset on Error" or a place the PIC will return if it gets lost in the program. Now I could be way off but this is my first ever go at programming a PIC.

    With out the comments
    Code ( (Unknown Language)):
    1.  
    2. main   {
    3. ORG 0x00    
    4. goto main    
    5. ORG 0x04    
    6. retfie        
    7. }
    8.  
    Kubeek I have seen others using the " Main (Void) "
    I'm not sure why is the 'Void" added to the Main ?

    I hope this helps because I have a few more errors that don't make sense.

    Thank you for your help.
    John
     
    Last edited by a moderator: Oct 30, 2012
  4. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    This is particulary ridiculous because MPLABX provides code templates. Which are intend to get rid of the embarassing situation not to be able to "get the ball rolling" with a PIC program.

    However working these monstroeuse softwares called IDE isn't always so easy these days. It can take a lot of effort even for people who use to program for years.

    So, the situation is similar. Even real programmers are sometimes baffled why something does not work.

    There might be something like "Get started" built in, but there is not really a step-by-step instruction manual explaining it down to all details.

    1. You need to apply a lot of guesswork

    2. Alternatively, shell out some $2000, and attend a class for embedded programming.

    May I ask the age of the OP? So if you don't have the $2000, and are not to bright at counting things together, yes you can ask at forums, but- I tell you, these problems will persist, and they will get worse. They will become so complex you need to write ten pages on a forum so it can be understood a little.

    If you pay $20 to $50, maybe someone will explain you all the steps how actually to make the built-in templates working. Programmers help other programmers for free, but if they come accross non-programmers, they want money. That's how they make their living.
     
  5. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    You shouldn't even need to write this.

    All you are doing is saying, on reset, go to main
    On interrupt, return.

    Don't enable interrupts and ain't worry about org unless you are using multiple interrupt vectors, which, I don't believe the 16F family has....

    Edit: I am assuming you are using HiTech's c compiler...
     
  6. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    I'm working on restoring our Baseball Scoreboard and I have knowledge with Electronics
    (OP)

    Which version of MPLABX are you using?

    Because this is what I see when I start it: A splash screen with "help". Which makes a post like this unneccessary, if it is examined, and used.
     
  7. JLeith

    Thread Starter New Member

    Oct 30, 2012
    7
    0
    Hello Takao21203,
    Thank you for the info. Sorry for placing information that I should know but I don’t know all the background of MPLAB X. The background for this project is repair of our local Baseball scoreboard that was damaged when someone tried to improve the speed of the digits. The scoreboard is a Flip Digit mechanical operation. When the board died it was moved to the bone yard and my best friend noticed it and brought to me to repair. Well I managed to get the digits to work after a year and noticed the main control board was beyond repair so I thought I would give it a go and learn C programming . So know it is time to work thru the process. I purchased the PICKit2 board from MicroChip and started to pick up books and tutorials on the net. I did find some PIC code on a school project back in 2002 that developed code for the Flip Digits used in the London Train Stations and built a 2 digit function al display. I adapted the code and have been painfully adapting it to the Scoreboard using 2012 chips and RF transmitters to control the board.
    On your question 1: I will have to do my best and stubble down several stairs till I can get the code to work. Bit by bit.
    On question 2: I don’t have $$ being retired and I was hoping I could spent some of my retirement time to learn PIC programming.
    I have done the tutorials to turn on the LED and move the Leds around but it has been hard to grasps the inner depth code.
    ----------------------------
    Hello Tshuck
    I will try and remove this code. You are correct I won’t be using any direct interrupts .
    Yes I have the compiler HiTech. I will add a picture to this post.
    Thank you for your help. I have also sent MicroChip support a error when I compile.
    I think I have add a picture.

    John
     
  8. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    I personally hate MPLAB X, I use the older versions....I like a windowed environment...

    Anyway... what I mean is that the typical code I use as a skeleton looks like this:
    Code ( (Unknown Language)):
    1.  
    2. #include "htc.h"
    3.  
    4. void Initialize();
    5.  
    6. void main()
    7. {
    8. Initialize();
    9. while(true)
    10. {
    11.   //do stuff
    12. }
    13. }
    14.  
    15. void Initialize()
    16. {
    17.   //do initialization routines...I/O,  osc., etc.
    18. }
    19.  
    This is a usual starting block for projects.
    Read the datasheet as this cannot. Be done without it...
     
  9. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    OK Thank you for giving this information.

    Do you know you can download XC8 compiler from the Microchip website?

    In the post you wrote you actually mix assembler and C together.

    If you only use assembler, then you need to use ORG. It's pretty straightforward:

    org 0x0000
    goto l_start

    org 0x0004
    ... some interrupt code
    retfie

    org 0x0100
    l_start:
    call l_init ; initializations

    l_main:
    ... some code
    goto l_main

    This is something that would be used for assembler.

    If you want to use C, and mix in assembler, you need to use special keywords. The information is included in the compiler manual. It is not so easy to read through this document.

    Normally, people either use C or assembler. In some cases assembler is mixed into C programs, but only for some special reasons. Don't become trapped trying to save program space with assembler.
     
  10. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    MPLAB X is better. You can for instance jump to the definition of a constant. The IDE will load the file where it is located. You can obtain a list of all occurances of a specific line or piece of code in a window at the bottom.

    The java they use is bad, tough, I can't navigate menu's properly. Yesterday I figured out, it is actually possible using the cursor keys.
     
  11. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    Here are some source codes for the 16f1824, one in assembler:

    http://pic.hitechworld.org/1824pcb.asm

    and one in C language:

    http://pic.hitechworld.org/data/gc_1824.zip

    You can see for instance how interrupt is used, and how the configuration/initialization is done. Assembler is always somehow tied to a specific PIC model, while C language sources can be "ported" to a different chip with little or no modifications. I used the same C source for an 18F chip, and did not really have to make much changes.

    In my opinion assembler sources are much harder to understand. Even if it sometimes look like assembler is easier to learn or to use, or is more powerful. In the long run, it produces a lot of confusion, and it is difficult to change the PIC model.

    The 16f1826 is "better" than the regular 16F PICs, for instance it can use linear 16-bit addressing for the RAM, and it can read the FLASH easily.
     
  12. JLeith

    Thread Starter New Member

    Oct 30, 2012
    7
    0
    Takao21203 thank you for taking the time to send some examples. I will look them over with great care to absorb everything I can. Yes you are correct I'm finding that C source is a lot easier to follow then Assembler. So today I will read over your info and then try and figure out what is wrong with my array.

    I have just ordered a Advanced PIC programming in C form Amazon so I'm hoping it will shed more light.

    John
     
  13. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    No sure this is the best idea. Do you have any links to this book. If it is written for another C compiler than the one you use. You may be better of returning it.
     
  14. JLeith

    Thread Starter New Member

    Oct 30, 2012
    7
    0
    Hello t06afre
    I was hoping for some info on Programming in C and seeing examples to help with my knowledge in PIC programing as you can see with my posting. With the great help from the members I'm getting a glimpse of where I should adventure into. I have posted a picture of the book. Now I know it is for a PIC18 but the price was $40 Canadian and I thought this might be ok with my budget seeing some of the other books were $140 + and I thought I would start on the lower end. I did get a book on C++ programming 800 + pages for a good price and have been reading it and adapting to C language.

    I'm having a tough time in finding material related to PIC programming most of the books relate to programming in general to printing or refer to simple led projects. No strong info on Array and multiple input reading.

    T06afre (Sorry I don't know your name) do you recommend a reading material that might help me with a deeper understanding. I have tried to find someone that I could share my design with and get their knowledge on the steps I will need.


    I have also added the general purpose of my project to the PIC16F1826.

    Well I'm back to working on how to control my program when a signal form the transmitter comes in. I looking at IF / Then statements and then off to an array to control the digits.

    John
     
  15. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    That book is made for the MikroC compiler. As I see it you will be better off sending that book back. For a full refund. Then instead let us help you. Let us start with your score board. Can you say something about what is needed in order to control the digits.
     
  16. JLeith

    Thread Starter New Member

    Oct 30, 2012
    7
    0
    Hi t06afre,
    I will return the book I thank you for your insight. I have posted the electronic drawing for the scoreboard.

    I have a Summary Document but it is a Document and can not be uploaded.

    t06afre if you email directly I can send you the word document. my email address is JLeith@Telus.net

    Here is a snap of the document
    Summary:
    1: The score Keeper press 1 Key on the remote (Ball) example. The RF Receiver Connection will produce a ground signal to (RB0)
    2: The Pic will receive the signal on RB0
    3: The Pic will then change the RB pins to outputs and send the necessary bit patterns to the driver section of the circuits.
    4. The Flip digits are Electromechanical operation and require a 250ms pulse of 12 volts to activate and deactivate the segments.

    5. The 250ms pulse is supplied by the timing pulse on RA4

    Thank you,
    John
     
Loading...