Hi Evan,
I am programming PIC16F1827 in which Timer1 is used to generate a delay of 100 msec. Code file is attached. First problem is that as per timer reload value of 15536 for 100msec, interrupt is generated at 25msec which I verified also from an oscilloscope at pin A2 whereas the following timer code in mplab generates 100msec interrupt which I verified.
void TMR1_Initialize(void)
{
//Set the Timer to the options selected in the GUI
You are getting the oscillator frequency divided by 4 times using InitTimer1( Osc, PS_1_8 )...
By preloading Timer1 with 15536 you have an overflow/interrupt every 50Kcycles.
At 4 Mhz, 1cycle is 1uSec (because of Fosc/4), times 8 (prescaler) 8usec, times 50K cycles = overflow at 400 mS.
If you use Fosc, at 4Mhz 1 cycle= 0.25uSec, times 8 = 2uSec, times 50K = 100mSec as expected..
Hope this solves yr problem..
Rgds,
GC
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks G. C. for your reply. Now there are two issues.
First, if I write as follows
#chip 16f1827, 16
#option explicit
then the code does not compile and gives the following error.
Error: Variable FOSC was not explicitly declared
Second, without option explicit, it compiles successfully and I am getting frequency of around 40Hz at LED pins.
But my target is to get 100msec interrupt at 16MHz internal oscillator.
I configured Timer1 control register at bit level using the following link and got the results with output frequency of 5 Hz at LED1 pin, 50% Duty Cycle, Ton=Toff=100msec. Sample code is attached. http://eng-serve.com/pic/pic_timer.html
for what concerns the compiler error on "#option explicit", I guess is something due to the GCB's header file for yr uC, as it seems to consider a Keyword as a program variable.. I am sure Evan will look onto it and fix it. For tests purposes, leave that line commented out.
For the Timer1 issue, I gave you the numbers for 4 MHz because that was the frequency you set in your first source code (the one named Test.gcb). It you run the uC at 16 Mhz, then you should change the row number 6 to reflect the 16Mhz speed: #chip 16f1827, 16 instead of #chip 16f1827, 4.
Then there is an error at line 106: InitTimer1( Osc, PS_1_8 )... Change it to InitTimer1(Osc, PS1_8). You should then get the 100mSec interrupt desired.
In your last code named led_blink there is an error at line 28: T1CON.T1OSCEN = 1. This will enable the clock input to Timer1 as if it comes from an external indipendent oscillator, and not from the uC base clock 16Mhz (Fosc/4). Change it as T1CON.T1OSCEN = 0.
But better yet, leave the configuration of the Timer to GCB and remove all the individual bits setting.. If you have an hw breadboard set up with the two leds on Ra2/Ra3, try to load the hex I attach here, togheter with corrected source (even here you had a syntax error on the Prescaler command) and simulation workspace on Pic SimLab. Test it out and you should get the leds blinking alternatively at about 100mSec LED1, 1Sec for LED2. You might probe them with a scope on Ra2/Ra3.
I will check your code. I tested LED_blink.gcb on hardware PIC and results were good.
As per my observation, there is some issue in configuring Timer Control Register.
Can I start by confirming the basics ? As I do not have that specific chip I need you to confirm that that following program operates at expected at 10Hz/Period=100ms.
This is important as this proves the oscillator registers controlling the frequency are operating as expected. If the oscillator registers are not correct then the setting of the timer1 will also be incorrect.
So, here this generates a 10Hz/Period=100ms square wave.
Did you see my post on the typing error of the Init Timer command?
I believe if you just remove the first undertscore the Compiler will create the correct code for the prescaler bits..
GC
**** copied down here too, for yr reference
"...... there is an error at line 106: InitTimer1( Osc, PS_1_8 )... Change it to InitTimer1(Osc, PS1_8)..... "
It's only a typo error you made on the SetTimer command line..
Use my source7hex and you'll see that everything works fine.
GC
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thank you Evan, rest of the GCBasic team and G. C. for your efforts to solve the issue.
I will test the code with new timer.h file on PIC16F1827 and will update.
Regards,
Awais
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi Evan,
I am programming PIC16F1827 in which Timer1 is used to generate a delay of 100 msec. Code file is attached. First problem is that as per timer reload value of 15536 for 100msec, interrupt is generated at 25msec which I verified also from an oscilloscope at pin A2 whereas the following timer code in mplab generates 100msec interrupt which I verified.
void TMR1_Initialize(void)
{
//Set the Timer to the options selected in the GUI
}
Kindly reply.
Regards,
Awais
Try to change line 106 to this:
InitTimer1( FOsc, PS_1_8 )
You are getting the oscillator frequency divided by 4 times using InitTimer1( Osc, PS_1_8 )...
By preloading Timer1 with 15536 you have an overflow/interrupt every 50Kcycles.
At 4 Mhz, 1cycle is 1uSec (because of Fosc/4), times 8 (prescaler) 8usec, times 50K cycles = overflow at 400 mS.
If you use Fosc, at 4Mhz 1 cycle= 0.25uSec, times 8 = 2uSec, times 50K = 100mSec as expected..
Hope this solves yr problem..
Rgds,
GC
Thanks G. C. for your reply. Now there are two issues.
First, if I write as follows
then the code does not compile and gives the following error.
Error: Variable FOSC was not explicitly declared
Second, without option explicit, it compiles successfully and I am getting frequency of around 40Hz at LED pins.
But my target is to get 100msec interrupt at 16MHz internal oscillator.
I configured Timer1 control register at bit level using the following link and got the results with output frequency of 5 Hz at LED1 pin, 50% Duty Cycle, Ton=Toff=100msec. Sample code is attached.
http://eng-serve.com/pic/pic_timer.html
Last edit: Awais 2023-11-26
Hi Awais,
for what concerns the compiler error on "#option explicit", I guess is something due to the GCB's header file for yr uC, as it seems to consider a Keyword as a program variable.. I am sure Evan will look onto it and fix it. For tests purposes, leave that line commented out.
For the Timer1 issue, I gave you the numbers for 4 MHz because that was the frequency you set in your first source code (the one named Test.gcb). It you run the uC at 16 Mhz, then you should change the row number 6 to reflect the 16Mhz speed: #chip 16f1827, 16 instead of #chip 16f1827, 4.
Then there is an error at line 106: InitTimer1( Osc, PS_1_8 )... Change it to InitTimer1(Osc, PS1_8). You should then get the 100mSec interrupt desired.
In your last code named led_blink there is an error at line 28: T1CON.T1OSCEN = 1. This will enable the clock input to Timer1 as if it comes from an external indipendent oscillator, and not from the uC base clock 16Mhz (Fosc/4). Change it as T1CON.T1OSCEN = 0.
But better yet, leave the configuration of the Timer to GCB and remove all the individual bits setting.. If you have an hw breadboard set up with the two leds on Ra2/Ra3, try to load the hex I attach here, togheter with corrected source (even here you had a syntax error on the Prescaler command) and simulation workspace on Pic SimLab. Test it out and you should get the leds blinking alternatively at about 100mSec LED1, 1Sec for LED2. You might probe them with a scope on Ra2/Ra3.
Pls let us know if this solves.
Bye,
GC
Hi G. C.,
I will check your code. I tested LED_blink.gcb on hardware PIC and results were good.
As per my observation, there is some issue in configuring Timer Control Register.
11:47:30.361 -> T1CON Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
11:47:30.401 ->
11:47:30.401 -> 0 0 0 0 0 0 0 1
Bits 4 and 5 should be 1 for prescale value of 1:8. See my below post.
Regards,
Awais
Yes, I am aware. I am tryin to determine what is incorrect. Hence, my post https://sourceforge.net/p/gcbasic/discussion/579125/thread/bf206f5808/?limit=25#0d37
Got it.
I think these wrong individual bit settings are due to your syntax error in GCB source code, see the above post of mine at:
"...... there is an error at line 106: InitTimer1( Osc, PS_1_8 )... Change it to InitTimer1(Osc, PS1_8)..... "
It's only a typo error you made on the SetTimer command line..
Use my source7hex and you'll see that everything works fine.
GC
I will update after testing the code on hardware.
Thanks.
Here is a sample program that blinks two leds alternatively at 1Sec..
Tested on the simulator, if you want to try out..
Best,
GC
Last edit: G. C. 2023-11-24
The chip frequency baseline for the C is the same as the GCBASIC frequency?
The timer clock source is the same in the C compared to GCBASIC?
You can check the GCBASIC registers by sending to a serial terminal and then to compare to determine any differences.
Evan
I checked the registers values. Thanks to William Roth for his code to Display Registers on a Terminal. https://sourceforge.net/p/gcbasic/discussion/629990/thread/f5d6355560/
Below is the serial output.
11:47:28.321 -> ------------------------------------------------------------
11:47:28.361 -> OSCCON Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
11:47:28.441 ->
11:47:28.441 -> 0 1 1 1 1 0 0 0
11:47:28.521 -> ------------------------------------------------------------
11:47:28.601 ->
11:47:28.601 ->
11:47:28.601 -> ------------------------------------------------------------
11:47:28.641 -> OSCSTAT Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
11:47:28.721 ->
11:47:28.721 -> 1 0 1 1 1 1 1 1
11:47:28.801 -> ------------------------------------------------------------
11:47:28.881 ->
11:47:28.881 ->
11:47:28.881 -> ------------------------------------------------------------
11:47:28.921 -> PIE2 Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
11:47:29.001 ->
11:47:29.001 -> 0 0 0 0 0 0 0 0
11:47:29.081 -> ------------------------------------------------------------
11:47:29.161 ->
11:47:29.161 ->
11:47:29.161 -> ------------------------------------------------------------
11:47:29.201 -> PIR2 Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
11:47:29.281 ->
11:47:29.281 -> 0 0 0 0 0 0 0 0
11:47:29.361 -> ------------------------------------------------------------
11:47:29.441 ->
11:47:29.441 ->
11:47:29.441 -> ------------------------------------------------------------
11:47:29.481 -> T1CON Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
11:47:29.561 ->
11:47:29.561 -> 0 0 0 0 0 0 0 1
11:47:29.641 -> ------------------------------------------------------------
11:47:29.721 ->
11:47:29.721 ->
11:47:29.721 -> ------------------------------------------------------------
11:47:29.761 -> TMR1H Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
11:47:29.841 ->
11:47:29.841 -> 1 1 0 1 0 1 1 0
11:47:29.921 -> ------------------------------------------------------------
11:47:30.001 ->
11:47:30.001 ->
11:47:30.001 -> ------------------------------------------------------------
11:47:30.041 -> TMR1L Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
11:47:30.121 ->
11:47:30.121 -> 1 0 0 1 1 0 0 1
11:47:30.201 -> ------------------------------------------------------------
11:47:30.281 ->
11:47:30.281 ->
11:47:30.281 -> ------------------------------------------------------------
11:47:30.361 -> T1CON Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
11:47:30.401 ->
11:47:30.401 -> 0 0 0 0 0 0 0 1
11:47:30.481 -> ------------------------------------------------------------
11:47:30.561 ->
11:47:30.561 ->
11:47:30.561 -> ------------------------------------------------------------
11:47:30.641 -> PIE1 Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
11:47:30.691 ->
11:47:30.691 -> 0 0 0 0 0 0 0 1
11:47:30.771 -> ------------------------------------------------------------
11:47:30.851 ->
11:47:30.851 ->
11:47:30.851 -> ------------------------------------------------------------
11:47:30.891 -> PIR1 Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
11:47:30.971 ->
11:47:30.971 -> 1 0 0 1 0 0 0 0
11:47:31.051 -> ------------------------------------------------------------
I observed that T1CON Bits 4 and 5 (T1CKPS) are 0 instead of 1 for prescale value of 1:8 with reference to PIC16F1827 datasheet DS41391D-page 185.
Awais
Hello, let us try to resolve this.
Can I start by confirming the basics ? As I do not have that specific chip I need you to confirm that that following program operates at expected at 10Hz/Period=100ms.
This is important as this proves the oscillator registers controlling the frequency are operating as expected. If the oscillator registers are not correct then the setting of the timer1 will also be incorrect.
So, here this generates a 10Hz/Period=100ms square wave.
Do it work for you? Please change the chip.
Last edit: Anobium 2023-11-27
Please find attached the test results with PIC16F1827 along with code file.
Regards,
Awais
That looks correct? that is is question to confirm things.
I loaded two programs in PIC16 Simulator IDE to see the value of T1CON.
File names are self explanatory.
Did you see my post on the typing error of the Init Timer command?
I believe if you just remove the first undertscore the Compiler will create the correct code for the prescaler bits..
GC
**** copied down here too, for yr reference
"...... there is an error at line 106: InitTimer1( Osc, PS_1_8 )... Change it to InitTimer1(Osc, PS1_8)..... "
It's only a typo error you made on the SetTimer command line..
Use my source7hex and you'll see that everything works fine.
GC
Thanks, GC. I missed that underscore mistake.
Regards,
Total brilliant spot that typo!
:-)
Here is a revised timer.h
This isolates AVR and PIC constants. This will prevent selection of PS_1_8 when a PIC....
Restart GC Studio to get the latest build, then, download this file.
Put the file in the gcbasic\include\lowlevel folder.
Enjoy
My little test program.
Supports GCBASIC commands and Handcranking the registers.
:-)
Last edit: Anobium 2023-11-27
When this thread is resolved... I think it is. :-)
Donate here
Please donate to the operational costs of GCBASIC for the coming year - Thank you. paypal.me/gcbasic
Thank you Evan, rest of the GCBasic team and G. C. for your efforts to solve the issue.
I will test the code with new timer.h file on PIC16F1827 and will update.
Regards,
Awais