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 CCS Technical Support

CCS (MPLAB) PIC10F320 – LED BLINKING in C language
Goto page 1, 2, 3, 4  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
viki2000



Joined: 08 May 2013
Posts: 233

View user's profile Send private message

CCS (MPLAB) PIC10F320 – LED BLINKING in C language
PostPosted: Thu May 30, 2013 7:49 am     Reply with quote

Computer used: Win7 on 64 bits and also Win7 on 32 bits.
CCS compiler version used is 4.140.
I had no success to make a simple LED blink. Somewhere I miss a setting and I need help.
RA2 is used as output for LED. I use an external power stabilized supply 5VDC for programming and for running the project.
Programmer is Pickit3.
Additionally I tried also with IDE is MPLAB 8.89 and also MPLAB-X 1.8, by using CCS compiler and also Hi-TECH 9.83 compiler.

The code used:

Code:
#include <PIC10F320_CCS_Blink.h>

void main ()
{

   //Example blinking LED program
   while (true)
   {
      output_low(LED);
      delay_ms(DELAY);
      output_high(LED);
      delay_ms(DELAY);
   }

}


And PIC10F320_CCS_Blink.h is:

Code:
#include <10F320.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC                    //Internal RC Osc
#FUSES NOBROWNOUT               //No brownout reset
#FUSES WDT_SW                   //No Watch Dog Timer, enabled in Software
#FUSES NOMCLR                   //Master Clear pin used for I/O
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOLPBOR                  //Low-Power Brownout reset is disabled

#use delay(int=8000000)

#define LED PIN_A2
#define DELAY 1000


1) When I use as Debugger MPLAB SIM and I look at the SFR, RA0 seems to work fine.
2) I made on the same hardware similar blink program for PIC10F200 and I have no problem – works fine.
3) I looked here: http://www.microchip.com/forums/tm.aspx?high=&m=623700&mpage=1#623700 and also in the next Japanese page (with Google translate): http://translate.google.com/translate?hl=en&sl=ja&u=http://blog.goo.ne.jp/toko0131/&prev=/search%3Fq%3Dblog.goo.ne.jp/toko0131%26biw%3D1920%26bih%3D880

and I copied the ASM program and then in MPLAB and made a new project, compiled with MPASM tool suite and burned on the same PIC10F320, on the same hardware and it works!!! The problem is I need it in C, because I want further development of the program.
4) I tried next 2 things:
a. One time I copied the entire code in MPLAB, in a new project with CCS toolsuite and compiled with CCS.
b. I imported the C file or once only the hex file (MPLAB-X) and tried to make a new project.
c. I had the same bad results, LED not blinking.
5) If in while loop I activate or deactivate RA0 by making PORTA=0b00000001 or PORTA=0b00000000 and nothing else in that while loop, then after compilation and burning into the PIC10F320, the LED comes OFF or ON depending how was set RA0 in while loop.
6) Seems more a problem of delay function, but I am not sure yet.
7) MPLAB-X even does not give any suggestion to delay_ms () when I press Ctrl+Space (=Autocomplete) and seems not recognized by MPLAB-X.
8) I have in mind few things:
- Maybe I miss a setting as fuses, include…whatever – but what?
- Maybe is an error with delay function and MPLAB and HITECH
Compiler when I use PIC10F320, then another delay function self made, customized should be probably done in the present software. (I looked) also here
http://www.ccsinfo.com/forum/viewtopic.php?t=49876&highlight=pic10f320
http://www.ccsinfo.com/forum/viewtopic.php?t=50019&highlight=pic10f320
http://www.ccsinfo.com/forum/viewtopic.php?t=49925&highlight=pic10f320

Do you have any suggestions?
- I mean, real code example for PIC10F320, C code and LED blinking
- I mean, another delay function, in C language
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu May 30, 2013 11:40 am     Reply with quote

