View previous topic :: View next topic |
Author |
Message |
Ruby
Joined: 04 Jul 2014 Posts: 44
|
TIMER0 & PIC16F877A |
Posted: Tue Jul 08, 2014 3:08 pm |
|
|
Hi everybody. Can someone tell me what i am doing wrong!!!!
Code: |
#include <16f877a.h>
#fuses hs,nowrt,nowdt,nodebug,brownout,nolvp,nocpd,put,noprotect
#use delay (clock=20000000)
#use rs232(baud=2400, xmit=PIN_C6, rcv=PIN_C7, bits=8, parity=N, errors)
#define LCD_ENABLE_PIN PIN_B0 //#define use_portb_lcd TRUE
#define LCD_RS_PIN PIN_B1
#define LCD_RW_PIN PIN_B2
#define LCD_DATA4 PIN_B4
#define LCD_DATA5 PIN_B5
#define LCD_DATA6 PIN_B6
#define LCD_DATA7 PIN_B7
#include <lcd.c> // For LCD and has to be after #use delay()
unsigned int i=0;
#int_TIMER0
void test()
{
i++;
if (i==100) {output_high (pin_D0);output_low(pin_d1);}
if (i==200) {output_low (pin_D0);output_high(pin_d1);i=0;}
}
void main ()
{
setup_timer_0(RTCC_DIV_256 | RTCC_EXT_L_TO_H );
set_timer0(0); //========> 20.000.000/4/256*1000=0.0512 msec. every 0.0512*100=0.512sec led will go on and off
enable_interrupts(int_timer0);
enable_interrupts(GLOBAL);
lcd_init();
delay_ms(100);
printf(lcd_putc,"\fREADY");
delay_ms(500);
printf(lcd_putc,"\nWAITING...");
while (TRUE)
{}}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jul 08, 2014 3:22 pm |
|
|
Quote: | setup_timer_0(RTCC_DIV_256 | RTCC_EXT_L_TO_H ); |
What is this ? Are you clocking the timer with an external signal ?
If so, what is the signal ? Describe it. If not, then change that
parameter to T0_INTERNAL.
Quote: | Hi everybody. Can someone tell me what i am doing wrong!!!! |
Also, in the future, tell us what the program is doing wrong, and tell us
how you expect it to behave, if the program was running correctly.
Example of what you should say in your post:
"I expect the LEDs on pins D0 and D1 to be blinking, but they are not even turning on." |
|
|
Ruby
Joined: 04 Jul 2014 Posts: 44
|
|
Posted: Tue Jul 08, 2014 3:39 pm |
|
|
Hi. You can see in the "test" that leds are getting on and off!
Maybe you can tell me how can I use external clock... |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Tue Jul 08, 2014 3:58 pm |
|
|
If 'what's wrong' is the LEDS do not flash then get rid of everything NOT LED related. That means delete all teh LCD code, RS232 code, etc.
In order to debug you MUST reduce your program to ONLY that code that you're trying to get 'up and running'.
jay |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jul 08, 2014 4:02 pm |
|
|
Quote: | Maybe you can tell me how can I use external clock |
What device do you have (outside the PIC) that can provide an external
clock to the PIC for Timer0 ? |
|
|
Ruby
Joined: 04 Jul 2014 Posts: 44
|
|
Posted: Tue Jul 08, 2014 8:33 pm |
|
|
I guess I am all wrong after all. I thought crystal is used for external clock. |
|
|
Ruby
Joined: 04 Jul 2014 Posts: 44
|
|
Posted: Wed Jul 09, 2014 2:53 am |
|
|
I am using 20mhz crystal connected to clki and clko with 2 capacitors which connected to ground. How can I use external clock? Fuses??? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19512
|
|
Posted: Wed Jul 09, 2014 9:33 am |
|
|
OK.
Deep breath.
There is a master oscillator (this is your crystal), that clocks the PIC CPU. It can also clock the timers.
However you are selecting to run your timer from and external clock.
The RTCC_EXT_L_TO_H setting says 'clock the RTCC from and external clock, and count the rising edges on this'.
Look at the data sheet.
'Block diagram of the timer0/WDT prescaler'
You will see that the input clock (top left corner), can come from Fosc/4 (your crystal), or from the RA4 pin.
You either need to select the internal clock, or feed a clock into this pin. |
|
|
Ruby
Joined: 04 Jul 2014 Posts: 44
|
|
Posted: Wed Jul 09, 2014 10:34 am |
|
|
Thanks for reply. Some questions...
Can I use 2 pwm external and 2 timers internal?
How can I feed ra4 pin? Ps: I need it for something else...
For internal timer, do I need to change anything in fuses?
And last question: Can I use the same program above by
changing RTCC_EXT.... to RTCC_INTERNAL....?
Thanks. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sat Jul 12, 2014 3:21 am |
|
|
Ruby wrote: | Thanks for reply. Some questions...
Can I use 2 pwm external and 2 timers internal?
How can I feed ra4 pin? Ps: I need it for something else...
For internal timer, do I need to change anything in fuses?
And last question: Can I use the same program above by
changing RTCC_EXT.... to RTCC_INTERNAL....?
Thanks. | You need to study:-
1) The microchip data sheet.
2) The CCS manual.
3) The .h file for you chip.
You will then find the answers to all your questions.
(That's what the rest of us have had to do.)
Mike |
|
|
Ruby
Joined: 04 Jul 2014 Posts: 44
|
|
Posted: Sat Jul 12, 2014 3:46 am |
|
|
The thing is I don't understand anything from datasheet. There is no examples in ccs about external clock.
So how can I "feed" Ra4 pin? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Sat Jul 12, 2014 5:19 am |
|
|
To 'feed' RA4, you just add a 5V CMOS/TTL compatible signal generator(aka 'external clock source') and pullup resistor.
This can be a clock oscillator module, a 555 with Rs and Cs, 2 transistor multivibrator with Rs and Cs,a CMOS 4020, even another PIC.
The point is this 'external clock' has to generate( send to the PIC) a known frequency that you've chosen.What you use as the 'source' is up to you, the designer.
The PIC datasheet does cover the technical details though I suspect that 99.5% of programmers use an internal clock and not the external clock option. It's probably only used when 'non standard' clock source is required say from a special piece of equipment.
hth
jay |
|
|
Ruby
Joined: 04 Jul 2014 Posts: 44
|
|
Posted: Sat Jul 12, 2014 5:25 am |
|
|
Thank you very very much.
Ps: This means that the crystal that I used not for timers just for pic right? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Sat Jul 12, 2014 5:47 am |
|
|
not quite.....
PICs have a LOT of 'options'....
the 'crystal' ( and 2 caps, 15-22pf) attached to pins 13 and 14( OSC1, OSC2) ARE the PICs main 'heartbeat' or master clock. It's the primary timing source.If you use a 4MHz xtal, it will give the PIC the '1us' timing you see in a lot of the charts and articles.
It is also a source for the timers, though depending on the PIC and how you 'configure' it, other 'sources', like the 'external clock' can be used.
Most programmers will use the internal sources (like clk/4) as it is cheaper( no additional parts),accurate(runs off the main clock), programmable(depending on timer used).
Using an external clock source is usually done for some 'odd' frequency that isn't easy for the PIC to generate OR to be 'synced' to an external device.
hth
jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19512
|
|
Posted: Mon Jul 14, 2014 12:55 am |
|
|
As I said multiple posts ago,
"
Look at the data sheet.
'Block diagram of the timer0/WDT prescaler'
You will see that the input clock (top left corner), can come from Fosc/4 (your crystal), or from the RA4 pin.
You either need to select the internal clock, or feed a clock into this pin.
"
Note all the "OR's".
It's _your_ choice which source you feed the timer from.
Look then at PCM_programmer's first post in this thread. Look at what he carefully _highlighted_ in one line from your code, and what he says below it.
Read what people are saying to you. |
|
|
|