CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

12F1840 APFCON Use
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
BLL



Joined: 11 Nov 2006
Posts: 181
Location: Birmingham, UK

View user's profile Send private message

12F1840 APFCON Use
PostPosted: Mon Oct 22, 2018 1:41 pm     Reply with quote

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: 19195

View user's profile Send private message

PostPosted: Mon Oct 22, 2018 1:51 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Oct 23, 2018 3:28 am     Reply with quote

Hi
Thanks for that - fiendish clever, these Chinese!!

Brian
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Tue Oct 23, 2018 3:40 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Oct 23, 2018 5:47 am     Reply with quote

Hi
Thanks for that.

Brian
BLL



Joined: 11 Nov 2006
Posts: 181
Location: Birmingham, UK

View user's profile Send private message

PostPosted: Wed Oct 24, 2018 5:46 am     Reply with quote

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: 19195

View user's profile Send private message

PostPosted: Wed Oct 24, 2018 5:54 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Oct 24, 2018 9:51 am     Reply with quote

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: 19195

View user's profile Send private message

PostPosted: Wed Oct 24, 2018 10:31 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Oct 24, 2018 12:26 pm     Reply with quote

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: 19195

View user's profile Send private message

PostPosted: Wed Oct 24, 2018 1:09 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Oct 24, 2018 1:53 pm     Reply with quote

Hi Ttelmah

Tried that and still nothing via serial port; no garbage nothing!

Brian
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Wed Oct 24, 2018 2:13 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Oct 24, 2018 2:28 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Oct 25, 2018 5:56 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
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