The code seems to work at first sight. Interestingly, MPLAB SIM complains about a WDT timeout,
although SWDTEN is cleared. Might be a MPLAB SIM or actual chip issue. I think it won't hurt to either set
NOWDT fuse or add restart_wdt to #use delay options.
temtronic



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

View user's profile Send private message

PostPosted: Thu May 30, 2013 6:56 pm     Reply with quote

I don't have that PIC but from previous 'why' questions...

might have something to do about the PIC default to analog pins ?

or

something about the 'CLC' aspect of that PIC..... ??



hth
jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri May 31, 2013 12:40 am     Reply with quote

This is probably the same old GP2 / Option register problem.
Try these changes to your program:
Code:

#define set_options(value)   {#ASM         \
                              MOVLW  value \
                              OPTION       \
                              #ENDASM}

//=========================
void main()
{
set_options(0xDF);  // Enable pin GP2 for normal i/o

while (true)
  {
   output_low(LED);
   delay_ms(DELAY);
   output_high(LED);
   delay_ms(DELAY);
  }

}
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Fri May 31, 2013 1:35 am     Reply with quote

Quote:
This is probably the same old GP2 / Option register problem.
According to datasheet 10F320 has no option register bits related to GPIO function, other than for 10F200.

Quote:
might have something to do about the PIC default to analog pins ?

Analog pin function doesn't affect the output function. It's also disabled by default for this chip.
Ttelmah



Joined: 11 Mar 2010
Posts: 19549

View user's profile Send private message

PostPosted: Fri May 31, 2013 1:39 am     Reply with quote

One other though that occurred to me last night, is it could very well be the simple "no current limiting resistor".....
If this wasn't present, the code would then try to turn on the LED, and die.

However the old GP2 defaults to being T0 input problem as PCM programmer says is the glaring one.
The setup_timer_0 instruction handles this for you:

Code:

void main(void)
{
   setup_timer_0(T0_INTERNAL);
   while (true)
   {
      output_low(LED);
      delay_ms(DELAY);
      output_high(LED);
      delay_ms(DELAY);
  }
}


There were problems in the past with this (hence PCM Programmers assembly bodge), but this has been fixed for quite a while, and didn't apply on this chip (where the option register is available at address 0xE as a normal register without using the option instruction). Smile

Best Wishes
viki2000



Joined: 08 May 2013
Posts: 233

View user's profile Send private message

PostPosted: Fri May 31, 2013 2:28 am     Reply with quote

I tried the suggestions above with no luck.

I used Win7 32 bits, MPLAB 8.89, CCS 4.140, next code:

Code:
#include <10F320.h>
#device adc=10

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC                    //Internal RC Osc
#FUSES BROWNOUT                  //Brownout reset
#FUSES NOMCLR                   //Master Clear pin used for I/O
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOLPBOR                  //Low-Power Brownout reset is disabled

#use delay(int=8000000)

#define LED PIN_A0
#define DELAY 1000

// delay_ms() work-around for 8 MHz oscillator:
int16 cnt;               
#define delay_ms(x)    \
cnt = x;                  \
   while(cnt)             \
       {                  \
       delay_cycles(250); \
       delay_cycles(250); \
       delay_cycles(250); \
       delay_cycles(250); \
       delay_cycles(250); \
       delay_cycles(250); \
       delay_cycles(250); \
       delay_cycles(250); \
       cnt--;             \
      }

void main()
{
   //Example blinking LED program
   while(true)
   {
      output_low(LED);
      delay_ms(DELAY);
      output_high(LED);
      delay_ms(DELAY);
   }

}



I even replaced the delays with some FOR loops and NOP function:

Code:
while(1){
   output_high(LED);
   for (i=0; i<2000; ++i){
      for (j=0; j<2000; ++j){
         NOP();
      }
   }
   output_low(LED);
   for (i=0; i<2000; ++i){
      for (j=0; j<2000; ++j){
         NOP();
      }
   }
}


Always, everything is complied without errors, only that LED does not blink, stays ON all the time.
I think is a setting for PIC10F320.

