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

Serial communication on 12F509
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
nailuy



Joined: 21 Sep 2010
Posts: 148

View user's profile Send private message

Serial communication on 12F509
PostPosted: Tue Jun 21, 2022 2:04 am     Reply with quote

I have question about this situation.
I want to make device who send/receive serial data at speed 57600kbps.
My device 12F509 have internal 4MHz.
Can I use function?
Code:
#use rs232( ... FORCE_SW)

This function is working at 9600kbps, but on higher speed not.
What possibility I have to make this to work?

PS: code work for transmit at speed up to 57600, but for receive not working until 9600.
temtronic



Joined: 01 Jul 2010
Posts: 8528
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Jun 21, 2022 5:10 am     Reply with quote

probably none.
if I recall right that PIC does not have a UART and NO interrupts, so serial has to be done in software aka 'bit banged'. With a 4MHz clock there isn't enough time. Sending might not be a problem, but to receive data, the PIC has to be in a 'tight' loop to poll the incoming data.
You should post your code, there may be some 'tricks' that can be done.
Early PC mice used small PICs like the 509, MC has the appnote online, done in assembler(long before CCS C came out...)
PrinceNai



Joined: 31 Oct 2016
Posts: 340
Location: Montenegro

View user's profile Send private message

PostPosted: Tue Jun 21, 2022 6:49 am     Reply with quote

One instruction cycle at 4MHz takes 1us. Bit duration at 57.600 is 17,36us, so you have 17 instructions to detect and store that bit. I'd guess it can't be done.
Ttelmah



Joined: 11 Mar 2010
Posts: 18124

View user's profile Send private message

PostPosted: Tue Jun 21, 2022 7:01 am     Reply with quote

Seriously, use a PIC suitable for the job....
It might just about be possible to get half duplex comms to work, but
not likely to be reliable, and if you actually want to do anything much with
the data you will also run out of ROM space.
Chips like the 12F1822, offer operation at up to 8* the oscillator rate,
and give you a UART as well....
The chip at heart is just not running fast enough to do this.
nailuy



Joined: 21 Sep 2010
Posts: 148

View user's profile Send private message

PostPosted: Tue Jun 21, 2022 7:10 am     Reply with quote

Thank you for reply.
Is true...

code from main:

Code:
for(;;)
{
RESTART_WDT();

if(kbhit()){         
   MEM_SERIAL = getc();
   }
output_toggle(B1);
}


code from setup 12F509:

Code:
#include <12F509.h>

#device *=8

#FUSES WDT                      //Watch Dog Timer
#FUSES INTRC                    //Internal RC Osc
#FUSES NOMCLR                   //Master Clear pin used for I/O
#use delay(internal=4M)

#use rs232(baud=28800,parity=N,xmit=PIN_B2,rcv=PIN_B5,bits=8,FORCE_SW)
#use FIXED_IO( B_outputs=PIN_B1 )
#use fast_io(B)
static unsigned int8 MEM_SERIAL;


this is maximium speed suported for reading data.
Now I change the project with dedicated EUSART.
I hope that's working.
nailuy



Joined: 21 Sep 2010
Posts: 148

View user's profile Send private message

PostPosted: Tue Jun 21, 2022 7:13 am     Reply with quote

nice 12F1822 but on local market is not, must wait too much.
Thank you.
Ttelmah



Joined: 11 Mar 2010
Posts: 18124

View user's profile Send private message

PostPosted: Tue Jun 21, 2022 9:56 am     Reply with quote

There are several others.
Do a search on the Microchip page for 8pin PIC with UART. Any of the more
modern chips will also go faster as well. 12F1840 is another.
nailuy



Joined: 21 Sep 2010
Posts: 148

View user's profile Send private message

the new 16f1826 serial comunication
PostPosted: Fri Jun 24, 2022 12:37 am     Reply with quote

Hi, I test to work my code in this new 16f1826, but is not working the code.

