View previous topic :: View next topic |
Author |
Message |
volcane
Joined: 23 Feb 2008 Posts: 29
|
Set TMR1H and TMR1L in Pic 16F877A |
Posted: Tue Mar 17, 2009 4:21 pm |
|
|
Hi!
how do i set a PIC16F877A TMR1H = 0x21 and TMR1L = 0x18 i tried with #byte TMR1H=0x21 and #byte TMR1L=0x18 but it seems not work |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Mar 17, 2009 4:36 pm |
|
|
Use the CCS function. That's what it's there for. It accurately creates
the code to load Timer1:
Code: |
... set_timer1(0x2118);
000F: MOVLW 21
0010: BCF STATUS.RP0
0011: MOVWF TMR1H
0012: MOVLW 18
0013: MOVWF TMR1L |
Also, #byte is used to tell the compiler the address of the register
(or other memory identifier name). #byte is not used to load the
register. |
|
|
volcane
Joined: 23 Feb 2008 Posts: 29
|
|
Posted: Tue Mar 17, 2009 5:14 pm |
|
|
Hi!
setting:
set_timer1 (0x2118);
.................... set_timer1(0x2118);
*
0035: MOVLW 21
0036: MOVWF 0F
0037: MOVLW 18
0038: MOVWF 0E
not TMR1H and TMR1L |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
volcane
Joined: 23 Feb 2008 Posts: 29
|
|
Posted: Tue Mar 17, 2009 5:36 pm |
|
|
Hi!
thanks, because it appears in your program instead of 0F and 0E TMR1H and TMR1L? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Mar 17, 2009 5:57 pm |
|
|
In MPLAB, you can set the List File Format to:
Quote: |
- Normal CCS format
- MPLAB format
- Symbolic format
|
This is done in the Project / Build Options menu.
I used the Symbolic format mode. |
|
|
volcane
Joined: 23 Feb 2008 Posts: 29
|
|
Posted: Tue Mar 17, 2009 6:41 pm |
|
|
Hi!
This instruction is placed in the interrupt function? or you can put in the main?
Example:
Code: |
#int_TIMER1
void TIMER1_isr()
{
set_timer1(0x7F81);
Ticker -= 65536;
if ( Ticker < 65536 ) {
Ticker = TIMER1_FREQUENCY;
seconds++;
}
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Mar 17, 2009 7:02 pm |
|
|
Are you asking about this function:
Put it anywhere in your program. |
|
|
volcane
Joined: 23 Feb 2008 Posts: 29
|
|
Posted: Tue Mar 17, 2009 7:50 pm |
|
|
Hi!
Code: | #include "C:\Programmi\PICC\Projects\interrupt ccs.h"
#define green_led pin_a5
#define yellow_led pin_b4
#define red_led pin_b5
//RTC variables
#define XTAL_FREQUENCY 20000000
#define TIMER1_FREQUENCY (XTAL_FREQUENCY /4)
int32 Ticker;
int8 Seconds=0;
// Initialize RTC
void Initialize_RTC(void)
{
Ticker = TIMER1_FREQUENCY;
setup_timer_1( T1_INTERNAL | T1_DIV_BY_2 );
enable_interrupts( INT_TIMER1 );
}
#int_TIMER1
void TIMER1_isr()
{
set_timer1(0x7F81); //setting TMR1H e TMR1L 76 cicli 1000,008ms
//13,158ms pari a 75,99939Hz
Ticker -= 65536; // Decrement ticker by clocks per interrupt
if ( Ticker < 65536 ) // If second has expired dopo 76 cicli quando Ticker =19264
{
Ticker = TIMER1_FREQUENCY;
seconds++; // Increment number of seconds
}
}
void main()
{
int8 prev_second;
int8 minutes=0;
Initialize_RTC();
enable_interrupts( GLOBAL );
output_low (green_led), output_high (red_led);
// loop forever
while(true)
{
// Toggle output every second
if (seconds != prev_second)
{
prev_second = seconds;
output_toggle (green_led);
if(Seconds == 60)
{
Seconds=0;
minutes++;
output_toggle (yellow_led);
}
if(minutes == 60)
{
minutes = 0;
output_toggle (red_led);
}
}
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Mar 17, 2009 7:58 pm |
|
|
You keep posting more and more code. I don't know what you want.
I don't want to do this. I'm done. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Mar 18, 2009 5:47 pm |
|
|
The posted code is a modified version of Neutone's Real Time Clock (RTC) code found at several places in this forum. Here a link to the version in the Code Library
Same as PCM Programmer I have no clue as to what the question is, but from the modifications made to the original source my guess is the clock is running too fast.
My suggestion: study the modifications you made. You made two vital changes and both are wrong. |
|
|
volcane
Joined: 23 Feb 2008 Posts: 29
|
|
Posted: Sat Mar 21, 2009 11:24 am |
|
|
Hi!
his is the code that I changed and I'm using with an error of 1 second in 24 hours :
Code: | #define green_led pin_a5
#define yellow_led pin_b4
#define red_led pin_b5
#define XTAL_FREQUENCY 20000000
int8 Ticker;
int8 Seconds=0;
void Initialize_RTC(void)
{
Ticker = 10;
setup_timer_1( T1_INTERNAL | T1_DIV_BY_8 );
enable_interrupts( INT_TIMER1 ); // Start RTC
}
#int_TIMER1
void TIMER1_isr()
{
set_timer1(0x0BDC);
Ticker -= 1;
if ( Ticker == 0 )
{
Ticker = 10;
seconds++;
}
}
void main()
{
//int8 prev_second;
int8 minutes=0;
Initialize_RTC();
enable_interrupts( GLOBAL );
output_low (green_led), output_high (red_led), output_high (yellow_led);
while(true)
if (Ticker > 5)
{
output_low (green_led);
}
else
{
output_high (green_led);
}
if(Seconds == 60) {
Seconds=0;
minutes++;
output_toggle (yellow_led);
}
if(minutes == 60) {
minutes = 0;
output_toggle (red_led);
}
} |
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sun Mar 22, 2009 6:53 pm |
|
|
volcane wrote: | his is the code that I changed and I'm using with an error of 1 second in 24 hours : | And your question is....?
Interrupts take some time to execute (all working registers have to be saved first). Depending on processor model and compiler version number this takes about 20 to 50 instruction times. Accuracy can be increased by changing the line:
Code: | set_timer1(0x0BDC); | to: Code: | set_timer1( get_timer1() + 0x0BDC); | This removes any (variable) delay from the the time your interrupt code is being executed. |
|
|
volcane
Joined: 23 Feb 2008 Posts: 29
|
|
Posted: Tue Mar 24, 2009 10:38 am |
|
|
ckielstra wrote: |
Code: | set_timer1(0x0BDC); | to: Code: | set_timer1( get_timer1() + 0x0BDC); | This removes any (variable) delay from the the time your interrupt code is being executed. |
thanks i try |
|
|
volcane
Joined: 23 Feb 2008 Posts: 29
|
|
Posted: Tue Mar 24, 2009 10:40 am |
|
|
PCM programmer wrote: | In MPLAB, you can set the List File Format to:
Quote: |
- Normal CCS format
- MPLAB format
- Symbolic format
|
This is done in the Project / Build Options menu.
I used the Symbolic format mode. |
which version of MPLAB and CCS compiler uses? |
|
|
|