Next step that I have in mind is to use a header debugger and watch the SFR in real time.

I even ordered from CCS the ICD-U64, so I can use the compiler - debugger in CCS IDE environment and not only in MPLAB as I do it now. But takes few days, 1 week until I get it. For the moment I have PICKIT3 from Microchip.

I have connected always the LED as was suggested above - always, and there is no hardware problem due to the next experience.

Important to mention that such simple program written in C, having MPLAB or MPLABX as IDE, and using HITECH, also CCS and also XC8 compilers did not solved the problem. In exchange, next ASM program, used on the same hardware, works:

Code:
; Blink LED on pin RA2 of 10F320
 
#include "p10f320.inc"
__CONFIG _CP_OFF & _FOSC_INTOSC & _WDTE_OFF & _MCLRE_OFF & _LVP_OFF
 
temp equ 0x60
temp1 equ 0x61
 
            ORG 0x00
 
            banksel OSCCON
            movlw b'00110000'
            movwf OSCCON                      ; 1MHz clock
            banksel ANSELA
            bcf ANSELA, 0x02                    ; Make LED pin digital
            banksel TRISA
            bcf TRISA, 0x02                       ; Make LED pin an output
 
loop:
            bsf LATA, 2
            call delay
            bcf LATA, 2
            call delay
            goto loop
delay
            movwf temp
d
            call delay1
            decfsz temp
            goto d
            return
delay1
            movlw d'205'
            movwf temp1
d1
            decfsz temp1
            goto d1
            goto $+1
            return
end


Who is able to "translate" the above ASM in C, don't care what compiler, or at least to notice a difference, what is missing, perhaps as settings, in C?
I can provide small programs for LED blinking used in all 3 compilers, which have no problems when are written in similar manner for PIC10F200 for instance, using the same hardware (PIC10F200 is compatible pin to pin with PIC10F320).
Ttelmah



Joined: 11 Mar 2010
Posts: 19549

View user's profile Send private message

PostPosted: Fri May 31, 2013 4:19 am     Reply with quote

Code:

while(1){
   output_high(LED);
   for (i=0; i<2000; ++i){
      for (j=0; j<2000; ++j){
         NOP();
      }
   }
   output_low(LED);
   for (i=0; i<2000; ++i){
      for (j=0; j<2000; ++j){
         NOP();
      }
   }
}

How is 'i' declared?. If this is an int, the LED would stay on for ever. It could never get to 2000.
viki2000



Joined: 08 May 2013
Posts: 233

View user's profile Send private message

PostPosted: Fri May 31, 2013 5:17 am     Reply with quote

The loops with FOR, i and j I tried using HITECH compiler, not CCS, but I expect similar bad result with CCS if I use int16 for i, j.
No problem there either: I used unsigned int. Has the same behavior as with delay.
It is something else, don't know yet what.


I play now with a header debugger for PIC10F320.
What do you suggest should I watch in real time?
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Fri May 31, 2013 5:26 am     Reply with quote

If I compile your program with CCS 4.141 I notice that it writes to PORTA instead of LATA as per your ASM example. Maybe try and use inline asm to write to LATA, 2 instead of output_high(LED).
Ttelmah



Joined: 11 Mar 2010
Posts: 19549

View user's profile Send private message

PostPosted: Fri May 31, 2013 8:08 am     Reply with quote

Read the data sheet on this one. Quote:

"A write operation to the LATA register has the same
effect as a write to the corresponding PORTA register.
A read of the LATA register reads of the values held in
the I/O PORT latches, while a read of the PORTA
register reads the actual I/O pin value."

The writes will behave the same. It is only reads that differ.

Best Wishes
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Fri May 31, 2013 9:13 am     Reply with quote

Quote:
I tried the suggestions above with no luck.

There are some more basic points beyond the specific LED blinking code that should be checked, e.g. does the
processor run at all, are you able to manipulate any IO pin? These things are easy to check with the device at
your fingertips, but it's a bit difficult to support from a distance.

