|
|
View previous topic :: View next topic |
Author |
Message |
mquemelli
Joined: 18 Oct 2016 Posts: 9
|
Problem using Timer1 on dsPIC33EP64MC202 |
Posted: Mon Mar 20, 2017 9:32 pm |
|
|
I'm trying to use Timer1 just to blink a LED, and I am using a dsPIC33EP64MC202 with a 12MHz crystal, clock is 140MHz. This is my header file:
Code: |
#include <33EP64MC202.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOJTAG //JTAG disabled
#FUSES CKSFSM //Clock Switching is enabled, fail Safe clock monitor is enabled
#device ICSP=1
#use delay(clock=140000000,crystal=12000000) // 70MIPS
#use FIXED_IO( B_outputs=PIN_B6,PIN_B5 )
#define LED PIN_B6
#define CS PIN_B5
#pin_select U1TX=PIN_B10
#pin_select U1RX=PIN_B11
#use rs232(UART1, baud=9600, stream=UART_PORT1)
#use spi(MASTER, SPI1, BAUD=14000000, MODE=2, BITS=8, stream=SPI_PORT1)
|
And this the source code c:
Code: |
#include <header.h>
void main(){
setup_timer1(TMR_EXTERNAL | TMR_DIV_BY_256); // 12MHz divided by 256 = 46875
while(TRUE){
if(get_timer1()>46875){ //blink each second
output_toggle(LED);
set_timer1(0);
}
}
}
|
But it doesn't work, Led is not blinking. Sorry if I did some big mistake, I am new using dsPIC. Could anybody help me to find what is wrong in my code? |
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Tue Mar 21, 2017 1:13 am |
|
|
What are clocking your timer as you selected external clock.
Maybe you were looking for this:
Code: | setup_timer1(TMR_INTERNAL | TMR_DIV_BY_256, 65535); |
This however will provide a overflow approximately every 240ms. Enough to see LED blink.
Regards |
|
|
mquemelli
Joined: 18 Oct 2016 Posts: 9
|
|
Posted: Tue Mar 21, 2017 9:09 am |
|
|
Sorry my delay Alan, thank you for answering.
I had tried what you say to me, and I also got nothing. I had tried use the timer2 and timer3 too, I changed from external source to internal and nothing works. It is weird because I made a test like that:
Code: |
while(get_timer()){
output_HIGH(LED);
}
|
Just to see if the timer start to count but the LED not even lights up. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Thu Mar 30, 2017 3:12 am |
|
|
Use the interrupt flag (not the interrupt):
Code: |
#include <header.h>
void main(void)
{
setup_timer1(TMR_EXTERNAL | TMR_DIV_BY_256, 46875); // 12MHz divided by 256 = 46875
while(TRUE)
{
if (interrupt_active(INT_TIMER1))
{ //blink each second
clear_interrupt(INT_TIMER1);
output_toggle(LED);
}
}
}
|
When timer1 reaches the defined count, it'll reset to 0, and set it's interrupt flag. Since the interrupt is not enabled, there will be no call to the handler, but your poll will see this.
The reason for your problem is types. The default integer in a PIC24/30/33, is a signed int16. As such the count can never be seen to go over 46875.
The 46875, will be seen as an int32 (since it is > 32767). The value from the timer will be cast to an int32, but will still be -ve, so will never get to your count....
To get it to work, you'd have to cast both sides of the test to unsigned int16.
A 'gotcha' with the default signed types.... |
|
|
|
|
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
|