View previous topic :: View next topic |
Author |
Message |
Ttelmah
Joined: 11 Mar 2010 Posts: 19254
|
|
Posted: Thu Oct 25, 2018 6:28 am |
|
|
5.007
Ouch....
That is a very early V5 compiler. The earliest I kept as 'possibly working', is later than this...
V5, was at least running down here, but at this early version, 4.141, was better.
On APFCON, try:
Code: |
#include <12F1840.h>
#fuses NOMCLR, INTRC_IO, NOWDT, BROWNOUT
#use delay(INTERNAL=4MHz)
#use rs232(baud=9600, UART1, ERRORS)
#byte APFCON = getenv("SFR:APFCON")
void main()
{
APFCON=0; //ensure APFCON does start as zero
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8|T1_GATE|T1_GATE_A3);
set_timer1(0);
for(;;)
{
printf("val: %Lu\r\n", get_timer1());
delay_ms(500);
}//endfor
}//end main
|
|
|
|
BLL
Joined: 11 Nov 2006 Posts: 181 Location: Birmingham, UK
|
|
Posted: Thu Oct 25, 2018 7:51 am |
|
|
Hi both
Thanks for the input but it still doesn't play!
Brian |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19254
|
|
Posted: Thu Oct 25, 2018 8:36 am |
|
|
OK. The issue with what you post may be that you are not deselecting the analog.
On these chips they default to all pins setup for analog I/O, and the older compilers do not change this until you tell them to. So:
Code: |
#include <12F1840.h>
#fuses NOMCLR, INTRC_IO, NOWDT, BROWNOUT
#use delay(INTERNAL=4MHz)
#use rs232(baud=9600, UART1, ERRORS)
#byte APFCON = getenv("SFR:APFCON")
void main()
{
setup_adc_ports(NO_ANALOGS); //essential
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8|T1_GATE|T1_GATE_A3);
set_timer1(0);
for(;;)
{
printf("val: %Lu\r\n", get_timer1());
delay_ms(500);
}//endfor
}//end main
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Oct 25, 2018 11:00 am |
|
|
I tested this in hardware and I got readable rs-232 output on TeraTerm
with both vs. 5.007 and vs. 5.081.
Here is a photo of the test setup. I have Pin A0 going to the Rx pin
on a Sparkfun TTL to RS-232 level converter board. The output of that
board goes to my PC, where it's displayed on TeraTerm.
Here is the test program. I commented out the T1 gate stuff but other
than that, it's the same.
Code: |
#include <12F1840.h>
#fuses NOMCLR, INTRC_IO, NOWDT, BROWNOUT
#use delay(INTERNAL=4MHz)
#use rs232(baud=9600, UART1, ERRORS)
//===============================
void main()
{
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8); // |T1_GATE|T1_GATE_A3);
set_timer1(0);
for(;;)
{
printf("val: %lu\r\n", get_timer1());
delay_ms(500);
}
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19254
|
|
Posted: Thu Oct 25, 2018 12:30 pm |
|
|
Well done.
I must admit my next suggestion was going to be to try the basic 'flash an LED' test, since I was beginning to suspect the chip is not actually running.
Your test shows things can work. |
|
|
BLL
Joined: 11 Nov 2006 Posts: 181 Location: Birmingham, UK
|
|
Posted: Fri Nov 02, 2018 12:57 pm |
|
|
Hi All
I eventually got it to work! Turned out to be a faulty USB/RS232 interface and a poorly chip!
However, I can't get this timer gate to behave at all.
I have a pulse train, which varies in width from 1ms to 2ms (coming from a radio control receiver).
I want to be able to act on a particular value, say turn an LED on if width > 1.1ms, but the values I get back vary so much that this just isn't possible with any degree of accuracy.
I have the chip using its internal osc at 4MHz.
How exactly can I get a decent read out of pulse width please?
Thanks again.
Brian |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9130 Location: Greensville,Ontario
|
|
Posted: Fri Nov 02, 2018 1:24 pm |
|
|
1st step is to use a real xtal and 2 caps that way you'll have a stable clock and a happy PIC.
I'll have to look at that PIC's datasheet to see what timer can be used to 'decode' the RC servo info.
You may want to use the 'search' option up top to see what others have done....you ain't the 1st !
Jay |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19254
|
|
Posted: Sat Nov 03, 2018 3:29 am |
|
|
Follow PCM_Programmer's link.
The reason it doesn't work, is you have nothing to reset the count. The 'gate' stops the timer from recording the count, except when the gate is active, but you have nothing resetting the count. So it counts for the first pulse, and then this count is added to the one of the next pulse, etc. etc.. Result, when you read the count, it is the sum of times from dozens of pulses....
Have a look in the data sheet at the diagram for "Timer1 gate enable mode". Note that the count keeps going up.
What you can do to use the gate, is to enable the Timer1 gate interrupt event. Then you will count, and receive an interrupt when the gate goes off. On this interrupt, you record the timer value, and clear the timer. Or you can use the gate 'single shot mode', clear the timer, and enable, then the counter will have the count for one pulse. Clear and re-enable to get a new time. |
|
|
BLL
Joined: 11 Nov 2006 Posts: 181 Location: Birmingham, UK
|
|
Posted: Sat Nov 03, 2018 5:45 am |
|
|
Hi all
Being fed up with timer1, I tried the CCP example and that was no better. The values varied enormously.
So, back to basics: My input is on A3
Code: | count=0;
while(!input(PIN_A3));
while(input(PIN_A3))
{
count++;
delay_us(1);
//switch output according to count value
}
|
This gives a completely stable value of count and does what I want!
Ah, the simple things are best!
Thanks all contributors for your help.
Brian |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Nov 03, 2018 8:23 am |
|
|
If it works with an i/o pin but not with Timer1 gate or the CCP, this points
to input voltage levels as the problem. You never told us what voltage
levels are coming from your RC receiver. The i/o pin requires only 2.0v
for a high level, but Timer1 and the CCP require 4.0v (with your 5v Vdd). |
|
|
BLL
Joined: 11 Nov 2006 Posts: 181 Location: Birmingham, UK
|
|
Posted: Sat Nov 03, 2018 12:47 pm |
|
|
The receiver is running at 5.1V and so this should not be a problem!
As I said, my simple solution is rock solid, more than could be said for gate or CCP!!
Brian |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19254
|
|
Posted: Sat Nov 03, 2018 1:16 pm |
|
|
No, there are lots of devices that run at 5v, and don't give 4v output. You really do need to look at the signal with a scope. The internal logic in the RC reciever may well be running off a regulated rail below the actual supply.... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Nov 03, 2018 1:22 pm |
|
|
Who's the manufacturer and what's the part number of the receiver ? |
|
|
BLL
Joined: 11 Nov 2006 Posts: 181 Location: Birmingham, UK
|
|
Posted: Sat Nov 03, 2018 1:32 pm |
|
|
Spektrum AR636A |
|
|
|