Quote:
I even ordered from CCS the ICD-U64, so I can use the compiler - debugger in CCS IDE environment and not only in MPLAB as I do it now. But takes few days, 1 week until I get it. For the moment I have PICKIT3 from Microchip.

I would basically suggest the opposite, use MPLAB with a Microchip debugging adapter. It's known to work
with all chips, also new ones.

I haven't been using CCS ICD for a while, but I presume, low level support (disassembly view, SFR
manipulation etc.) is still better in MPLAB.

But as far as I see, there's no ICD Debug header for PIC10F320 available yet. So neither MPLAB
debuggers nor CCS ICD will help you.
viki2000



Joined: 08 May 2013
Posts: 233

View user's profile Send private message

PostPosted: Fri May 31, 2013 9:26 am     Reply with quote

In my trials I used also LATA instead of PORTA, unfortunately without success.

Because I have the header debugger part, today I figured out how to use it and I looked in Debug mode, in MPLAB and MPLAB-X, when C code was written once for XC8 and once for HITECH.
Then I looked also in Debug mode when I created the project with ASM.

For CCS compiler, Pickit 3, PIC10F320, MPLAB and header debugger does not work.
I ordered ICD-U64 and I will try directly in CCS IDE environment, without any MPLAB.
I do not know if it will work for PIC10F320, but I will see that hopefully by the end of the next week.
Until then, I compared in MPLAB these 3: ASM, XC8, HITECH in debug mode, looking at SFR.

I was amazed. Next week I will record the screen during debugging mode with full view of all SFR.

Because in ASM I have seen OSCCON beside LATA and PORTA affected, I looked first at OSCCON in Debug mode.
It has the same value 0x39.

For ASM, the LATA and PORTA are changing in the same time nice, changing only one bit.
For XC8 and HITECH, LATA is changing nice as in ASM, but PORTA is changing strange. The last 3 bits are sometimes 111 or 100 or 110 or 000, do not know exactly the sequence or if it is random.
That is strange and amazed me.

If we look at all these CONFIG or FUSES and also at the descriptions inside the header file (.h) for CCS or HITECH or XC8 then we see those special functions for blocks attached to the port. I guess is wise to disable them, but I am not so sure about the syntax necessary to be used.
On the other hand ASM code does not mention them: not as enable and not as disable.
Could it be that ASM compiler from Microchip will put them off automatically when they are not mentioned? And the rest of C compilers have to mention them as off (each compiler in its way)?
I see no other bug except those blocks tight to PORTA.

What do you think? Do you have any idea about the syntax used to disable those blocks?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri May 31, 2013 9:51 am     Reply with quote

Did you see the posts that Ttlemah and I did on set_options() and
setup_timer_0() ? I don't think you did. Because your reponse
includes a delay routine that was in a post that I deleted. I removed
that post after 5 minutes because I thought about the problem some
more and then posted the set_options() solution. Then Ttelmah showed
how it could be done by using the setup_timer_0() function. Try this:
Add the line in bold below:
Quote:

void main(void)
{
setup_timer_0(T0_INTERNAL);

while (true)
{
output_low(LED);
delay_ms(DELAY);
output_high(LED);
delay_ms(DELAY);
}
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri May 31, 2013 11:17 am     Reply with quote

Also, post a schematic or a description of your LED circuit.

In the circuit below, the PIC supplies the power to run the LED.
I think you should connect your LED in this way:
Code:

PIC
pin      330 ohms      LED       
A2    ---/\/\/\/------->|----
                            |
                            |
                          -----  Ground 
                           ---
                            -


The cathode of the LED goes to ground. The anode connects to the PIC
pin. So when the PIC outputs a high level, the LED will turn on.

The resistor can be some other similar value, such as 470 ohms, if you
don't have 330 ohms available.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2, 3, 4  Next
Page 1 of 4

 
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