View previous topic :: View next topic |
Author |
Message |
BLL
Joined: 11 Nov 2006 Posts: 181 Location: Birmingham, UK
|
12F1840 APFCON Use |
Posted: Mon Oct 22, 2018 1:41 pm |
|
|
Hi,
I am new to this PIC. I have been looking through the forum and I came across the code:
#byte APFCON = getenv("SFR:APFCON")
#bit T1GSEL = APFCON.3
Can someone please explain what it means/what it does?
Thanks
Brian |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19229
|
|
Posted: Mon Oct 22, 2018 1:51 pm |
|
|
Alternate Pin Function.
It's like a lesser version of PPS. Allows specific peripherals to be routed to a couple of different pins. IN this case the Timer1 gate function can be routed to RA3 or RA4 (RA4 for zero in the register bit).
With the #bit setup as shown:
T1GSEL=0;
will put this function onto RA4, while:
T1GSEL=1;
Will put this on RA3. |
|
|
BLL
Joined: 11 Nov 2006 Posts: 181 Location: Birmingham, UK
|
|
Posted: Tue Oct 23, 2018 3:28 am |
|
|
Hi
Thanks for that - fiendish clever, these Chinese!!
Brian |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19229
|
|
Posted: Tue Oct 23, 2018 3:40 am |
|
|
It's possibly worth adding, that this is not needed on current compilers. They now include the option set set these values in the peripheral setups. So in this case:
setup_timer_1(T1_EXTERNAL | T1_DIV_BY_1 | T1_GATE | T1_GATE_A3);
Automatically connects the gate to A3, setting this bit in the APFCON register:
Code: |
.................... setup_timer_1(T1_EXTERNAL | T1_DIV_BY_1 | T1_GATE | T1_GATE_A3);
000B: BSF APFCON.T1GSEL
000C: MOVLW 85
000D: MOVLB 00
000E: MOVWF T1CON
000F: MOVLW 80
0010: MOVWF T1GCON
|
|
|
|
BLL
Joined: 11 Nov 2006 Posts: 181 Location: Birmingham, UK
|
|
Posted: Tue Oct 23, 2018 5:47 am |
|
|
Hi
Thanks for that.
Brian |
|
|
BLL
Joined: 11 Nov 2006 Posts: 181 Location: Birmingham, UK
|
|
Posted: Wed Oct 24, 2018 5:46 am |
|
|
I continue to mess with this chip.
I have TIGSEL on RA3.
I now want to set up the EUSART, so I can look at the timer1 values returned for various pulsewidths into the gate.
The pinout on the datasheet shows 2 lots of pins as TXD and RXD.
Which are selected by default and how can they be set using APFCON please?
At the moment, I have the line:
#use rs232(baud=9600, UART1)
Thanks
Brian |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19229
|
|
Posted: Wed Oct 24, 2018 5:54 am |
|
|
The APFCON register defaults to 0 (data sheet tells you this).
Use the UART name, rather than pins.
#use RS232(UART1, baud=9600, ERRORS)
This will default to using RA0/RA1.
If you want to change this, set the bits in APFCON. So to use RA4/RA5:
BIT_SET(APFCON,7); //RX to RA5
BIT_SET(APFCON,2); //TX to RA4 |
|
|
BLL
Joined: 11 Nov 2006 Posts: 181 Location: Birmingham, UK
|
|
Posted: Wed Oct 24, 2018 9:51 am |
|
|
Hi Ttelmah
Thank you again.
I cannot get the serial port to play.
I am using a FTDI USB to serial converter from my Win10 laptop.
If I link TXD and RXD, then sent characters are received, so that's working.
However, when I connect to the PIC, pins 5 & 6, there is nothing.
In my .h file, I have:
Code: |
#include <12F1840.h>
#fuses NOMCLR, INTRC_IO, NOWDT, BROWNOUT
#use delay(clock=4MHz)
#use rs232(baud=9600, UART1)
|
At the top of main, I have:
Code: |
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8|T1_GATE|T1_GATE_A3);
set_timer1(0); |
and in a for(;;) loop, I have:
Code: |
printf("val: %Lu\r\n", get_timer1());
delay_ms(500); |
I'm stumped!
Brian |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19229
|
|
Posted: Wed Oct 24, 2018 10:31 am |
|
|
Are you sure the FTDI module you have is a logic output one, not RS232?. If not, you will need RS232 converters.
Also are you sure it's baud rate is set to 9600?.
As a general comment, you should _always_ have 'ERRORS' in the #use RS232 for a hardware port, unless your code has it's own error handing. This won't matter here (for transmit), but not having this can result in the UART becoming hung, if data is received and not handled....
You say 'pins 5 & 6'. This is wrong. The pins are 6 & 7 for the default serial connection. Also remember the ground connection must also be made. |
|
|
BLL
Joined: 11 Nov 2006 Posts: 181 Location: Birmingham, UK
|
|
Posted: Wed Oct 24, 2018 12:26 pm |
|
|
Hi Ttelmah
The pin numbers were a typo! I do have the ground connection.
I am definitely at 9600Bd.
I have added ERRORS and still absolutely nothing from the PIC is being received!
The FTDI is definitely a TTL level output - I have used it before on another PIC.
Brian |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19229
|
|
Posted: Wed Oct 24, 2018 1:09 pm |
|
|
I would change one thing:
#use delay(INTERNAL=4MHz)
Your existing code does not tell the internal oscillator what speed to select.
It is unlikely to be selecting 4MHz. The default unspecified, looks to be 16MHz, so your baud rate will be very wrong.... |
|
|
BLL
Joined: 11 Nov 2006 Posts: 181 Location: Birmingham, UK
|
|
Posted: Wed Oct 24, 2018 1:53 pm |
|
|
Hi Ttelmah
Tried that and still nothing via serial port; no garbage nothing!
Brian |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19229
|
|
Posted: Wed Oct 24, 2018 2:13 pm |
|
|
Try the simple expedient of setting APFCON to zero. It is meant to be zero, and the compiler assumes it is, but if it wasn't, then the pins could be routing to the alternative pins. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Oct 24, 2018 2:28 pm |
|
|
BLL wrote: |
Tried that and still nothing via serial port; no garbage nothing!
|
Post your complete current test program. Don't post snippets. Post all of it.
Also post your CCS compiler version, and the Vdd voltage you're using for
your PIC. |
|
|
BLL
Joined: 11 Nov 2006 Posts: 181 Location: Birmingham, UK
|
|
Posted: Thu Oct 25, 2018 5:56 am |
|
|
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);
}//endfor
}//end main |
Compiler version is 5.007. VDD is 5V
Brian |
|
|
|