Code:
void main()
{
setup_wdt(WDT_1MS);      //~1,0 ms reset
//CREN=1;
//SYNC=0;
//SPEN=1;

putc(0X05);
putc(0X10);
putc(0X11);

for(;;)
{
RESTART_WDT();
putc(0X25);
putc(0X20);
putc(0X21);

if(kbhit()){         
   MEM_SERIAL = getc(); 
   }

//output_toggle(B4);
}


and header configuration is:
Code:
#include <16F1826.h>

#device ADC=8

#FUSES NOWDT                      //Watch Dog Timer
#FUSES NOMCLR                     //Master Clear pin enabled
#FUSES PROTECT                  //Code protected from reads
#FUSES CPD                      //Data EEPROM Code Protected
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES NOWRT                    //Program memory not write protected
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES BORV19                   //Brownout reset at 1.9V*/

#use delay(internal=2MHz)
#use rs232(UART1,baud=9600)


I test this code to see what I do wrong.
I'm not receiving not even 05, 10, 11, so code or configuration is wrong?
PCM programmer



Joined: 06 Sep 2003
Posts: 21667

View user's profile Send private message

PostPosted: Fri Jun 24, 2022 1:35 am     Reply with quote

Why are you using the WDT in a program under development ?
For years, we have said not to do that.

You have WDT set for a 1 ms timeout.

At 9600 baud, it takes 1.04 ms to transmit one character.
You then send 3 characters. Of course it's going to get a WDT reset.

Also, why are you running at 2 MHz ? You were running at 4 MHz on
the previous PIC.

We can't fix these kinds of mistakes. You have to think and do it right.
nailuy



Joined: 21 Sep 2010
Posts: 148

View user's profile Send private message

PostPosted: Fri Jun 24, 2022 1:48 am     Reply with quote

dear PCM programmer thank you for reply,

I deactivated the:
Code:
//#FUSES NOWDT                      //Watch Dog Timer
//setup_wdt(WDT_1MS);      //~1,0 ms reset
//RESTART_WDT();

is still not working.
nailuy



Joined: 21 Sep 2010
Posts: 148

View user's profile Send private message

PostPosted: Fri Jun 24, 2022 2:49 am     Reply with quote

dear PCM programmer.
UART does not affect the program or delaying the cycles.
Now I using the hardware UART of 16F1826. At beginning I use software of 12F509.
So problem is somewhere else But I don't know.
on page 285 (26.0 ENHANCED UNIVERSAL SYNCHRONOUS ASYNCHRONOUS RECEIVER TRANSMITTER (EUSART)) I read the doc, and must control separately 3 registers
Transmit Status and Control (TXSTA)
Receive Status and Control (RCSTA)
Baud Rate Control (BAUDCON)
I think I need some new example code to lean how to implement this...
temtronic



Joined: 01 Jul 2010
Posts: 8528
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Jun 24, 2022 4:29 am     Reply with quote

1st problem I see is that you've 'split' the program into 'main() and some unnamed 'header'. It is far better to have EVERYTHING as ONE program. That allows us to see the entire program AND the ability to 'copy/paste/compile/test' on our computers.

2nd, you really should code a '1Hz LED ' test program. Add an LED, 470r resistor to the PIC and have main() 'toggle' that LED. This program should flash the LED at about 1Hz and will confirm the PIC hardware does work.

3rd use a faster clock ! At least 4MHz

4th, CCS supplies lots of example programs and an FAQ section in the manual.You can also search this forum for '1hz LED' for a program example.

Once you get the LED to flash properly THEN try using the UART. BTW you do have the proper TTL to RS232 interface wired in ??
PrinceNai



Joined: 31 Oct 2016
Posts: 340
Location: Montenegro

View user's profile Send private message

PostPosted: Fri Jun 24, 2022 11:55 am     Reply with quote

As the others have said, increase the clock. This chip goes to 32MHz. Make a test program to see if the chip is working.

You say the program isn't working. What isn't working? Nothing at all? Sending? Receiving?

Quote:


I read the doc, and must control separately 3 registers
Transmit Status and Control (TXSTA)
Receive Status and Control (RCSTA)
Baud Rate Control (BAUDCON)


You don't have to control those manually. CCS does that for you behind the scenes. What are you sending to? Where do you check if anything was sent? What are you receiving from?
Ttelmah



Joined: 11 Mar 2010
Posts: 18124

View user's profile Send private message

PostPosted: Fri Jun 24, 2022 10:01 pm     Reply with quote

#use rs232 - this sets up baudcon

getc - this reada rcsta
putc - this writes txsta

You do not need to touch these registers yourself.

What you do ned to do is take things _one step at a time_.
This is critical in programming.

Start by adding an LED to a pin (with a suitable current limiting resistor),
and having a simple progem to flash this once per second.
Once you have this working, you know that the chip fuses and clock
are correct (once it flashes at once per second).
Once this is one, you can then move to starting 'serial'.
Serial will not work, till the chip is running, and is running at the right
speed.

Then the big question comes 'where is this serial coming from and going'?.

Understand that the PIC, does not 'talk' RS232 (the name of the #use
is misleading here). The PIC talk TTL serial. To make this into RS232,
a buffer chip is needed. So tell us how the serial connection is wired,
and to what?.
temtronic



Joined: 01 Jul 2010
Posts: 8528
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Jun 25, 2022 6:34 am     Reply with quote

gee, I sure hope this is REAL hardware and not, ugh, dare I day ,'Proteus' Confused
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